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.language.ref;
018
019import org.apache.camel.Exchange;
020import org.apache.camel.Expression;
021import org.apache.camel.IsSingleton;
022import org.apache.camel.Predicate;
023import org.apache.camel.builder.ExpressionBuilder;
024import org.apache.camel.spi.Language;
025import org.apache.camel.support.ExpressionAdapter;
026import org.apache.camel.util.ExpressionToPredicateAdapter;
027import org.apache.camel.util.PredicateToExpressionAdapter;
028
029/**
030 * A language for referred expressions or predicates.
031 */
032public class RefLanguage implements Language, IsSingleton {
033
034    public static Expression ref(Object value) {
035        String ref = value.toString();
036        return ExpressionBuilder.refExpression(ref);
037    }
038
039    public Predicate createPredicate(String expression) {
040        return ExpressionToPredicateAdapter.toPredicate(createExpression(expression));
041    }
042
043    public Expression createExpression(final String expression) {
044        final Expression exp = RefLanguage.ref(expression);
045        return new ExpressionAdapter() {
046            public Object evaluate(Exchange exchange) {
047                Expression target = null;
048
049                Object lookup = exp.evaluate(exchange, Object.class);
050
051                // must favor expression over predicate
052                if (lookup instanceof Expression) {
053                    target = (Expression) lookup;
054                } else if (lookup instanceof Predicate) {
055                    target = PredicateToExpressionAdapter.toExpression((Predicate) lookup);
056                }
057
058                if (target != null) {
059                    return target.evaluate(exchange, Object.class);
060                } else {
061                    throw new IllegalArgumentException("Cannot find expression or predicate in registry with ref: " + expression);
062                }
063            }
064
065            public String toString() {
066                return exp.toString();
067            }
068        };
069    }
070
071    public boolean isSingleton() {
072        return true;
073    }
074}