Search in sources :

Example 1 with ComponentModel

use of org.apache.camel.maven.connector.model.ComponentModel in project camel by apache.

the class SpringBootAutoConfigurationMojo method createConnectorConfigurationSource.

private void createConnectorConfigurationSource(String packageName, ComponentModel model, String javaType, String connectorScheme, List<String> componentOptions) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    int pos = javaType.lastIndexOf(".");
    String name = javaType.substring(pos + 1);
    name = name.replace("Component", "ConnectorConfiguration");
    javaClass.setPackage(packageName).setName(name);
    String doc = "Generated by camel-connector-maven-plugin - do not edit this file!";
    if (!Strings.isBlank(model.getDescription())) {
        doc = model.getDescription() + "\n\n" + doc;
    }
    // replace Component with Connector
    doc = doc.replaceAll("Component", "Connector");
    doc = doc.replaceAll("component", "connector");
    javaClass.getJavaDoc().setFullText(doc);
    // compute the configuration prefix to use with spring boot configuration
    String prefix = "";
    if (!"false".equalsIgnoreCase(configurationPrefix)) {
        // make sure prefix is in lower case
        prefix = configurationPrefix.toLowerCase(Locale.US);
        if (!prefix.endsWith(".")) {
            prefix += ".";
        }
    }
    prefix += connectorScheme.toLowerCase(Locale.US);
    javaClass.addAnnotation("org.springframework.boot.context.properties.ConfigurationProperties").setStringValue("prefix", prefix);
    for (ComponentOptionModel option : model.getComponentOptions()) {
        // only include the options that has been explicit configured in the camel-connector.json file
        boolean accepted = false;
        if (componentOptions != null) {
            accepted = componentOptions.stream().anyMatch(o -> o.equals(option.getName()));
        }
        if (accepted) {
            String type = option.getJavaType();
            PropertySource<JavaClassSource> prop = javaClass.addProperty(type, option.getName());
            if ("true".equals(option.getDeprecated())) {
                prop.getField().addAnnotation(Deprecated.class);
                prop.getAccessor().addAnnotation(Deprecated.class);
                prop.getMutator().addAnnotation(Deprecated.class);
                // DeprecatedConfigurationProperty must be on getter when deprecated
                prop.getAccessor().addAnnotation(DeprecatedConfigurationProperty.class);
            }
            if (!Strings.isBlank(option.getDescription())) {
                prop.getField().getJavaDoc().setFullText(option.getDescription());
            }
            if (!Strings.isBlank(option.getDefaultValue())) {
                if ("java.lang.String".equals(option.getJavaType())) {
                    prop.getField().setStringInitializer(option.getDefaultValue());
                } else if ("long".equals(option.getJavaType()) || "java.lang.Long".equals(option.getJavaType())) {
                    // the value should be a Long number
                    String value = option.getDefaultValue() + "L";
                    prop.getField().setLiteralInitializer(value);
                } else if ("integer".equals(option.getType()) || "boolean".equals(option.getType())) {
                    prop.getField().setLiteralInitializer(option.getDefaultValue());
                } else if (!Strings.isBlank(option.getEnums())) {
                    String enumShortName = type.substring(type.lastIndexOf(".") + 1);
                    prop.getField().setLiteralInitializer(enumShortName + "." + option.getDefaultValue());
                    javaClass.addImport(model.getJavaType());
                }
            }
        }
    }
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
}
Also used : StringHelper.getSafeValue(org.apache.camel.maven.connector.util.StringHelper.getSafeValue) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) ComponentModel(org.apache.camel.maven.connector.model.ComponentModel) Parameter(org.apache.maven.plugins.annotations.Parameter) JSonSchemaHelper(org.apache.camel.maven.connector.util.JSonSchemaHelper) ArrayList(java.util.ArrayList) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) Mojo(org.apache.maven.plugins.annotations.Mojo) Roaster(org.jboss.forge.roaster.Roaster) StringHelper.getShortJavaType(org.apache.camel.maven.connector.util.StringHelper.getShortJavaType) Locale(java.util.Locale) Map(java.util.Map) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) PropertySource(org.jboss.forge.roaster.model.source.PropertySource) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ComponentOptionModel(org.apache.camel.maven.connector.model.ComponentOptionModel) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) Formatter(org.jboss.forge.roaster.model.util.Formatter) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Strings(org.jboss.forge.roaster.model.util.Strings) Lazy(org.springframework.context.annotation.Lazy) Bean(org.springframework.context.annotation.Bean) FileHelper.loadText(org.apache.camel.maven.connector.util.FileHelper.loadText) Collections(java.util.Collections) AbstractMojo(org.apache.maven.plugin.AbstractMojo) InputStream(java.io.InputStream) Import(org.jboss.forge.roaster.model.source.Import) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ComponentOptionModel(org.apache.camel.maven.connector.model.ComponentOptionModel)

Example 2 with ComponentModel

use of org.apache.camel.maven.connector.model.ComponentModel in project camel by apache.

