Search in sources :

Example 1 with LightContainer

use of org.talend.sdk.component.runtime.serialization.LightContainer in project component-runtime by Talend.

the class InterceptorTest method run.

@Test
void run(final TemporaryFolder temporaryFolder) throws Exception {
    final File pluginFolder = new File(temporaryFolder.getRoot(), "test-plugins_" + UUID.randomUUID().toString());
    pluginFolder.mkdirs();
    final File plugin = pluginGenerator.createChainPlugin(pluginFolder, "plugin.jar");
    try (final ComponentManager manager = new ComponentManager(new File("target/test-dependencies"), "META-INF/test/dependencies", "org.talend.test:type=plugin,value=%s")) {
        manager.addPlugin(plugin.getAbsolutePath());
        final List<Object> collect = manager.find(c -> c.get(ComponentManager.AllServices.class).getServices().values().stream()).filter(c -> c.getClass().getName().endsWith("SuperService$$TalendServiceProxy")).collect(toList());
        assertEquals(1, collect.size());
        final Object instance = collect.iterator().next();
        final Method method = instance.getClass().getMethod("canBeLong", int.class);
        assertEquals("exec_1/1", method.invoke(instance, 1));
        assertEquals("exec_1/1", method.invoke(instance, 1));
        sleep(450);
        assertEquals("exec_1/2", method.invoke(instance, 1));
        assertEquals("exec_3/3", method.invoke(instance, 3));
        assertEquals("exec_4/4", method.invoke(instance, 4));
        final LightContainer container = manager.find(c -> Stream.of(c.get(LightContainer.class))).findFirst().get();
        DynamicContainerFinder.LOADERS.put("plugin", container.classloader());
        DynamicContainerFinder.SERVICES.put(instance.getClass().getSuperclass(), instance);
        final Object roundTrip = roundTrip(instance);
        assertEquals(roundTrip, instance);
    } finally {
        // clean temp files
        DynamicContainerFinder.LOADERS.clear();
        DynamicContainerFinder.SERVICES.clear();
    }
}
Also used : PluginGenerator(org.talend.sdk.component.runtime.manager.asm.PluginGenerator) UUID(java.util.UUID) Serializer.roundTrip(org.talend.sdk.component.runtime.manager.test.Serializer.roundTrip) LightContainer(org.talend.sdk.component.runtime.serialization.LightContainer) File(java.io.File) Test(org.junit.jupiter.api.Test) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) WithTemporaryFolder(org.talend.sdk.component.junit.base.junit5.WithTemporaryFolder) TemporaryFolder(org.talend.sdk.component.junit.base.junit5.TemporaryFolder) DynamicContainerFinder(org.talend.sdk.component.runtime.manager.serialization.DynamicContainerFinder) Thread.sleep(java.lang.Thread.sleep) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) Method(java.lang.reflect.Method) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) LightContainer(org.talend.sdk.component.runtime.serialization.LightContainer) Method(java.lang.reflect.Method) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 2 with LightContainer

use of org.talend.sdk.component.runtime.serialization.LightContainer in project component-runtime by Talend.

the class StandaloneContainerFinder method find.

// IMPORTANT: don't abuse of lambdas here, it is on the runtime codepath
@Override
public LightContainer find(final String plugin) {
    final ComponentManager manager = ComponentManager.instance();
    Optional<Container> optionalContainer = manager.findPlugin(plugin);
    if (!optionalContainer.isPresent()) {
        log.info("Didn't find plugin " + plugin + ", had: " + manager.availablePlugins());
        // nested in current jar.
        try {
            optionalContainer = manager.findPlugin(manager.addPlugin(plugin));
        } catch (final IllegalArgumentException iae) {
            // concurrent request?
            optionalContainer = manager.findPlugin(plugin);
        }
    }
    if (optionalContainer.isPresent()) {
        final LightContainer lightContainer = optionalContainer.get().get(LightContainer.class);
        if (lightContainer != null) {
            return lightContainer;
        }
    }
    // TCCL
    return super.find(plugin);
}
Also used : Container(org.talend.sdk.component.container.Container) LightContainer(org.talend.sdk.component.runtime.serialization.LightContainer) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) LightContainer(org.talend.sdk.component.runtime.serialization.LightContainer)

Aggregations

ComponentManager (org.talend.sdk.component.runtime.manager.ComponentManager)2 LightContainer (org.talend.sdk.component.runtime.serialization.LightContainer)2 File (java.io.File)1 Thread.sleep (java.lang.Thread.sleep)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 UUID (java.util.UUID)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Test (org.junit.jupiter.api.Test)1 Container (org.talend.sdk.component.container.Container)1 TemporaryFolder (org.talend.sdk.component.junit.base.junit5.TemporaryFolder)1 WithTemporaryFolder (org.talend.sdk.component.junit.base.junit5.WithTemporaryFolder)1 PluginGenerator (org.talend.sdk.component.runtime.manager.asm.PluginGenerator)1 DynamicContainerFinder (org.talend.sdk.component.runtime.manager.serialization.DynamicContainerFinder)1 Serializer.roundTrip (org.talend.sdk.component.runtime.manager.test.Serializer.roundTrip)1