Search in sources :

Example 1 with TempClassLoader

use of org.apache.openejb.core.TempClassLoader in project tomee by apache.

the class ClassLoaderUtil method createTempClassLoader.

public static URLClassLoader createTempClassLoader(final String appId, final URL[] rawUrls, final ClassLoader parent) {
    String updatedAppId = appId;
    if (appId != null) {
        // here we often get the full path of the app as id where later it is simply the name of the file/dir
        final File file = new File(appId);
        if (file.exists()) {
            updatedAppId = file.getName();
            if (updatedAppId.endsWith(".war") || updatedAppId.endsWith(".ear")) {
                updatedAppId = updatedAppId.substring(0, updatedAppId.length() - ".war".length());
            }
        }
    }
    // from the app
    final ClassLoaderConfigurer configurer1 = QuickJarsTxtParser.parse(new File(appId, "META-INF/" + QuickJarsTxtParser.FILE_NAME));
    final ClassLoaderConfigurer configurer2 = QuickJarsTxtParser.parse(new File(appId, "WEB-INF/" + QuickJarsTxtParser.FILE_NAME));
    // external config
    ClassLoaderConfigurer configurer3 = ClassLoaderUtil.configurer(updatedAppId);
    if (configurer3 == null) {
        // try the complete path
        configurer3 = ClassLoaderUtil.configurer(appId);
    }
    final URL[] urls;
    if (configurer1 == null && configurer2 == null && configurer3 == null) {
        urls = rawUrls;
    } else {
        final CompositeClassLoaderConfigurer configurer = new CompositeClassLoaderConfigurer(configurer1, configurer2, configurer3);
        final Collection<URL> list = new ArrayList<>(Arrays.asList(rawUrls));
        ClassLoaderConfigurer.Helper.configure(list, configurer);
        urls = list.toArray(new URL[list.size()]);
    }
    return new TempClassLoader(createClassLoader(appId, urls, parent));
}
Also used : TempClassLoader(org.apache.openejb.core.TempClassLoader) CompositeClassLoaderConfigurer(org.apache.openejb.classloader.CompositeClassLoaderConfigurer) ArrayList(java.util.ArrayList) ClassLoaderConfigurer(org.apache.openejb.classloader.ClassLoaderConfigurer) CompositeClassLoaderConfigurer(org.apache.openejb.classloader.CompositeClassLoaderConfigurer) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) URL(java.net.URL)

Example 2 with TempClassLoader

use of org.apache.openejb.core.TempClassLoader in project tomee by apache.

the class JpaTest method createEntityManagerFactory.

private EntityManagerFactory createEntityManagerFactory() throws Exception {
    final PersistenceClassLoaderHandler persistenceClassLoaderHandler = new PersistenceClassLoaderHandler() {

        public void addTransformer(final String unitId, final ClassLoader classLoader, final ClassFileTransformer classFileTransformer) {
        /*
                Instrumentation instrumentation = Agent.getInstrumentation();
                instrumentation.addTransformer(classFileTransformer);
                */
        }

        public void destroy(final String unitId) {
        }

        public ClassLoader getNewTempClassLoader(final ClassLoader classLoader) {
            return new TempClassLoader(classLoader);
        }
    };
    /*
        Agent.getInstrumentation().addTransformer(new ClassFileTransformer() {
            public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
                if (!className.equals("org/apache/openejb/core/cmp/jpa/Employee")) {
                    return null;
                }
                byte[] newBytes = addNewField(classfileBuffer);
                return newBytes;
            }
        });
        */
    final PersistenceUnitInfoImpl unitInfo = new PersistenceUnitInfoImpl(persistenceClassLoaderHandler);
    unitInfo.setPersistenceUnitName("CMP");
    unitInfo.setPersistenceProviderClassName(PERSISTENCE_PROVIDER);
    unitInfo.setClassLoader(getClass().getClassLoader());
    unitInfo.setExcludeUnlistedClasses(false);
    unitInfo.setJtaDataSource(jtaDs);
    unitInfo.setNonJtaDataSource(nonJtaDs);
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.Employee");
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.Bill");
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.EmbeddedBill");
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.Person");
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.EmbeddedBillPk");
    unitInfo.getMappingFileNames().add("META-INF/jpa-test-mappings.xml");
    // Handle Properties
    final Properties properties = new Properties();
    properties.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
    properties.setProperty("openjpa.Log", "DefaultLevel=WARN");
    properties.setProperty("openjpa.RuntimeUnenhancedClasses", "supported");
    unitInfo.setProperties(properties);
    unitInfo.setTransactionType(transactionType);
    unitInfo.getManagedClassNames().add("org.apache.openejb.core.cmp.jpa.Employee");
    final PersistenceProvider persistenceProvider = (PersistenceProvider) getClass().getClassLoader().loadClass(PERSISTENCE_PROVIDER).newInstance();
    final EntityManagerFactory emf = persistenceProvider.createContainerEntityManagerFactory(unitInfo, new HashMap());
    return emf;
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) PersistenceClassLoaderHandler(org.apache.openejb.persistence.PersistenceClassLoaderHandler) TempClassLoader(org.apache.openejb.core.TempClassLoader) PersistenceUnitInfoImpl(org.apache.openejb.persistence.PersistenceUnitInfoImpl) HashMap(java.util.HashMap) PersistenceProvider(javax.persistence.spi.PersistenceProvider) EntityManagerFactory(javax.persistence.EntityManagerFactory) TempClassLoader(org.apache.openejb.core.TempClassLoader) Properties(java.util.Properties)

