Search in sources :

Example 1 with Container

use of org.talend.sdk.component.container.Container in project component-runtime by Talend.

the class ComponentResource method getDetail.

/**
 * Returns the set of metadata about a few components identified by their 'id'.
 *
 * @param language the language for display names/placeholders/....
 * @param ids the component identifiers to request.
 * @return the list of details for the requested components.
 */
// TODO: max ids.length
@GET
// bulk mode to avoid to fetch components one by one when reloading a pipeline/job
@Path("details")
public ComponentDetailList getDetail(@QueryParam("language") @DefaultValue("en") final String language, @QueryParam("identifiers") final String[] ids) {
    if (ids == null || ids.length == 0) {
        return new ComponentDetailList(emptyList());
    }
    final Map<String, ErrorPayload> errors = new HashMap<>();
    final List<ComponentDetail> details = Stream.of(ids).map(id -> ofNullable(componentDao.findById(id)).orElseGet(() -> {
        errors.put(id, new ErrorPayload(COMPONENT_MISSING, "No component '" + id + "'"));
        return null;
    })).filter(Objects::nonNull).map(meta -> {
        final Optional<Container> plugin = manager.findPlugin(meta.getParent().getPlugin());
        if (!plugin.isPresent()) {
            errors.put(meta.getId(), new ErrorPayload(PLUGIN_MISSING, "No plugin '" + meta.getParent().getPlugin() + "'"));
            return null;
        }
        final Container container = plugin.get();
        final Optional<DesignModel> model = ofNullable(meta.get(DesignModel.class));
        if (!model.isPresent()) {
            errors.put(meta.getId(), new ErrorPayload(DESIGN_MODEL_MISSING, "No design model '" + meta.getId() + "'"));
            return null;
        }
        final Locale locale = localeMapper.mapLocale(language);
        final ComponentDetail componentDetail = new ComponentDetail();
        componentDetail.setLinks(emptyList());
        componentDetail.setId(createMetaId(container, meta));
        componentDetail.setVersion(meta.getVersion());
        componentDetail.setIcon(meta.getIcon());
        componentDetail.setInputFlows(model.get().getInputFlows());
        componentDetail.setOutputFlows(model.get().getOutputFlows());
        componentDetail.setType(ComponentFamilyMeta.ProcessorMeta.class.isInstance(meta) ? "processor" : "input");
        componentDetail.setDisplayName(meta.findBundle(container.getLoader(), locale).displayName().orElse(meta.getName()));
        componentDetail.setProperties(propertiesService.buildProperties(meta.getParameterMetas(), container.getLoader(), locale, null).collect(toList()));
        componentDetail.setActions(actionsService.findActions(meta.getParent().getName(), container, locale, meta));
        return componentDetail;
    }).filter(Objects::nonNull).collect(toList());
    if (!errors.isEmpty()) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(errors).build());
    }
    return new ComponentDetailList(details);
}
Also used : Locale(java.util.Locale) WebApplicationException(javax.ws.rs.WebApplicationException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DesignModel(org.talend.sdk.component.design.extension.DesignModel) ComponentDetailList(org.talend.sdk.component.server.front.model.ComponentDetailList) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Container(org.talend.sdk.component.container.Container) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) ComponentDetail(org.talend.sdk.component.server.front.model.ComponentDetail) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with Container

use of org.talend.sdk.component.container.Container in project component-runtime by Talend.

the class ComponentResource method familyIcon.

/**
 * Returns a particular family icon in raw bytes.
 *
 * @param id the family identifier.
 * @return the family icon in binary form.
 */