the class SpringBootAutoConfigurationMojo method generateComponentModel.

private static ComponentModel generateComponentModel(String json) {
    List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("component", json, false);
    ComponentModel component = new ComponentModel(true);
    component.setScheme(getSafeValue("scheme", rows));
    component.setSyntax(getSafeValue("syntax", rows));
    component.setAlternativeSyntax(getSafeValue("alternativeSyntax", rows));
    component.setTitle(getSafeValue("title", rows));
    component.setDescription(getSafeValue("description", rows));
    component.setFirstVersion(getSafeValue("firstVersion", rows));
    component.setLabel(getSafeValue("label", rows));
    component.setDeprecated(getSafeValue("deprecated", rows));
    component.setConsumerOnly(getSafeValue("consumerOnly", rows));
    component.setProducerOnly(getSafeValue("producerOnly", rows));
    component.setJavaType(getSafeValue("javaType", rows));
    component.setGroupId(getSafeValue("groupId", rows));
    component.setArtifactId(getSafeValue("artifactId", rows));
    component.setVersion(getSafeValue("version", rows));
    rows = JSonSchemaHelper.parseJsonSchema("componentProperties", json, true);
    for (Map<String, String> row : rows) {
        ComponentOptionModel option = new ComponentOptionModel();
        option.setName(getSafeValue("name", row));
        option.setDisplayName(getSafeValue("displayName", row));
        option.setKind(getSafeValue("kind", row));
        option.setType(getSafeValue("type", row));
        option.setJavaType(getSafeValue("javaType", row));
        option.setDeprecated(getSafeValue("deprecated", row));
        option.setDescription(getSafeValue("description", row));
        option.setDefaultValue(getSafeValue("defaultValue", row));
        option.setEnums(getSafeValue("enum", row));
        component.addComponentOption(option);
    }
    return component;
}
Also used : ComponentModel(org.apache.camel.maven.connector.model.ComponentModel) Map(java.util.Map) ComponentOptionModel(org.apache.camel.maven.connector.model.ComponentOptionModel)

Example 3 with ComponentModel

use of org.apache.camel.maven.connector.model.ComponentModel in project camel by apache.

the class SpringBootAutoConfigurationMojo method executeConnector.

@SuppressWarnings("unchecked")
private void executeConnector() throws Exception {
    String javaType = null;
    String connectorScheme = null;
    List<String> componentOptions = null;
    File file = new File(classesDirectory, "camel-connector.json");
    if (file.exists()) {
        ObjectMapper mapper = new ObjectMapper();
        Map dto = mapper.readValue(file, Map.class);
        javaType = (String) dto.get("javaType");
        connectorScheme = (String) dto.get("scheme");
        componentOptions = (List) dto.get("componentOptions");
    }
    // find the component dependency and get its .json file
    file = new File(classesDirectory, "camel-component-schema.json");
    if (file.exists() && javaType != null && connectorScheme != null) {
        String json = loadText(new FileInputStream(file));
        ComponentModel model = generateComponentModel(json);
        // resolvePropertyPlaceholders is an option which only make sense to use if the component has other options
        boolean hasOptions = model.getComponentOptions().stream().anyMatch(o -> !o.getName().equals("resolvePropertyPlaceholders"));
        // use springboot as sub package name so the code is not in normal
        // package so the Spring Boot JARs can be optional at runtime
        int pos = javaType.lastIndexOf(".");
        String pkg = javaType.substring(0, pos) + ".springboot";
        // we only create spring boot auto configuration if there is options to configure
        if (hasOptions) {
            getLog().info("Generating Spring Boot AutoConfiguration for Connector: " + model.getScheme());
            createConnectorConfigurationSource(pkg, model, javaType, connectorScheme, componentOptions);
            createConnectorAutoConfigurationSource(pkg, hasOptions, javaType, connectorScheme);
            createConnectorSpringFactorySource(pkg, javaType);
        }
    }
}
Also used : ComponentModel(org.apache.camel.maven.connector.model.ComponentModel) File(java.io.File) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileInputStream(java.io.FileInputStream)

Aggregations

Map (java.util.Map)3 ComponentModel (org.apache.camel.maven.connector.model.ComponentModel)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 ComponentOptionModel (org.apache.camel.maven.connector.model.ComponentOptionModel)2 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Locale (java.util.Locale)1 FileHelper.loadText (org.apache.camel.maven.connector.util.FileHelper.loadText)1 JSonSchemaHelper (org.apache.camel.maven.connector.util.JSonSchemaHelper)1 StringHelper.getSafeValue (org.apache.camel.maven.connector.util.StringHelper.getSafeValue)1 StringHelper.getShortJavaType (org.apache.camel.maven.connector.util.StringHelper.getShortJavaType)1 FileUtils (org.apache.commons.io.FileUtils)1 AbstractMojo (org.apache.maven.plugin.AbstractMojo)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 LifecyclePhase (org.apache.maven.plugins.annotations.LifecyclePhase)1