Search in sources :

Example 16 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class TokenizeLanguage method createExpression.

/**
     * Creates a tokenize expression.
     */
public Expression createExpression() {
    ObjectHelper.notNull(token, "token");
    // validate some invalid combinations
    if (endToken != null && inheritNamespaceTagName != null) {
        throw new IllegalArgumentException("Cannot have both xml and pair tokenizer enabled.");
    }
    if (isXml() && (endToken != null || includeTokens)) {
        throw new IllegalArgumentException("Cannot have both xml and pair tokenizer enabled.");
    }
    Expression answer = null;
    if (isXml()) {
        answer = ExpressionBuilder.tokenizeXMLExpression(token, inheritNamespaceTagName);
    } else if (endToken != null) {
        answer = ExpressionBuilder.tokenizePairExpression(token, endToken, includeTokens);
    }
    if (answer == null) {
        // use the regular tokenizer
        Expression exp = headerName == null ? ExpressionBuilder.bodyExpression() : ExpressionBuilder.headerExpression(headerName);
        if (regex) {
            answer = ExpressionBuilder.regexTokenizeExpression(exp, token);
        } else {
            answer = ExpressionBuilder.tokenizeExpression(exp, token);
        }
        if (group == 0 && skipFirst) {
            // wrap in skip first (if group then it has its own skip first logic)
            answer = ExpressionBuilder.skipFirstExpression(answer);
        }
    }
    // if group then wrap answer in group expression
    if (group > 0) {
        if (isXml()) {
            answer = ExpressionBuilder.groupXmlIteratorExpression(answer, group);
        } else {
            answer = ExpressionBuilder.groupIteratorExpression(answer, token, group, skipFirst);
        }
    }
    return answer;
}
Also used : Expression(org.apache.camel.Expression)

Example 17 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class ServiceCallDefinition method createProcessor.

// *****************************
// Processor Factory
// *****************************
@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    final CamelContext camelContext = routeContext.getCamelContext();
    final ServiceDiscovery serviceDiscovery = retrieveServiceDiscovery(camelContext);
    final ServiceFilter serviceFilter = retrieveServiceFilter(camelContext);
    final ServiceChooser serviceChooser = retrieveServiceChooser(camelContext);
    final LoadBalancer loadBalancer = retrieveLoadBalancer(camelContext);
    final Expression expression = retrieveExpression(camelContext);
    if (loadBalancer instanceof CamelContextAware) {
        ((CamelContextAware) loadBalancer).setCamelContext(camelContext);
    }
    if (loadBalancer instanceof ServiceDiscoveryAware) {
        ((ServiceDiscoveryAware) loadBalancer).setServiceDiscovery(serviceDiscovery);
    }
    if (loadBalancer instanceof ServiceFilterAware) {
        ((ServiceFilterAware) loadBalancer).setServiceFilter(serviceFilter);
    }
    if (loadBalancer instanceof ServiceChooserAware) {
        ((ServiceChooserAware) loadBalancer).setServiceChooser(serviceChooser);
    }
    // The component is used to configure the default scheme to use (eg camel component name).
    // The component configured on EIP takes precedence vs configured on configuration.
    String component = this.component;
    if (component == null) {
        ServiceCallConfigurationDefinition conf = retrieveConfig(camelContext);
        if (conf != null) {
            component = conf.getComponent();
        }
    }
    if (component == null) {
        ServiceCallConfigurationDefinition conf = retrieveDefaultConfig(camelContext);
        if (conf != null) {
            component = conf.getComponent();
        }
    }
    return new DefaultServiceCallProcessor(camelContext, name, component, uri, pattern, loadBalancer, expression);
}
Also used : CamelContext(org.apache.camel.CamelContext) PassThroughServiceFilter(org.apache.camel.impl.cloud.PassThroughServiceFilter) ServiceFilter(org.apache.camel.cloud.ServiceFilter) HealthyServiceFilter(org.apache.camel.impl.cloud.HealthyServiceFilter) CamelContextAware(org.apache.camel.CamelContextAware) ServiceDiscoveryAware(org.apache.camel.cloud.ServiceDiscoveryAware) ServiceChooserAware(org.apache.camel.cloud.ServiceChooserAware) DefaultServiceCallProcessor(org.apache.camel.impl.cloud.DefaultServiceCallProcessor) DefaultLoadBalancer(org.apache.camel.impl.cloud.DefaultLoadBalancer) LoadBalancer(org.apache.camel.cloud.LoadBalancer) ServiceFilterAware(org.apache.camel.cloud.ServiceFilterAware) ServiceChooser(org.apache.camel.cloud.ServiceChooser) RoundRobinServiceChooser(org.apache.camel.impl.cloud.RoundRobinServiceChooser) RandomServiceChooser(org.apache.camel.impl.cloud.RandomServiceChooser) DefaultServiceCallExpression(org.apache.camel.impl.cloud.DefaultServiceCallExpression) Expression(org.apache.camel.Expression) ServiceDiscovery(org.apache.camel.cloud.ServiceDiscovery)

