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 static SimpleBuilder simpleF(String formatText, Object...values) { 057 return simple(String.format(formatText, values)); 058 } 059 060 public static SimpleBuilder simpleF(String formatText, Class<?> resultType, Object...values) { 061 return simple(String.format(formatText, values), resultType); 062 } 063 064 public String getText() { 065 return text; 066 } 067 068 public Class<?> getResultType() { 069 return resultType; 070 } 071 072 public void setResultType(Class<?> resultType) { 073 this.resultType = resultType; 074 } 075 076 public SimpleBuilder resultType(Class<?> resultType) { 077 setResultType(resultType); 078 return this; 079 } 080 081 public boolean matches(Exchange exchange) { 082 if (predicate == null) { 083 predicate = createPredicate(exchange); 084 } 085 return predicate.matches(exchange); 086 } 087 088 public <T> T evaluate(Exchange exchange, Class<T> type) { 089 if (expression == null) { 090 expression = createExpression(exchange); 091 } 092 return expression.evaluate(exchange, type); 093 } 094 095 private Predicate createPredicate(Exchange exchange) { 096 SimpleLanguage simple = (SimpleLanguage) exchange.getContext().resolveLanguage("simple"); 097 try { 098 // resolve property placeholders 099 String resolve = exchange.getContext().resolvePropertyPlaceholders(text); 100 // and optional it be refer to an external script on the file/classpath 101 resolve = ResourceHelper.resolveOptionalExternalScript(exchange.getContext(), exchange, resolve); 102 return simple.createPredicate(resolve); 103 } catch (Exception e) { 104 throw ObjectHelper.wrapCamelExecutionException(exchange, e); 105 } 106 } 107 108 private Expression createExpression(Exchange exchange) { 109 SimpleLanguage simple = (SimpleLanguage) exchange.getContext().resolveLanguage("simple"); 110 try { 111 // resolve property placeholders 112 String resolve = exchange.getContext().resolvePropertyPlaceholders(text); 113 // and optional it be refer to an external script on the file/classpath 114 resolve = ResourceHelper.resolveOptionalExternalScript(exchange.getContext(), exchange, resolve); 115 return simple.createExpression(resolve, resultType); 116 } catch (Exception e) { 117 throw ObjectHelper.wrapCamelExecutionException(exchange, e); 118 } 119 } 120 121 public String toString() { 122 return "Simple: " + text; 123 } 124}