Search in sources :

Example 11 with Container

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

the class DocumentationResource method getDocumentation.

/**
 * Returns an asciidoctor version of the documentation for the component represented by its identifier `id`.
 *
 * Format can be either asciidoc or html - if not it will fallback on asciidoc - and if html is selected you get
 * a partial document.
 *
 * IMPORTANT: it is recommended to use asciidoc format and handle the conversion on your side if you can,
 * the html flavor handles a limited set of the asciidoc syntax only like plain arrays, paragraph and titles.
 *
 * The documentation will likely be the family documentation but you can use anchors to access a particular
 * component (_componentname_inlowercase).
 *
 * @param id the component identifier.
 * @param language the expected language for the documentation (default to en if not found).
 * @param format the expected format (asciidoc or html).
 * @return the documentation for that component.
 */
@GET
@Path("component/{id}")
@Produces(MediaType.APPLICATION_JSON)
public DocumentationContent getDocumentation(@PathParam("id") final String id, @QueryParam("language") @DefaultValue("en") final String language, @QueryParam("format") @DefaultValue("asciidoc") final String format) {
    final Locale locale = localeMapper.mapLocale(language);
    final Container container = ofNullable(componentDao.findById(id)).map(meta -> manager.findPlugin(meta.getParent().getPlugin()).orElseThrow(() -> new WebApplicationException(Response.status(NOT_FOUND).entity(new ErrorPayload(ErrorDictionary.PLUGIN_MISSING, "No plugin '" + meta.getParent().getPlugin() + "'")).build()))).orElseThrow(() -> new WebApplicationException(Response.status(NOT_FOUND).entity(new ErrorPayload(ErrorDictionary.COMPONENT_MISSING, "No component '" + id + "'")).build()));
    // rendering to html can be slow so do it lazily and once
    DocumentationCache cache = container.get(DocumentationCache.class);
    if (cache == null) {
        synchronized (container) {
            cache = container.get(DocumentationCache.class);
            if (cache == null) {
                cache = new DocumentationCache();
                container.set(DocumentationCache.class, cache);
            }
        }
    }
    return cache.documentations.computeIfAbsent(new DocKey(id, language, format), key -> {
        // todo: handle i18n properly, for now just fallback on not suffixed version and assume the dev put it
        // in the comp
        final String content = Stream.of("documentation_" + locale.getLanguage() + ".adoc", "documentation_" + language + ".adoc", "documentation.adoc").map(name -> container.getLoader().getResource("TALEND-INF/" + name)).filter(Objects::nonNull).findFirst().map(url -> {
            try (final BufferedReader stream = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) {
                return stream.lines().collect(joining("\n"));
            } catch (final IOException e) {
                throw new WebApplicationException(Response.status(INTERNAL_SERVER_ERROR).entity(new ErrorPayload(ErrorDictionary.UNEXPECTED, e.getMessage())).build());
            }
        }).map(value -> {
            switch(format) {
                case "html":
                case "html5":
                    return adoc.toHtml(value);
                case "asciidoc":
                case "adoc":
                default:
                    return value;
            }
        }).orElseThrow(() -> new WebApplicationException(Response.status(NOT_FOUND).entity(new ErrorPayload(ErrorDictionary.COMPONENT_MISSING, "No component '" + id + "'")).build()));
        return new DocumentationContent(format, content);
    });
}
Also used : Locale(java.util.Locale) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) DocumentationContent(org.talend.sdk.component.server.front.model.DocumentationContent) LocaleMapper(org.talend.sdk.component.server.service.LocaleMapper) QueryParam(javax.ws.rs.QueryParam) Locale(java.util.Locale) DefaultValue(javax.ws.rs.DefaultValue) Container(org.talend.sdk.component.container.Container) AsciidoctorService(org.talend.sdk.component.server.service.AsciidoctorService) Optional.ofNullable(java.util.Optional.ofNullable) INTERNAL_SERVER_ERROR(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR) NOT_FOUND(javax.ws.rs.core.Response.Status.NOT_FOUND) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors.joining(java.util.stream.Collectors.joining) StandardCharsets(java.nio.charset.StandardCharsets) ErrorDictionary(org.talend.sdk.component.server.front.model.ErrorDictionary) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) ComponentDao(org.talend.sdk.component.server.dao.ComponentDao) Data(lombok.Data) WebApplicationException(javax.ws.rs.WebApplicationException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) BufferedReader(java.io.BufferedReader) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) WebApplicationException(javax.ws.rs.WebApplicationException) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) DocumentationContent(org.talend.sdk.component.server.front.model.DocumentationContent) Container(org.talend.sdk.component.container.Container) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Objects(java.util.Objects) BufferedReader(java.io.BufferedReader) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 12 with Container

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

