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 024 /** 025 * Creates an {@link org.apache.camel.language.Simple} language builder. 026 * <p/> 027 * This builder is available in the Java DSL from the {@link RouteBuilder} which means that using 028 * simple language for {@link Expression}s or {@link Predicate}s is very easy with the help of this builder. 029 * 030 * @version 031 */ 032 public class SimpleBuilder implements Predicate, Expression { 033 034 private final String text; 035 private Class<?> resultType; 036 // cache the expression/predicate 037 private volatile Expression expression; 038 private volatile Predicate predicate; 039 040 public SimpleBuilder(String text) { 041 this.text = text; 042 } 043 044 public static SimpleBuilder simple(String text) { 045 return new SimpleBuilder(text); 046 } 047 048 public static SimpleBuilder simple(String text, Class<?> resultType) { 049 SimpleBuilder answer = simple(text); 050 answer.setResultType(resultType); 051 return answer; 052 } 053 054 public String getText() { 055 return text; 056 } 057 058 public Class<?> getResultType() { 059 return resultType; 060 } 061 062 public void setResultType(Class<?> resultType) { 063 this.resultType = resultType; 064 } 065 066 public SimpleBuilder resultType(Class<?> resultType) { 067 setResultType(resultType); 068 return this; 069 } 070 071 public boolean matches(Exchange exchange) { 072 if (predicate == null) { 073 predicate = exchange.getContext().resolveLanguage("simple").createPredicate(text); 074 } 075 return predicate.matches(exchange); 076 } 077 078 public <T> T evaluate(Exchange exchange, Class<T> type) { 079 if (expression == null) { 080 expression = createExpression(exchange); 081 } 082 return expression.evaluate(exchange, type); 083 } 084 085 private Expression createExpression(Exchange exchange) { 086 SimpleLanguage simple = (SimpleLanguage) exchange.getContext().resolveLanguage("simple"); 087 if (resultType != null) { 088 simple.setResultType(resultType); 089 } 090 return simple.createExpression(text); 091 } 092 093 public String toString() { 094 return "Simple: " + text; 095 } 096 }