Search in sources :

Example 1 with MethodSource

use of org.jboss.forge.roaster.model.source.MethodSource in project camel by apache.

the class RouteBuilderParser method parseRouteBuilderEndpoints.

/**
     * Parses the java source class to discover Camel endpoints.
     *
     * @param clazz                        the java source class
     * @param baseDir                      the base of the source code
     * @param fullyQualifiedFileName       the fully qualified source code file name
     * @param endpoints                    list to add discovered and parsed endpoints
     * @param unparsable                   list of unparsable nodes
     * @param includeInlinedRouteBuilders  whether to include inlined route builders in the parsing
     */
public static void parseRouteBuilderEndpoints(JavaClassSource clazz, String baseDir, String fullyQualifiedFileName, List<CamelEndpointDetails> endpoints, List<String> unparsable, boolean includeInlinedRouteBuilders) {
    // look for fields which are not used in the route
    for (FieldSource<JavaClassSource> field : clazz.getFields()) {
        // is the field annotated with a Camel endpoint
        String uri = null;
        Expression exp = null;
        for (Annotation ann : field.getAnnotations()) {
            boolean valid = "org.apache.camel.EndpointInject".equals(ann.getQualifiedName()) || "org.apache.camel.cdi.Uri".equals(ann.getQualifiedName());
            if (valid) {
                exp = (Expression) ann.getInternal();
                if (exp instanceof SingleMemberAnnotation) {
                    exp = ((SingleMemberAnnotation) exp).getValue();
                } else if (exp instanceof NormalAnnotation) {
                    List values = ((NormalAnnotation) exp).values();
                    for (Object value : values) {
                        MemberValuePair pair = (MemberValuePair) value;
                        if ("uri".equals(pair.getName().toString())) {
                            exp = pair.getValue();
                            break;
                        }
                    }
                }
                uri = CamelJavaParserHelper.getLiteralValue(clazz, null, exp);
            }
        }
        // we only want to add fields which are not used in the route
        if (!Strings.isBlank(uri) && findEndpointByUri(endpoints, uri) == null) {
            // we only want the relative dir name from the
            String fileName = fullyQualifiedFileName;
            if (fileName.startsWith(baseDir)) {
                fileName = fileName.substring(baseDir.length() + 1);
            }
            String id = field.getName();
            CamelEndpointDetails detail = new CamelEndpointDetails();
            detail.setFileName(fileName);
            detail.setClassName(clazz.getQualifiedName());
            detail.setEndpointInstance(id);
            detail.setEndpointUri(uri);
            detail.setEndpointComponentName(endpointComponentName(uri));
            // favor the position of the expression which had the actual uri
            Object internal = exp != null ? exp : field.getInternal();
            // find position of field/expression
            if (internal instanceof ASTNode) {
                int pos = ((ASTNode) internal).getStartPosition();
                int line = findLineNumber(fullyQualifiedFileName, pos);
                if (line > -1) {
                    detail.setLineNumber("" + line);
                }
            }
            // we do not know if this field is used as consumer or producer only, but we try
            // to find out by scanning the route in the configure method below
            endpoints.add(detail);
        }
    }
    // find all the configure methods
    List<MethodSource<JavaClassSource>> methods = new ArrayList<>();
    MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
    if (method != null) {
        methods.add(method);
    }
    if (includeInlinedRouteBuilders) {
        List<MethodSource<JavaClassSource>> inlinedMethods = CamelJavaParserHelper.findInlinedConfigureMethods(clazz);
        if (!inlinedMethods.isEmpty()) {
            methods.addAll(inlinedMethods);
        }
    }
    // determine this to ensure when we edit the endpoint we should only the options accordingly
    for (MethodSource<JavaClassSource> configureMethod : methods) {
        // consumers only
        List<ParserResult> uris = CamelJavaParserHelper.parseCamelConsumerUris(configureMethod, true, true);
        for (ParserResult result : uris) {
            if (!result.isParsed()) {
                if (unparsable != null) {
                    unparsable.add(result.getElement());
                }
            } else {
                CamelEndpointDetails detail = findEndpointByUri(endpoints, result.getElement());
                if (detail != null) {
                    // its a consumer only
                    detail.setConsumerOnly(true);
                } else {
                    String fileName = fullyQualifiedFileName;
                    if (fileName.startsWith(baseDir)) {
                        fileName = fileName.substring(baseDir.length() + 1);
                    }
                    detail = new CamelEndpointDetails();
                    detail.setFileName(fileName);
                    detail.setClassName(clazz.getQualifiedName());
                    detail.setMethodName(configureMethod.getName());
                    detail.setEndpointInstance(null);
                    detail.setEndpointUri(result.getElement());
                    int line = findLineNumber(fullyQualifiedFileName, result.getPosition());
                    if (line > -1) {
                        detail.setLineNumber("" + line);
                    }
                    detail.setEndpointComponentName(endpointComponentName(result.getElement()));
                    detail.setConsumerOnly(true);
                    detail.setProducerOnly(false);
                    endpoints.add(detail);
                }
            }
        }
        // producer only
        uris = CamelJavaParserHelper.parseCamelProducerUris(configureMethod, true, true);
        for (ParserResult result : uris) {
            if (!result.isParsed()) {
                if (unparsable != null) {
                    unparsable.add(result.getElement());
                }
            } else {
                CamelEndpointDetails detail = findEndpointByUri(endpoints, result.getElement());
                if (detail != null) {
                    if (detail.isConsumerOnly()) {
                        // its both a consumer and producer
                        detail.setConsumerOnly(false);
                        detail.setProducerOnly(false);
                    } else {
                        // its a producer only
                        detail.setProducerOnly(true);
                    }
                }
                // the same endpoint uri may be used in multiple places in the same route
                // so we should maybe add all of them
                String fileName = fullyQualifiedFileName;
                if (fileName.startsWith(baseDir)) {
                    fileName = fileName.substring(baseDir.length() + 1);
                }
                detail = new CamelEndpointDetails();
                detail.setFileName(fileName);
                detail.setClassName(clazz.getQualifiedName());
                detail.setMethodName(configureMethod.getName());
                detail.setEndpointInstance(null);
                detail.setEndpointUri(result.getElement());
                int line = findLineNumber(fullyQualifiedFileName, result.getPosition());
                if (line > -1) {
                    detail.setLineNumber("" + line);
                }
                detail.setEndpointComponentName(endpointComponentName(result.getElement()));
                detail.setConsumerOnly(false);
                detail.setProducerOnly(true);
                endpoints.add(detail);
            }
        }
    }
}
Also used : CamelEndpointDetails(org.apache.camel.parser.model.CamelEndpointDetails) SingleMemberAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation) ArrayList(java.util.ArrayList) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) SingleMemberAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation) Annotation(org.jboss.forge.roaster.model.Annotation) NormalAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation) MemberValuePair(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MemberValuePair) Expression(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression) ASTNode(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) NormalAnnotation(org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with MethodSource

use of org.jboss.forge.roaster.model.source.MethodSource in project camel by apache.

the class SpringBootAutoConfigurationMojo method createDataFormatAutoConfigurationSource.

private void createDataFormatAutoConfigurationSource(String packageName, DataFormatModel model, List<String> dataFormatAliases, boolean hasOptions, String overrideDataFormatName) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("DataFormat", "DataFormatAutoConfiguration");
    javaClass.setPackage(packageName).setName(name);
    String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
    javaClass.getJavaDoc().setFullText(doc);
    javaClass.addAnnotation(Configuration.class);
    javaClass.addAnnotation(ConditionalOnBean.class).setStringValue("type", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addAnnotation(Conditional.class).setLiteralValue(name + ".Condition.class");
    javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    String configurationName = name.replace("DataFormatAutoConfiguration", "DataFormatConfiguration");
    if (hasOptions) {
        AnnotationSource<JavaClassSource> ann = javaClass.addAnnotation(EnableConfigurationProperties.class);
        ann.setLiteralValue("value", configurationName + ".class");
        javaClass.addImport("java.util.HashMap");
        javaClass.addImport("java.util.Map");
        javaClass.addImport("org.apache.camel.util.IntrospectionSupport");
    }
    javaClass.addImport("org.apache.camel.CamelContextAware");
    javaClass.addImport(model.getJavaType());
    javaClass.addImport("org.apache.camel.CamelContext");
    javaClass.addImport("org.apache.camel.RuntimeCamelException");
    javaClass.addImport("org.apache.camel.spi.DataFormat");
    javaClass.addImport("org.apache.camel.spi.DataFormatFactory");
    String body = createDataFormatBody(model.getShortJavaType(), hasOptions);
    String methodName = "configure" + model.getShortJavaType() + "Factory";
    MethodSource<JavaClassSource> method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType("org.apache.camel.spi.DataFormatFactory");
    method.addParameter("CamelContext", "camelContext").setFinal(true);
    if (hasOptions) {
        method.addParameter(configurationName, "configuration").setFinal(true);
    }
    // Determine all the aliases
    // adding the '-dataformat' suffix to prevent collision with component names
    String[] springBeanAliases = dataFormatAliases.stream().map(alias -> alias + "-dataformat-factory").toArray(size -> new String[size]);
    method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
    method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");
    // Generate Condition
    javaClass.addNestedType(createConditionType(javaClass, "camel.dataformat", (overrideDataFormatName != null ? overrideDataFormatName : model.getName()).toLowerCase(Locale.US)));
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
    writeAdditionalSpringMetaData("camel", "dataformat", (overrideDataFormatName != null ? overrideDataFormatName : model.getName()).toLowerCase(Locale.US));
}
Also used : NestedConfigurationProperty(org.springframework.boot.context.properties.NestedConfigurationProperty) Arrays(java.util.Arrays) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) URL(java.net.URL) Type(org.jboss.forge.roaster.model.Type) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage) GsonBuilder(com.google.gson.GsonBuilder) UriParam(org.apache.camel.spi.UriParam) ConditionContext(org.springframework.context.annotation.ConditionContext) URLClassLoader(java.net.URLClassLoader) Matcher(java.util.regex.Matcher) Roaster(org.jboss.forge.roaster.Roaster) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) Gson(com.google.gson.Gson) Map(java.util.Map) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Configuration(org.springframework.context.annotation.Configuration) JSonSchemaHelper.getPropertyJavaType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyJavaType) List(java.util.List) RelaxedPropertyResolver(org.springframework.boot.bind.RelaxedPropertyResolver) Resource(org.apache.maven.model.Resource) Formatter(org.jboss.forge.roaster.model.util.Formatter) DataFormatOptionModel(org.apache.camel.maven.packaging.model.DataFormatOptionModel) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Modifier(java.lang.reflect.Modifier) Strings(org.jboss.forge.roaster.model.util.Strings) Lazy(org.springframework.context.annotation.Lazy) JSonSchemaHelper.getSafeValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getSafeValue) Pattern(java.util.regex.Pattern) Conditional(org.springframework.context.annotation.Conditional) AbstractMojo(org.apache.maven.plugin.AbstractMojo) Import(org.jboss.forge.roaster.model.source.Import) LanguageOptionModel(org.apache.camel.maven.packaging.model.LanguageOptionModel) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) UriPath(org.apache.camel.spi.UriPath) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) Scope(org.springframework.context.annotation.Scope) ArrayList(java.util.ArrayList) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) HashSet(java.util.HashSet) JavaType(org.jboss.forge.roaster.model.JavaType) ComponentOptionModel(org.apache.camel.maven.packaging.model.ComponentOptionModel) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) EndpointOptionModel(org.apache.camel.maven.packaging.model.EndpointOptionModel) LinkedList(java.util.LinkedList) PropertySource(org.jboss.forge.roaster.model.source.PropertySource) JSonSchemaHelper.getPropertyDefaultValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyDefaultValue) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty) IOException(java.io.IOException) 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) SpringBootCondition(org.springframework.boot.autoconfigure.condition.SpringBootCondition) ConditionOutcome(org.springframework.boot.autoconfigure.condition.ConditionOutcome) PackageHelper.loadText(org.apache.camel.maven.packaging.PackageHelper.loadText) JSonSchemaHelper.getPropertyType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyType) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Bean(org.springframework.context.annotation.Bean) AnnotatedTypeMetadata(org.springframework.core.type.AnnotatedTypeMetadata) Collections(java.util.Collections) DataFormatModel(org.apache.camel.maven.packaging.model.DataFormatModel) InputStream(java.io.InputStream) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Conditional(org.springframework.context.annotation.Conditional) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with MethodSource

