Uploaded image for project: 'Aries'
  1. Aries
  2. ARIES-1447

JPAWeavingHook transforming with wrong classloader

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Not A Problem
    • jpa-2.2.0
    • None
    • JPA
    • None
    • jpa 1.0.1

    • Patch

    Description

      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.

      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() {
      
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            tuomas_kiviaho Tuomas Kiviaho
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: