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 */ 017 package org.apache.camel.builder; 018 019 import org.apache.camel.Exchange; 020 import org.apache.camel.Expression; 021 import org.apache.camel.Predicate; 022 import org.apache.camel.language.simple.SimpleLanguage; 023 import org.apache.camel.util.ObjectHelper; 024 025 /** 026 * Creates an {@link org.apache.camel.language.Simple} language builder. 027 * <p/> 028 * This builder is available in the Java DSL from the {@link RouteBuilder} which means that using 029 * simple language for {@link Expression}s or {@link Predicate}s is very easy with the help of this builder. 030 * 031 * @version 032 */ 033 public class SimpleBuilder implements Predicate, Expression { 034 035 private final String text; 036 private Class<?> resultType; 037 // cache the expression/predicate 038 private volatile Expression expression; 039 private volatile Predicate predicate; 040 041 public SimpleBuilder(String text) { 042 this.text = text; 043 } 044 045 public static SimpleBuilder simple(String text) { 046 return new SimpleBuilder(text); 047 } 048 049 public static SimpleBuilder simple(String text, Class<?> resultType) { 050 SimpleBuilder answer = simple(text); 051 answer.setResultType(resultType); 052 return answer; 053 } 054 055 public String getText() { 056 return text; 057 } 058 059 public Class<?> getResultType() { 060 return resultType; 061 } 062 063 public void setResultType(Class<?> resultType) { 064 this.resultType = resultType; 065 } 066 067 public SimpleBuilder resultType(Class<?> resultType) { 068 setResultType(resultType); 069 return this; 070 } 071 072 public boolean matches(Exchange exchange) { 073 if (predicate == null) { 074 predicate = createPredicate(exchange); 075 } 076 return predicate.matches(exchange); 077 } 078 079 public <T> T evaluate(Exchange exchange, Class<T> type) { 080 if (expression == null) { 081 expression = createExpression(exchange); 082 } 083 return expression.evaluate(exchange, type); 084 } 085 086 private Predicate createPredicate(Exchange exchange) { 087 SimpleLanguage simple = (SimpleLanguage) exchange.getContext().resolveLanguage("simple"); 088 // resolve property placeholders 089 try { 090 String resolve = exchange.getContext().resolvePropertyPlaceholders(text); 091 return simple.createPredicate(resolve); 092 } catch (Exception e) { 093 throw ObjectHelper.wrapCamelExecutionException(exchange, e); 094 } 095 } 096 097 private Expression createExpression(Exchange exchange) { 098 SimpleLanguage simple = (SimpleLanguage) exchange.getContext().resolveLanguage("simple"); 099 // resolve property placeholders 100 try { 101 String resolve = exchange.getContext().resolvePropertyPlaceholders(text); 102 Expression exp = simple.createExpression(resolve); 103 if (resultType != null) { 104 exp = ExpressionBuilder.convertToExpression(exp, resultType); 105 } 106 return exp; 107 } catch (Exception e) { 108 throw ObjectHelper.wrapCamelExecutionException(exchange, e); 109 } 110 } 111 112 public String toString() { 113 return "Simple: " + text; 114 } 115 }