You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by dw...@apache.org on 2010/03/12 20:05:31 UTC

svn commit: r922397 - in /openjpa/trunk: openjpa-persistence/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/ openjpa/ openjpa/src/main/java/

Author: dwoods
Date: Fri Mar 12 19:05:30 2010
New Revision: 922397

URL: http://svn.apache.org/viewvc?rev=922397&view=rev
Log:
OPENJPA-1491 When used in an OSGi environment with Apache Aries, the org.apache.openjpa.kernel.Bootstrap should use the OpenJPA bundle classloader instead of the application bundle classloader to instatiate a BrokerFactory.  Also moved our BundleActivator from the openjpa module to openjpa-persistence module with a BundleUtils wrapper to remove direct OSGi class refs, so the BundleContext in it could be accessed by PersistenceProviderImpl.

Added:
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/BundleDelegatingClassLoader.java   (with props)
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/BundleUtils.java   (with props)
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/PersistenceActivator.java   (with props)
Removed:
    openjpa/trunk/openjpa/src/main/java/
Modified:
    openjpa/trunk/openjpa-persistence/pom.xml
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
    openjpa/trunk/openjpa/pom.xml

Modified: openjpa/trunk/openjpa-persistence/pom.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/pom.xml?rev=922397&r1=922396&r2=922397&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/pom.xml (original)
+++ openjpa/trunk/openjpa-persistence/pom.xml Fri Mar 12 19:05:30 2010
@@ -48,6 +48,13 @@
             <artifactId>geronimo-validation_1.0_spec</artifactId>
             <scope>provided</scope>
         </dependency>        
+        <!-- for osgi bundle activator code, use same level as Apache Aries -->
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <version>4.2.0</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
     
     <build>

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java?rev=922397&r1=922396&r2=922397&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java (original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java Fri Mar 12 19:05:30 2010
@@ -50,6 +50,7 @@ import org.apache.openjpa.lib.util.Optio
 import org.apache.openjpa.meta.AbstractCFMetaDataFactory;
 import org.apache.openjpa.meta.MetaDataModes;
 import org.apache.openjpa.meta.MetaDataRepository;
+import org.apache.openjpa.persistence.osgi.BundleUtils;
 import org.apache.openjpa.persistence.validation.ValidationUtils;
 import org.apache.openjpa.util.ClassResolver;
 
@@ -179,18 +180,25 @@ public class PersistenceProviderImpl
                 cp.addProperty("openjpa." + BrokerValue.KEY, getDefaultBrokerAlias());
             }
 
