001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.builder;
018
019import org.apache.camel.Exchange;
020import org.apache.camel.Expression;
021import org.apache.camel.Predicate;
022import org.apache.camel.language.simple.SimpleLanguage;
023import org.apache.camel.util.ObjectHelper;
024import org.apache.camel.util.ResourceHelper;
025
026/**
027 * Creates an {@link org.apache.camel.language.Simple} language builder.
028 * <p/>
029 * This builder is available in the Java DSL from the {@link RouteBuilder} which means that using
030 * simple language for {@link Expression}s or {@link Predicate}s is very easy with the help of this builder.
031 *
032 * @version 
033 */
034public class SimpleBuilder implements Predicate, Expression {
035
036    private final String text;
037    private Class<?> resultType;
038    // cache the expression/predicate
039    private volatile Expression expression;
040    private volatile Predicate predicate;
041
042    public SimpleBuilder(String text) {
043        this.text = text;
044    }
045
046    public static SimpleBuilder simple(String text) {
047        return new SimpleBuilder(text);
048    }
049
050    public static SimpleBuilder simple(String text, Class<?> resultType) {
051        SimpleBuilder answer = simple(text);
052        answer.setResultType(resultType);
053        return answer;
054    }
055
056    public String getText() {
057        return text;
058    }
059
060    public Class<?> getResultType() {
061        return resultType;
062    }
063
064    public void setResultType(Class<?> resultType) {
065        this.resultType = resultType;
066    }
067
068    public SimpleBuilder resultType(Class<?> resultType) {
069        setResultType(resultType);
070        return this;
071    }
072
073    public boolean matches(Exchange exchange) {
074        if (predicate == null) {
075            predicate = createPredicate(exchange);
076        }
077        return predicate.matches(exchange);
078    }
079
080    public <T> T evaluate(Exchange exchange, Class<T> type) {
081        if (expression == null) {
082            expression = createExpression(exchange);
083        }
084        return expression.evaluate(exchange, type);
085    }
086
087    private Predicate createPredicate(Exchange exchange) {
088        SimpleLanguage simple = (SimpleLanguage) exchange.getContext().resolveLanguage("simple");
089        try {
090            // resolve property placeholders
091            String resolve = exchange.getContext().resolvePropertyPlaceholders(text);
092            // and optional it be refer to an external script on the file/classpath
093            resolve = ResourceHelper.resolveOptionalExternalScript(exchange.getContext(), resolve);
094            return simple.createPredicate(resolve);
095        } catch (Exception e) {
096            throw ObjectHelper.wrapCamelExecutionException(exchange, e);
097        }
098    }
099
100    private Expression createExpression(Exchange exchange) {
101        SimpleLanguage simple = (SimpleLanguage) exchange.getContext().resolveLanguage("simple");
102        try {
103            // resolve property placeholders
104            String resolve = exchange.getContext().resolvePropertyPlaceholders(text);
105            // and optional it be refer to an external script on the file/classpath
106            resolve = ResourceHelper.resolveOptionalExternalScript(exchange.getContext(), resolve);
107            return simple.createExpression(resolve, resultType);
108        } catch (Exception e) {
109            throw ObjectHelper.wrapCamelExecutionException(exchange, e);
110        }
111    }
112
113    public String toString() {
114        return "Simple: " + text;
115    }
116}