Search in sources :

Example 1 with ConfigurableClassLoader

use of org.talend.sdk.component.classloader.ConfigurableClassLoader in project component-runtime by Talend.

the class BeamIOTransformer method transform.

@Override
public byte[] transform(final ClassLoader loader, final String className, final Class<?> classBeingRedefined, final ProtectionDomain protectionDomain, final byte[] classfileBuffer) {
    if (className == null || !ConfigurableClassLoader.class.isInstance(loader)) {
        return classfileBuffer;
    }
    final ConfigurableClassLoader classLoader = ConfigurableClassLoader.class.cast(loader);
    final URLClassLoader tmpLoader = classLoader.createTemporaryCopy();
    final Thread thread = Thread.currentThread();
    final ClassLoader old = thread.getContextClassLoader();
    thread.setContextClassLoader(tmpLoader);
    try {
        if (shouldForceContextualSerialization(tmpLoader, className)) {
            return rewrite(classLoader, className, classfileBuffer, tmpLoader);
        }
    } finally {
        thread.setContextClassLoader(old);
    }
    return classfileBuffer;
}
Also used : ConfigurableClassLoader(org.talend.sdk.component.classloader.ConfigurableClassLoader) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) ConfigurableClassLoader(org.talend.sdk.component.classloader.ConfigurableClassLoader)

Example 2 with ConfigurableClassLoader

use of org.talend.sdk.component.classloader.ConfigurableClassLoader in project component-runtime by Talend.

the class NestedJarArchiveTest method xbeanNestedScanning.

@Test
void xbeanNestedScanning(final TestInfo info, final TemporaryFolder temporaryFolder) throws IOException {
    final File jar = createPlugin(temporaryFolder.getRoot(), info.getTestMethod().get().getName());
    final ConfigurableClassLoader configurableClassLoader = new ConfigurableClassLoader("", new URL[0], new URLClassLoader(new URL[] { jar.toURI().toURL() }, Thread.currentThread().getContextClassLoader()), n -> true, n -> true, new String[] { "com/foo/bar/1.0/bar-1.0.jar" });
    try (final JarInputStream jis = new JarInputStream(configurableClassLoader.getResourceAsStream("MAVEN-INF/repository/com/foo/bar/1.0/bar-1.0.jar"))) {
        assertNotNull(jis, "test is wrongly setup, no nested jar, fix the createPlugin() method please");
        final AnnotationFinder finder = new AnnotationFinder(new NestedJarArchive(jis, configurableClassLoader));
        final List<Class<?>> annotatedClasses = finder.findAnnotatedClasses(Processor.class);
        assertEquals(1, annotatedClasses.size());
        assertEquals("org.talend.test.generated." + info.getTestMethod().get().getName() + ".Components", annotatedClasses.iterator().next().getName());
    } finally {
        URLClassLoader.class.cast(configurableClassLoader.getParent()).close();
    }
}
Also used : ConfigurableClassLoader(org.talend.sdk.component.classloader.ConfigurableClassLoader) JarInputStream(java.util.jar.JarInputStream) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) Test(org.junit.jupiter.api.Test)

Aggregations

URLClassLoader (java.net.URLClassLoader)2 ConfigurableClassLoader (org.talend.sdk.component.classloader.ConfigurableClassLoader)2 File (java.io.File)1 URL (java.net.URL)1 JarInputStream (java.util.jar.JarInputStream)1 AnnotationFinder (org.apache.xbean.finder.AnnotationFinder)1 Test (org.junit.jupiter.api.Test)1