use of org.jboss.forge.roaster.model.source.MethodSource in project camel by apache.

the class RoasterMyLocalAddRouteBuilderTest method parse.

@Test
public void parse() throws Exception {
    JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyLocalAddRouteBuilderTest.java"));
    MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
    Assert.assertNull(method);
    List<MethodSource<JavaClassSource>> methods = CamelJavaParserHelper.findInlinedConfigureMethods(clazz);
    Assert.assertEquals(1, methods.size());
    method = methods.get(0);
    List<ParserResult> list = CamelJavaParserHelper.parseCamelConsumerUris(method, true, true);
    for (ParserResult result : list) {
        LOG.info("Consumer: " + result.getElement());
    }
    list = CamelJavaParserHelper.parseCamelProducerUris(method, true, true);
    for (ParserResult result : list) {
        LOG.info("Producer: " + result.getElement());
    }
}
Also used : ParserResult(org.apache.camel.parser.ParserResult) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) File(java.io.File) Test(org.junit.Test)

Example 4 with MethodSource

use of org.jboss.forge.roaster.model.source.MethodSource in project camel by apache.

the class SpringBootAutoConfigurationMojo method createLanguageAutoConfigurationSource.

private void createLanguageAutoConfigurationSource(String packageName, LanguageModel model, List<String> languageAliases, boolean hasOptions, String overrideLanguageName) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("Language", "LanguageAutoConfiguration");
    javaClass.setPackage(packageName).setName(name);
    String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
    javaClass.getJavaDoc().setFullText(doc);
    javaClass.addAnnotation(Configuration.class);
    javaClass.addAnnotation(ConditionalOnBean.class).setStringValue("type", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addAnnotation(Conditional.class).setLiteralValue(name + ".Condition.class");
    javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    String configurationName = name.replace("LanguageAutoConfiguration", "LanguageConfiguration");
    if (hasOptions) {
        AnnotationSource<JavaClassSource> ann = javaClass.addAnnotation(EnableConfigurationProperties.class);
        ann.setLiteralValue("value", configurationName + ".class");
        javaClass.addImport("java.util.HashMap");
        javaClass.addImport("java.util.Map");
        javaClass.addImport("org.apache.camel.util.IntrospectionSupport");
    }
    javaClass.addImport("org.apache.camel.CamelContextAware");
    javaClass.addImport(model.getJavaType());
    javaClass.addImport("org.apache.camel.CamelContext");
    String body = createLanguageBody(model.getShortJavaType(), hasOptions);
    String methodName = "configure" + model.getShortJavaType();
    MethodSource<JavaClassSource> method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType(model.getShortJavaType()).addThrows(Exception.class);
    method.addParameter("CamelContext", "camelContext");
    method.addParameter(configurationName, "configuration");
    // Determine all the aliases
    // adding the '-language' suffix to prevent collision with component names
    String[] springBeanAliases = languageAliases.stream().map(alias -> alias + "-language").toArray(size -> new String[size]);
    method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
    method.addAnnotation(Scope.class).setStringValue("prototype");
    method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");
    // Generate Condition
    javaClass.addNestedType(createConditionType(javaClass, "camel.language", (overrideLanguageName != null ? overrideLanguageName : model.getName()).toLowerCase(Locale.US)));
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
    writeAdditionalSpringMetaData("camel", "language", (overrideLanguageName != null ? overrideLanguageName : model.getName()).toLowerCase(Locale.US));
}
Also used : NestedConfigurationProperty(org.springframework.boot.context.properties.NestedConfigurationProperty) Arrays(java.util.Arrays) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) URL(java.net.URL) Type(org.jboss.forge.roaster.model.Type) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage) GsonBuilder(com.google.gson.GsonBuilder) UriParam(org.apache.camel.spi.UriParam) ConditionContext(org.springframework.context.annotation.ConditionContext) URLClassLoader(java.net.URLClassLoader) Matcher(java.util.regex.Matcher) Roaster(org.jboss.forge.roaster.Roaster) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) Gson(com.google.gson.Gson) Map(java.util.Map) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Configuration(org.springframework.context.annotation.Configuration) JSonSchemaHelper.getPropertyJavaType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyJavaType) List(java.util.List) RelaxedPropertyResolver(org.springframework.boot.bind.RelaxedPropertyResolver) Resource(org.apache.maven.model.Resource) Formatter(org.jboss.forge.roaster.model.util.Formatter) DataFormatOptionModel(org.apache.camel.maven.packaging.model.DataFormatOptionModel) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Modifier(java.lang.reflect.Modifier) Strings(org.jboss.forge.roaster.model.util.Strings) Lazy(org.springframework.context.annotation.Lazy) JSonSchemaHelper.getSafeValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getSafeValue) Pattern(java.util.regex.Pattern) Conditional(org.springframework.context.annotation.Conditional) AbstractMojo(org.apache.maven.plugin.AbstractMojo) Import(org.jboss.forge.roaster.model.source.Import) LanguageOptionModel(org.apache.camel.maven.packaging.model.LanguageOptionModel) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) UriPath(org.apache.camel.spi.UriPath) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) Scope(org.springframework.context.annotation.Scope) ArrayList(java.util.ArrayList) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) HashSet(java.util.HashSet) JavaType(org.jboss.forge.roaster.model.JavaType) ComponentOptionModel(org.apache.camel.maven.packaging.model.ComponentOptionModel) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) EndpointOptionModel(org.apache.camel.maven.packaging.model.EndpointOptionModel) LinkedList(java.util.LinkedList) PropertySource(org.jboss.forge.roaster.model.source.PropertySource) JSonSchemaHelper.getPropertyDefaultValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyDefaultValue) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty) IOException(java.io.IOException) 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) SpringBootCondition(org.springframework.boot.autoconfigure.condition.SpringBootCondition) ConditionOutcome(org.springframework.boot.autoconfigure.condition.ConditionOutcome) PackageHelper.loadText(org.apache.camel.maven.packaging.PackageHelper.loadText) JSonSchemaHelper.getPropertyType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyType) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Bean(org.springframework.context.annotation.Bean) AnnotatedTypeMetadata(org.springframework.core.type.AnnotatedTypeMetadata) Collections(java.util.Collections) DataFormatModel(org.apache.camel.maven.packaging.model.DataFormatModel) InputStream(java.io.InputStream) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Conditional(org.springframework.context.annotation.Conditional) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Bean(org.springframework.context.annotation.Bean) Scope(org.springframework.context.annotation.Scope)