@GET
@Path("icon/family/{id}")
public Response familyIcon(@PathParam("id") final String id) {
    // todo: add caching if SvgIconResolver becomes used a lot - not the case ATM
    final ComponentFamilyMeta meta = componentFamilyDao.findById(id);
    if (meta == null) {
        return Response.status(Response.Status.NOT_FOUND).entity(new ErrorPayload(ErrorDictionary.COMPONENT_MISSING, "No family for identifier: " + id)).type(APPLICATION_JSON_TYPE).build();
    }
    final Optional<Container> plugin = manager.findPlugin(meta.getPlugin());
    if (!plugin.isPresent()) {
        return Response.status(Response.Status.NOT_FOUND).entity(new ErrorPayload(ErrorDictionary.PLUGIN_MISSING, "No plugin '" + meta.getPlugin() + "' for identifier: " + id)).type(APPLICATION_JSON_TYPE).build();
    }
    final IconResolver.Icon iconContent = iconResolver.resolve(plugin.get().getLoader(), meta.getIcon());
    if (iconContent == null) {
        return Response.status(Response.Status.NOT_FOUND).entity(new ErrorPayload(ErrorDictionary.ICON_MISSING, "No icon for family identifier: " + id)).type(APPLICATION_JSON_TYPE).build();
    }
    return Response.ok(iconContent.getBytes()).type(iconContent.getType()).build();
}
Also used : ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Container(org.talend.sdk.component.container.Container) IconResolver(org.talend.sdk.component.server.service.IconResolver) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with Container

use of org.talend.sdk.component.container.Container in project component-runtime by Talend.

the class ComponentManagerTest method doCheckRegistry.

private void doCheckRegistry(final File plugin1, final File plugin2, final ComponentManager manager) throws Exception {
    Stream.of(plugin1, plugin2).map(File::getAbsolutePath).forEach(manager::addPlugin);
    final List<ContainerComponentRegistry> registries = manager.find(c -> Stream.of(c.get(ContainerComponentRegistry.class))).collect(toList());
    // we saw both plugin
    assertEquals(2, registries.size());
    registries.forEach(registry -> {
        final Container container = manager.find(c -> registry == c.get(ContainerComponentRegistry.class) ? Stream.of(c) : Stream.empty()).findFirst().get();
        assertEquals(1, registry.getServices().size());
        assertNotNull(registry.getServices().iterator().next().getInstance());
        final Collection<ServiceMeta.ActionMeta> actions = registry.getServices().iterator().next().getActions();
        assertEquals(1, actions.size());
        assertEquals(pluginGenerator.toPackage(container.getId()) + ".AModel", actions.iterator().next().getInvoker().apply(null).getClass().getName());
        assertEquals(1, registry.getComponents().size());
        registry.getComponents().forEach((name, component) -> {
            assertEquals("comp", name);
            assertEquals(name, component.getName());
            assertEquals(singletonList("Misc"), component.getCategories());
            assertEquals(1, component.getProcessors().size());
            component.getProcessors().forEach((procName, processorMeta) -> {
                assertEquals("proc", procName);
                assertEquals("default", processorMeta.getIcon());
                final String packageName = pluginGenerator.toPackage(container.getId());
                final Processor processor = processorMeta.getInstantiator().apply(emptyMap());
                assertNotNull(processor);
                final Object model;
                try {
                    final String className = packageName + ".AModel";
                    model = container.getLoader().loadClass(className).getConstructor().newInstance();
                } catch (final Exception e) {
                    fail(e.getMessage());
                    throw new IllegalArgumentException(e);
                }
                runProcessorLifecycle(model, processor);
            });
        });
    });
    // now try to execute the processor outside the correct TCCL and ensure it still
    // works
    final Container container = manager.find(Stream::of).findFirst().get();
    final String packageName = pluginGenerator.toPackage(container.getId());
    final ContainerComponentRegistry registry = container.get(ContainerComponentRegistry.class);
    final ComponentFamilyMeta componentFamilyMeta = registry.getComponents().values().iterator().next();
    final ComponentFamilyMeta.ProcessorMeta processorMeta = componentFamilyMeta.getProcessors().values().iterator().next();
    final Processor processor = processorMeta.getInstantiator().apply(emptyMap());
    final Object aModel = container.getLoader().loadClass(packageName + ".AModel").getConstructor().newInstance();
    runProcessorLifecycle(aModel, processor);
    // finally ensure it is serializable
    DynamicContainerFinder.LOADERS.clear();
    manager.find(Stream::of).forEach(c -> DynamicContainerFinder.LOADERS.put(c.getId(), c.getLoader()));
    runProcessorLifecycle(aModel, copy(processor, container.getLoader()));
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) AttributeNotFoundException(javax.management.AttributeNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) PluginGenerator(org.talend.sdk.component.runtime.manager.asm.PluginGenerator) ObjectInputStream(java.io.ObjectInputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collections.singletonList(java.util.Collections.singletonList) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ByteArrayInputStream(java.io.ByteArrayInputStream) WithTemporaryFolder(org.talend.sdk.component.junit.base.junit5.WithTemporaryFolder) MBeanServer(javax.management.MBeanServer) DynamicContainerFinder(org.talend.sdk.component.runtime.manager.serialization.DynamicContainerFinder) ObjectOutputStream(java.io.ObjectOutputStream) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ManagementFactory(java.lang.management.ManagementFactory) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) Collections.emptyMap(java.util.Collections.emptyMap) Container(org.talend.sdk.component.container.Container) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) IOException(java.io.IOException) UUID(java.util.UUID) ObjectName(javax.management.ObjectName) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) Processor(org.talend.sdk.component.runtime.output.Processor) EnhancedObjectInputStream(org.talend.sdk.component.runtime.serialization.EnhancedObjectInputStream) File(java.io.File) Test(org.junit.jupiter.api.Test) MBeanException(javax.management.MBeanException) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TemporaryFolder(org.talend.sdk.component.junit.base.junit5.TemporaryFolder) Processor(org.talend.sdk.component.runtime.output.Processor) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) IOException(java.io.IOException) MBeanException(javax.management.MBeanException) Container(org.talend.sdk.component.container.Container) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectOutputStream(java.io.ObjectOutputStream) EnhancedObjectInputStream(org.talend.sdk.component.runtime.serialization.EnhancedObjectInputStream) Stream(java.util.stream.Stream)

