From ef809f735403b4a1bd4ba29b072365abbfc2a0a5 Mon Sep 17 00:00:00 2001 From: Romain Manni-Bucau Date: Thu, 26 Oct 2017 11:41:28 +0200 Subject: [PATCH 1/2] first pass to support source component generation --- .../manager/xbean/KnownJarsFilter.java | 12 +- component-starter-server/pom.xml | 8 +- .../starter/server/front/ProjectResource.java | 59 ++- .../starter/server/model/ProjectModel.java | 12 + .../server/service/ProjectGenerator.java | 9 +- .../server/service/domain/ProjectRequest.java | 34 +- .../server/service/facet/FacetGenerator.java | 2 + .../facet/component/ComponentGenerator.java | 337 ++++++++++-------- .../generator/component/Configuration.java | 24 +- .../resources/generator/component/Mapper.java | 4 +- .../resources/generator/component/Source.java | 5 +- .../ComponentGeneratorConfigurationTest.java | 112 ++++++ .../component/ComponentGeneratorTest.java | 193 ++++++++++ 13 files changed, 607 insertions(+), 204 deletions(-) create mode 100644 component-starter-server/src/test/java/org/talend/component/starter/server/service/facet/component/ComponentGeneratorConfigurationTest.java create mode 100644 component-starter-server/src/test/java/org/talend/component/starter/server/service/facet/component/ComponentGeneratorTest.java diff --git a/component-runtime-manager/src/main/java/org/talend/component/runtime/manager/xbean/KnownJarsFilter.java b/component-runtime-manager/src/main/java/org/talend/component/runtime/manager/xbean/KnownJarsFilter.java index 9de34b47aa099..a1165b6229631 100644 --- a/component-runtime-manager/src/main/java/org/talend/component/runtime/manager/xbean/KnownJarsFilter.java +++ b/component-runtime-manager/src/main/java/org/talend/component/runtime/manager/xbean/KnownJarsFilter.java @@ -64,6 +64,7 @@ public class KnownJarsFilter implements Predicate { add("axis-"); add("axis2-"); add("batchee-jbatch"); + add("batik-"); add("bcprov-"); add("beam-sdks-"); add("beam-runners-"); @@ -80,6 +81,7 @@ public class KnownJarsFilter implements Predicate { add("charsets.jar"); add("cldrdata"); add("commons-"); + add("components-api"); add("component-api"); add("component-spi"); add("component-runtime"); @@ -91,7 +93,9 @@ public class KnownJarsFilter implements Predicate { add("cssparser-"); add("curator-"); add("cxf-"); + add("cdi-"); add("deploy.jar"); + add("deltaspike-"); add("derby-"); add("derbyclient-"); add("derbynet-"); @@ -103,6 +107,7 @@ public class KnownJarsFilter implements Predicate { add("ehcache-"); add("error_prone_annotations"); add("FastInfoset"); + add("freemarker-"); add("j2objc-annotations"); add("jackson-"); add("jaxb-"); @@ -110,6 +115,7 @@ public class KnownJarsFilter implements Predicate { add("jets3t"); add("jfr.jar"); add("jfxrt.jar"); + add("jmustache"); add("jna-"); add("jnr-"); add("johnzon-"); @@ -118,6 +124,7 @@ public class KnownJarsFilter implements Predicate { add("jolokia-serv"); add("json-simple-"); add("json4s-"); + add("jsr250-"); add("fusemq-leveldb-"); add("geronimo-"); add("google-"); @@ -213,8 +220,7 @@ public class KnownJarsFilter implements Predicate { add("mapstruct-"); add("maven-"); add("mbean-annotation-api-"); - add("meecrowave-maven-"); - add("meecrowave-gradle-"); + add("meecrowave-"); add("mesos-"); add("metrics-"); add("mimepull-"); @@ -294,6 +300,7 @@ public class KnownJarsFilter implements Predicate { add("swizzle-"); add("sxc-"); add("tachyon-"); + add("talend-icon"); add("testng-"); add("tomcat-annotations"); add("tomcat-api"); @@ -338,6 +345,7 @@ public class KnownJarsFilter implements Predicate { add("xml-resolver-"); add("xmlbeans-"); add("xmlenc-"); + add("xmlgraphics-"); add("xmlpull-"); add("xmlrpc-"); add("xmlschema-"); diff --git a/component-starter-server/pom.xml b/component-starter-server/pom.xml index 2ba8985487959..168b0b63fdb9d 100644 --- a/component-starter-server/pom.xml +++ b/component-starter-server/pom.xml @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, + distributed under the License is distributed on an "AS model.getCategory())IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. @@ -86,6 +86,12 @@ ${project.version} provided true + + + * + * + + diff --git a/component-starter-server/src/main/java/org/talend/component/starter/server/front/ProjectResource.java b/component-starter-server/src/main/java/org/talend/component/starter/server/front/ProjectResource.java index f103bfeedbf51..f38f98ffc9abd 100644 --- a/component-starter-server/src/main/java/org/talend/component/starter/server/front/ProjectResource.java +++ b/component-starter-server/src/main/java/org/talend/component/starter/server/front/ProjectResource.java @@ -1,17 +1,17 @@ /** - * Copyright (C) 2006-2017 Talend Inc. - www.talend.com + * Copyright (C) 2006-2017 Talend Inc. - www.talend.com * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package org.talend.component.starter.server.front; @@ -97,34 +97,33 @@ private ProjectRequest toRequest(final ProjectModel model) { packageBase = "component"; } - return new ProjectRequest( - ofNullable(model.getBuildType()).orElse("maven").toLowerCase(Locale.ENGLISH), + return new ProjectRequest(ofNullable(model.getBuildType()).orElse("maven").toLowerCase(Locale.ENGLISH), new ProjectRequest.BuildConfiguration( ofNullable(model.getName()).orElse("A Talend generated Component Starter Project"), ofNullable(model.getDescription()).orElse("An application generated by the Talend Component Kit Starter"), - "jar", - packageBase, - ofNullable(model.getArtifact()).orElse("application"), - ofNullable(model.getVersion()).orElse("0.0.1-SNAPSHOT"), - "1.8"), + "jar", packageBase, ofNullable(model.getArtifact()).orElse("application"), + ofNullable(model.getVersion()).orElse("0.0.1-SNAPSHOT"), "1.8"), ofNullable(model.getPackageBase()).orElse("com.application").replace('/', '.'), ofNullable(model.getFacets()).orElse(emptyList()), - ofNullable(model.getSources()) - .map(s -> s.stream() - .map(i -> new ProjectRequest.SourceConfiguration(i.getName(), i.isGenericOutput(), toStructure(i.getOutputStructure()))) - .collect(toList())) - .orElse(emptyList()), - ofNullable(model.getProcessors()) - .map(s -> s.stream().map(i -> new ProjectRequest.ProcessorConfiguration(i.getName(), i.isGenericInputs(), i.isGenericOutputs(), + ofNullable(model.getSources()).map(s -> s.stream() + .map(i -> new ProjectRequest.SourceConfiguration(i.getName(), i.getIcon(), i.isGenericOutput(), + toStructure(i.getConfigurationStructure()), toStructure(i.getOutputStructure()))) + .collect(toList())).orElse(emptyList()), + ofNullable(model.getProcessors()).map(s -> s.stream() + .map(i -> new ProjectRequest.ProcessorConfiguration(i.getName(), i.getIcon(), + toStructure(i.getConfigurationStructure()), i.isGenericInputs(), i.isGenericOutputs(), i.getInputs(), i.getOutputs(), - ofNullable(i.getInputStructures()).map(is -> is.stream().map(this::toStructure).collect(toList())).orElse(emptyList()), - ofNullable(i.getOutputStructures()).map(is -> is.stream().map(this::toStructure).collect(toList())).orElse(emptyList()))) - .collect(toList())) - .orElse(emptyList())); + ofNullable(i.getInputStructures()).map(is -> is.stream().map(this::toStructure).collect(toList())) + .orElse(emptyList()), + ofNullable(i.getOutputStructures()) + .map(is -> is.stream().map(this::toStructure).collect(toList())).orElse(emptyList()))) + .collect(toList())).orElse(emptyList()), + model.getFamily(), model.getCategory()); } private ProjectRequest.DataStructure toStructure(final ProjectModel.Model model) { - return new ProjectRequest.DataStructure(model.getEntries() == null ? emptyList() : model.getEntries().stream() - .map(e -> new ProjectRequest.Entry(e.getName(), e.getType())).collect(toList())); + return new ProjectRequest.DataStructure(model.getEntries() == null ? emptyList() + : model.getEntries().stream().map(e -> new ProjectRequest.Entry(e.getName(), e.getType(), + e.getModel() != null ? toStructure(e.getModel()) : null)).collect(toList())); } } diff --git a/component-starter-server/src/main/java/org/talend/component/starter/server/model/ProjectModel.java b/component-starter-server/src/main/java/org/talend/component/starter/server/model/ProjectModel.java index 30b161cd543e6..9388b8255196c 100644 --- a/component-starter-server/src/main/java/org/talend/component/starter/server/model/ProjectModel.java +++ b/component-starter-server/src/main/java/org/talend/component/starter/server/model/ProjectModel.java @@ -30,6 +30,8 @@ public class ProjectModel { private String name; private String description; private String packageBase; + private String family; + private String category; // private String packaging; // jar only for now // private String javaVersion; // enforce to java 8 for now private Collection facets; @@ -42,8 +44,12 @@ public static class Source { private final String name; + private final String icon; + private final boolean genericOutput; + private final Model configurationStructure; + private final Model outputStructure; } @@ -61,6 +67,8 @@ public static class Entry { private final String name; private final String type; + + private final Model model; } @Getter @@ -69,6 +77,10 @@ public static class Processor { private final String name; + private final String icon; + + private final Model configurationStructure; + private final boolean genericInputs; private final boolean genericOutputs; diff --git a/component-starter-server/src/main/java/org/talend/component/starter/server/service/ProjectGenerator.java b/component-starter-server/src/main/java/org/talend/component/starter/server/service/ProjectGenerator.java index 3049ece9565ce..c9883a8aa5326 100644 --- a/component-starter-server/src/main/java/org/talend/component/starter/server/service/ProjectGenerator.java +++ b/component-starter-server/src/main/java/org/talend/component/starter/server/service/ProjectGenerator.java @@ -138,11 +138,10 @@ public void generate(final ProjectRequest request, final OutputStream outputStre files.put("README.adoc", readmeGenerator.createReadme(request.getBuildConfiguration().getName(), filePerFacet)); } - filePerFacet.put(componentGenerator, - componentGenerator.create(request.getPackageBase(), build, facets, request.getSources(), request.getProcessors()) - .peek(file -> files.put(file.getPath(), file.getContent())) - .map(FacetGenerator.InMemoryFile::getPath) - .collect(toList())); + componentGenerator.create( + request.getPackageBase(), build, facets, request.getFamily(), request.getCategory(), + request.getSources(), request.getProcessors()) + .forEach(file -> files.put(file.getPath(), file.getContent())); // now create the zip prefixing it with the artifact value final String rootName = request.getBuildConfiguration().getArtifact(); diff --git a/component-starter-server/src/main/java/org/talend/component/starter/server/service/domain/ProjectRequest.java b/component-starter-server/src/main/java/org/talend/component/starter/server/service/domain/ProjectRequest.java index ed67da7f0f944..d4af3b5d48c1e 100644 --- a/component-starter-server/src/main/java/org/talend/component/starter/server/service/domain/ProjectRequest.java +++ b/component-starter-server/src/main/java/org/talend/component/starter/server/service/domain/ProjectRequest.java @@ -1,17 +1,17 @@ /** - * Copyright (C) 2006-2017 Talend Inc. - www.talend.com + * Copyright (C) 2006-2017 Talend Inc. - www.talend.com * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package org.talend.component.starter.server.service.domain; @@ -36,14 +36,22 @@ public class ProjectRequest { private final Collection processors; + private final String family; + + private final String category; + @Getter @RequiredArgsConstructor public static class SourceConfiguration { private final String name; + private final String icon; + private final boolean genericOutput; + private final DataStructure configuration; + private final DataStructure outputStructure; } @@ -61,6 +69,8 @@ public static class Entry { private final String name; private final String type; + + private final DataStructure nestedType; } @Getter @@ -69,6 +79,10 @@ public static class ProcessorConfiguration { private final String name; + private final String icon; + + private final DataStructure configuration; + private final boolean genericInputs; private final boolean genericOutputs; diff --git a/component-starter-server/src/main/java/org/talend/component/starter/server/service/facet/FacetGenerator.java b/component-starter-server/src/main/java/org/talend/component/starter/server/service/facet/FacetGenerator.java index 0d1f3b0a046cc..3f2faa0ed7710 100644 --- a/component-starter-server/src/main/java/org/talend/component/starter/server/service/facet/FacetGenerator.java +++ b/component-starter-server/src/main/java/org/talend/component/starter/server/service/facet/FacetGenerator.java @@ -24,6 +24,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; +import lombok.ToString; public interface FacetGenerator { @@ -48,6 +49,7 @@ default Stream dependencies(final Collection facets) { } @Getter + @ToString @RequiredArgsConstructor class InMemoryFile { diff --git a/component-starter-server/src/main/java/org/talend/component/starter/server/service/facet/component/ComponentGenerator.java b/component-starter-server/src/main/java/org/talend/component/starter/server/service/facet/component/ComponentGenerator.java index 2bac1c49bffcb..ed0f3cbe8b313 100644 --- a/component-starter-server/src/main/java/org/talend/component/starter/server/service/facet/component/ComponentGenerator.java +++ b/component-starter-server/src/main/java/org/talend/component/starter/server/service/facet/component/ComponentGenerator.java @@ -1,26 +1,30 @@ /** - * Copyright (C) 2006-2017 Talend Inc. - www.talend.com + * Copyright (C) 2006-2017 Talend Inc. - www.talend.com * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package org.talend.component.starter.server.service.facet.component; +import static java.util.Collections.emptyList; import static java.util.Locale.ENGLISH; +import static java.util.Optional.ofNullable; import static java.util.stream.Collectors.toList; import static org.apache.commons.lang3.StringUtils.capitalize; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.function.BiConsumer; import java.util.stream.Stream; import javax.enterprise.context.ApplicationScoped; @@ -35,13 +39,13 @@ import lombok.Data; @ApplicationScoped -public class ComponentGenerator implements FacetGenerator { +public class ComponentGenerator { @Inject private TemplateRenderer tpl; - @Override - public Stream create(final String packageBase, final Build build, final Collection facets, + public Stream create(final String packageBase, final Build build, + final Collection facets, final String family, final String category, final Collection sources, final Collection processors) { final String mainJava = build.getMainJavaDirectory() + '/' + packageBase.replace('.', '/'); @@ -53,137 +57,200 @@ public Stream create(final String packageBase, final Build build, final String serviceName = toJavaName(build.getArtifact()) + "Service"; - final InMemoryFile packageInfo = new InMemoryFile(mainJava + "/package-info.java", + final FacetGenerator.InMemoryFile packageInfo = new FacetGenerator.InMemoryFile(mainJava + "/package-info.java", tpl.render("generator/component/package-info.java", new HashMap() { { put("package", packageBase + ".service"); - put("family", build.getArtifact()); - put("category", build.getArtifact()); + put("family", ofNullable(family).orElse(build.getArtifact())); + put("category", ofNullable(category).orElse(build.getArtifact())); } })); - return Stream.concat(Stream.of(packageInfo, new InMemoryFile(mainJava + "/service/" + serviceName + ".java", - tpl.render("generator/component/Service.java", new HashMap() { + return Stream + .concat(Stream.of(packageInfo, new FacetGenerator.InMemoryFile(mainJava + "/service/" + serviceName + ".java", + tpl.render("generator/component/Service.java", new HashMap() { - { - put("className", serviceName); - put("package", packageBase + ".service"); - } - }))), - Stream.concat(sources == null ? Stream.empty() : sources.stream().flatMap(source -> { - final String baseName = toJavaName(source.getName()); - final String className = baseName + "Source"; - final String mapperName = baseName + "Mapper"; - final String configurationClassName = className + "Configuration"; - final String modelClassName = baseName + "Record"; - return Stream.of(new InMemoryFile(mainJava + "/source/" + className + ".java", - tpl.render("generator/component/Mapper.java", new HashMap() { + { + put("className", serviceName); + put("package", packageBase + ".service"); + } + }))), + Stream.concat( + sources == null ? Stream.empty() : createSourceFiles(packageBase, sources, mainJava, serviceName), + processors == null ? Stream.empty() + : createProcessorFiles(packageBase, processors, mainJava, serviceName))); + } + + private Stream createProcessorFiles(final String packageBase, + final Collection processors, final String mainJava, final String serviceName) { + return processors.stream().flatMap(processor -> { + final String baseName = toJavaName(processor.getName()); + final String className = baseName + "Processor"; + final String configurationClassName = className + "Configuration"; + final String modelClassName = baseName + "Record"; + // todo: inputs/outputs/templates etc + + return Stream.of(new FacetGenerator.InMemoryFile(mainJava + "/processor/" + className + ".java", + tpl.render("generator/component/Processor.java", new HashMap() { + + { + put("className", className); + put("package", packageBase + ".source"); + put("serviceName", serviceName); + put("servicePackage", packageBase + ".service"); + put("configurationName", configurationClassName); + put("modelName", modelClassName); + } + })), new FacetGenerator.InMemoryFile(mainJava + "/processor/" + configurationClassName + ".java", + tpl.render("generator/component/Configuration.java", new HashMap() { { - put("className", mapperName); + put("className", className); put("package", packageBase + ".source"); - put("serviceName", serviceName); - put("servicePackage", packageBase + ".service"); - put("configurationName", configurationClassName); - put("sourceName", className); + // TODO: configurable configuration } - })), - new InMemoryFile(mainJava + "/source/" + className + ".java", - tpl.render("generator/component/Source.java", new HashMap() { - - { - put("className", className); - put("package", packageBase + ".source"); - put("serviceName", serviceName); - put("servicePackage", packageBase + ".service"); - put("configurationName", configurationClassName); - put("modelName", modelClassName); - } - })), - new InMemoryFile(mainJava + "/source/" + configurationClassName + ".java", - tpl.render("generator/component/Configuration.java", new HashMap() { - - { - put("className", className); - put("package", packageBase + ".source"); - // TODO: configurable configuration - } - })), - new InMemoryFile(mainJava + "/source/" + modelClassName + ".java", - tpl.render("generator/component/Model.java", new HashMap() { - - { - put("className", className); - put("package", packageBase + ".source"); - put("generic", source.isGenericOutput()); - if (!source.isGenericOutput() && source.getOutputStructure() != null - && source.getOutputStructure().getEntries() != null) { - put("structure", source.getOutputStructure().getEntries().stream() - .map(e -> new Property(e.getName(), capitalize(e.getName()), toJavaType(e.getType()))) - .collect(toList())); - } - } - }))); - }), processors == null ? Stream.empty() : processors.stream().flatMap(processor -> { - final String baseName = toJavaName(processor.getName()); - final String className = baseName + "Processor"; - final String configurationClassName = className + "Configuration"; - final String modelClassName = baseName + "Record"; - // todo: inputs/outputs/templates etc - - return Stream.of(new InMemoryFile(mainJava + "/processor/" + className + ".java", - tpl.render("generator/component/Processor.java", new HashMap() { - - { - put("className", className); - put("package", packageBase + ".source"); - put("serviceName", serviceName); - put("servicePackage", packageBase + ".service"); - put("configurationName", configurationClassName); - put("modelName", modelClassName); - } - })), - new InMemoryFile(mainJava + "/processor/" + configurationClassName + ".java", - tpl.render("generator/component/Configuration.java", new HashMap() { - - { - put("className", className); - put("package", packageBase + ".source"); - // TODO: configurable configuration - } - }))/*, - new InMemoryFile(mainJava + "/source/" + modelClassName + ".java", - tpl.render("generator/component/Model.java", new HashMap() { - - { - put("className", className); - put("package", packageBase + ".source"); - put("generic", processor.isGenericOutput()); - if (!processor.isGenericOutput() && processor.getOutputStructure() != null - && processor.getOutputStructure().getEntries() != null) { - put("structure", processor.getOutputStructure().getEntries().stream() - .map(e -> new Property(e.getName(), capitalize(e.getName()), toJavaType(e.getType()))) - .collect(toList())); - } - } - }))*/); + }))); + /* + * , + * new InMemoryFile(mainJava + "/source/" + modelClassName + ".java", + * tpl.render("generator/component/Model.java", new HashMap() { + * + * { + * put("className", className); + * put("package", packageBase + ".source"); + * put("generic", processor.isGenericOutput()); + * if (!processor.isGenericOutput() && processor.getOutputStructure() != null + * && processor.getOutputStructure().getEntries() != null) { + * put("structure", processor.getOutputStructure().getEntries().stream() + * .map(e -> new Property(e.getName(), capitalize(e.getName()), toJavaType(e.getType()))) + * .collect(toList())); + * } + * } + * })) + */ + }); + } + + private Stream createSourceFiles(final String packageBase, + final Collection sources, final String mainJava, final String serviceName) { + return sources.stream().flatMap(source -> { + final String baseName = toJavaName(source.getName()); + final String sourceName = baseName + "Source"; + final String mapperName = baseName + "Mapper"; + final String configurationClassName = sourceName + "Configuration"; + final String modelClassName = source.isGenericOutput() ? "ObjectMap" : baseName + "Record"; + final String sourcePackage = packageBase + ".source"; + + final Collection files = new ArrayList<>(); + files.add(new FacetGenerator.InMemoryFile(mainJava + "/source/" + mapperName + ".java", + tpl.render("generator/component/Mapper.java", new HashMap() { + + { + put("className", mapperName); + put("package", sourcePackage); + put("serviceName", serviceName); + put("servicePackage", packageBase + ".service"); + put("configurationName", configurationClassName); + put("sourceName", sourceName); + put("icon", ofNullable(source.getIcon()).filter(s -> !s.isEmpty()).orElse("Icon.IconType.STAR")); + } + }))); + files.add(new FacetGenerator.InMemoryFile(mainJava + "/source/" + sourceName + ".java", + tpl.render("generator/component/Source.java", new HashMap() { + + { + put("className", sourceName); + put("package", sourcePackage); + put("serviceName", serviceName); + put("servicePackage", packageBase + ".service"); + put("configurationName", configurationClassName); + put("modelName", modelClassName); + put("generic", source.isGenericOutput()); + } + }))); + generateConfiguration(null, sourcePackage, mainJava, source.getConfiguration(), configurationClassName, files); + generateModel(null, sourcePackage, mainJava, source.isGenericOutput() ? null : source.getOutputStructure(), + modelClassName, files); + return files.stream(); + }); + } + + private void generateModel(final String root, final String packageBase, final String mainJava, + final ProjectRequest.DataStructure structure, final String modelClassName, + final Collection files) { + files.add(new FacetGenerator.InMemoryFile(mainJava + "/source/" + modelClassName + ".java", + tpl.render("generator/component/Model.java", new HashMap() { + + { + put("className", modelClassName); + put("package", packageBase); + put("generic", structure == null); + if (structure != null && structure.getEntries() != null) { + put("structure", structure.getEntries().stream().map(e -> new Property(e.getName(), + capitalize(e.getName()), toJavaType(root, packageBase, e, (fqn, nested) -> { + final int li = fqn.lastIndexOf('.'); + final String pck = li > 0 ? fqn.substring(0, li) : ""; + final String cn = li > 0 ? fqn.substring(li + 1) : fqn; + generateModel((root == null ? "" : root) + capitalize(cn), pck, mainJava, + e.getNestedType(), cn, files); + }))).collect(toList())); + } + } }))); } - private String toJavaType(final String type) { - if (type == null) { + private void generateConfiguration(final String root, final String packageBase, final String mainJava, + final ProjectRequest.DataStructure structure, final String configurationClassName, + final Collection files) { + files.add(new FacetGenerator.InMemoryFile(mainJava + "/source/" + configurationClassName + ".java", + tpl.render("generator/component/Configuration.java", new HashMap() { + + { + put("className", configurationClassName); + put("package", packageBase); + put("structure", + structure != null && structure.getEntries() != null ? structure.getEntries().stream() + .map(e -> new Property(e.getName(), "get" + capitalize(e.getName()), + toJavaType(root, packageBase, e, (fqn, nested) -> { + final int li = fqn.lastIndexOf('.'); + final String pck = li > 0 ? fqn.substring(0, li) : ""; + final String cn = li > 0 ? fqn.substring(li + 1) : fqn; + generateConfiguration((root == null ? "" : root) + capitalize(cn), pck, + mainJava, nested, cn, files); + }))) + .collect(toList()) : emptyList()); + } + }))); + } + + private String toJavaType(final String root, final String pack, final ProjectRequest.Entry entry, + final BiConsumer nestedGenerator) { + final String type = entry.getType(); + if (type == null || type.isEmpty()) { + if (entry.getNestedType() != null) { + final String name = (root == null ? "" : root) + capitalize(entry.getName()) + "Configuration"; + nestedGenerator.accept(pack + '.' + name, entry.getNestedType()); + return name; + } return "String"; } switch (type.toLowerCase(ENGLISH)) { - case "boolean": - return "boolean"; - case "double": - return "double"; - case "integer": - return "int"; - default: - return "String"; + case "boolean": + return "boolean"; + case "double": + return "double"; + case "integer": + return "int"; + case "uri": // todo: import + return "java.net.URI"; + case "url": // todo: import + return "java.net.URL"; + case "file": // todo: import + return "java.io.File"; + case "string": + default: + return "String"; } } @@ -191,26 +258,14 @@ private String toJavaName(final String name) { return capitalize(name.replace("-", "_").replace(" ", "_")); } - @Override - public String description() { - throw new UnsupportedOperationException("Internal generator"); - } - - @Override - public String name() { - throw new UnsupportedOperationException("Internal generator"); - } - - @Override - public Category category() { - throw new UnsupportedOperationException("Internal generator"); - } - @Data @AllArgsConstructor public static class Property { + private final String name; + private final String methodName; + private final String type; } } diff --git a/component-starter-server/src/main/resources/generator/component/Configuration.java b/component-starter-server/src/main/resources/generator/component/Configuration.java index abc3d778ccfff..30216a7a7258a 100644 --- a/component-starter-server/src/main/resources/generator/component/Configuration.java +++ b/component-starter-server/src/main/resources/generator/component/Configuration.java @@ -3,22 +3,24 @@ import java.util.List; import org.talend.component.api.configuration.Option; -import org.talend.component.api.configuration.ui.OptionsOrder; +import org.talend.component.api.configuration.ui.layout.GridLayout; // generated configuration with query and addresses options, customize it to your need -@OptionsOrder({ "query", "addresses" }) +@GridLayout({ + // the generated layout put one configuration entry per line, + // customize it as much as needed{{#structure}} + @GridLayout.Row({ "{{name}}" }){{^-last}},{{/-last}}{{/structure}} +}) public class {{className}} { + {{#structure}} @Option - private String query; + private {{type}} {{name}};{{^-last}} - @Option - private List addresses; - - public String getQuery() { - return query; - } +{{/-last}}{{/structure}} + {{#structure}} - public List getAddresses() { - return addresses; + public {{type}} {{methodName}}() { + return {{name}}; } +{{/structure}} } diff --git a/component-starter-server/src/main/resources/generator/component/Mapper.java b/component-starter-server/src/main/resources/generator/component/Mapper.java index a1032f0608a18..4e37b42dda7cb 100644 --- a/component-starter-server/src/main/resources/generator/component/Mapper.java +++ b/component-starter-server/src/main/resources/generator/component/Mapper.java @@ -16,8 +16,8 @@ // // this class role is to enable the work to be distributed in environments supporting it. // -@Version -@Icon(Icon.IconType.DB_INPUT) +@Version(1) // default version is 1, if some configuration changes happen between 2 versions you can add a migrationHandler +@Icon({{icon}}) // you can use a custom one using @Icon(value=CUSTOM, custom="filename") and adding icons/filename_icon32.png in resources @PartitionMapper(name = "input") public class {{className}} implements Serializable { private final {{configurationName}} configuration; diff --git a/component-starter-server/src/main/resources/generator/component/Source.java b/component-starter-server/src/main/resources/generator/component/Source.java index ddae1ebb8f244..a577cdba77b73 100644 --- a/component-starter-server/src/main/resources/generator/component/Source.java +++ b/component-starter-server/src/main/resources/generator/component/Source.java @@ -3,7 +3,8 @@ import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; -import org.talend.component.api.input.Producer; +import org.talend.component.api.input.Producer;{{#generic}} +import org.talend.component.api.processor.data.ObjectMap;{{/generic}} import {{servicePackage}}.{{serviceName}}; @@ -24,7 +25,7 @@ public void init() { } @Producer - public {{modelName} next() { + public {{modelName}} next() { // this is the method allowing you to go through the dataset associated // to the component configuration // diff --git a/component-starter-server/src/test/java/org/talend/component/starter/server/service/facet/component/ComponentGeneratorConfigurationTest.java b/component-starter-server/src/test/java/org/talend/component/starter/server/service/facet/component/ComponentGeneratorConfigurationTest.java new file mode 100644 index 0000000000000..facf130d6512e --- /dev/null +++ b/component-starter-server/src/test/java/org/talend/component/starter/server/service/facet/component/ComponentGeneratorConfigurationTest.java @@ -0,0 +1,112 @@ +/** + * Copyright (C) 2006-2017 Talend Inc. - www.talend.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.talend.component.starter.server.service.facet.component; + +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; +import static java.util.Collections.singleton; +import static java.util.stream.Collectors.toList; +import static org.apache.commons.lang3.StringUtils.capitalize; +import static org.junit.Assert.assertEquals; + +import java.util.HashMap; + +import javax.inject.Inject; + +import org.apache.meecrowave.junit.MonoMeecrowave; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestRule; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.talend.component.starter.server.service.domain.ProjectRequest; +import org.talend.component.starter.server.service.template.TemplateRenderer; + +import lombok.Data; + +@RunWith(Parameterized.class) +public class ComponentGeneratorConfigurationTest { + + @Rule + public final TestRule container = new MonoMeecrowave.Rule().inject(this); + + @Parameterized.Parameters(name = "{0}") + public static Iterable scenarii() { + return asList( + // no field + new Scenario(new ProjectRequest.DataStructure(emptyList()), + "package demo.source;\n" + "\n" + "import java.util.List;\n" + "\n" + + "import org.talend.component.api.configuration.Option;\n" + + "import org.talend.component.api.configuration.ui.layout.GridLayout;\n" + "\n" + + "// generated configuration with query and addresses options, customize it to your need\n" + + "@GridLayout({\n" + " // the generated layout put one configuration entry per line,\n" + + " // customize it as much as needed\n" + "})\n" + "public class Demo {\n" + "\n" + "}"), + // string field + new Scenario(new ProjectRequest.DataStructure(singleton(new ProjectRequest.Entry("name", "String", null))), + "package demo.source;\n" + "\n" + "import java.util.List;\n" + "\n" + + "import org.talend.component.api.configuration.Option;\n" + + "import org.talend.component.api.configuration.ui.layout.GridLayout;\n" + "\n" + + "// generated configuration with query and addresses options, customize it to your need\n" + + "@GridLayout({\n" + " // the generated layout put one configuration entry per line,\n" + + " // customize it as much as needed\n" + " @GridLayout.Row({ \"name\" })\n" + "})\n" + + "public class Demo {\n" + " @Option\n" + " private String name;\n" + "\n" + + " public String getName() {\n" + " return name;\n" + " }\n" + "}"), + // string field + int field + new Scenario( + new ProjectRequest.DataStructure(asList(new ProjectRequest.Entry("name", "String", null), + new ProjectRequest.Entry("age", "int", null))), + "package demo.source;\n" + "\n" + "import java.util.List;\n" + "\n" + + "import org.talend.component.api.configuration.Option;\n" + + "import org.talend.component.api.configuration.ui.layout.GridLayout;\n" + "\n" + + "// generated configuration with query and addresses options, customize it to your need\n" + + "@GridLayout({\n" + " // the generated layout put one configuration entry per line,\n" + + " // customize it as much as needed\n" + " @GridLayout.Row({ \"name\" }),\n" + + " @GridLayout.Row({ \"age\" })\n" + "})\n" + "public class Demo {\n" + " @Option\n" + + " private String name;\n" + "\n" + " @Option\n" + " private int age;\n" + "\n" + + " public String getName() {\n" + " return name;\n" + " }\n" + "\n" + + " public int getAge() {\n" + " return age;\n" + " }\n" + "}")); + } + + @Parameterized.Parameter + public Scenario scenario; + + @Inject + private TemplateRenderer renderer; + + @Test + public void run() { + final String result = renderer.render("generator/component/Configuration.java", new HashMap() { + + { + put("className", "Demo"); + put("package", "demo.source"); + put("structure", + scenario.structure.getEntries().stream().map( + e -> new ComponentGenerator.Property(e.getName(), "get" + capitalize(e.getName()), e.getType())) + .collect(toList())); + } + }); + assertEquals(scenario.expectedOutput, result); + } + + @Data + private static class Scenario { + + private final ProjectRequest.DataStructure structure; + + private final String expectedOutput; + } +} diff --git a/component-starter-server/src/test/java/org/talend/component/starter/server/service/facet/component/ComponentGeneratorTest.java b/component-starter-server/src/test/java/org/talend/component/starter/server/service/facet/component/ComponentGeneratorTest.java new file mode 100644 index 0000000000000..267321aad284d --- /dev/null +++ b/component-starter-server/src/test/java/org/talend/component/starter/server/service/facet/component/ComponentGeneratorTest.java @@ -0,0 +1,193 @@ +/** + * Copyright (C) 2006-2017 Talend Inc. - www.talend.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.talend.component.starter.server.service.facet.component; + +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; +import static java.util.Collections.emptySet; +import static java.util.Collections.singleton; +import static java.util.stream.Collectors.toMap; +import static org.junit.Assert.assertEquals; + +import java.util.Map; + +import javax.inject.Inject; + +import org.apache.meecrowave.junit.MonoMeecrowave; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.talend.component.starter.server.service.domain.Build; +import org.talend.component.starter.server.service.domain.ProjectRequest; +import org.talend.component.starter.server.service.facet.FacetGenerator; + +@RunWith(MonoMeecrowave.Runner.class) +public class ComponentGeneratorTest { + + @Inject + private ComponentGenerator generator; + + private Build build = new Build("test", "src/main/java", "src/test/java", "src/main/resources", "src/test/resources", + "src/main/webapp", "pom.xml", "some pom", "target"); + + @Test + public void source() { + final Map files = generator + .create("com.foo", build, emptySet(), "superfamily", "supercategory", + singleton(new ProjectRequest.SourceConfiguration("mycomp", "", false, + new ProjectRequest.DataStructure(singleton(new ProjectRequest.Entry("name", "string", null))), + new ProjectRequest.DataStructure(singleton(new ProjectRequest.Entry("name", "string", null))))), + emptyList()) + .collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); + assertEquals(6, files.size()); + assertEquals( + "// this tells the framework in which family (group of components) and categories (UI grouping)\n" + + "// the components in the nested packages belong to\n" + + "@Components(family = \"superfamily\", categories = \"supercategory\")\n" + "package com.foo.service;\n" + + "\n" + "import org.talend.component.api.component.Components;\n", + files.get("src/main/java/com/foo/package-info.java")); + assertEquals( + "package com.foo.service;\n" + "\n" + "import org.talend.component.api.service.Service;\n" + "\n" + "@Service\n" + + "public class SalesforceService {\n" + "\n" + + " // you can put logic here you can reuse in components\n" + "\n" + "}", + files.get("src/main/java/com/foo/service/TestService.java")); + assertEquals("package com.foo.source;\n" + "\n" + "import javax.annotation.PostConstruct;\n" + + "import javax.annotation.PreDestroy;\n" + "\n" + "import org.talend.component.api.input.Producer;\n" + "\n" + + "import com.foo.service.TestService;\n" + "\n" + "public class MycompSource implements Serializable {\n" + + " private final MycompSourceConfiguration configuration;\n" + " private final TestService service;\n" + + "\n" + " public MycompSource(@Option(\"configuration\") final MycompSourceConfiguration configuration,\n" + + " final TestService service) {\n" + " this.configuration = configuration;\n" + + " this.service = service;\n" + " }\n" + "\n" + " @PostConstruct\n" + " public void init() {\n" + + " // this method will be executed once for the whole component execution,\n" + + " // this is where you can establish a connection for instance\n" + " }\n" + "\n" + " @Producer\n" + + " public MycompRecord next() {\n" + + " // this is the method allowing you to go through the dataset associated\n" + + " // to the component configuration\n" + " //\n" + + " // return null means the dataset has no more data to go through\n" + " return null;\n" + + " }\n" + "\n" + " @PreDestroy\n" + " public void release() {\n" + + " // this is the symmetric method of the init() one,\n" + + " // release potential connections you created or data you cached\n" + " }\n" + "}", + files.get("src/main/java/com/foo/source/MycompSource.java")); + assertEquals("package com.foo.source;\n" + "\n" + "import static java.util.Collections.singletonList;\n" + "\n" + + "import org.talend.component.api.component.Icon;\n" + "import org.talend.component.api.component.Version;\n" + + "import org.talend.component.api.configuration.Option;\n" + "import org.talend.component.api.input.Assessor;\n" + + "import org.talend.component.api.input.Emitter;\n" + "import org.talend.component.api.input.PartitionSize;\n" + + "import org.talend.component.api.input.PartitionMapper;\n" + "import org.talend.component.api.input.Split;\n" + + "\n" + "import com.foo.service.TestService;\n" + "\n" + "//\n" + + "// this class role is to enable the work to be distributed in environments supporting it.\n" + "//\n" + + "@Version(1) // default version is 1, if some configuration changes happen between 2 versions you can add a migrationHandler\n" + + "@Icon(Icon.IconType.STAR) // you can use a custom one using @Icon(value=CUSTOM, custom=\"filename\") and adding icons/filename_icon32.png in resources\n" + + "@PartitionMapper(name = \"input\")\n" + "public class MycompMapper implements Serializable {\n" + + " private final MycompSourceConfiguration configuration;\n" + " private final TestService service;\n" + + "\n" + " public MycompMapper(@Option(\"configuration\") final MycompSourceConfiguration configuration,\n" + + " final TestService service) {\n" + " this.configuration = configuration;\n" + + " this.service = service;\n" + " }\n" + "\n" + " @Assessor\n" + + " public long estimateSize() throws SQLException {\n" + + " // this method should return the estimation of the dataset size\n" + + " // it is recommanded to return a byte value\n" + + " // if you don't have the exact size you can use a rough estimation\n" + " return 1L;\n" + + " }\n" + "\n" + " @Split\n" + + " public List split(@PartitionSize final long bundles) throws SQLException {\n" + + " // overall idea here is to split the work related to configuration in bundles of size \"bundles\"\n" + + " //\n" + " // for instance if your estimateSize() returned 1000 and you can run on 10 nodes\n" + + " // then the environment can decide to run it concurrently (10 * 100).\n" + + " // In this case bundles = 100 and we must try to return 10 MycompMapper with 1/10 of the overall work each.\n" + + " //\n" + + " // default implementation returns this which means it doesn't support the work to be split\n" + + " return singletonList(this);\n" + " }\n" + "\n" + " @Emitter\n" + + " public MycompSource createWorker() {\n" + " // here we create an actual worker,\n" + + " // you are free to rework the configuration etc but our default generated implementation\n" + + " // propagates the partition mapper entries.\n" + + " return new MycompSource(configuration, service);\n" + " }\n" + "}", + files.get("src/main/java/com/foo/source/MycompMapper.java")); + assertEquals( + "package com.foo.source;\n" + "\n" + "import java.util.List;\n" + "\n" + + "import org.talend.component.api.configuration.Option;\n" + + "import org.talend.component.api.configuration.ui.layout.GridLayout;\n" + "\n" + + "// generated configuration with query and addresses options, customize it to your need\n" + + "@GridLayout({\n" + " // the generated layout put one configuration entry per line,\n" + + " // customize it as much as needed\n" + " @GridLayout.Row({ \"name\" })\n" + "})\n" + + "public class MycompSourceConfiguration {\n" + " @Option\n" + " private String name;\n" + "\n" + + " public String getName() {\n" + " return name;\n" + " }\n" + "}", + files.get("src/main/java/com/foo/source/MycompSourceConfiguration.java")); + assertEquals("package com.foo.source;\n" + "\n" + "// this is the pojo which will be used to represent your data\n" + + "public class MycompRecord {\n" + "\n" + " private String name;\n" + "\n" + " public String getName() {\n" + + " return name;\n" + " }\n" + "\n" + " public void setName(final String name) {\n" + + " this.name = name;\n" + " }\n" + " \n" + "}", + files.get("src/main/java/com/foo/source/MycompRecord.java")); + } + + @Test + public void sourceComplexConfiguration() { + final Map files = generator + .create("com.foo", build, emptySet(), "superfamily", "supercategory", + singleton(new ProjectRequest.SourceConfiguration("mycomp", "", false, + new ProjectRequest.DataStructure(singleton(new ProjectRequest.Entry("person", "", + new ProjectRequest.DataStructure(asList(new ProjectRequest.Entry("name", "string", null), + new ProjectRequest.Entry("age", "int", null)))))), + new ProjectRequest.DataStructure(emptyList()))), + emptyList()) + .collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); + + assertEquals("package com.foo.source;\n" + "\n" + "import java.util.List;\n" + "\n" + + "import org.talend.component.api.configuration.Option;\n" + + "import org.talend.component.api.configuration.ui.layout.GridLayout;\n" + "\n" + + "// generated configuration with query and addresses options, customize it to your need\n" + "@GridLayout({\n" + + " // the generated layout put one configuration entry per line,\n" + + " // customize it as much as needed\n" + " @GridLayout.Row({ \"person\" })\n" + "})\n" + + "public class MycompSourceConfiguration {\n" + " @Option\n" + " private PersonConfiguration person;\n" + + "\n" + " public PersonConfiguration getPerson() {\n" + " return person;\n" + " }\n" + "}", + files.get("src/main/java/com/foo/source/MycompSourceConfiguration.java")); + assertEquals( + "package com.foo.source;\n" + "\n" + "import java.util.List;\n" + "\n" + + "import org.talend.component.api.configuration.Option;\n" + + "import org.talend.component.api.configuration.ui.layout.GridLayout;\n" + "\n" + + "// generated configuration with query and addresses options, customize it to your need\n" + + "@GridLayout({\n" + " // the generated layout put one configuration entry per line,\n" + + " // customize it as much as needed\n" + " @GridLayout.Row({ \"name\" }),\n" + + " @GridLayout.Row({ \"age\" })\n" + "})\n" + "public class PersonConfiguration {\n" + " @Option\n" + + " private String name;\n" + "\n" + " @Option\n" + " private String age;\n" + "\n" + + " public String getName() {\n" + " return name;\n" + " }\n" + "\n" + + " public String getAge() {\n" + " return age;\n" + " }\n" + "}", + files.get("src/main/java/com/foo/source/PersonConfiguration.java")); + } + + @Test + public void genericSource() { + final Map files = generator + .create("com.foo", build, emptySet(), "superfamily", "supercategory", + singleton(new ProjectRequest.SourceConfiguration("mycomp", "", true, null, null)), emptyList()) + .collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); + + assertEquals("package com.foo.source;\n" + "\n" + "import javax.annotation.PostConstruct;\n" + + "import javax.annotation.PreDestroy;\n" + "\n" + "import org.talend.component.api.input.Producer;\n" + + "import org.talend.component.api.processor.data.ObjectMap;\n" + "\n" + "import com.foo.service.TestService;\n" + + "\n" + "public class MycompSource implements Serializable {\n" + + " private final MycompSourceConfiguration configuration;\n" + " private final TestService service;\n" + + "\n" + " public MycompSource(@Option(\"configuration\") final MycompSourceConfiguration configuration,\n" + + " final TestService service) {\n" + " this.configuration = configuration;\n" + + " this.service = service;\n" + " }\n" + "\n" + " @PostConstruct\n" + " public void init() {\n" + + " // this method will be executed once for the whole component execution,\n" + + " // this is where you can establish a connection for instance\n" + " }\n" + "\n" + " @Producer\n" + + " public ObjectMap next() {\n" + + " // this is the method allowing you to go through the dataset associated\n" + + " // to the component configuration\n" + " //\n" + + " // return null means the dataset has no more data to go through\n" + " return null;\n" + + " }\n" + "\n" + " @PreDestroy\n" + " public void release() {\n" + + " // this is the symmetric method of the init() one,\n" + + " // release potential connections you created or data you cached\n" + " }\n" + "}", + files.get("src/main/java/com/foo/source/MycompSource.java")); + } +} From e1edb9667413032059229d3630add235980ee1bf Mon Sep 17 00:00:00 2001 From: Romain Manni-Bucau Date: Thu, 26 Oct 2017 14:37:25 +0200 Subject: [PATCH 2/2] basic starter processor generation logic --- .../starter/server/front/ProjectResource.java | 49 ++- .../starter/server/model/ProjectModel.java | 47 ++- .../server/service/ProjectGenerator.java | 58 ++- .../server/service/domain/ProjectRequest.java | 24 +- .../facet/component/ComponentGenerator.java | 153 +++++--- .../generator/component/GenericRecord.java | 23 ++ .../resources/generator/component/Mapper.java | 2 +- .../generator/component/Processor.java | 41 ++- .../component/ComponentGeneratorTest.java | 337 ++++++++++++++++-- 9 files changed, 578 insertions(+), 156 deletions(-) create mode 100644 component-starter-server/src/main/resources/generator/component/GenericRecord.java diff --git a/component-starter-server/src/main/java/org/talend/component/starter/server/front/ProjectResource.java b/component-starter-server/src/main/java/org/talend/component/starter/server/front/ProjectResource.java index f38f98ffc9abd..1ca5b59646031 100644 --- a/component-starter-server/src/main/java/org/talend/component/starter/server/front/ProjectResource.java +++ b/component-starter-server/src/main/java/org/talend/component/starter/server/front/ProjectResource.java @@ -16,6 +16,7 @@ package org.talend.component.starter.server.front; import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; import static java.util.Collections.singletonList; import static java.util.Optional.ofNullable; import static java.util.stream.Collectors.toList; @@ -105,25 +106,39 @@ private ProjectRequest toRequest(final ProjectModel model) { ofNullable(model.getVersion()).orElse("0.0.1-SNAPSHOT"), "1.8"), ofNullable(model.getPackageBase()).orElse("com.application").replace('/', '.'), ofNullable(model.getFacets()).orElse(emptyList()), - ofNullable(model.getSources()).map(s -> s.stream() - .map(i -> new ProjectRequest.SourceConfiguration(i.getName(), i.getIcon(), i.isGenericOutput(), - toStructure(i.getConfigurationStructure()), toStructure(i.getOutputStructure()))) - .collect(toList())).orElse(emptyList()), - ofNullable(model.getProcessors()).map(s -> s.stream() - .map(i -> new ProjectRequest.ProcessorConfiguration(i.getName(), i.getIcon(), - toStructure(i.getConfigurationStructure()), i.isGenericInputs(), i.isGenericOutputs(), - i.getInputs(), i.getOutputs(), - ofNullable(i.getInputStructures()).map(is -> is.stream().map(this::toStructure).collect(toList())) - .orElse(emptyList()), - ofNullable(i.getOutputStructures()) - .map(is -> is.stream().map(this::toStructure).collect(toList())).orElse(emptyList()))) - .collect(toList())).orElse(emptyList()), + ofNullable(model.getSources()) + .map(s -> s.stream() + .map(i -> new ProjectRequest.SourceConfiguration(i.getName(), i.getIcon(), + toStructure(false, i.getConfigurationStructure()).getStructure(), + toStructure(i.isGenericOutput(), i.getOutputStructure()))) + .collect(toList())) + .orElse(emptyList()), + ofNullable(model.getProcessors()) + .map(s -> s.stream() + .map(i -> new ProjectRequest.ProcessorConfiguration(i.getName(), i.getIcon(), + toStructure(false, i.getConfigurationStructure()).getStructure(), + ofNullable(i.getInputStructures()) + .map(is -> is.stream() + .collect(toMap(ProjectModel.NamedModel::getName, + nm -> toStructure(nm.isGeneric(), nm.getStructure())))) + .orElse(emptyMap()), + ofNullable(i.getOutputStructures()) + .map(is -> is.stream() + .collect(toMap(ProjectModel.NamedModel::getName, + nm -> toStructure(nm.isGeneric(), nm.getStructure())))) + .orElse(emptyMap()))) + .collect(toList())) + .orElse(emptyList()), model.getFamily(), model.getCategory()); } - private ProjectRequest.DataStructure toStructure(final ProjectModel.Model model) { - return new ProjectRequest.DataStructure(model.getEntries() == null ? emptyList() - : model.getEntries().stream().map(e -> new ProjectRequest.Entry(e.getName(), e.getType(), - e.getModel() != null ? toStructure(e.getModel()) : null)).collect(toList())); + private ProjectRequest.StructureConfiguration toStructure(final boolean generic, final ProjectModel.Model model) { + return new ProjectRequest.StructureConfiguration(!generic ? new ProjectRequest.DataStructure(model.getEntries() == null + ? emptyList() + : model.getEntries().stream() + .map(e -> new ProjectRequest.Entry(e.getName(), e.getType(), + e.getModel() != null ? toStructure(false, e.getModel()).getStructure() : null)) + .collect(toList())) + : null, generic); } } diff --git a/component-starter-server/src/main/java/org/talend/component/starter/server/model/ProjectModel.java b/component-starter-server/src/main/java/org/talend/component/starter/server/model/ProjectModel.java index 9388b8255196c..0d99f30a9ef55 100644 --- a/component-starter-server/src/main/java/org/talend/component/starter/server/model/ProjectModel.java +++ b/component-starter-server/src/main/java/org/talend/component/starter/server/model/ProjectModel.java @@ -1,17 +1,17 @@ /** - * Copyright (C) 2006-2017 Talend Inc. - www.talend.com + * Copyright (C) 2006-2017 Talend Inc. - www.talend.com * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package org.talend.component.starter.server.model; @@ -23,19 +23,31 @@ @Data public class ProjectModel { + private String buildType; + private String version; + private String group; + private String artifact; + private String name; + private String description; + private String packageBase; + private String family; + private String category; + // private String packaging; // jar only for now // private String javaVersion; // enforce to java 8 for now private Collection facets; + private Collection sources; + private Collection processors; @Getter @@ -81,16 +93,19 @@ public static class Processor { private final Model configurationStructure; - private final boolean genericInputs; + private final Collection inputStructures; - private final boolean genericOutputs; + private final Collection outputStructures; + } - private final Collection inputs; + @Getter + @RequiredArgsConstructor + public static class NamedModel { - private final Collection outputs; + private final String name; - private final Collection inputStructures; + private final boolean generic; - private final Collection outputStructures; + private final Model structure; } } diff --git a/component-starter-server/src/main/java/org/talend/component/starter/server/service/ProjectGenerator.java b/component-starter-server/src/main/java/org/talend/component/starter/server/service/ProjectGenerator.java index c9883a8aa5326..4eb62134e1417 100644 --- a/component-starter-server/src/main/java/org/talend/component/starter/server/service/ProjectGenerator.java +++ b/component-starter-server/src/main/java/org/talend/component/starter/server/service/ProjectGenerator.java @@ -1,17 +1,17 @@ /** - * Copyright (C) 2006-2017 Talend Inc. - www.talend.com + * Copyright (C) 2006-2017 Talend Inc. - www.talend.com * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package org.talend.component.starter.server.service; @@ -55,6 +55,7 @@ @ApplicationScoped public class ProjectGenerator { + @Getter private final Map generators = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); @@ -90,12 +91,10 @@ public void generate(final ProjectRequest request, final OutputStream outputStre // build dependencies to give them to the build final Collection facets = ofNullable(request.getFacets()).orElse(emptyList()); - final List dependencies = new ArrayList<>(facets.stream() - .map(this.facets::get) - .flatMap(f -> f.dependencies(facets)) - .collect(toSet())); + final List dependencies = new ArrayList<>( + facets.stream().map(this.facets::get).flatMap(f -> f.dependencies(facets)).collect(toSet())); dependencies.sort((o1, o2) -> { - { // by scope + { // by scope final int scope1 = scopesOrdering.indexOf(o1.getScope()); final int scope2 = scopesOrdering.indexOf(o2.getScope()); final int scopeDiff = scope1 - scope2; @@ -104,7 +103,7 @@ public void generate(final ProjectRequest request, final OutputStream outputStre } } - { // by group + { // by group final int comp = o1.getGroup().compareTo(o2.getGroup()); if (comp != 0) { return comp; @@ -119,29 +118,26 @@ public void generate(final ProjectRequest request, final OutputStream outputStre dependencies.add(0, Dependency.componentApi()); // create the build to be able to generate the files - final Build build = generator.createBuild(request.getBuildConfiguration(), request.getPackageBase(), dependencies, facets); + final Build build = generator.createBuild(request.getBuildConfiguration(), request.getPackageBase(), dependencies, + facets); files.put(build.getBuildFileName(), build.getBuildFileContent()); // generate facet files - final Map> filePerFacet = facets.stream() - .map(s -> s.toLowerCase(Locale.ENGLISH)) - .collect(toMap(this.facets::get, f -> { - final FacetGenerator g = this.facets.get(f); - return g.create(request.getPackageBase(), build, facets, request.getSources(), request.getProcessors()) - .peek(file -> files.put(file.getPath(), file.getContent())) - .map(FacetGenerator.InMemoryFile::getPath) - .collect(toList()); - })); + final Map> filePerFacet = facets.stream().map(s -> s.toLowerCase(Locale.ENGLISH)) + .collect(toMap(this.facets::get, f -> { + final FacetGenerator g = this.facets.get(f); + return g.create(request.getPackageBase(), build, facets, request.getSources(), request.getProcessors()) + .peek(file -> files.put(file.getPath(), file.getContent())).map(FacetGenerator.InMemoryFile::getPath) + .collect(toList()); + })); // generate README.adoc if needed if (!files.containsKey("README.adoc")) { files.put("README.adoc", readmeGenerator.createReadme(request.getBuildConfiguration().getName(), filePerFacet)); } - componentGenerator.create( - request.getPackageBase(), build, facets, request.getFamily(), request.getCategory(), - request.getSources(), request.getProcessors()) - .forEach(file -> files.put(file.getPath(), file.getContent())); + componentGenerator.create(request.getPackageBase(), build, request.getFamily(), request.getCategory(), + request.getSources(), request.getProcessors()).forEach(file -> files.put(file.getPath(), file.getContent())); // now create the zip prefixing it with the artifact value final String rootName = request.getBuildConfiguration().getArtifact(); @@ -174,7 +170,7 @@ public void generate(final ProjectRequest request, final OutputStream outputStre files.forEach((path, content) -> { try { zip.putNextEntry(new ZipEntry(rootName + '/' + path)); - zip.write(content.replace("\r", "")/*avoid side effect of the OS*/.getBytes(StandardCharsets.UTF_8)); + zip.write(content.replace("\r", "")/* avoid side effect of the OS */.getBytes(StandardCharsets.UTF_8)); zip.closeEntry(); } catch (final IOException e) { throw new IllegalStateException(e); diff --git a/component-starter-server/src/main/java/org/talend/component/starter/server/service/domain/ProjectRequest.java b/component-starter-server/src/main/java/org/talend/component/starter/server/service/domain/ProjectRequest.java index d4af3b5d48c1e..705c2f1b9a5ec 100644 --- a/component-starter-server/src/main/java/org/talend/component/starter/server/service/domain/ProjectRequest.java +++ b/component-starter-server/src/main/java/org/talend/component/starter/server/service/domain/ProjectRequest.java @@ -15,7 +15,10 @@ */ package org.talend.component.starter.server.service.domain; +import static java.util.Collections.emptyList; + import java.util.Collection; +import java.util.Map; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -48,11 +51,9 @@ public static class SourceConfiguration { private final String icon; - private final boolean genericOutput; - private final DataStructure configuration; - private final DataStructure outputStructure; + private final StructureConfiguration outputStructure; } @Getter @@ -77,23 +78,26 @@ public static class Entry { @RequiredArgsConstructor public static class ProcessorConfiguration { + public static final DataStructure OBJECT_MAP = new DataStructure(emptyList()); + private final String name; private final String icon; private final DataStructure configuration; - private final boolean genericInputs; - - private final boolean genericOutputs; + private final Map inputStructures; - private final Collection inputs; + private final Map outputStructures; + } - private final Collection outputs; + @Getter + @RequiredArgsConstructor + public static class StructureConfiguration { - private final Collection inputStructures; + private final DataStructure structure; - private final Collection outputStructures; + private final boolean generic; } @Getter diff --git a/component-starter-server/src/main/java/org/talend/component/starter/server/service/facet/component/ComponentGenerator.java b/component-starter-server/src/main/java/org/talend/component/starter/server/service/facet/component/ComponentGenerator.java index ed0f3cbe8b313..d0a0ea49bd4f9 100644 --- a/component-starter-server/src/main/java/org/talend/component/starter/server/service/facet/component/ComponentGenerator.java +++ b/component-starter-server/src/main/java/org/talend/component/starter/server/service/facet/component/ComponentGenerator.java @@ -16,9 +16,11 @@ package org.talend.component.starter.server.service.facet.component; import static java.util.Collections.emptyList; +import static java.util.Collections.emptySet; import static java.util.Locale.ENGLISH; import static java.util.Optional.ofNullable; import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toSet; import static org.apache.commons.lang3.StringUtils.capitalize; import java.util.ArrayList; @@ -44,9 +46,8 @@ public class ComponentGenerator { @Inject private TemplateRenderer tpl; - public Stream create(final String packageBase, final Build build, - final Collection facets, final String family, final String category, - final Collection sources, + public Stream create(final String packageBase, final Build build, final String family, + final String category, final Collection sources, final Collection processors) { final String mainJava = build.getMainJavaDirectory() + '/' + packageBase.replace('.', '/'); @@ -67,79 +68,106 @@ public Stream create(final String packageBase, fina } })); - return Stream - .concat(Stream.of(packageInfo, new FacetGenerator.InMemoryFile(mainJava + "/service/" + serviceName + ".java", - tpl.render("generator/component/Service.java", new HashMap() { - - { - put("className", serviceName); - put("package", packageBase + ".service"); - } - }))), - Stream.concat( - sources == null ? Stream.empty() : createSourceFiles(packageBase, sources, mainJava, serviceName), - processors == null ? Stream.empty() - : createProcessorFiles(packageBase, processors, mainJava, serviceName))); + final Collection files = new ArrayList<>(); + files.add(packageInfo); + files.add(new FacetGenerator.InMemoryFile(mainJava + "/service/" + serviceName + ".java", + tpl.render("generator/component/Service.java", new HashMap() { + + { + put("className", serviceName); + put("package", packageBase + ".service"); + } + }))); + + // boolean needsObjectMapImpl = false; // we have a default impl in api, no need to generate one + if (sources != null && !sources.isEmpty()) { + files.addAll(createSourceFiles(packageBase, sources, mainJava, serviceName).collect(toList())); + // needsObjectMapImpl = sources.stream().anyMatch(ProjectRequest.SourceConfiguration::isGenericOutput); + } + if (processors != null && !processors.isEmpty()) { + files.addAll(createProcessorFiles(packageBase, processors, mainJava, serviceName).collect(toList())); + /* + * if (!needsObjectMapImpl) { + * needsObjectMapImpl = processors.stream().anyMatch(ProjectRequest.ProcessorConfiguration::isGenericOutputs); + * } + */ + } + + return files.stream(); } private Stream createProcessorFiles(final String packageBase, final Collection processors, final String mainJava, final String serviceName) { return processors.stream().flatMap(processor -> { + final boolean isOutput = processor.getOutputStructures() == null || processor.getOutputStructures().isEmpty(); + final String baseName = toJavaName(processor.getName()); final String className = baseName + "Processor"; final String configurationClassName = className + "Configuration"; - final String modelClassName = baseName + "Record"; - // todo: inputs/outputs/templates etc + final String processorFinalPackage = isOutput ? "output" : "processor"; + final String processorPackage = packageBase + "." + processorFinalPackage; + + final Collection files = new ArrayList<>(); - return Stream.of(new FacetGenerator.InMemoryFile(mainJava + "/processor/" + className + ".java", + final Collection outputNames = !isOutput ? processor.getOutputStructures().entrySet().stream().map(e -> { + final String javaName = sanitizeConnectionName(e.getKey()); + if (e.getValue().isGeneric()) { + return new Connection(e.getKey(), javaName, "ObjectMap"); + } + + final String outputClassName = capitalize(processor.getName() + capitalize(javaName + "Output")); + generateModel(null, processorPackage, mainJava, e.getValue().getStructure(), outputClassName, files); + return new Connection(e.getKey(), javaName, outputClassName); + }).collect(toSet()) : emptySet(); + + final Collection inputNames = processor.getInputStructures() != null + ? processor.getInputStructures().entrySet().stream().map(e -> { + final String javaName = sanitizeConnectionName(e.getKey()); + if (e.getValue().isGeneric()) { + return new Connection(e.getKey(), javaName, "ObjectMap"); + } + + final String inputClassName = capitalize(processor.getName() + capitalize(javaName + "Input")); + generateModel(null, processorPackage, mainJava, e.getValue().getStructure(), inputClassName, files); + return new Connection(e.getKey(), javaName, inputClassName); + }).collect(toSet()) + : emptySet(); + + generateConfiguration(null, processorPackage, mainJava, processor.getConfiguration(), configurationClassName, files); + + files.add(new FacetGenerator.InMemoryFile(mainJava + "/" + processorFinalPackage + "/" + className + ".java", tpl.render("generator/component/Processor.java", new HashMap() { { + put("name", processor.getName()); put("className", className); - put("package", packageBase + ".source"); + put("package", packageBase + "." + processorFinalPackage); put("serviceName", serviceName); put("servicePackage", packageBase + ".service"); put("configurationName", configurationClassName); - put("modelName", modelClassName); + put("outputs", outputNames); + put("inputs", inputNames); + put("icon", ofNullable(processor.getIcon()).filter(s -> !s.isEmpty()).orElse("Icon.IconType.STAR")); + put("generic", outputNames.stream().anyMatch(o -> o.type.equals("ObjectMap")) + || inputNames.stream().anyMatch(o -> o.type.equals("ObjectMap"))); } - })), new FacetGenerator.InMemoryFile(mainJava + "/processor/" + configurationClassName + ".java", - tpl.render("generator/component/Configuration.java", new HashMap() { - - { - put("className", className); - put("package", packageBase + ".source"); - // TODO: configurable configuration - } - }))); - /* - * , - * new InMemoryFile(mainJava + "/source/" + modelClassName + ".java", - * tpl.render("generator/component/Model.java", new HashMap() { - * - * { - * put("className", className); - * put("package", packageBase + ".source"); - * put("generic", processor.isGenericOutput()); - * if (!processor.isGenericOutput() && processor.getOutputStructure() != null - * && processor.getOutputStructure().getEntries() != null) { - * put("structure", processor.getOutputStructure().getEntries().stream() - * .map(e -> new Property(e.getName(), capitalize(e.getName()), toJavaType(e.getType()))) - * .collect(toList())); - * } - * } - * })) - */ + }))); + + return files.stream(); }); } private Stream createSourceFiles(final String packageBase, final Collection sources, final String mainJava, final String serviceName) { return sources.stream().flatMap(source -> { + final boolean generic = source.getOutputStructure() == null || source.getOutputStructure().isGeneric() + || source.getOutputStructure().getStructure() == null; + final String baseName = toJavaName(source.getName()); final String sourceName = baseName + "Source"; final String mapperName = baseName + "Mapper"; final String configurationClassName = sourceName + "Configuration"; - final String modelClassName = source.isGenericOutput() ? "ObjectMap" : baseName + "Record"; + final String modelClassName = generic ? "ObjectMap" : baseName + "Record"; final String sourcePackage = packageBase + ".source"; final Collection files = new ArrayList<>(); @@ -147,6 +175,7 @@ private Stream createSourceFiles(final String packa tpl.render("generator/component/Mapper.java", new HashMap() { { + put("name", source.getName()); put("className", mapperName); put("package", sourcePackage); put("serviceName", serviceName); @@ -166,11 +195,11 @@ private Stream createSourceFiles(final String packa put("servicePackage", packageBase + ".service"); put("configurationName", configurationClassName); put("modelName", modelClassName); - put("generic", source.isGenericOutput()); + put("generic", generic); } }))); generateConfiguration(null, sourcePackage, mainJava, source.getConfiguration(), configurationClassName, files); - generateModel(null, sourcePackage, mainJava, source.isGenericOutput() ? null : source.getOutputStructure(), + generateModel(null, sourcePackage, mainJava, generic ? null : source.getOutputStructure().getStructure(), modelClassName, files); return files.stream(); }); @@ -179,7 +208,8 @@ private Stream createSourceFiles(final String packa private void generateModel(final String root, final String packageBase, final String mainJava, final ProjectRequest.DataStructure structure, final String modelClassName, final Collection files) { - files.add(new FacetGenerator.InMemoryFile(mainJava + "/source/" + modelClassName + ".java", + files.add(new FacetGenerator.InMemoryFile( + mainJava + "/" + packageBase.substring(packageBase.lastIndexOf('.') + 1) + "/" + modelClassName + ".java", tpl.render("generator/component/Model.java", new HashMap() { { @@ -203,7 +233,8 @@ private void generateModel(final String root, final String packageBase, final St private void generateConfiguration(final String root, final String packageBase, final String mainJava, final ProjectRequest.DataStructure structure, final String configurationClassName, final Collection files) { - files.add(new FacetGenerator.InMemoryFile(mainJava + "/source/" + configurationClassName + ".java", + files.add(new FacetGenerator.InMemoryFile( + mainJava + "/" + packageBase.substring(packageBase.lastIndexOf('.') + 1) + "/" + configurationClassName + ".java", tpl.render("generator/component/Configuration.java", new HashMap() { { @@ -240,6 +271,7 @@ private String toJavaType(final String root, final String pack, final ProjectReq return "boolean"; case "double": return "double"; + case "int": case "integer": return "int"; case "uri": // todo: import @@ -258,6 +290,10 @@ private String toJavaName(final String name) { return capitalize(name.replace("-", "_").replace(" ", "_")); } + private String sanitizeConnectionName(final String name) { + return name.replace("_", "").replace(" ", ""); + } + @Data @AllArgsConstructor public static class Property { @@ -268,4 +304,15 @@ public static class Property { private final String type; } + + @Data + @AllArgsConstructor + public static class Connection { + + private final String name; + + private final String javaName; + + private final String type; + } } diff --git a/component-starter-server/src/main/resources/generator/component/GenericRecord.java b/component-starter-server/src/main/resources/generator/component/GenericRecord.java new file mode 100644 index 0000000000000..223f59a218e2f --- /dev/null +++ b/component-starter-server/src/main/resources/generator/component/GenericRecord.java @@ -0,0 +1,23 @@ +package {{package}}; + +import org.talend.component.api.processor.data.ObjectMap; + +// generated configuration with query and addresses options, customize it to your need +@GridLayout({ + // the generated layout put one configuration entry per line, + // customize it as much as needed{{#structure}} + @GridLayout.Row({ "{{name}}" }){{^-last}},{{/-last}}{{/structure}} +}) +public class {{className}} { + {{#structure}} + @Option + private {{type}} {{name}};{{^-last}} + +{{/-last}}{{/structure}} + {{#structure}} + + public {{type}} {{methodName}}() { + return {{name}}; + } +{{/structure}} +} diff --git a/component-starter-server/src/main/resources/generator/component/Mapper.java b/component-starter-server/src/main/resources/generator/component/Mapper.java index 4e37b42dda7cb..867dcaefff5c6 100644 --- a/component-starter-server/src/main/resources/generator/component/Mapper.java +++ b/component-starter-server/src/main/resources/generator/component/Mapper.java @@ -18,7 +18,7 @@ // @Version(1) // default version is 1, if some configuration changes happen between 2 versions you can add a migrationHandler @Icon({{icon}}) // you can use a custom one using @Icon(value=CUSTOM, custom="filename") and adding icons/filename_icon32.png in resources -@PartitionMapper(name = "input") +@PartitionMapper(name = "{{name}}") public class {{className}} implements Serializable { private final {{configurationName}} configuration; private final {{serviceName}} service; diff --git a/component-starter-server/src/main/resources/generator/component/Processor.java b/component-starter-server/src/main/resources/generator/component/Processor.java index ddae1ebb8f244..1fb567f6864f5 100644 --- a/component-starter-server/src/main/resources/generator/component/Processor.java +++ b/component-starter-server/src/main/resources/generator/component/Processor.java @@ -3,10 +3,22 @@ import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; -import org.talend.component.api.input.Producer; +import org.talend.component.api.component.Icon; +import org.talend.component.api.component.Version; +import org.talend.component.api.configuration.Option; +import org.talend.component.api.processor.AfterGroup; +import org.talend.component.api.processor.BeforeGroup; +import org.talend.component.api.processor.Processor;{{#inputs.length}} +import org.talend.component.api.processor.Input;{{/inputs.length}}{{#outputs.length}} +import org.talend.component.api.processor.Output; +import org.talend.component.api.processor.OutputEmitter;{{/outputs.length}}{{#generic}} +import org.talend.component.api.processor.data.ObjectMap;{{/generic}} import {{servicePackage}}.{{serviceName}}; +@Version(1) // default version is 1, if some configuration changes happen between 2 versions you can add a migrationHandler +@Icon({{icon}}) // you can use a custom one using @Icon(value=CUSTOM, custom="filename") and adding icons/filename_icon32.png in resources +@Processor(name = "{{name}}") public class {{className}} implements Serializable { private final {{configurationName}} configuration; private final {{serviceName}} service; @@ -21,20 +33,35 @@ public class {{className}} implements Serializable { public void init() { // this method will be executed once for the whole component execution, // this is where you can establish a connection for instance + // Note: if you don't need it you can delete it + } + + @BeforeGroup + public void beforeGroup() { + // if the environment supports chunking this method is called at the beginning if a chunk + // it can be used to start a local transaction specific to the backend you use + // Note: if you don't need it you can delete it } @Producer - public {{modelName} next() { - // this is the method allowing you to go through the dataset associated - // to the component configuration - // - // return null means the dataset has no more data to go through - return null; + public void onNext({{#inputs}} + @Input("{{name}}") final {{type}} {{javaName}}Input{{^-last}},{{/-last}}{{/inputs}}{{#outputs}} + @Output("{{name}}") final OutputEmitter<{{type}}> {{javaName}}Output{{^-last}},{{/-last}}{{/outputs}}) { + // this is the method allowing you to handle the input(s) and emit the output(s) + // after some custom logic you put here, to send a value to next element you can use an + // output parameter and call emit(value). + } + + @AfterGroup + public void afterGroup() { + // symmetric method of the beforeGroup() executed after the chunk processing + // Note: if you don't need it you can delete it } @PreDestroy public void release() { // this is the symmetric method of the init() one, // release potential connections you created or data you cached + // Note: if you don't need it you can delete it } } diff --git a/component-starter-server/src/test/java/org/talend/component/starter/server/service/facet/component/ComponentGeneratorTest.java b/component-starter-server/src/test/java/org/talend/component/starter/server/service/facet/component/ComponentGeneratorTest.java index 267321aad284d..96beae905b7ed 100644 --- a/component-starter-server/src/test/java/org/talend/component/starter/server/service/facet/component/ComponentGeneratorTest.java +++ b/component-starter-server/src/test/java/org/talend/component/starter/server/service/facet/component/ComponentGeneratorTest.java @@ -17,12 +17,16 @@ import static java.util.Arrays.asList; import static java.util.Collections.emptyList; -import static java.util.Collections.emptySet; import static java.util.Collections.singleton; +import static java.util.Collections.singletonList; +import static java.util.Collections.singletonMap; import static java.util.stream.Collectors.toMap; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import java.util.HashMap; import java.util.Map; +import java.util.Set; import javax.inject.Inject; @@ -44,12 +48,11 @@ public class ComponentGeneratorTest { @Test public void source() { - final Map files = generator - .create("com.foo", build, emptySet(), "superfamily", "supercategory", - singleton(new ProjectRequest.SourceConfiguration("mycomp", "", false, - new ProjectRequest.DataStructure(singleton(new ProjectRequest.Entry("name", "string", null))), - new ProjectRequest.DataStructure(singleton(new ProjectRequest.Entry("name", "string", null))))), - emptyList()) + final Set sources = singleton(new ProjectRequest.SourceConfiguration("mycomp", "", + new ProjectRequest.DataStructure(singleton(new ProjectRequest.Entry("name", "string", null))), + new ProjectRequest.StructureConfiguration( + new ProjectRequest.DataStructure(singleton(new ProjectRequest.Entry("name", "string", null))), false))); + final Map files = generator.create("com.foo", build, "superfamily", "supercategory", sources, emptyList()) .collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); assertEquals(6, files.size()); assertEquals( @@ -89,7 +92,7 @@ public void source() { + "// this class role is to enable the work to be distributed in environments supporting it.\n" + "//\n" + "@Version(1) // default version is 1, if some configuration changes happen between 2 versions you can add a migrationHandler\n" + "@Icon(Icon.IconType.STAR) // you can use a custom one using @Icon(value=CUSTOM, custom=\"filename\") and adding icons/filename_icon32.png in resources\n" - + "@PartitionMapper(name = \"input\")\n" + "public class MycompMapper implements Serializable {\n" + + "@PartitionMapper(name = \"mycomp\")\n" + "public class MycompMapper implements Serializable {\n" + " private final MycompSourceConfiguration configuration;\n" + " private final TestService service;\n" + "\n" + " public MycompMapper(@Option(\"configuration\") final MycompSourceConfiguration configuration,\n" + " final TestService service) {\n" + " this.configuration = configuration;\n" @@ -131,15 +134,13 @@ public void source() { @Test public void sourceComplexConfiguration() { - final Map files = generator - .create("com.foo", build, emptySet(), "superfamily", "supercategory", - singleton(new ProjectRequest.SourceConfiguration("mycomp", "", false, - new ProjectRequest.DataStructure(singleton(new ProjectRequest.Entry("person", "", - new ProjectRequest.DataStructure(asList(new ProjectRequest.Entry("name", "string", null), - new ProjectRequest.Entry("age", "int", null)))))), - new ProjectRequest.DataStructure(emptyList()))), - emptyList()) - .collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); + final Map files = generator.create("com.foo", build, "superfamily", "supercategory", + singleton(new ProjectRequest.SourceConfiguration("mycomp", "", + new ProjectRequest.DataStructure(singleton(new ProjectRequest.Entry("person", "", + new ProjectRequest.DataStructure(asList(new ProjectRequest.Entry("name", "string", null), + new ProjectRequest.Entry("age", "int", null)))))), + new ProjectRequest.StructureConfiguration(new ProjectRequest.DataStructure(emptyList()), false))), + emptyList()).collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); assertEquals("package com.foo.source;\n" + "\n" + "import java.util.List;\n" + "\n" + "import org.talend.component.api.configuration.Option;\n" @@ -158,17 +159,17 @@ public void sourceComplexConfiguration() { + "@GridLayout({\n" + " // the generated layout put one configuration entry per line,\n" + " // customize it as much as needed\n" + " @GridLayout.Row({ \"name\" }),\n" + " @GridLayout.Row({ \"age\" })\n" + "})\n" + "public class PersonConfiguration {\n" + " @Option\n" - + " private String name;\n" + "\n" + " @Option\n" + " private String age;\n" + "\n" + + " private String name;\n" + "\n" + " @Option\n" + " private int age;\n" + "\n" + " public String getName() {\n" + " return name;\n" + " }\n" + "\n" - + " public String getAge() {\n" + " return age;\n" + " }\n" + "}", + + " public int getAge() {\n" + " return age;\n" + " }\n" + "}", files.get("src/main/java/com/foo/source/PersonConfiguration.java")); } @Test public void genericSource() { final Map files = generator - .create("com.foo", build, emptySet(), "superfamily", "supercategory", - singleton(new ProjectRequest.SourceConfiguration("mycomp", "", true, null, null)), emptyList()) + .create("com.foo", build, "superfamily", "supercategory", + singleton(new ProjectRequest.SourceConfiguration("mycomp", "", null, null)), emptyList()) .collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); assertEquals("package com.foo.source;\n" + "\n" + "import javax.annotation.PostConstruct;\n" @@ -190,4 +191,298 @@ public void genericSource() { + " // release potential connections you created or data you cached\n" + " }\n" + "}", files.get("src/main/java/com/foo/source/MycompSource.java")); } + + @Test + public void isolatedProcessor() { + final Map files = generator + .create("com.foo", build, "superfamily", "supercategory", emptyList(), + singletonList(new ProjectRequest.ProcessorConfiguration("tProc", "", null, null, null))) + .collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); + + assertEquals("package com.foo.output;\n" + "\n" + "import javax.annotation.PostConstruct;\n" + + "import javax.annotation.PreDestroy;\n" + "\n" + "import org.talend.component.api.component.Icon;\n" + + "import org.talend.component.api.component.Version;\n" + + "import org.talend.component.api.configuration.Option;\n" + + "import org.talend.component.api.processor.AfterGroup;\n" + + "import org.talend.component.api.processor.BeforeGroup;\n" + + "import org.talend.component.api.processor.Processor;\n" + "\n" + "import com.foo.service.TestService;\n" + "\n" + + "@Version(1) // default version is 1, if some configuration changes happen between 2 versions you can add a migrationHandler\n" + + "@Icon(Icon.IconType.STAR) // you can use a custom one using @Icon(value=CUSTOM, custom=\"filename\") and adding icons/filename_icon32.png in resources\n" + + "@Processor(name = \"tProc\")\n" + "public class TProcProcessor implements Serializable {\n" + + " private final TProcProcessorConfiguration configuration;\n" + " private final TestService service;\n" + + "\n" + " public TProcProcessor(@Option(\"configuration\") final TProcProcessorConfiguration configuration,\n" + + " final TestService service) {\n" + " this.configuration = configuration;\n" + + " this.service = service;\n" + " }\n" + "\n" + " @PostConstruct\n" + " public void init() {\n" + + " // this method will be executed once for the whole component execution,\n" + + " // this is where you can establish a connection for instance\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @BeforeGroup\n" + + " public void beforeGroup() {\n" + + " // if the environment supports chunking this method is called at the beginning if a chunk\n" + + " // it can be used to start a local transaction specific to the backend you use\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @Producer\n" + + " public void onNext() {\n" + + " // this is the method allowing you to handle the input(s) and emit the output(s)\n" + + " // after some custom logic you put here, to send a value to next element you can use an\n" + + " // output parameter and call emit(value).\n" + " }\n" + "\n" + " @AfterGroup\n" + + " public void afterGroup() {\n" + + " // symmetric method of the beforeGroup() executed after the chunk processing\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @PreDestroy\n" + + " public void release() {\n" + " // this is the symmetric method of the init() one,\n" + + " // release potential connections you created or data you cached\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "}", + files.get("src/main/java/com/foo/output/TProcProcessor.java")); + } + + @Test + public void processorOutput() { + final Map files = generator.create("com.foo", build, "superfamily", "supercategory", emptyList(), + singletonList(new ProjectRequest.ProcessorConfiguration("tProc", "", null, null, + new HashMap() { + + { + put("__default__", new ProjectRequest.StructureConfiguration(new ProjectRequest.DataStructure( + singletonList(new ProjectRequest.Entry("name", "string", null))), false)); + } + }))) + .collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); + + assertEquals("package com.foo.processor;\n" + "\n" + "import javax.annotation.PostConstruct;\n" + + "import javax.annotation.PreDestroy;\n" + "\n" + "import org.talend.component.api.component.Icon;\n" + + "import org.talend.component.api.component.Version;\n" + + "import org.talend.component.api.configuration.Option;\n" + + "import org.talend.component.api.processor.AfterGroup;\n" + + "import org.talend.component.api.processor.BeforeGroup;\n" + + "import org.talend.component.api.processor.Processor;\n" + "\n" + "import com.foo.service.TestService;\n" + "\n" + + "@Version(1) // default version is 1, if some configuration changes happen between 2 versions you can add a migrationHandler\n" + + "@Icon(Icon.IconType.STAR) // you can use a custom one using @Icon(value=CUSTOM, custom=\"filename\") and adding icons/filename_icon32.png in resources\n" + + "@Processor(name = \"tProc\")\n" + "public class TProcProcessor implements Serializable {\n" + + " private final TProcProcessorConfiguration configuration;\n" + " private final TestService service;\n" + + "\n" + " public TProcProcessor(@Option(\"configuration\") final TProcProcessorConfiguration configuration,\n" + + " final TestService service) {\n" + " this.configuration = configuration;\n" + + " this.service = service;\n" + " }\n" + "\n" + " @PostConstruct\n" + " public void init() {\n" + + " // this method will be executed once for the whole component execution,\n" + + " // this is where you can establish a connection for instance\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @BeforeGroup\n" + + " public void beforeGroup() {\n" + + " // if the environment supports chunking this method is called at the beginning if a chunk\n" + + " // it can be used to start a local transaction specific to the backend you use\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @Producer\n" + + " public void onNext(\n" + + " @Output(\"__default__\") final OutputEmitter defaultOutput) {\n" + + " // this is the method allowing you to handle the input(s) and emit the output(s)\n" + + " // after some custom logic you put here, to send a value to next element you can use an\n" + + " // output parameter and call emit(value).\n" + " }\n" + "\n" + " @AfterGroup\n" + + " public void afterGroup() {\n" + + " // symmetric method of the beforeGroup() executed after the chunk processing\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @PreDestroy\n" + + " public void release() {\n" + " // this is the symmetric method of the init() one,\n" + + " // release potential connections you created or data you cached\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "}", + files.get("src/main/java/com/foo/processor/TProcProcessor.java")); + assertEquals("package com.foo.processor;\n" + "\n" + "// this is the pojo which will be used to represent your data\n" + + "public class TProcDefaultOutput {\n" + "\n" + " private String name;\n" + "\n" + + " public String getName() {\n" + " return name;\n" + " }\n" + "\n" + + " public void setName(final String name) {\n" + " this.name = name;\n" + " }\n" + " \n" + "}", + files.get("src/main/java/com/foo/processor/TProcDefaultOutput.java")); + } + + @Test + public void processorGenericOutput() { + final Map files = generator + .create("com.foo", build, "superfamily", "supercategory", emptyList(), + singletonList(new ProjectRequest.ProcessorConfiguration("tProc", "", null, null, + singletonMap("__default__", new ProjectRequest.StructureConfiguration(null, true))))) + .collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); + + assertEquals("package com.foo.processor;\n" + "\n" + "import javax.annotation.PostConstruct;\n" + + "import javax.annotation.PreDestroy;\n" + "\n" + "import org.talend.component.api.component.Icon;\n" + + "import org.talend.component.api.component.Version;\n" + + "import org.talend.component.api.configuration.Option;\n" + + "import org.talend.component.api.processor.AfterGroup;\n" + + "import org.talend.component.api.processor.BeforeGroup;\n" + + "import org.talend.component.api.processor.Processor;\n" + + "import org.talend.component.api.processor.data.ObjectMap;\n" + "\n" + "import com.foo.service.TestService;\n" + + "\n" + + "@Version(1) // default version is 1, if some configuration changes happen between 2 versions you can add a migrationHandler\n" + + "@Icon(Icon.IconType.STAR) // you can use a custom one using @Icon(value=CUSTOM, custom=\"filename\") and adding icons/filename_icon32.png in resources\n" + + "@Processor(name = \"tProc\")\n" + "public class TProcProcessor implements Serializable {\n" + + " private final TProcProcessorConfiguration configuration;\n" + " private final TestService service;\n" + + "\n" + " public TProcProcessor(@Option(\"configuration\") final TProcProcessorConfiguration configuration,\n" + + " final TestService service) {\n" + " this.configuration = configuration;\n" + + " this.service = service;\n" + " }\n" + "\n" + " @PostConstruct\n" + " public void init() {\n" + + " // this method will be executed once for the whole component execution,\n" + + " // this is where you can establish a connection for instance\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @BeforeGroup\n" + + " public void beforeGroup() {\n" + + " // if the environment supports chunking this method is called at the beginning if a chunk\n" + + " // it can be used to start a local transaction specific to the backend you use\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @Producer\n" + + " public void onNext(\n" + + " @Output(\"__default__\") final OutputEmitter defaultOutput) {\n" + + " // this is the method allowing you to handle the input(s) and emit the output(s)\n" + + " // after some custom logic you put here, to send a value to next element you can use an\n" + + " // output parameter and call emit(value).\n" + " }\n" + "\n" + " @AfterGroup\n" + + " public void afterGroup() {\n" + + " // symmetric method of the beforeGroup() executed after the chunk processing\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @PreDestroy\n" + + " public void release() {\n" + " // this is the symmetric method of the init() one,\n" + + " // release potential connections you created or data you cached\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "}", + files.get("src/main/java/com/foo/processor/TProcProcessor.java")); + assertNull(files.get("src/main/java/com/foo/processor/TProcDefaultOutput.java")); + } + + @Test + public void processorInput() { + final Map files = generator + .create("com.foo", build, "superfamily", "supercategory", emptyList(), + singletonList(new ProjectRequest.ProcessorConfiguration("tProc", "", null, + singletonMap("__default__", + new ProjectRequest.StructureConfiguration(new ProjectRequest.DataStructure( + singleton(new ProjectRequest.Entry("name", "string", null))), false)), + null))) + .collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); + + assertEquals("package com.foo.output;\n" + "\n" + "import javax.annotation.PostConstruct;\n" + + "import javax.annotation.PreDestroy;\n" + "\n" + "import org.talend.component.api.component.Icon;\n" + + "import org.talend.component.api.component.Version;\n" + + "import org.talend.component.api.configuration.Option;\n" + + "import org.talend.component.api.processor.AfterGroup;\n" + + "import org.talend.component.api.processor.BeforeGroup;\n" + + "import org.talend.component.api.processor.Processor;\n" + "\n" + "import com.foo.service.TestService;\n" + "\n" + + "@Version(1) // default version is 1, if some configuration changes happen between 2 versions you can add a migrationHandler\n" + + "@Icon(Icon.IconType.STAR) // you can use a custom one using @Icon(value=CUSTOM, custom=\"filename\") and adding icons/filename_icon32.png in resources\n" + + "@Processor(name = \"tProc\")\n" + "public class TProcProcessor implements Serializable {\n" + + " private final TProcProcessorConfiguration configuration;\n" + " private final TestService service;\n" + + "\n" + " public TProcProcessor(@Option(\"configuration\") final TProcProcessorConfiguration configuration,\n" + + " final TestService service) {\n" + " this.configuration = configuration;\n" + + " this.service = service;\n" + " }\n" + "\n" + " @PostConstruct\n" + " public void init() {\n" + + " // this method will be executed once for the whole component execution,\n" + + " // this is where you can establish a connection for instance\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @BeforeGroup\n" + + " public void beforeGroup() {\n" + + " // if the environment supports chunking this method is called at the beginning if a chunk\n" + + " // it can be used to start a local transaction specific to the backend you use\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @Producer\n" + + " public void onNext(\n" + " @Input(\"__default__\") final TProcDefaultInput defaultInput) {\n" + + " // this is the method allowing you to handle the input(s) and emit the output(s)\n" + + " // after some custom logic you put here, to send a value to next element you can use an\n" + + " // output parameter and call emit(value).\n" + " }\n" + "\n" + " @AfterGroup\n" + + " public void afterGroup() {\n" + + " // symmetric method of the beforeGroup() executed after the chunk processing\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @PreDestroy\n" + + " public void release() {\n" + " // this is the symmetric method of the init() one,\n" + + " // release potential connections you created or data you cached\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "}", + files.get("src/main/java/com/foo/output/TProcProcessor.java")); + assertEquals("package com.foo.output;\n" + "\n" + "// this is the pojo which will be used to represent your data\n" + + "public class TProcDefaultInput {\n" + "\n" + " private String name;\n" + "\n" + + " public String getName() {\n" + " return name;\n" + " }\n" + "\n" + + " public void setName(final String name) {\n" + " this.name = name;\n" + " }\n" + " \n" + "}", + files.get("src/main/java/com/foo/output/TProcDefaultInput.java")); + } + + @Test + public void processorGenericInput() { + final Map files = generator + .create("com.foo", build, "superfamily", "supercategory", emptyList(), + singletonList(new ProjectRequest.ProcessorConfiguration("tProc", "", null, + singletonMap("__default__", new ProjectRequest.StructureConfiguration(null, true)), null))) + .collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); + + assertEquals("package com.foo.output;\n" + "\n" + "import javax.annotation.PostConstruct;\n" + + "import javax.annotation.PreDestroy;\n" + "\n" + "import org.talend.component.api.component.Icon;\n" + + "import org.talend.component.api.component.Version;\n" + + "import org.talend.component.api.configuration.Option;\n" + + "import org.talend.component.api.processor.AfterGroup;\n" + + "import org.talend.component.api.processor.BeforeGroup;\n" + + "import org.talend.component.api.processor.Processor;\n" + + "import org.talend.component.api.processor.data.ObjectMap;\n" + "\n" + "import com.foo.service.TestService;\n" + + "\n" + + "@Version(1) // default version is 1, if some configuration changes happen between 2 versions you can add a migrationHandler\n" + + "@Icon(Icon.IconType.STAR) // you can use a custom one using @Icon(value=CUSTOM, custom=\"filename\") and adding icons/filename_icon32.png in resources\n" + + "@Processor(name = \"tProc\")\n" + "public class TProcProcessor implements Serializable {\n" + + " private final TProcProcessorConfiguration configuration;\n" + " private final TestService service;\n" + + "\n" + " public TProcProcessor(@Option(\"configuration\") final TProcProcessorConfiguration configuration,\n" + + " final TestService service) {\n" + " this.configuration = configuration;\n" + + " this.service = service;\n" + " }\n" + "\n" + " @PostConstruct\n" + " public void init() {\n" + + " // this method will be executed once for the whole component execution,\n" + + " // this is where you can establish a connection for instance\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @BeforeGroup\n" + + " public void beforeGroup() {\n" + + " // if the environment supports chunking this method is called at the beginning if a chunk\n" + + " // it can be used to start a local transaction specific to the backend you use\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @Producer\n" + + " public void onNext(\n" + " @Input(\"__default__\") final ObjectMap defaultInput) {\n" + + " // this is the method allowing you to handle the input(s) and emit the output(s)\n" + + " // after some custom logic you put here, to send a value to next element you can use an\n" + + " // output parameter and call emit(value).\n" + " }\n" + "\n" + " @AfterGroup\n" + + " public void afterGroup() {\n" + + " // symmetric method of the beforeGroup() executed after the chunk processing\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @PreDestroy\n" + + " public void release() {\n" + " // this is the symmetric method of the init() one,\n" + + " // release potential connections you created or data you cached\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "}", + files.get("src/main/java/com/foo/output/TProcProcessor.java")); + } + + @Test + public void standardProcessor() { + final Map files = generator.create("com.foo", build, "superfamily", "supercategory", emptyList(), + singletonList(new ProjectRequest.ProcessorConfiguration("tProc", "", null, + singletonMap("__default__", new ProjectRequest.StructureConfiguration(null, true)), + new HashMap() { + + { + put("__default__", new ProjectRequest.StructureConfiguration( + new ProjectRequest.DataStructure(singleton(new ProjectRequest.Entry("age", "int", null))), + false)); + put("reject", new ProjectRequest.StructureConfiguration(null, true)); + } + }))) + .collect(toMap(FacetGenerator.InMemoryFile::getPath, FacetGenerator.InMemoryFile::getContent)); + + assertEquals("package com.foo.processor;\n" + "\n" + "import javax.annotation.PostConstruct;\n" + + "import javax.annotation.PreDestroy;\n" + "\n" + "import org.talend.component.api.component.Icon;\n" + + "import org.talend.component.api.component.Version;\n" + + "import org.talend.component.api.configuration.Option;\n" + + "import org.talend.component.api.processor.AfterGroup;\n" + + "import org.talend.component.api.processor.BeforeGroup;\n" + + "import org.talend.component.api.processor.Processor;\n" + + "import org.talend.component.api.processor.data.ObjectMap;\n" + "\n" + "import com.foo.service.TestService;\n" + + "\n" + + "@Version(1) // default version is 1, if some configuration changes happen between 2 versions you can add a migrationHandler\n" + + "@Icon(Icon.IconType.STAR) // you can use a custom one using @Icon(value=CUSTOM, custom=\"filename\") and adding icons/filename_icon32.png in resources\n" + + "@Processor(name = \"tProc\")\n" + "public class TProcProcessor implements Serializable {\n" + + " private final TProcProcessorConfiguration configuration;\n" + " private final TestService service;\n" + + "\n" + " public TProcProcessor(@Option(\"configuration\") final TProcProcessorConfiguration configuration,\n" + + " final TestService service) {\n" + " this.configuration = configuration;\n" + + " this.service = service;\n" + " }\n" + "\n" + " @PostConstruct\n" + " public void init() {\n" + + " // this method will be executed once for the whole component execution,\n" + + " // this is where you can establish a connection for instance\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @BeforeGroup\n" + + " public void beforeGroup() {\n" + + " // if the environment supports chunking this method is called at the beginning if a chunk\n" + + " // it can be used to start a local transaction specific to the backend you use\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @Producer\n" + + " public void onNext(\n" + " @Input(\"__default__\") final ObjectMap defaultInput\n" + + " @Output(\"reject\") final OutputEmitter rejectOutput,\n" + + " @Output(\"__default__\") final OutputEmitter defaultOutput) {\n" + + " // this is the method allowing you to handle the input(s) and emit the output(s)\n" + + " // after some custom logic you put here, to send a value to next element you can use an\n" + + " // output parameter and call emit(value).\n" + " }\n" + "\n" + " @AfterGroup\n" + + " public void afterGroup() {\n" + + " // symmetric method of the beforeGroup() executed after the chunk processing\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "\n" + " @PreDestroy\n" + + " public void release() {\n" + " // this is the symmetric method of the init() one,\n" + + " // release potential connections you created or data you cached\n" + + " // Note: if you don't need it you can delete it\n" + " }\n" + "}", + files.get("src/main/java/com/foo/processor/TProcProcessor.java")); + assertEquals( + "package com.foo.processor;\n" + "\n" + "// this is the pojo which will be used to represent your data\n" + + "public class TProcDefaultOutput {\n" + "\n" + " private int age;\n" + "\n" + + " public int getAge() {\n" + " return age;\n" + " }\n" + "\n" + + " public void setAge(final int age) {\n" + " this.age = age;\n" + " }\n" + " \n" + "}", + files.get("src/main/java/com/foo/processor/TProcDefaultOutput.java")); + } }