aries-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tuomas Kiviaho (JIRA)" <j...@apache.org>
Subject [jira] [Created] (ARIES-1447) JPAWeavingHook transforming with wrong classloader
Date Mon, 02 Nov 2015 11:45:27 GMT
Tuomas Kiviaho created ARIES-1447:
-------------------------------------

             Summary: JPAWeavingHook transforming with wrong classloader
                 Key: ARIES-1447
                 URL: https://issues.apache.org/jira/browse/ARIES-1447
             Project: Aries
          Issue Type: Bug
          Components: JPA
    Affects Versions: jpa-2.2.0
         Environment: jpa 1.0.1
            Reporter: Tuomas Kiviaho


{{PersistenceUnitInfo}} has a {{getNewTempClassLoader}} which includes both provider and persistence
unit classloader. The provider classloader is important when enhancing the class, but now
the transformations is only done against persistence unit classloader that doesn't yet have
needed the dynamic imports in place.

As a workaround, I am mimicking the proper approach by using {{TempBundleDelegatingClassLoader}}
in my patched version of {{WrappingTransformer}}

BTW: The {{org.apache.aries.jpa.container.weaving.packages}} (that seems to be completely
removed from 2.x???) could be replaced with ASM code that looks for referenced packages before
and after transformation and adds the difference to dynamic imports. How I'm adding packages
manually because the wildcard approach just doesn't seem right.

{code:title=org/apache/aries/jpa/container/weaving/impl/WrappingTransformer.java }
@@ -26,7 +26,9 @@
 import javax.persistence.spi.ClassTransformer;
 
 import org.apache.aries.jpa.container.impl.NLS;
+import org.apache.aries.jpa.container.unit.impl.TempBundleDelegatingClassLoader;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleReference;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.wiring.BundleCapability;
@@ -35,6 +37,7 @@
 
 class WrappingTransformer implements ClassTransformer {
   private final ClassTransformer delegate;
+  private final ServiceReference<?> persistenceProvider;
   private final Collection<String> packageImportsToAdd = new HashSet<String>();
   
   public WrappingTransformer(ClassTransformer delegate,
@@ -48,6 +51,7 @@
     }
     
     this.delegate = delegate;
+    this.persistenceProvider = persistenceProvider;
     
     Object packages = persistenceProvider.getProperty("org.apache.aries.jpa.container.weaving.packages");
     
@@ -56,7 +60,7 @@
         packageImportsToAdd.add(s);
       }
     } else {
-      Bundle provider = persistenceProvider.getBundle();
+      Bundle provider = persistenceProvider.getBundle(); 
       String suffix = ";" + Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE + "=" +
       provider.getSymbolicName() + ";" + Constants.BUNDLE_VERSION_ATTRIBUTE 
       + "=" + provider.getVersion();
@@ -70,11 +74,25 @@
 
   public WrappingTransformer(ClassTransformer transformer) {
     delegate = transformer;
+    persistenceProvider = null;
   }
 
   public byte[] transform(ClassLoader arg0, String arg1, Class<?> arg2,
       ProtectionDomain arg3, byte[] arg4) throws IllegalClassFormatException {
-    return delegate.transform(arg0, arg1, arg2, arg3, arg4);
+    Bundle bundle = this.persistenceProvider.getBundle();
+    BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
+    ClassLoader classLoader = bundleWiring.getClassLoader();
+    BundleReference bundleReference = (BundleReference) arg0;
+    bundle = bundleReference.getBundle();
+    classLoader = new TempBundleDelegatingClassLoader(bundle, classLoader);
+    Thread thread = Thread.currentThread();
+    ClassLoader contextClassLoader = thread.getContextClassLoader();
+    thread.setContextClassLoader(classLoader);
+    try {
+      return delegate.transform(classLoader, arg1, arg2, arg3, arg4);
+    } finally {
+      thread.setContextClassLoader(contextClassLoader);
+    }
   }
   
   public Collection<String> getPackagesToAdd() {

{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message