Search in sources :

Example 1 with CamelEndpointDetails

use of org.apache.camel.parser.model.CamelEndpointDetails in project camel by apache.

the class XmlRouteParser method parseXmlRouteEndpoints.

/**
     * Parses the XML source to discover Camel endpoints.
     *
     * @param xml                     the xml file as input stream
     * @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
     */
public static void parseXmlRouteEndpoints(InputStream xml, String baseDir, String fullyQualifiedFileName, List<CamelEndpointDetails> endpoints) throws Exception {
    // find all the endpoints (currently only <endpoint> and within <route>)
    // try parse it as dom
    Document dom = null;
    try {
        dom = XmlLineNumberParser.parseXml(xml);
    } catch (Exception e) {
    // ignore as the xml file may not be valid at this point
    }
    if (dom != null) {
        List<Node> nodes = CamelXmlHelper.findAllEndpoints(dom);
        for (Node node : nodes) {
            String uri = getSafeAttribute(node, "uri");
            if (uri != null) {
                // trim and remove whitespace noise
                uri = trimEndpointUri(uri);
            }
            if (!Strings.isBlank(uri)) {
                String id = getSafeAttribute(node, "id");
                String lineNumber = (String) node.getUserData(XmlLineNumberParser.LINE_NUMBER);
                String lineNumberEnd = (String) node.getUserData(XmlLineNumberParser.LINE_NUMBER_END);
                // we only want the relative dir name from the resource directory, eg META-INF/spring/foo.xml
                String fileName = fullyQualifiedFileName;
                if (fileName.startsWith(baseDir)) {
                    fileName = fileName.substring(baseDir.length() + 1);
                }
                boolean consumerOnly = false;
                boolean producerOnly = false;
                String nodeName = node.getNodeName();
                if ("from".equals(nodeName) || "pollEnrich".equals(nodeName)) {
                    consumerOnly = true;
                } else if ("to".equals(nodeName) || "enrich".equals(nodeName) || "wireTap".equals(nodeName)) {
                    producerOnly = true;
                }
                CamelEndpointDetails detail = new CamelEndpointDetails();
                detail.setFileName(fileName);
                detail.setLineNumber(lineNumber);
                detail.setLineNumberEnd(lineNumberEnd);
                detail.setEndpointInstance(id);
                detail.setEndpointUri(uri);
                detail.setEndpointComponentName(endpointComponentName(uri));
                detail.setConsumerOnly(consumerOnly);
                detail.setProducerOnly(producerOnly);
                endpoints.add(detail);
            }
        }
    }
}
Also used : CamelEndpointDetails(org.apache.camel.parser.model.CamelEndpointDetails) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Example 2 with CamelEndpointDetails

use of org.apache.camel.parser.model.CamelEndpointDetails 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 3 with CamelEndpointDetails

use of org.apache.camel.parser.model.CamelEndpointDetails in project camel by apache.

the class RoasterSimpleProcessorTest method parse.

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

Example 4 with CamelEndpointDetails

use of org.apache.camel.parser.model.CamelEndpointDetails in project camel by apache.

the class RoasterSimpleToFTest method parse.

@Test
public void parse() throws Exception {
    JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MySimpleToFRoute.java"));
    MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
    List<CamelEndpointDetails> details = new ArrayList<>();
    RouteBuilderParser.parseRouteBuilderEndpoints(clazz, ".", "src/test/java/org/apache/camel/parser/java/MySimpleToFRoute.java", details);
    LOG.info("{}", details);
    List<ParserResult> list = CamelJavaParserHelper.parseCamelConsumerUris(method, true, true);
    for (ParserResult result : list) {
        LOG.info("Consumer: " + result.getElement());
    }
    Assert.assertEquals("direct:start", list.get(0).getElement());
    list = CamelJavaParserHelper.parseCamelProducerUris(method, true, true);
    for (ParserResult result : list) {
        LOG.info("Producer: " + result.getElement());
    }
    Assert.assertEquals("toF", list.get(0).getNode());
    Assert.assertEquals("log:a?level={{%s}}", list.get(0).getElement());
    Assert.assertEquals(1, list.size());
    Assert.assertEquals(2, details.size());
    Assert.assertEquals("direct:start", details.get(0).getEndpointUri());
    Assert.assertEquals("log:a?level={{%s}}", details.get(1).getEndpointUri());
}
Also used : CamelEndpointDetails(org.apache.camel.parser.model.CamelEndpointDetails) ParserResult(org.apache.camel.parser.ParserResult) ArrayList(java.util.ArrayList) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) File(java.io.File) Test(org.junit.Test)

Example 5 with CamelEndpointDetails

use of org.apache.camel.parser.model.CamelEndpointDetails in project camel by apache.

the class RoasterSplitTokenizeTest method parse.

@Test
public void parse() throws Exception {
    JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/SplitTokenizeTest.java"));
    MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
    List<CamelEndpointDetails> details = new ArrayList<>();
    RouteBuilderParser.parseRouteBuilderEndpoints(clazz, "src/test/java", "org/apache/camel/parser/SplitTokenizeTest.java", details);
    LOG.info("{}", details);
    List<ParserResult> list = CamelJavaParserHelper.parseCamelConsumerUris(method, true, true);
    for (ParserResult result : list) {
        LOG.info("Consumer: " + result.getElement());
    }
    Assert.assertEquals("direct:a", list.get(0).getElement());
    Assert.assertEquals("direct:b", list.get(1).getElement());
    Assert.assertEquals("direct:c", list.get(2).getElement());
    Assert.assertEquals("direct:d", list.get(3).getElement());
    Assert.assertEquals("direct:e", list.get(4).getElement());
    Assert.assertEquals("direct:f", list.get(5).getElement());
    list = CamelJavaParserHelper.parseCamelProducerUris(method, true, true);
    for (ParserResult result : list) {
        LOG.info("Producer: " + result.getElement());
    }
    Assert.assertEquals("mock:split", list.get(0).getElement());
    Assert.assertEquals("mock:split", list.get(1).getElement());
    Assert.assertEquals("mock:split", list.get(2).getElement());
    Assert.assertEquals("mock:split", list.get(3).getElement());
    Assert.assertEquals("mock:split", list.get(4).getElement());
    Assert.assertEquals("mock:split", list.get(5).getElement());
    Assert.assertEquals(12, details.size());
    Assert.assertEquals("direct:a", details.get(0).getEndpointUri());
    Assert.assertEquals("mock:split", details.get(11).getEndpointUri());
}
Also used : CamelEndpointDetails(org.apache.camel.parser.model.CamelEndpointDetails) ParserResult(org.apache.camel.parser.ParserResult) ArrayList(java.util.ArrayList) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) File(java.io.File) Test(org.junit.Test)

Aggregations

CamelEndpointDetails (org.apache.camel.parser.model.CamelEndpointDetails)10 ArrayList (java.util.ArrayList)9 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)7 Test (org.junit.Test)7 File (java.io.File)6 ParserResult (org.apache.camel.parser.ParserResult)5 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 List (java.util.List)2 LinkedHashSet (java.util.LinkedHashSet)1 CamelCatalog (org.apache.camel.catalog.CamelCatalog)1 DefaultCamelCatalog (org.apache.camel.catalog.DefaultCamelCatalog)1 EndpointValidationResult (org.apache.camel.catalog.EndpointValidationResult)1 SimpleValidationResult (org.apache.camel.catalog.SimpleValidationResult)1 LuceneSuggestionStrategy (org.apache.camel.catalog.lucene.LuceneSuggestionStrategy)1 MavenVersionManager (org.apache.camel.catalog.maven.MavenVersionManager)1 CamelSimpleExpressionDetails (org.apache.camel.parser.model.CamelSimpleExpressionDetails)1 Resource (org.apache.maven.model.Resource)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1