ENTAXY-248 release 1.8.1

This commit is contained in:
2022-02-28 15:20:38 +03:00
parent 4d274c4fcc
commit c826adf1db
1958 changed files with 195926 additions and 10280 deletions

View File

@ -5,7 +5,7 @@
<parent>
<groupId>ru.entaxy.esb.platform.runtime.base.connecting</groupId>
<artifactId>generator</artifactId>
<version>1.8.0</version>
<version>1.8.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -20,9 +20,16 @@
<bundle.osgi.export.pkg>
ru.entaxy.esb.platform.runtime.base.connecting.generator.ftl
</bundle.osgi.export.pkg>
<freemarker.version>2.3.29</freemarker.version>
</properties>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
<version>1.4.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ru.entaxy.esb.platform.runtime.base.connecting.generator</groupId>
<artifactId>generator-api</artifactId>
@ -34,9 +41,38 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>${freemarker.version}</version>
</dependency>
<!-- dependency>
<groupId>ru.entaxy.esb.system.management.blueprint.generator</groupId>
<artifactId>blueprint-generator</artifactId>
<version>${project.version}</version>
</dependency -->
<dependency>
<groupId>
ru.entaxy.esb.platform.runtime.base.connecting.generator
</groupId>
<artifactId>generator-factory</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>${maven-bundle-plugin.version}</version>
<extensions>true</extensions>
<configuration>
<instructions>
<_dsannotations>*</_dsannotations>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -19,48 +19,251 @@
*/
package ru.entaxy.esb.platform.runtime.base.connecting.generator.ftl;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generator;
import ru.entaxy.esb.system.core.template.Template;
import ru.entaxy.esb.system.management.blueprint.generator.Blueprint;
import ru.entaxy.esb.system.management.blueprint.generator.BlueprintGenerator;
import static freemarker.template.Configuration.SQUARE_BRACKET_INTERPOLATION_SYNTAX;
import static freemarker.template.Configuration.SQUARE_BRACKET_TAG_SYNTAX;
import java.io.IOException;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FTLGenerator implements Generator {
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import freemarker.cache.URLTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import ru.entaxy.base.generator.template.TemplateAwareGenerator;
import ru.entaxy.base.generator.template.TemplateImpl;
import ru.entaxy.esb.platform.runtime.base.connecting.generator.AbstractSelfPublishGenerator;
import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generated;
import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generator;
import ru.entaxy.esb.platform.runtime.base.connecting.generator.GeneratorService;
import ru.entaxy.esb.platform.runtime.base.connecting.generator.factory.GeneratorFactory;
import ru.entaxy.esb.system.common.exception.TemplateNotFound;
@Component(
service = {GeneratorService.class},
property = {GeneratorService.PROP_GENERATOR_TYPE + "=" + FTLGenerator.GENERATOR_NAME},
immediate = true
)
public class FTLGenerator extends AbstractSelfPublishGenerator<FTLGenerator>
implements Generator, TemplateAwareGenerator {
public final static String GENERATOR_NAME = "ftl";
private static final Logger log = LoggerFactory.getLogger(FTLGenerator.class);
private BlueprintGenerator blueprintGenerator;
private Template template;
public static final String PROP_TEMPLATE_NAME = "template.name";
public static final String PROP_TEMPLATE_PATH = "template.path";
private static final String ATTR_GENERATED_TYPE = "generated.type";
private static final String FTL_EXTENSION = ".ftl";
private static final String XML_EXTENSION = ".xml";
private static final String DEFAULT_TEMPLATE_PATH = "/template/";
// private BlueprintGenerator blueprintGenerator;
private ru.entaxy.base.generator.template.Template template;
@Activate
public void activate() {
GeneratorFactory.register(FTLGenerator.GENERATOR_NAME, this);
}
@Deactivate
public void deactivate() {
GeneratorFactory.unregister(GENERATOR_NAME);
}
@Override
public Blueprint generate(Map<String, Object> properties) throws Exception {
Map<String, String> params = (Map)properties;
log.debug("\nTemplateLocation: {}\nTemplateName: {}\nConnectionName: {}",
template.getTemplateLocation(), template.getTemplateName(), params.get("connectionName"));
Blueprint blueprint = getBlueprintGenerator().createBlueprint(template.getTemplateLocation(),
template.getTemplateName(), params.get("connectionName"), params);
// TODO: 09.07.2021 Artifact wrapper needed instead of Blueprint (not only blueprint can be return from generator)
return blueprint;
public List<String> getSupportedTemplateTypes() {
return Arrays.asList(new String[] {"ftl"});
}
@Override
public Generated generate(ru.entaxy.base.generator.template.Template template, Map<String, Object> properties)
throws Exception {
return generateForTemplate(template, properties);
}
@Override
public Generated generate(Map<String, Object> properties) throws Exception {
prepareTemplate(properties);
return generateForTemplate(template, properties);
}
public Generated generateForTemplate(ru.entaxy.base.generator.template.Template template,
Map<String, Object> properties) throws Exception {
URL templateURL = template.getTemplateLocation()==null
?FrameworkUtil.getBundle(FTLGenerator.class).getEntry(DEFAULT_TEMPLATE_PATH)
:template.getTemplateLocation();
return doGenerate(
templateURL,
template.getTemplateFullName(),
properties);
}
public void setTemplate(Template template) {
protected static class Enricher {
Generated origin;
Map<String, Object> properties = new HashMap<>();
String extraPrefix = "extra.";
public Enricher(Generated origin) {
this.origin = origin;
}
public Enricher cleanOrigin() {
this.origin.getProperties().clear();
return this;
}
public Enricher property(String name, Object value) {
properties.put(name, value);
return this;
}
public Enricher properties(Map<String, Object> properties) {
this.properties.putAll(properties);
return this;
}
public Enricher extraPrefix(String extraPrefix) {
this.extraPrefix = extraPrefix;
return this;
}
public Enricher extra(String name, Object value) {
properties.put(extraPrefix + name, value);
return this;
}
public Enricher extras(Map<String, Object> properties) {
for (Map.Entry<String, Object> entry: properties.entrySet())
this.properties.put(extraPrefix + entry.getKey(), entry.getValue());
return this;
}
public Generated get() {
this.origin.getProperties().putAll(this.properties);
this.origin.getProperties().put("_extraPrefix", extraPrefix);
return this.origin;
}
}
protected static Enricher enrich(Generated origin) {
return new Enricher(origin);
}
private Generated doGenerate(URL templateUrl, String templateFullName, Map<String, Object> params)
throws IOException, TemplateException, TemplateNotFound {
Template temp = getTemplateByFullName(templateUrl, templateFullName);
Object value = temp.getCustomAttribute(FTLGenerator.ATTR_GENERATED_TYPE);
String type = null==value?null:value.toString();
log.info("--TEMPLATE--\n" + templateUrl.toString() + templateFullName + "\ngenerated.type: " + (null==value?Generated.GENERATED_TYPE_UNKNOWN:value.toString()));
try (StringWriter writer = new StringWriter()) {
temp.process(params, writer);
return enrich(Generated.create(type, writer.toString()))
.properties(params)
.extraPrefix("generator.")
.extra("templateUrl", templateUrl)
.extra("templateFullName", value)
.get();
}
}
protected void prepareTemplate(Map<String, Object> generationProperties) {
if (this.template != null)
return;
Map<String, Object> finalPropertes = new HashMap<>();
finalPropertes.putAll(initialProperties);
finalPropertes.putAll(generationProperties);
TemplateImpl temp = new TemplateImpl();
temp.setBundleContext(
(BundleContext)finalPropertes.getOrDefault(Generator.PROP_TARGET_BUNDLE_CONTEXT
, FrameworkUtil.getBundle(FTLGenerator.class).getBundleContext())
);
temp.setTemplateName((String)finalPropertes.getOrDefault(PROP_TEMPLATE_NAME
, (String)finalPropertes.getOrDefault(Generator.PROP_USAGE_TYPE
, "root")));
temp.setPath((String)finalPropertes.getOrDefault(PROP_TEMPLATE_NAME
, DEFAULT_TEMPLATE_PATH)
);
temp.setTemplateFullName(temp.getTemplateFileName() + FTL_EXTENSION);
this.template = temp;
}
private Generated doGenerate(URL templateUrl, String templateName, String systemName, Map<String, String> params)
throws IOException, TemplateException, TemplateNotFound {
Template temp = getTemplateByName(templateUrl, templateName);
Object value = temp.getCustomAttribute(FTLGenerator.ATTR_GENERATED_TYPE);
String type = null==value?null:value.toString();
log.info("--BLUEPRINT--\n" + templateUrl.toString() + templateName + "\ngenerated.type: " + (null==value?Generated.GENERATED_TYPE_UNKNOWN:value.toString()));
try (StringWriter writer = new StringWriter()) {
temp.process(params, writer);
return Generated.create(type, writer.toString());
}
}
private Template getTemplateByFullName(URL templateUrl, String templateFullName) throws IOException, TemplateNotFound {
Configuration cfg = getConfiguration(templateUrl);
return cfg.getTemplate(templateFullName);
}
private Template getTemplateByName(URL templateUrl, String templateName) throws IOException, TemplateNotFound {
return getTemplateByFullName(templateUrl, templateName + FTL_EXTENSION);
}
private Configuration getConfiguration(URL templateUrl) throws TemplateNotFound {
Configuration freemarkerConfig = new Configuration();
freemarkerConfig.setObjectWrapper(new DefaultObjectWrapper());
freemarkerConfig.setLocalizedLookup(false);
freemarkerConfig.setTemplateLoader(getURLTemplateLoader(templateUrl));
freemarkerConfig.setInterpolationSyntax(SQUARE_BRACKET_INTERPOLATION_SYNTAX);
freemarkerConfig.setTagSyntax(SQUARE_BRACKET_TAG_SYNTAX);
return freemarkerConfig;
}
private URLTemplateLoader getURLTemplateLoader(URL templateUrl) {
return new URLTemplateLoader() {
@Override
protected URL getURL(String templateName) {
try {
return new URL(templateUrl + templateName);
} catch (MalformedURLException e) {
log.error(e.getMessage(), e);
}
return null;
}
};
}
@Deprecated
public void setTemplate(ru.entaxy.base.generator.template.Template template) {
this.template = template;
}
public BlueprintGenerator getBlueprintGenerator() {
if (blueprintGenerator == null) {
BundleContext bundleContext = FrameworkUtil.getBundle(BlueprintGenerator.class).getBundleContext();
ServiceReference<BlueprintGenerator> blueprintGeneratorServiceReference =
bundleContext.getServiceReference(BlueprintGenerator.class);
blueprintGenerator = bundleContext.getService(blueprintGeneratorServiceReference);
}
return blueprintGenerator;
}
}