Example 4 with Container

use of org.talend.sdk.component.container.Container 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)

Example 5 with Container

use of org.talend.sdk.component.container.Container in project component-runtime by Talend.

the class ContainerManagerTest method autoContainerId.

@Test
void autoContainerId(final TempJars jars) {
    Stream.of("ziplock-7.00.3.jar", "ziplock-7.3.jar", "ziplock-7.3-SNAPSHOT.jar", "ziplock-7.3.0-SNAPSHOT.jar").forEach(jarName -> {
        try (final ContainerManager manager = createDefaultManager()) {
            final File module = createZiplockJar(jars);
            final File jar = new File(module.getParentFile(), jarName);
            assertTrue(module.renameTo(jar));
            final Container container = manager.builder(jar.getAbsolutePath()).create();
            assertEquals("ziplock", /* no version, no .jar */
            container.getId());
        }
    });
}
Also used : ContainerManager(org.talend.sdk.component.container.ContainerManager) Container(org.talend.sdk.component.container.Container) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

Container (org.talend.sdk.component.container.Container)13 ComponentManager (org.talend.sdk.component.runtime.manager.ComponentManager)6 File (java.io.File)4 Stream (java.util.stream.Stream)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 Test (org.junit.jupiter.api.Test)4 ErrorPayload (org.talend.sdk.component.server.front.model.error.ErrorPayload)4 IOException (java.io.IOException)3 Collection (java.util.Collection)3 Locale (java.util.Locale)3 Optional.ofNullable (java.util.Optional.ofNullable)3 ComponentFamilyMeta (org.talend.sdk.component.runtime.manager.ComponentFamilyMeta)3 Collections.singletonList (java.util.Collections.singletonList)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Collectors.toList (java.util.stream.Collectors.toList)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Data (lombok.Data)2 ContainerManager (org.talend.sdk.component.container.ContainerManager)2 ContainerComponentRegistry (org.talend.sdk.component.runtime.manager.ContainerComponentRegistry)2