Example 3 with TempClassLoader

use of org.apache.openejb.core.TempClassLoader in project tomee by apache.

the class UnenhancedTest method runTest.

private void runTest(final String methodName, final PersistenceUnitTransactionType transactionType, final boolean enhance) throws Exception {
    this.enhance = enhance;
    final ClassLoader loader = new FilteredChildFirstClassLoader(getClass().getClassLoader(), "org.apache.openejb.core.cmp.jpa");
    final PersistenceClassLoaderHandler persistenceClassLoaderHandler = new PersistenceClassLoaderHandler() {

        public void addTransformer(final String unitId, final ClassLoader classLoader, final ClassFileTransformer classFileTransformer) {
            final Instrumentation instrumentation = Agent.getInstrumentation();
            if (instrumentation != null) {
                instrumentation.addTransformer(new ControllableTransformer(classFileTransformer));
            }
        }

        public void destroy(final String unitId) {
        }

        public ClassLoader getNewTempClassLoader(final ClassLoader classLoader) {
            return new TempClassLoader(classLoader);
        }
    };
    final PersistenceUnitInfoImpl unitInfo = new PersistenceUnitInfoImpl(persistenceClassLoaderHandler);
    unitInfo.setPersistenceUnitName("CMP");
    unitInfo.setPersistenceProviderClassName(PERSISTENCE_PROVIDER);
    unitInfo.setClassLoader(loader);
    unitInfo.setExcludeUnlistedClasses(false);
    unitInfo.setJtaDataSource(jtaDs);
    unitInfo.setNonJtaDataSource(nonJtaDs);
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.ComplexSuperclass");
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.ComplexSubclass");
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.ComplexStandalone");
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.GeneratedStandalone");
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.GeneratedSuperclass");
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.GeneratedSubclass");
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.OneStandalone");
    unitInfo.addManagedClassName("org.apache.openejb.core.cmp.jpa.ManyStandalone");
    // Handle Properties
    final Properties properties = new Properties();
    properties.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(SchemaAction='add,deleteTableContents',ForeignKeys=true)");
    properties.setProperty("openjpa.RuntimeUnenhancedClasses", "supported");
    properties.setProperty("openjpa.Log", "DefaultLevel=INFO");
    unitInfo.setProperties(properties);
    unitInfo.setTransactionType(transactionType);
    unitInfo.getManagedClassNames().add("org.apache.openejb.core.cmp.jpa.Employee");
    final PersistenceProvider persistenceProvider = (PersistenceProvider) getClass().getClassLoader().loadClass(PERSISTENCE_PROVIDER).newInstance();
    entityManagerFactory = persistenceProvider.createContainerEntityManagerFactory(unitInfo, new HashMap());
    // create the test object (via reflection)
    final Object testObject = loader.loadClass("org.apache.openejb.core.cmp.jpa.UnenhancedUnits").newInstance();
    set(testObject, "TransactionManager", TransactionManager.class, transactionManager);
    set(testObject, "EntityManagerFactory", EntityManagerFactory.class, entityManagerFactory);
    // invoke the test (via reflection)
    Thread.currentThread().setContextClassLoader(loader);
    invoke(testObject, "setUp");
    try {
        invoke(testObject, methodName);
    } finally {
        invoke(testObject, "tearDown");
    }
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) TempClassLoader(org.apache.openejb.core.TempClassLoader) PersistenceUnitInfoImpl(org.apache.openejb.persistence.PersistenceUnitInfoImpl) HashMap(java.util.HashMap) PersistenceProvider(javax.persistence.spi.PersistenceProvider) Instrumentation(java.lang.instrument.Instrumentation) Properties(java.util.Properties) PersistenceClassLoaderHandler(org.apache.openejb.persistence.PersistenceClassLoaderHandler) URLClassLoader(java.net.URLClassLoader) TempClassLoader(org.apache.openejb.core.TempClassLoader)

Aggregations

TempClassLoader (org.apache.openejb.core.TempClassLoader)3 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 PersistenceProvider (javax.persistence.spi.PersistenceProvider)2 PersistenceClassLoaderHandler (org.apache.openejb.persistence.PersistenceClassLoaderHandler)2 PersistenceUnitInfoImpl (org.apache.openejb.persistence.PersistenceUnitInfoImpl)2 File (java.io.File)1 Instrumentation (java.lang.instrument.Instrumentation)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 JarFile (java.util.jar.JarFile)1 ZipFile (java.util.zip.ZipFile)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 ClassLoaderConfigurer (org.apache.openejb.classloader.ClassLoaderConfigurer)1 CompositeClassLoaderConfigurer (org.apache.openejb.classloader.CompositeClassLoaderConfigurer)1