-            BrokerFactory factory = getBrokerFactory(cp, poolValue, pui.getClassLoader());
-            if (transformerException != null) {
-                Log log = factory.getConfiguration().getLog(OpenJPAConfiguration.LOG_RUNTIME);
-                if (log.isTraceEnabled()) {
-                    log.warn(_loc.get("transformer-registration-error-ex", pui), transformerException);
-                } else {
-                    log.warn(_loc.get("transformer-registration-error", pui));
-                }
+            // OPENJPA-1491 If running under OSGi, use the Bundle's ClassLoader instead of the application one
+            BrokerFactory factory;
+            if (BundleUtils.runningUnderOSGi()) {
+                factory = getBrokerFactory(cp, poolValue, BundleUtils.getBundleClassLoader());
+            } else {
+                factory = getBrokerFactory(cp, poolValue, pui.getClassLoader());
             }
+
             OpenJPAConfiguration conf = factory.getConfiguration();
             setPersistenceEnvironmentInfo(conf, pui);
             _log = conf.getLog(OpenJPAConfiguration.LOG_RUNTIME);
+            // now we can log any transformer exceptions from above
+            if (transformerException != null) {
+                if (_log.isTraceEnabled()) {
+                    _log.warn(_loc.get("transformer-registration-error-ex", pui), transformerException);
+                } else {
+                    _log.warn(_loc.get("transformer-registration-error", pui));
+                }
+            }
 
             // Create appropriate LifecycleEventManager
             loadValidator(factory);

Added: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/BundleDelegatingClassLoader.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/BundleDelegatingClassLoader.java?rev=922397&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/BundleDelegatingClassLoader.java (added)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/BundleDelegatingClassLoader.java Fri Mar 12 19:05:30 2010
@@ -0,0 +1,57 @@
+/*
+ * 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 WARRANTIESOR 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.openjpa.persistence.osgi;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+
+import org.osgi.framework.Bundle;
+
+/**
+ * This is a simple ClassLoader that delegates to the Bundle
+ * and is used by the PersistenceUnitInfo
+ *
+ * @version $Rev$ $Date$
+ */
+public class BundleDelegatingClassLoader extends ClassLoader {
+
+  private final Bundle bundle;
+  
+  public BundleDelegatingClassLoader(Bundle b) {
+    bundle = b;
+  }
+  
+  @Override
+  protected Class<?> findClass(String className) throws ClassNotFoundException {
+    return bundle.loadClass(className);
+  }
+
+  @Override
+  protected URL findResource(String resName) {
+    return bundle.getResource(resName);
+  }
+
+  @SuppressWarnings("unchecked")
+  @Override
+  protected Enumeration<URL> findResources(String resName) throws IOException {
+    return bundle.getResources(resName);
+  }
+
+}

Propchange: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/BundleDelegatingClassLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/BundleUtils.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/BundleUtils.java?rev=922397&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/BundleUtils.java (added)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/BundleUtils.java Fri Mar 12 19:05:30 2010
@@ -0,0 +1,66 @@
+/*
+ * 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 WARRANTIESOR 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.openjpa.persistence.osgi;
+
+// DO NOT import any org.osgi packages or classes here!
+
+/**
+ * OSGi helper class and methods.
+ * All routines MUST use reflection, so we don't have any hard-coded
+ * runtime depends on OSGi classes for Java SE and EE users.
+ *
+ * @version $Rev$ $Date$
+ */
+public class BundleUtils {
+
+    public static boolean runningUnderOSGi() {
+        try {
+            @SuppressWarnings("unused")
+            Class<?> c = Class.forName("org.osgi.framework.Bundle");
+            c = Class.forName("org.osgi.framework.BundleActivator");
+            c = Class.forName("org.osgi.framework.BundleContext");
+            c = Class.forName("org.osgi.framework.ServiceRegistration");
+            return true;
+        } catch (ClassNotFoundException e) {
+            // no-op - catch, eat and return false below
+        } catch (NoClassDefFoundError e) {
+            // no-op - catch, eat and return false below
+        }
+        return false;
+    }
+
+    /* (non-Javadoc)
+     * OPENJPA-1491 Allow us to use the OSGi Bundle's ClassLoader instead of the application one.
+     * Uses reflection so we don't have any runtime depends on OSGi classes for Java SE and EE users.
+     */
+    public static ClassLoader getBundleClassLoader() {
+        ClassLoader cl = null;
+        if (runningUnderOSGi()) {
+            try {
+                Class<?> c = Class.forName("org.apache.openjpa.persistence.osgi.PersistenceActivator");
+                cl = (ClassLoader) c.getMethod("getBundleClassLoader").invoke(null);
+            } catch (Throwable t) {
+                // fail-fast
+                throw new RuntimeException(t);
+            }
+        }
+        return cl;
+    }
+
+}

Propchange: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/BundleUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/PersistenceActivator.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/PersistenceActivator.java?rev=922397&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/PersistenceActivator.java (added)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/PersistenceActivator.java Fri Mar 12 19:05:30 2010
@@ -0,0 +1,80 @@
+/**
+ *  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.openjpa.persistence.osgi;
+
+import java.util.Hashtable;
+
+import javax.persistence.spi.PersistenceProvider;
+
+import org.apache.openjpa.persistence.PersistenceProviderImpl;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+
+/**
+ * Used to discover/resolve JPA providers in an OSGi environment.
+ *
+ * @version $Rev$ $Date$
+ */
+public class PersistenceActivator implements BundleActivator {
+    // following is so Aries can find and extend us for OSGi RFC 143
+    public static final String PERSISTENCE_PROVIDER_ARIES = "javax.persistence.provider";
+    // following would be set by Aries to expose their OSGi enabled provider
+    public static final String PERSISTENCE_PROVIDER = PersistenceProvider.class.getName();
+    public static final String OSGI_PERSISTENCE_PROVIDER = PersistenceProviderImpl.class.getName();
+    private static BundleContext ctx = null;
+    private static ServiceRegistration svcReg = null;
+
+    /* (non-Javadoc)
+     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+     */
+    public void start(BundleContext ctx) throws Exception {
+        this.ctx = ctx;
+        PersistenceProvider provider = new PersistenceProviderImpl();
+        Hashtable<String, String> props = new Hashtable<String, String>();
+        props.put(PERSISTENCE_PROVIDER_ARIES, OSGI_PERSISTENCE_PROVIDER);
+        svcReg = ctx.registerService(PERSISTENCE_PROVIDER, provider, props);
+    }
+
+    /* (non-Javadoc)
+     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+     */
+    public void stop(BundleContext ctx) throws Exception {
+        if (svcReg != null) {
+            svcReg.unregister();
+            svcReg = null;
+        }
+        this.ctx = null;
+    }
+
+    /* (non-Javadoc)
+     * OPENJPA-1491 Allow us to use the OSGi Bundle's ClassLoader instead of the application one.
+     * This class and method are dynamically loaded by BundleUtils, so any method signature changes
+     * here need to also be reflected in BundleUtils.getBundleClassLoader()
+     */
+    public static ClassLoader getBundleClassLoader() {
+        ClassLoader cl = null;
+        if (ctx != null) {
+            Bundle b = ctx.getBundle();
+            cl = new BundleDelegatingClassLoader(b);
+        }
+        return cl;
+    }
+
+}

Propchange: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/osgi/PersistenceActivator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/trunk/openjpa/pom.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa/pom.xml?rev=922397&r1=922396&r2=922397&view=diff
==============================================================================
--- openjpa/trunk/openjpa/pom.xml (original)
+++ openjpa/trunk/openjpa/pom.xml Fri Mar 12 19:05:30 2010
@@ -134,9 +134,9 @@
                     <instructions>
                         <!-- OSGi Bundle Metadata -->
                         <Bundle-DocURL>${project.url}</Bundle-DocURL>
-                        <Bundle-Activator>org.apache.openjpa.osgi.PersistenceActivator</Bundle-Activator>
+                        <Bundle-Activator>org.apache.openjpa.persistence.osgi.PersistenceActivator</Bundle-Activator>
                         <Private-Package />
-                        <Export-Package>org.apache.openjpa.*;version=${pom.version}</Export-Package>
+                        <Export-Package>org.apache.openjpa.*;version=${project.version}</Export-Package>
                         <Import-Package>com.ibm.*;resolution:=optional,org.postgresql.*;resolution:=optional,org.apache.tools.ant.*;resolution:=optional,org.apache.log4j.*;resolution:=optional,javax.activation.xa*;resolution:=optional,javax.jms.*;version="[1.1.0,1.2)";resolution:=optional,javax.transaction.*;version="[1.1.0,1.2)";resolution:=optional,javax.validation.*;version="[1.0.0,1.1)";resolution:=optional,javax.xml.bind.*;resolution:=optional,serp.*;resolution:=optional,javax.persistence.*;version="[2.0.0,2.1)",*</Import-Package>
                         <!-- Eclipse metadata -->
                         <Eclipse-Autostart>false</Eclipse-Autostart>
@@ -236,18 +236,5 @@
             <artifactId>openjpa-slice</artifactId>
             <version>${pom.version}</version>
         </dependency>
-        <!-- for osgi bundle activator code -->
-        <dependency>
-            <groupId>org.apache.felix</groupId>
-            <artifactId>org.osgi.core</artifactId>
-            <version>1.4.0</version>
-            <scope>provided</scope>
-            <exclusions>
-                <exclusion>
-                    <groupId>org.apache.felix</groupId>
-                    <artifactId>org.osgi.foundation</artifactId>
-                </exclusion>
-            </exclusions>
-        </dependency>
     </dependencies>
 </project>