Example 18 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class ServiceCallDefinition method retrieveExpression.

// ******************************************
// Expression
// ******************************************
private Expression retrieveExpression(CamelContext camelContext, Function<CamelContext, ServiceCallConfigurationDefinition> function) throws Exception {
    Expression answer = null;
    ServiceCallConfigurationDefinition config = function.apply(camelContext);
    if (config != null) {
        if (config.getExpressionConfiguration() != null) {
            answer = config.getExpressionConfiguration().newInstance(camelContext);
        } else {
            answer = retrieve(Expression.class, camelContext, config::getExpression, config::getExpressionRef);
        }
    }
    return answer;
}
Also used : DefaultServiceCallExpression(org.apache.camel.impl.cloud.DefaultServiceCallExpression) Expression(org.apache.camel.Expression)

Example 19 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class ServiceCallExpressionConfiguration method newInstance.

// *************************************************************************
// Factory
// *************************************************************************
@Override
public Expression newInstance(CamelContext camelContext) throws Exception {
    ObjectHelper.notNull(factoryKey, "Expression factoryKey");
    Expression answer;
    // First try to find the factory from the registry.
    ServiceExpressionFactory factory = CamelContextHelper.lookup(camelContext, factoryKey, ServiceExpressionFactory.class);
    if (factory != null) {
        // If a factory is found in the registry do not re-configure it as
        // it should be pre-configured.
        answer = factory.newInstance(camelContext);
    } else {
        Class<?> type;
        try {
            // Then use Service factory.
            type = camelContext.getFactoryFinder(RESOURCE_PATH).findClass(factoryKey);
        } catch (Exception e) {
            throw new NoFactoryAvailableException(RESOURCE_PATH + factoryKey, e);
        }
        if (type != null) {
            if (ServiceExpressionFactory.class.isAssignableFrom(type)) {
                factory = (ServiceExpressionFactory) camelContext.getInjector().newInstance(type);
            } else {
                throw new IllegalArgumentException("Resolving Expression: " + factoryKey + " detected type conflict: Not a ExpressionFactory implementation. Found: " + type.getName());
            }
        }
        try {
            Map<String, Object> parameters = new HashMap<>();
            IntrospectionSupport.getProperties(this, parameters, null, false);
            parameters.put("properties", getPropertiesAsMap(camelContext));
            postProcessFactoryParameters(camelContext, parameters);
            IntrospectionSupport.setProperties(factory, parameters);
            answer = factory.newInstance(camelContext);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
    return answer;
}
Also used : Expression(org.apache.camel.Expression) HashMap(java.util.HashMap) ServiceExpressionFactory(org.apache.camel.cloud.ServiceExpressionFactory) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException)

Example 20 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class ExpressionDefinition method getLabel.

/**
     * Returns some descriptive text to describe this node
     */
public String getLabel() {
    Predicate predicate = getPredicate();
    if (predicate != null) {
        return predicate.toString();
    }
    Expression expressionValue = getExpressionValue();
    if (expressionValue != null) {
        return expressionValue.toString();
    }
    String exp = getExpression();
    return exp != null ? exp : "";
}
Also used : Expression(org.apache.camel.Expression) Predicate(org.apache.camel.Predicate)

Aggregations

Expression (org.apache.camel.Expression)183 Exchange (org.apache.camel.Exchange)44 Processor (org.apache.camel.Processor)24 Predicate (org.apache.camel.Predicate)22 DefaultExchange (org.apache.camel.impl.DefaultExchange)21 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)18 ArrayList (java.util.ArrayList)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)15 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)15 Language (org.apache.camel.spi.Language)15 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)14 SendProcessor (org.apache.camel.processor.SendProcessor)14 Test (org.junit.Test)8 SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)7 File (java.io.File)4 ExecutorService (java.util.concurrent.ExecutorService)4 SimpleIllegalSyntaxException (org.apache.camel.language.simple.types.SimpleIllegalSyntaxException)4 HashMap (java.util.HashMap)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 CamelExchangeException (org.apache.camel.CamelExchangeException)3