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.simple; 018 019import java.util.Map; 020 021import org.apache.camel.Expression; 022import org.apache.camel.Predicate; 023import org.apache.camel.StaticService; 024import org.apache.camel.builder.ExpressionBuilder; 025import org.apache.camel.support.LanguageSupport; 026import org.apache.camel.util.CamelContextHelper; 027import org.apache.camel.util.LRUCache; 028import org.apache.camel.util.LRUCacheFactory; 029import org.apache.camel.util.ObjectHelper; 030import org.apache.camel.util.PredicateToExpressionAdapter; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034/** 035 * A <a href="http://camel.apache.org/simple.html">simple language</a> 036 * which maps simple property style notations to access headers and bodies. 037 * Examples of supported expressions are: 038 * <ul> 039 * <li>exchangeId to access the exchange id</li> 040 * <li>id to access the inbound message id</li> 041 * <li>in.body or body to access the inbound body</li> 042 * <li>in.body.OGNL or body.OGNL to access the inbound body using an OGNL expression</li> 043 * <li>mandatoryBodyAs(<classname>) to convert the in body to the given type, will throw exception if not possible to convert</li> 044 * <li>bodyAs(<classname>) to convert the in body to the given type, will return null if not possible to convert</li> 045 * <li>headerAs(<key>, <classname>) to convert the in header to the given type, will return null if not possible to convert</li> 046 * <li>out.body to access the inbound body</li> 047 * <li>in.header.foo or header.foo to access an inbound header called 'foo'</li> 048 * <li>in.header.foo[bar] or header.foo[bar] to access an inbound header called 'foo' as a Map and lookup the map with 'bar' as key</li> 049 * <li>in.header.foo.OGNL or header.OGNL to access an inbound header called 'foo' using an OGNL expression</li> 050 * <li>out.header.foo to access an outbound header called 'foo'</li> 051 * <li>property.foo to access the exchange property called 'foo'</li> 052 * <li>property.foo.OGNL to access the exchange property called 'foo' using an OGNL expression</li> 053 * <li>sys.foo to access the system property called 'foo'</li> 054 * <li>sysenv.foo to access the system environment called 'foo'</li> 055 * <li>exception.messsage to access the exception message</li> 056 * <li>threadName to access the current thread name</li> 057 * <li>date:<command> evaluates to a Date object 058 * Supported commands are: <tt>now</tt> for current timestamp, 059 * <tt>in.header.xxx</tt> or <tt>header.xxx</tt> to use the Date object in the in header. 060 * <tt>out.header.xxx</tt> to use the Date object in the out header. 061 * <tt>property.xxx</tt> to use the Date object in the exchange property. 062 * <tt>file</tt> for the last modified timestamp of the file (available with a File consumer). 063 * Command accepts offsets such as: <tt>now-24h</tt> or <tt>in.header.xxx+1h</tt> or even <tt>now+1h30m-100</tt>. 064 * </li> 065 * <li>date:<command>:<pattern> for date formatting using {@link java.text.SimpleDateFormat} patterns</li> 066 * <li>date-with-timezone:<command>:<timezone>:<pattern> for date formatting using {@link java.text.SimpleDateFormat} timezones and patterns</li> 067 * <li>bean:<bean expression> to invoke a bean using the 068 * {@link org.apache.camel.language.bean.BeanLanguage BeanLanguage}</li> 069 * <li>properties:<[locations]>:<key> for using property placeholders using the 070 * {@link org.apache.camel.component.properties.PropertiesComponent}. 071 * The locations parameter is optional and you can enter multiple locations separated with comma. 072 * </li> 073* </ul> 074 * <p/> 075 * The simple language supports OGNL notation when accessing either body or header. 076 * <p/> 077 * The simple language now also includes file language out of the box which means the following expression is also 078 * supported: 079 * <ul> 080 * <li><tt>file:name</tt> to access the file name (is relative, see note below))</li> 081 * <li><tt>file:name.noext</tt> to access the file name with no extension</li> 082 * <li><tt>file:name.ext</tt> to access the file extension</li> 083 * <li><tt>file:ext</tt> to access the file extension</li> 084 * <li><tt>file:onlyname</tt> to access the file name (no paths)</li> 085 * <li><tt>file:onlyname.noext</tt> to access the file name (no paths) with no extension </li> 086 * <li><tt>file:parent</tt> to access the parent file name</li> 087 * <li><tt>file:path</tt> to access the file path name</li> 088 * <li><tt>file:absolute</tt> is the file regarded as absolute or relative</li> 089 * <li><tt>file:absolute.path</tt> to access the absolute file path name</li> 090 * <li><tt>file:length</tt> to access the file length as a Long type</li> 091 * <li><tt>file:size</tt> to access the file length as a Long type</li> 092 * <li><tt>file:modified</tt> to access the file last modified as a Date type</li> 093 * </ul> 094 * The <b>relative</b> file is the filename with the starting directory clipped, as opposed to <b>path</b> that will 095 * return the full path including the starting directory. 096 * <br/> 097 * The <b>only</b> file is the filename only with all paths clipped. 098 */ 099public class SimpleLanguage extends LanguageSupport implements StaticService { 100 101 private static final Logger LOG = LoggerFactory.getLogger(SimpleLanguage.class); 102 103 // singleton for expressions without a result type 104 private static final SimpleLanguage SIMPLE = new SimpleLanguage(); 105 106 boolean allowEscape = true; 107 108 // use caches to avoid re-parsing the same expressions over and over again 109 private Map<String, Expression> cacheExpression; 110 private Map<String, Predicate> cachePredicate; 111 112 /** 113 * Default constructor. 114 */ 115 public SimpleLanguage() { 116 } 117 118 @Override 119 @SuppressWarnings("unchecked") 120 public void start() throws Exception { 121 // setup cache which requires CamelContext to be set first 122 if (cacheExpression == null && cachePredicate == null && getCamelContext() != null) { 123 int maxSize = CamelContextHelper.getMaximumSimpleCacheSize(getCamelContext()); 124 if (maxSize > 0) { 125 cacheExpression = LRUCacheFactory.newLRUCache(16, maxSize, false); 126 cachePredicate = LRUCacheFactory.newLRUCache(16, maxSize, false); 127 LOG.debug("Simple language predicate/expression cache size: {}", maxSize); 128 } else { 129 LOG.debug("Simple language disabled predicate/expression cache"); 130 } 131 } 132 } 133 134 @Override 135 public void stop() throws Exception { 136 if (cachePredicate instanceof LRUCache) { 137 if (LOG.isDebugEnabled()) { 138 LRUCache cache = (LRUCache) cachePredicate; 139 LOG.debug("Clearing simple language predicate cache[size={}, hits={}, misses={}, evicted={}]", 140 cache.size(), cache.getHits(), cache.getMisses(), cache.getEvicted()); 141 } 142 } 143 if (cacheExpression instanceof LRUCache) { 144 if (LOG.isDebugEnabled()) { 145 LRUCache cache = (LRUCache) cacheExpression; 146 LOG.debug("Clearing simple language expression cache[size={}, hits={}, misses={}, evicted={}]", 147 cache.size(), cache.getHits(), cache.getMisses(), cache.getEvicted()); 148 } 149 } 150 } 151 152 @SuppressWarnings("deprecation") 153 public Predicate createPredicate(String expression) { 154 ObjectHelper.notNull(expression, "expression"); 155 156 Predicate answer = cachePredicate != null ? cachePredicate.get(expression) : null; 157 if (answer == null) { 158 159 expression = loadResource(expression); 160 161 // support old simple language syntax 162 answer = SimpleBackwardsCompatibleParser.parsePredicate(expression, allowEscape); 163 if (answer != null) { 164 LOG.warn("Simple language is using deprecated syntax: {}. You should migrate to use newer syntax that uses ${xxx} style.", expression); 165 } 166 if (answer == null) { 167 // use the new parser 168 SimplePredicateParser parser = new SimplePredicateParser(expression, allowEscape, cacheExpression); 169 answer = parser.parsePredicate(); 170 } 171 if (cachePredicate != null && answer != null) { 172 cachePredicate.put(expression, answer); 173 } 174 } 175 176 return answer; 177 } 178 179 @SuppressWarnings("deprecation") 180 public Expression createExpression(String expression) { 181 ObjectHelper.notNull(expression, "expression"); 182 183 Expression answer = cacheExpression != null ? cacheExpression.get(expression) : null; 184 if (answer == null) { 185 186 expression = loadResource(expression); 187 188 // support old simple language syntax 189 answer = SimpleBackwardsCompatibleParser.parseExpression(expression, allowEscape); 190 if (answer != null) { 191 LOG.warn("Simple language is using deprecated syntax: {}. You should migrate to use newer syntax that uses ${xxx} style.", expression); 192 } 193 if (answer == null) { 194 // use the new parser 195 SimpleExpressionParser parser = new SimpleExpressionParser(expression, allowEscape, cacheExpression); 196 answer = parser.parseExpression(); 197 } 198 if (cacheExpression != null && answer != null) { 199 cacheExpression.put(expression, answer); 200 } 201 } 202 203 return answer; 204 } 205 206 /** 207 * Creates a new {@link Expression}. 208 * <p/> 209 * <b>Important:</b> If you need to use a predicate (function to return true|false) then use 210 * {@link #predicate(String)} instead. 211 */ 212 public static Expression simple(String expression) { 213 return expression(expression); 214 } 215 216 /** 217 * Creates a new {@link Expression} (or {@link Predicate} 218 * if the resultType is a <tt>Boolean</tt>, or <tt>boolean</tt> type). 219 */ 220 public static Expression simple(String expression, Class<?> resultType) { 221 return new SimpleLanguage().createExpression(expression, resultType); 222 } 223 224 public Expression createExpression(String expression, Class<?> resultType) { 225 if (resultType == Boolean.class || resultType == boolean.class) { 226 // if its a boolean as result then its a predicate 227 Predicate predicate = createPredicate(expression); 228 return PredicateToExpressionAdapter.toExpression(predicate); 229 } else { 230 Expression exp = createExpression(expression); 231 if (resultType != null) { 232 exp = ExpressionBuilder.convertToExpression(exp, resultType); 233 } 234 return exp; 235 } 236 } 237 238 /** 239 * Creates a new {@link Expression}. 240 * <p/> 241 * <b>Important:</b> If you need to use a predicate (function to return true|false) then use 242 * {@link #predicate(String)} instead. 243 */ 244 public static Expression expression(String expression) { 245 return SIMPLE.createExpression(expression); 246 } 247 248 /** 249 * Creates a new {@link Predicate}. 250 */ 251 public static Predicate predicate(String predicate) { 252 return SIMPLE.createPredicate(predicate); 253 } 254 255 /** 256 * Does the expression include a simple function. 257 * 258 * @param expression the expression 259 * @return <tt>true</tt> if one or more simple function is included in the expression 260 */ 261 public static boolean hasSimpleFunction(String expression) { 262 return SimpleTokenizer.hasFunctionStartToken(expression); 263 } 264 265 /** 266 * Change the start tokens used for functions. 267 * <p/> 268 * This can be used to alter the function tokens to avoid clashes with other 269 * frameworks etc. 270 * <p/> 271 * The default start tokens is <tt>${</tt> and <tt>$simple{</tt>. 272 * 273 * @param startToken new start token(s) to be used for functions 274 * @deprecated will be removed in Camel 3 275 */ 276 @Deprecated 277 public static void changeFunctionStartToken(String... startToken) { 278 SimpleTokenizer.changeFunctionStartToken(startToken); 279 } 280 281 /** 282 * Change the end tokens used for functions. 283 * <p/> 284 * This can be used to alter the function tokens to avoid clashes with other 285 * frameworks etc. 286 * <p/> 287 * The default end token is <tt>}</tt> 288 * 289 * @param endToken new end token(s) to be used for functions 290 * @deprecated will be removed in Camel 3 291 */ 292 @Deprecated 293 public static void changeFunctionEndToken(String... endToken) { 294 SimpleTokenizer.changeFunctionEndToken(endToken); 295 } 296 297 /** 298 * Change the start token used for functions. 299 * <p/> 300 * This can be used to alter the function tokens to avoid clashes with other 301 * frameworks etc. 302 * <p/> 303 * The default start tokens is <tt>${</tt> and <tt>$simple{</tt>. 304 * 305 * @param startToken new start token to be used for functions 306 * @deprecated will be removed in Camel 3 307 */ 308 @Deprecated 309 public void setFunctionStartToken(String startToken) { 310 changeFunctionStartToken(startToken); 311 } 312 313 /** 314 * Change the end token used for functions. 315 * <p/> 316 * This can be used to alter the function tokens to avoid clashes with other 317 * frameworks etc. 318 * <p/> 319 * The default end token is <tt>}</tt> 320 * 321 * @param endToken new end token to be used for functions 322 * @deprecated will be removed in Camel 3 323 */ 324 @Deprecated 325 public void setFunctionEndToken(String endToken) { 326 changeFunctionEndToken(endToken); 327 } 328 329}