Author: aadamchik
Date: Sun Nov 24 08:49:48 2013
New Revision: 1544944
URL: http://svn.apache.org/r1544944
Log:
CAY-1882 Porting to OSGi environment
a basic OSGi Module
* AdhocObjectFactory with Osgi-friendly classloaders
Added:
cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/
cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiModule.java
cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactory.java
cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/configuration/osgi/
cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactoryTest.java
Added:
cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiModule.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiModule.java?rev=1544944&view=auto
==============================================================================
---
cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiModule.java
(added)
+++
cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiModule.java
Sun Nov 24 08:49:48 2013
@@ -0,0 +1,36 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.configuration.osgi;
+
+import org.apache.cayenne.di.AdhocObjectFactory;
+import org.apache.cayenne.di.Binder;
+import org.apache.cayenne.di.Module;
+
+/**
+ * A DI module that helps to bootstrap Cayenne in OSGi environment.
+ *
+ * @since 3.2
+ */
+public class OsgiModule implements Module {
+
+ @Override
+ public void configure(Binder binder) {
+
binder.bind(AdhocObjectFactory.class).to(SplitClassLoaderAdhocObjectFactory.class);
+ }
+}
Added:
cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactory.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactory.java?rev=1544944&view=auto
==============================================================================
---
cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactory.java
(added)
+++
cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactory.java
Sun Nov 24 08:49:48 2013
@@ -0,0 +1,72 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.configuration.osgi;
+
+import org.apache.cayenne.di.AdhocObjectFactory;
+import org.apache.cayenne.di.spi.DefaultAdhocObjectFactory;
+
+/**
+ * An OSGi-friendly {@link AdhocObjectFactory} that tries to detect the right
+ * ClassLoader for any given resource based on the package name.
+ *
+ * @since 3.2
+ */
+public class SplitClassLoaderAdhocObjectFactory extends
DefaultAdhocObjectFactory {
+
+ private static final String CAYENNE_PACKAGE = "org/apache/cayenne";
+ private static final String CAYENNE_DI_PACKAGE_SUFFIX = "/di";
+
+ private ClassLoader cayenneServerClassLoader;
+ private ClassLoader cayenneDiClassLoader;
+
+ public SplitClassLoaderAdhocObjectFactory() {
+ this.cayenneDiClassLoader = AdhocObjectFactory.class.getClassLoader();
+ this.cayenneServerClassLoader =
SplitClassLoaderAdhocObjectFactory.class.getClassLoader();
+ }
+
+ @Override
+ public ClassLoader getClassLoader(String resourceName) {
+
+ if (resourceName == null || resourceName.length() < 2) {
+ return applicationClassLoader(resourceName);
+ }
+
+ String normalizedName = resourceName.charAt(0) == '/' ?
resourceName.substring(1) : resourceName;
+ if (normalizedName.startsWith(CAYENNE_PACKAGE)) {
+
+ return
(normalizedName.substring(CAYENNE_PACKAGE.length()).startsWith(CAYENNE_DI_PACKAGE_SUFFIX))
? cayenneDiClassLoader()
+ : cayenneServerClassLoader();
+ }
+
+ return applicationClassLoader(resourceName);
+ }
+
+ protected ClassLoader applicationClassLoader(String resourceName) {
+ return super.getClassLoader(resourceName);
+ }
+
+ protected ClassLoader cayenneDiClassLoader() {
+ return cayenneDiClassLoader;
+ }
+
+ protected ClassLoader cayenneServerClassLoader() {
+ return cayenneServerClassLoader;
+ }
+
+}
Added:
cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactoryTest.java
URL:
http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactoryTest.java?rev=1544944&view=auto
==============================================================================
---
cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactoryTest.java
(added)
+++
cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactoryTest.java
Sun Nov 24 08:49:48 2013
@@ -0,0 +1,59 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.configuration.osgi;
+
+import static org.mockito.Mockito.mock;
+import junit.framework.TestCase;
+
+public class SplitClassLoaderAdhocObjectFactoryTest extends TestCase {
+
+ public void testGetClassLoader() {
+
+ final ClassLoader appCl = mock(ClassLoader.class);
+ final ClassLoader diCl = mock(ClassLoader.class);
+ final ClassLoader serverCl = mock(ClassLoader.class);
+
+ SplitClassLoaderAdhocObjectFactory factory = new
SplitClassLoaderAdhocObjectFactory() {
+ @Override
+ protected ClassLoader applicationClassLoader(String resourceName) {
+ return appCl;
+ }
+
+ @Override
+ protected ClassLoader cayenneDiClassLoader() {
+ return diCl;
+ }
+
+ @Override
+ protected ClassLoader cayenneServerClassLoader() {
+ return serverCl;
+ }
+ };
+
+ assertSame(appCl, factory.getClassLoader(null));
+ assertSame(appCl, factory.getClassLoader(""));
+ assertSame(appCl, factory.getClassLoader("org/example/test"));
+ assertSame(appCl, factory.getClassLoader("/org/example/test"));
+ assertSame(serverCl,
factory.getClassLoader("/org/apache/cayenne/access/DataContext.class"));
+ assertSame(diCl,
factory.getClassLoader("/org/apache/cayenne/di/Injector.class"));
+ assertSame(diCl,
factory.getClassLoader("org/apache/cayenne/di/Injector.class"));
+
+ }
+
+}