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);
});
}
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());
}
}
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()));
}
Aggregations