You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2013/11/24 16:17:39 UTC

svn commit: r1544991 - in /cayenne/main/trunk/cayenne-server/src: main/java/org/apache/cayenne/configuration/osgi/ test/java/org/apache/cayenne/configuration/osgi/

Author: aadamchik
Date: Sun Nov 24 15:17:38 2013
New Revision: 1544991

URL: http://svn.apache.org/r1544991
Log:
CAY-1882 Porting to OSGi environment

* OsgiModuleBuilder
* adding a way to register JDBC driver

Added:
    cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiModuleBuilder.java
Modified:
    cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/DefaultOsgiEnvironment.java
    cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiDataDomainProvider.java
    cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiEnvironment.java
    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/SplitClassLoaderAdhocObjectFactoryTest.java

Modified: cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/DefaultOsgiEnvironment.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/DefaultOsgiEnvironment.java?rev=1544991&r1=1544990&r2=1544991&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/DefaultOsgiEnvironment.java (original)
+++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/DefaultOsgiEnvironment.java Sun Nov 24 15:17:38 2013
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.configuration.osgi;
 
+import java.util.Map;
+
 import org.apache.cayenne.di.Injector;
 
 /**
@@ -28,17 +30,19 @@ public class DefaultOsgiEnvironment impl
     private ClassLoader applicationClassLoader;
     private ClassLoader cayenneServerClassLoader;
     private ClassLoader cayenneDiClassLoader;
+    private Map<String, ClassLoader> perResourceClassLoaders;
 
-    public DefaultOsgiEnvironment(ClassLoader applicationClassLoader) {
+    public DefaultOsgiEnvironment(ClassLoader applicationClassLoader, Map<String, ClassLoader> perResourceClassLoaders) {
         this.applicationClassLoader = applicationClassLoader;
         this.cayenneDiClassLoader = Injector.class.getClassLoader();
         this.cayenneServerClassLoader = DefaultOsgiEnvironment.class.getClassLoader();
+        this.perResourceClassLoaders = perResourceClassLoaders;
     }
 
     @Override
-    public ClassLoader applicationClassLoader(String resourceName) {
-        // return preset classloader regardless of the resource name...
-        return applicationClassLoader;
+    public ClassLoader resourceClassLoader(String resourceName) {
+        ClassLoader cl = perResourceClassLoaders.get(resourceName);
+        return cl != null ? cl : applicationClassLoader;
     }
 
     @Override

Modified: cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiDataDomainProvider.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiDataDomainProvider.java?rev=1544991&r1=1544990&r2=1544991&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiDataDomainProvider.java (original)
+++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiDataDomainProvider.java Sun Nov 24 15:17:38 2013
@@ -50,7 +50,7 @@ public class OsgiDataDomainProvider exte
 
             // using fake package name... may not work with all implementations
             // of osgiEnvironment?
-            thread.setContextClassLoader(osgiEnvironment.applicationClassLoader("com/"));
+            thread.setContextClassLoader(osgiEnvironment.resourceClassLoader("com/"));
 
             DataDomain domain = super.get();
             EntityResolver entityResolver = domain.getEntityResolver();

Modified: cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiEnvironment.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiEnvironment.java?rev=1544991&r1=1544990&r2=1544991&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiEnvironment.java (original)
+++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiEnvironment.java Sun Nov 24 15:17:38 2013
@@ -26,7 +26,7 @@ package org.apache.cayenne.configuration
  */
 public interface OsgiEnvironment {
 
-    ClassLoader applicationClassLoader(String resourceName);
+    ClassLoader resourceClassLoader(String resourceName);
 
     ClassLoader cayenneDiClassLoader();
 

Modified: 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=1544991&r1=1544990&r2=1544991&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiModule.java (original)
+++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiModule.java Sun Nov 24 15:17:38 2013
@@ -18,6 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.configuration.osgi;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.cayenne.access.DataDomain;
 import org.apache.cayenne.di.AdhocObjectFactory;
 import org.apache.cayenne.di.Binder;
@@ -30,33 +33,30 @@ import org.apache.cayenne.di.Module;
  */
 public class OsgiModule implements Module {
 
-    /**
-     * A factory method that creates a new OsgiModule, initialized with any
-     * class from the OSGi bundle that contains Cayenne mapping and persistent
-     * classes. This is likely the the bundle that is calling this method.
-     */
-    public static OsgiModule forProject(Class<?> typeFromProjectBundle) {
-
-        if (typeFromProjectBundle == null) {
-            throw new NullPointerException("Null 'typeFromProjectBundle'");
-        }
-
-        OsgiModule module = new OsgiModule();
-        module.typeFromProjectBundle = typeFromProjectBundle;
-        return module;
+    private Class<?> typeFromProjectBundle;
+    private Map<String, ClassLoader> perTypeClassLoaders;
+
+    OsgiModule() {
+        this.perTypeClassLoaders = new HashMap<String, ClassLoader>();
     }
 
-    private Class<?> typeFromProjectBundle;
+    void setTypeFromProjectBundle(Class<?> typeFromProjectBundle) {
+        this.typeFromProjectBundle = typeFromProjectBundle;
+    }
 
-    private OsgiModule() {
+    void putClassLoader(String type, ClassLoader classLoader) {
+        perTypeClassLoaders.put(type, classLoader);
     }
 
     @Override
     public void configure(Binder binder) {
-        binder.bind(OsgiEnvironment.class).toInstance(
-                new DefaultOsgiEnvironment(typeFromProjectBundle.getClassLoader()));
+        binder.bind(OsgiEnvironment.class).toInstance(createOsgiEnvironment());
 
         binder.bind(AdhocObjectFactory.class).to(SplitClassLoaderAdhocObjectFactory.class);
         binder.bind(DataDomain.class).toProvider(OsgiDataDomainProvider.class);
     }
+
+    private OsgiEnvironment createOsgiEnvironment() {
+        return new DefaultOsgiEnvironment(typeFromProjectBundle.getClassLoader(), perTypeClassLoaders);
+    }
 }

Added: cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiModuleBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiModuleBuilder.java?rev=1544991&view=auto
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiModuleBuilder.java (added)
+++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/OsgiModuleBuilder.java Sun Nov 24 15:17:38 2013
@@ -0,0 +1,85 @@
+/*****************************************************************
+ *   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 java.sql.Driver;
+
+import org.apache.cayenne.di.Module;
+
+/**
+ * A builder of a DI module that helps to bootstrap Cayenne in OSGi environment.
+ * 
+ * @since 3.2
+ */
+public class OsgiModuleBuilder {
+
+    private OsgiModule module;
+
+    public static OsgiModuleBuilder forProject(Class<?> typeFromProjectBundle) {
+
+        if (typeFromProjectBundle == null) {
+            throw new NullPointerException("Null 'typeFromProjectBundle'");
+        }
+
+        return new OsgiModuleBuilder(typeFromProjectBundle);
+    }
+
+    private OsgiModuleBuilder(Class<?> typeFromProjectBundle) {
+        this.module = new OsgiModule();
+        module.setTypeFromProjectBundle(typeFromProjectBundle);
+    }
+
+    /**
+     * Registers a JDBC driver class used by Cayenne. This is an optional piece
+     * of information used by Cayenne to find JDBC driver in case of Cayenne
+     * managed DataSource. E.g. don't use this when you are using a JNDI
+     * DataSource.
+     */
+    public OsgiModuleBuilder withDriver(Class<? extends Driver> driverType) {
+        return withType(driverType);
+    }
+
+    /**
+     * Registers an arbitrary Java class. If Cayenne tries to load it via
+     * reflection later on, it will be using ClassLoader attached to the class
+     * passed to this method.
+     */
+    public OsgiModuleBuilder withType(Class<?> type) {
+        module.putClassLoader(type.getName(), type.getClassLoader());
+        return this;
+    }
+
+    /**
+     * Registers an arbitrary Java class. If Cayenne tries to load it via
+     * reflection later on, it will be using ClassLoader attached to the class
+     * passed to this method.
+     */
+    public OsgiModuleBuilder withTypes(Class<?>... types) {
+        if (types != null) {
+            for (Class<?> type : types) {
+                module.putClassLoader(type.getName(), type.getClassLoader());
+            }
+        }
+        return this;
+    }
+
+    public Module module() {
+        return module;
+    }
+}

Modified: 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=1544991&r1=1544990&r2=1544991&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactory.java (original)
+++ cayenne/main/trunk/cayenne-server/src/main/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactory.java Sun Nov 24 15:17:38 2013
@@ -43,7 +43,7 @@ class SplitClassLoaderAdhocObjectFactory
     public ClassLoader getClassLoader(String resourceName) {
 
         if (resourceName == null || resourceName.length() < 2) {
-            return applicationClassLoader(resourceName);
+            return resourceClassLoader(resourceName);
         }
 
         String normalizedName = resourceName.charAt(0) == '/' ? resourceName.substring(1) : resourceName;
@@ -53,11 +53,11 @@ class SplitClassLoaderAdhocObjectFactory
                     : cayenneServerClassLoader();
         }
 
-        return applicationClassLoader(resourceName);
+        return resourceClassLoader(resourceName);
     }
 
-    protected ClassLoader applicationClassLoader(String resourceName) {
-        return osgiEnvironment.applicationClassLoader(resourceName);
+    protected ClassLoader resourceClassLoader(String resourceName) {
+        return osgiEnvironment.resourceClassLoader(resourceName);
     }
 
     protected ClassLoader cayenneDiClassLoader() {

Modified: 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=1544991&r1=1544990&r2=1544991&view=diff
==============================================================================
--- cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactoryTest.java (original)
+++ cayenne/main/trunk/cayenne-server/src/test/java/org/apache/cayenne/configuration/osgi/SplitClassLoaderAdhocObjectFactoryTest.java Sun Nov 24 15:17:38 2013
@@ -32,7 +32,7 @@ public class SplitClassLoaderAdhocObject
         final ClassLoader serverCl = mock(ClassLoader.class);
 
         OsgiEnvironment osgiEnvironment = mock(OsgiEnvironment.class);
-        when(osgiEnvironment.applicationClassLoader(anyString())).thenReturn(appCl);
+        when(osgiEnvironment.resourceClassLoader(anyString())).thenReturn(appCl);
         when(osgiEnvironment.cayenneDiClassLoader()).thenReturn(diCl);
         when(osgiEnvironment.cayenneServerClassLoader()).thenReturn(serverCl);