Search in sources :

Example 6 with ExpressionDefinition

use of org.apache.camel.model.language.ExpressionDefinition in project camel by apache.

the class ValidatorListCommandTest method doTest.

private String doTest(boolean verbose) throws Exception {
    CamelContext context = new DefaultCamelContext();
    EndpointValidatorDefinition evd = new EndpointValidatorDefinition();
    evd.setType("xml:foo");
    evd.setUri("direct:validator");
    context.getValidators().add(evd);
    PredicateValidatorDefinition pvd = new PredicateValidatorDefinition();
    pvd.setType(this.getClass());
    pvd.setExpression(new ExpressionDefinition(ExpressionBuilder.bodyExpression()));
    context.getValidators().add(pvd);
    CustomValidatorDefinition cvd = new CustomValidatorDefinition();
    cvd.setType("custom");
    cvd.setClassName(MyValidator.class.getName());
    context.getValidators().add(cvd);
    context.setNameStrategy(new ExplicitCamelContextNameStrategy("foobar"));
    context.start();
    CamelController controller = new DummyCamelController(context);
    OutputStream os = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(os);
    ValidatorListCommand command = new ValidatorListCommand(null, false, verbose, false);
    command.execute(controller, ps, null);
    String out = os.toString();
    assertNotNull(out);
    LOG.info("\n\n{}\n", out);
    context.stop();
    return out;
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) PrintStream(java.io.PrintStream) ExplicitCamelContextNameStrategy(org.apache.camel.impl.ExplicitCamelContextNameStrategy) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) PredicateValidatorDefinition(org.apache.camel.model.validator.PredicateValidatorDefinition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) EndpointValidatorDefinition(org.apache.camel.model.validator.EndpointValidatorDefinition) CustomValidatorDefinition(org.apache.camel.model.validator.CustomValidatorDefinition) ExpressionDefinition(org.apache.camel.model.language.ExpressionDefinition)

Example 7 with ExpressionDefinition

use of org.apache.camel.model.language.ExpressionDefinition in project camel by apache.

the class CreateModelFromXmlTest method assertNamespacesPresent.

private void assertNamespacesPresent(RoutesDefinition routesDefinition, Map<String, String> expectedNamespaces) {
    for (RouteDefinition route : routesDefinition.getRoutes()) {
        Iterator<ExpressionNode> it = filterTypeInOutputs(route.getOutputs(), ExpressionNode.class);
        if (it.hasNext()) {
            ExpressionNode en = it.next();
            ExpressionDefinition ed = en.getExpression();
            NamespaceAware na = null;
            Expression exp = ed.getExpressionValue();
            if (exp != null && exp instanceof NamespaceAware) {
                na = (NamespaceAware) exp;
            } else if (ed instanceof NamespaceAware) {
                na = (NamespaceAware) ed;
            }
            assertNotNull(na);
            assertEquals(expectedNamespaces, na.getNamespaces());
        } else {
            fail("Expected to find at least one ExpressionNode in route");
        }
    }
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) Expression(org.apache.camel.Expression) ExpressionNode(org.apache.camel.model.ExpressionNode) NamespaceAware(org.apache.camel.spi.NamespaceAware) ExpressionDefinition(org.apache.camel.model.language.ExpressionDefinition)

Example 8 with ExpressionDefinition

use of org.apache.camel.model.language.ExpressionDefinition in project camel by apache.

the class ProcessorDefinition method createOutputsProcessorImpl.

protected Processor createOutputsProcessorImpl(RouteContext routeContext, Collection<ProcessorDefinition<?>> outputs) throws Exception {
    List<Processor> list = new ArrayList<Processor>();
    for (ProcessorDefinition<?> output : outputs) {
        // allow any custom logic before we create the processor
        output.preCreateProcessor();
        // resolve properties before we create the processor
        ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext.getCamelContext(), output);
        // resolve constant fields (eg Exchange.FILE_NAME)
        ProcessorDefinitionHelper.resolveKnownConstantFields(output);
        // also resolve properties and constant fields on embedded expressions
        ProcessorDefinition<?> me = (ProcessorDefinition<?>) output;
        if (me instanceof ExpressionNode) {
            ExpressionNode exp = (ExpressionNode) me;
            ExpressionDefinition expressionDefinition = exp.getExpression();
            if (expressionDefinition != null) {
                // resolve properties before we create the processor
                ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext.getCamelContext(), expressionDefinition);
                // resolve constant fields (eg Exchange.FILE_NAME)
                ProcessorDefinitionHelper.resolveKnownConstantFields(expressionDefinition);
            }
        }
        Processor processor = createProcessor(routeContext, output);
        // inject id
        if (processor instanceof IdAware) {
            String id = output.idOrCreate(routeContext.getCamelContext().getNodeIdFactory());
            ((IdAware) processor).setId(id);
        }
        if (output instanceof Channel && processor == null) {
            continue;
        }
        Processor channel = wrapChannel(routeContext, processor, output);
        list.add(channel);
    }
    // if more than one output wrap than in a composite processor else just keep it as is
    Processor processor = null;
    if (!list.isEmpty()) {
        if (list.size() == 1) {
            processor = list.get(0);
        } else {
            processor = createCompositeProcessor(routeContext, list);
        }
    }
    return processor;
}
Also used : InterceptEndpointProcessor(org.apache.camel.processor.InterceptEndpointProcessor) Processor(org.apache.camel.Processor) IdAware(org.apache.camel.spi.IdAware) Channel(org.apache.camel.Channel) DefaultChannel(org.apache.camel.processor.interceptor.DefaultChannel) ArrayList(java.util.ArrayList) ExpressionDefinition(org.apache.camel.model.language.ExpressionDefinition)