Example 5 with MethodSource

use of org.jboss.forge.roaster.model.source.MethodSource in project camel by apache.

the class SpringBootAutoConfigurationMojo method createComponentAutoConfigurationSource.

private void createComponentAutoConfigurationSource(String packageName, ComponentModel model, List<String> componentAliases, boolean hasOptions, String overrideComponentName) throws MojoFailureException {
    final JavaClassSource javaClass = Roaster.create(JavaClassSource.class);
    int pos = model.getJavaType().lastIndexOf(".");
    String name = model.getJavaType().substring(pos + 1);
    name = name.replace("Component", "ComponentAutoConfiguration");
    javaClass.setPackage(packageName).setName(name);
    String doc = "Generated by camel-package-maven-plugin - do not edit this file!";
    javaClass.getJavaDoc().setFullText(doc);
    javaClass.addAnnotation(Configuration.class);
    javaClass.addAnnotation(ConditionalOnBean.class).setStringValue("type", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    javaClass.addAnnotation(Conditional.class).setLiteralValue(name + ".Condition.class");
    javaClass.addAnnotation(AutoConfigureAfter.class).setStringValue("name", "org.apache.camel.spring.boot.CamelAutoConfiguration");
    String configurationName = name.replace("ComponentAutoConfiguration", "ComponentConfiguration");
    if (hasOptions) {
        AnnotationSource<JavaClassSource> ann = javaClass.addAnnotation(EnableConfigurationProperties.class);
        ann.setLiteralValue("value", configurationName + ".class");
        javaClass.addImport("java.util.HashMap");
        javaClass.addImport("java.util.Map");
        javaClass.addImport("org.apache.camel.util.IntrospectionSupport");
    }
    javaClass.addImport(model.getJavaType());
    javaClass.addImport("org.apache.camel.CamelContext");
    // add method for auto configure
    String body = createComponentBody(model.getShortJavaType(), hasOptions);
    String methodName = "configure" + model.getShortJavaType();
    MethodSource<JavaClassSource> method = javaClass.addMethod().setName(methodName).setPublic().setBody(body).setReturnType(model.getShortJavaType()).addThrows(Exception.class);
    method.addParameter("CamelContext", "camelContext");
    if (hasOptions) {
        method.addParameter(configurationName, "configuration");
    }
    // Determine all the aliases
    String[] springBeanAliases = componentAliases.stream().map(alias -> alias + "-component").toArray(size -> new String[size]);
    method.addAnnotation(Lazy.class);
    method.addAnnotation(Bean.class).setStringArrayValue("name", springBeanAliases);
    method.addAnnotation(ConditionalOnClass.class).setLiteralValue("value", "CamelContext.class");
    method.addAnnotation(ConditionalOnMissingBean.class).setLiteralValue("value", model.getShortJavaType() + ".class");
    // Generate Condition
    javaClass.addNestedType(createConditionType(javaClass, "camel.component", (overrideComponentName != null ? overrideComponentName : model.getScheme()).toLowerCase(Locale.US)));
    sortImports(javaClass);
    String fileName = packageName.replaceAll("\\.", "\\/") + "/" + name + ".java";
    writeSourceIfChanged(javaClass, fileName);
    writeAdditionalSpringMetaData("camel", "component", (overrideComponentName != null ? overrideComponentName : model.getScheme()).toLowerCase(Locale.US));
}
Also used : NestedConfigurationProperty(org.springframework.boot.context.properties.NestedConfigurationProperty) Arrays(java.util.Arrays) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) URL(java.net.URL) Type(org.jboss.forge.roaster.model.Type) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage) GsonBuilder(com.google.gson.GsonBuilder) UriParam(org.apache.camel.spi.UriParam) ConditionContext(org.springframework.context.annotation.ConditionContext) URLClassLoader(java.net.URLClassLoader) Matcher(java.util.regex.Matcher) Roaster(org.jboss.forge.roaster.Roaster) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) Gson(com.google.gson.Gson) Map(java.util.Map) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Configuration(org.springframework.context.annotation.Configuration) JSonSchemaHelper.getPropertyJavaType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyJavaType) List(java.util.List) RelaxedPropertyResolver(org.springframework.boot.bind.RelaxedPropertyResolver) Resource(org.apache.maven.model.Resource) Formatter(org.jboss.forge.roaster.model.util.Formatter) DataFormatOptionModel(org.apache.camel.maven.packaging.model.DataFormatOptionModel) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Modifier(java.lang.reflect.Modifier) Strings(org.jboss.forge.roaster.model.util.Strings) Lazy(org.springframework.context.annotation.Lazy) JSonSchemaHelper.getSafeValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getSafeValue) Pattern(java.util.regex.Pattern) Conditional(org.springframework.context.annotation.Conditional) AbstractMojo(org.apache.maven.plugin.AbstractMojo) Import(org.jboss.forge.roaster.model.source.Import) LanguageOptionModel(org.apache.camel.maven.packaging.model.LanguageOptionModel) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) UriPath(org.apache.camel.spi.UriPath) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) LanguageModel(org.apache.camel.maven.packaging.model.LanguageModel) Scope(org.springframework.context.annotation.Scope) ArrayList(java.util.ArrayList) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) HashSet(java.util.HashSet) JavaType(org.jboss.forge.roaster.model.JavaType) ComponentOptionModel(org.apache.camel.maven.packaging.model.ComponentOptionModel) ComponentModel(org.apache.camel.maven.packaging.model.ComponentModel) EndpointOptionModel(org.apache.camel.maven.packaging.model.EndpointOptionModel) LinkedList(java.util.LinkedList) PropertySource(org.jboss.forge.roaster.model.source.PropertySource) JSonSchemaHelper.getPropertyDefaultValue(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyDefaultValue) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) DeprecatedConfigurationProperty(org.springframework.boot.context.properties.DeprecatedConfigurationProperty) IOException(java.io.IOException) 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) SpringBootCondition(org.springframework.boot.autoconfigure.condition.SpringBootCondition) ConditionOutcome(org.springframework.boot.autoconfigure.condition.ConditionOutcome) PackageHelper.loadText(org.apache.camel.maven.packaging.PackageHelper.loadText) JSonSchemaHelper.getPropertyType(org.apache.camel.maven.packaging.JSonSchemaHelper.getPropertyType) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Bean(org.springframework.context.annotation.Bean) AnnotatedTypeMetadata(org.springframework.core.type.AnnotatedTypeMetadata) Collections(java.util.Collections) DataFormatModel(org.apache.camel.maven.packaging.model.DataFormatModel) InputStream(java.io.InputStream) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) AutoConfigureAfter(org.springframework.boot.autoconfigure.AutoConfigureAfter) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Conditional(org.springframework.context.annotation.Conditional) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)5 MethodSource (org.jboss.forge.roaster.model.source.MethodSource)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Gson (com.google.gson.Gson)3 GsonBuilder (com.google.gson.GsonBuilder)3 BufferedReader (java.io.BufferedReader)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Modifier (java.lang.reflect.Modifier)3 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)3 URLClassLoader (java.net.URLClassLoader)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 HashMap (java.util.HashMap)3