the class ComponentMetadataMojo method doWork.

@Override
protected void doWork(final ComponentManager manager, final Container container, final ContainerComponentRegistry registry) throws MojoExecutionException, MojoFailureException {
    final File output = new File(classes, location);
    if (!output.getParentFile().exists() && !output.getParentFile().mkdirs()) {
        throw new MojoExecutionException("Can't create " + output);
    }
    final Collection<Component> components = registry.getComponents().values().stream().flatMap(c -> Stream.concat(c.getPartitionMappers().values().stream().map(p -> new Component(p.getParent().getCategories(), p.getParent().getName(), p.getName(), p.findBundle(container.getLoader(), Locale.ENGLISH).displayName().orElse(p.getName()), p.getIcon(), emptyList(), singletonList("MAIN"))), c.getProcessors().values().stream().map(p -> {
        final Method listener = p.getListener();
        return new Component(p.getParent().getCategories(), p.getParent().getName(), p.getName(), p.findBundle(container.getLoader(), Locale.ENGLISH).displayName().orElse(p.getName()), p.getIcon(), getDesignModel(p).getInputFlows(), getDesignModel(p).getOutputFlows());
    }))).collect(toList());
    try (final Jsonb mapper = inPluginContext(JsonbBuilder::newBuilder).withConfig(new JsonbConfig().setProperty("johnzon.cdi.activated", false).setProperty("johnzon.attributeOrder", String.CASE_INSENSITIVE_ORDER)).build()) {
        container.execute(() -> {
            try {
                mapper.toJson(new ComponentContainer(components), new FileOutputStream(output));
            } catch (final FileNotFoundException e) {
                throw new IllegalStateException(e);
            }
            getLog().info("Created " + output);
            return null;
        });
    } catch (final Exception e) {
        throw new MojoExecutionException(e.getMessage());
    }
}
Also used : JsonbBuilder(javax.json.bind.JsonbBuilder) ContainerComponentRegistry(org.talend.sdk.component.runtime.manager.ContainerComponentRegistry) Container(org.talend.sdk.component.container.Container) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) FileOutputStream(java.io.FileOutputStream) Parameter(org.apache.maven.plugins.annotations.Parameter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) JsonbConfig(javax.json.bind.JsonbConfig) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Collections.singletonList(java.util.Collections.singletonList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Mojo(org.apache.maven.plugins.annotations.Mojo) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Locale(java.util.Locale) PROCESS_CLASSES(org.apache.maven.plugins.annotations.LifecyclePhase.PROCESS_CLASSES) Data(lombok.Data) Jsonb(javax.json.bind.Jsonb) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) COMPILE_PLUS_RUNTIME(org.apache.maven.plugins.annotations.ResolutionScope.COMPILE_PLUS_RUNTIME) AllArgsConstructor(lombok.AllArgsConstructor) Method(java.lang.reflect.Method) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) JsonbBuilder(javax.json.bind.JsonbBuilder) FileNotFoundException(java.io.FileNotFoundException) Method(java.lang.reflect.Method) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Jsonb(javax.json.bind.Jsonb) JsonbConfig(javax.json.bind.JsonbConfig) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 13 with Container

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

the class ContainerManagerTest method manage.

// note: in this method we could keep a ref of list or finds but
// we abuse intentionally of the manager methods
// to ensure they are reentrant
@Test
void manage(final TempJars jars) {
    final ContainerManager ref;
    final Collection<Container> containers;
    try (final ContainerManager manager = createDefaultManager()) {
        ref = manager;
        final Container container = manager.builder("foo", createZiplockJar(jars).getAbsolutePath()).create();
        assertNotNull(container);
        // container is not tested here but in ContainerTest. Here we just take care
        // of the manager.
        assertEquals(1, manager.findAll().size());
        assertEquals(singletonList(container), new ArrayList<>(manager.findAll()));
        assertTrue(manager.find("foo").isPresent());
        assertEquals(container, manager.find("foo").get());
        final Container xbean = manager.builder("bar", createZiplockJar(jars).getAbsolutePath()).create();
        assertEquals(2, manager.findAll().size());
        Stream.of(container, xbean).forEach(c -> assertTrue(manager.findAll().contains(c)));
        containers = manager.findAll();
    }
    // now manager is closed so all containers are cleaned up
    assertTrue(ref.isClosed());
    containers.forEach(c -> assertTrue(c.isClosed()));
}
Also used : ContainerManager(org.talend.sdk.component.container.ContainerManager) Container(org.talend.sdk.component.container.Container) 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