Example 9 with ExpressionDefinition

use of org.apache.camel.model.language.ExpressionDefinition in project camel by apache.

the class ExpressionNodeHelper method toExpressionDefinition.

/**
     * Determines which {@link ExpressionDefinition} describes the given predicate best possible.
     * <p/>
     * This implementation will use types such as {@link SimpleExpression}, {@link XPathExpression} etc.
     * if the given predicate is detect as such a type.
     *
     * @param predicate the predicate
     * @return a definition which describes the predicate
     */
public static ExpressionDefinition toExpressionDefinition(Predicate predicate) {
    if (predicate instanceof SimpleBuilder) {
        SimpleBuilder builder = (SimpleBuilder) predicate;
        // we keep the original expression by using the constructor that accepts an expression
        SimpleExpression answer = new SimpleExpression(builder);
        answer.setExpression(builder.getText());
        return answer;
    } else if (predicate instanceof XPathBuilder) {
        XPathBuilder builder = (XPathBuilder) predicate;
        // we keep the original expression by using the constructor that accepts an expression
        XPathExpression answer = new XPathExpression(builder);
        answer.setExpression(builder.getText());
        return answer;
    } else if (predicate instanceof ValueBuilder) {
        // ValueBuilder wraps the actual predicate so unwrap
        ValueBuilder builder = (ValueBuilder) predicate;
        Expression expression = builder.getExpression();
        if (expression instanceof Predicate) {
            predicate = (Predicate) expression;
        }
    }
    if (predicate instanceof ExpressionDefinition) {
        return (ExpressionDefinition) predicate;
    }
    return new ExpressionDefinition(predicate);
}
Also used : XPathExpression(org.apache.camel.model.language.XPathExpression) ValueBuilder(org.apache.camel.builder.ValueBuilder) SimpleBuilder(org.apache.camel.builder.SimpleBuilder) XPathExpression(org.apache.camel.model.language.XPathExpression) SimpleExpression(org.apache.camel.model.language.SimpleExpression) Expression(org.apache.camel.Expression) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) ExpressionDefinition(org.apache.camel.model.language.ExpressionDefinition) SimpleExpression(org.apache.camel.model.language.SimpleExpression) Predicate(org.apache.camel.Predicate)

Example 10 with ExpressionDefinition

use of org.apache.camel.model.language.ExpressionDefinition in project camel by apache.

the class ModelHelper method getNamespaceAwareFromExpression.

private static NamespaceAware getNamespaceAwareFromExpression(ExpressionNode expressionNode) {
    ExpressionDefinition ed = expressionNode.getExpression();
    NamespaceAware na = null;
    Expression exp = ed.getExpressionValue();
    if (exp instanceof NamespaceAware) {
        na = (NamespaceAware) exp;
    } else if (ed instanceof NamespaceAware) {
        na = (NamespaceAware) ed;
    }
    return na;
}
Also used : Expression(org.apache.camel.Expression) NamespaceAware(org.apache.camel.spi.NamespaceAware) ExpressionDefinition(org.apache.camel.model.language.ExpressionDefinition)

Aggregations

ExpressionDefinition (org.apache.camel.model.language.ExpressionDefinition)11 Expression (org.apache.camel.Expression)3 Processor (org.apache.camel.Processor)2 SimpleBuilder (org.apache.camel.builder.SimpleBuilder)2 ValueBuilder (org.apache.camel.builder.ValueBuilder)2 XPathBuilder (org.apache.camel.builder.xml.XPathBuilder)2 SimpleExpression (org.apache.camel.model.language.SimpleExpression)2 XPathExpression (org.apache.camel.model.language.XPathExpression)2 InterceptEndpointProcessor (org.apache.camel.processor.InterceptEndpointProcessor)2 IdAware (org.apache.camel.spi.IdAware)2 NamespaceAware (org.apache.camel.spi.NamespaceAware)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)1 XmlAccessorType (javax.xml.bind.annotation.XmlAccessorType)1 CamelContext (org.apache.camel.CamelContext)1 Channel (org.apache.camel.Channel)1 Predicate (org.apache.camel.Predicate)1