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.model.language; 018 019 import java.util.List; 020 021 import javax.xml.bind.annotation.XmlAccessType; 022 import javax.xml.bind.annotation.XmlAccessorType; 023 import javax.xml.bind.annotation.XmlAttribute; 024 import javax.xml.bind.annotation.XmlID; 025 import javax.xml.bind.annotation.XmlRootElement; 026 import javax.xml.bind.annotation.XmlTransient; 027 import javax.xml.bind.annotation.XmlType; 028 import javax.xml.bind.annotation.XmlValue; 029 import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; 030 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 031 032 import org.apache.camel.CamelContext; 033 import org.apache.camel.Exchange; 034 import org.apache.camel.Expression; 035 import org.apache.camel.Predicate; 036 import org.apache.camel.builder.PredicateBuilder; 037 import org.apache.camel.impl.DefaultRouteContext; 038 import org.apache.camel.spi.Language; 039 import org.apache.camel.spi.RouteContext; 040 import org.apache.camel.util.CollectionStringBuffer; 041 import org.apache.camel.util.IntrospectionSupport; 042 import org.apache.camel.util.ObjectHelper; 043 044 /** 045 * A useful base class for an expression 046 * 047 * @version $Revision: 790458 $ 048 */ 049 @XmlRootElement 050 @XmlType(name = "expression") 051 @XmlAccessorType(XmlAccessType.FIELD) 052 public class ExpressionDefinition implements Expression, Predicate { 053 @XmlAttribute 054 @XmlJavaTypeAdapter(CollapsedStringAdapter.class) 055 @XmlID 056 private String id; 057 @XmlValue 058 private String expression; 059 @XmlTransient 060 private Predicate predicate; 061 @XmlTransient 062 private Expression expressionValue; 063 @XmlTransient 064 private ExpressionDefinition expressionType; 065 066 public ExpressionDefinition() { 067 } 068 069 public ExpressionDefinition(String expression) { 070 this.expression = expression; 071 } 072 073 public ExpressionDefinition(Predicate predicate) { 074 this.predicate = predicate; 075 } 076 077 public ExpressionDefinition(Expression expression) { 078 this.expressionValue = expression; 079 } 080 081 public static String getLabel(List<ExpressionDefinition> expressions) { 082 CollectionStringBuffer buffer = new CollectionStringBuffer(); 083 for (ExpressionDefinition expression : expressions) { 084 buffer.append(expression.getLabel()); 085 } 086 return buffer.toString(); 087 } 088 089 @Override 090 public String toString() { 091 StringBuilder sb = new StringBuilder(); 092 if (getLanguage() != null) { 093 sb.append(getLanguage()).append("{"); 094 } 095 if (getExpression() != null) { 096 sb.append(getExpression()); 097 } 098 if (getPredicate() != null) { 099 sb.append(getPredicate().toString()); 100 } 101 if (getExpressionValue() != null) { 102 sb.append(getExpressionValue().toString()); 103 } 104 if (getLanguage() != null) { 105 sb.append("}"); 106 } 107 return sb.toString(); 108 } 109 110 public Object evaluate(Exchange exchange) { 111 return evaluate(exchange, Object.class); 112 } 113 114 public <T> T evaluate(Exchange exchange, Class<T> type) { 115 if (expressionValue == null) { 116 RouteContext routeContext = new DefaultRouteContext(exchange.getContext()); 117 expressionValue = createExpression(routeContext); 118 } 119 ObjectHelper.notNull(expressionValue, "expressionValue"); 120 return expressionValue.evaluate(exchange, type); 121 } 122 123 public void assertMatches(String text, Exchange exchange) throws AssertionError { 124 if (!matches(exchange)) { 125 throw new AssertionError(text + getExpression() + " for exchange: " + exchange); 126 } 127 } 128 129 public boolean matches(Exchange exchange) { 130 if (predicate == null) { 131 RouteContext routeContext = new DefaultRouteContext(exchange.getContext()); 132 predicate = createPredicate(routeContext); 133 } 134 ObjectHelper.notNull(predicate, "predicate"); 135 return predicate.matches(exchange); 136 } 137 138 public String getLanguage() { 139 return ""; 140 } 141 142 public Predicate createPredicate(RouteContext routeContext) { 143 if (predicate == null) { 144 if (expressionType != null) { 145 predicate = expressionType.createPredicate(routeContext); 146 } else if (expressionValue != null) { 147 predicate = PredicateBuilder.toPredicate(expressionValue); 148 } else if (getExpression() != null) { 149 ObjectHelper.notNull("language", getLanguage()); 150 CamelContext camelContext = routeContext.getCamelContext(); 151 Language language = camelContext.resolveLanguage(getLanguage()); 152 predicate = language.createPredicate(getExpression()); 153 configurePredicate(routeContext, predicate); 154 } 155 } 156 return predicate; 157 } 158 159 public Expression createExpression(RouteContext routeContext) { 160 if (expressionValue == null) { 161 if (expressionType != null) { 162 expressionValue = expressionType.createExpression(routeContext); 163 } else if (getExpression() != null) { 164 ObjectHelper.notNull("language", getLanguage()); 165 CamelContext camelContext = routeContext.getCamelContext(); 166 Language language = camelContext.resolveLanguage(getLanguage()); 167 expressionValue = language.createExpression(getExpression()); 168 configureExpression(routeContext, expressionValue); 169 } 170 } 171 return expressionValue; 172 } 173 174 public String getExpression() { 175 return expression; 176 } 177 178 public void setExpression(String expression) { 179 this.expression = expression; 180 } 181 182 /** 183 * Gets the value of the id property. 184 */ 185 public String getId() { 186 return id; 187 } 188 189 /** 190 * Sets the value of the id property. 191 */ 192 public void setId(String value) { 193 this.id = value; 194 } 195 196 public Predicate getPredicate() { 197 return predicate; 198 } 199 200 public Expression getExpressionValue() { 201 return expressionValue; 202 } 203 204 protected void setExpressionValue(Expression expressionValue) { 205 this.expressionValue = expressionValue; 206 } 207 208 public ExpressionDefinition getExpressionType() { 209 return expressionType; 210 } 211 212 /** 213 * Returns some descriptive text to describe this node 214 */ 215 public String getLabel() { 216 String language = getExpression(); 217 if (ObjectHelper.isEmpty(language)) { 218 Predicate predicate = getPredicate(); 219 if (predicate != null) { 220 return predicate.toString(); 221 } 222 Expression expressionValue = getExpressionValue(); 223 if (expressionValue != null) { 224 return expressionValue.toString(); 225 } 226 } else { 227 return language; 228 } 229 return ""; 230 } 231 232 /** 233 * Allows derived classes to set a lazily created expressionType instance 234 * such as if using the {@link org.apache.camel.builder.ExpressionClause} 235 */ 236 protected void setExpressionType(ExpressionDefinition expressionType) { 237 this.expressionType = expressionType; 238 } 239 240 protected void configurePredicate(RouteContext routeContext, Predicate predicate) { 241 } 242 243 protected void configureExpression(RouteContext routeContext, Expression expression) { 244 } 245 246 /** 247 * Sets a named property on the object instance using introspection 248 */ 249 protected void setProperty(Object bean, String name, Object value) { 250 try { 251 IntrospectionSupport.setProperty(bean, name, value); 252 } catch (Exception e) { 253 throw new IllegalArgumentException("Failed to set property " + name + " on " + bean 254 + ". Reason: " + e, e); 255 } 256 } 257 }