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(&lt;classname&gt;) to convert the in body to the given type, will throw exception if not possible to convert</li>
044 * <li>bodyAs(&lt;classname&gt;) to convert the in body to the given type, will return null if not possible to convert</li>
045 * <li>headerAs(&lt;key&gt;, &lt;classname&gt;) 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:&lt;command&gt; 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:&lt;command&gt;:&lt;pattern&gt; for date formatting using {@link java.text.SimpleDateFormat} patterns</li>
066 * <li>date-with-timezone:&lt;command&gt;:&lt;timezone&gt;:&lt;pattern&gt; for date formatting using {@link java.text.SimpleDateFormat} timezones and patterns</li>
067 * <li>bean:&lt;bean expression&gt; to invoke a bean using the
068 * {@link org.apache.camel.language.bean.BeanLanguage BeanLanguage}</li>
069 * <li>properties:&lt;[locations]&gt;:&lt;key&gt; 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                // use the new parser
165                SimplePredicateParser parser = new SimplePredicateParser(expression, allowEscape, cacheExpression);
166                answer = parser.parsePredicate();
167            }
168            if (cachePredicate != null && answer != null) {
169                cachePredicate.put(expression, answer);
170            }
171        }
172
173        return answer;
174    }
175
176    @SuppressWarnings("deprecation")
177    public Expression createExpression(String expression) {
178        ObjectHelper.notNull(expression, "expression");
179
180        Expression answer = cacheExpression != null ? cacheExpression.get(expression) : null;
181        if (answer == null) {
182
183            expression = loadResource(expression);
184
185            // support old simple language syntax
186            answer = SimpleBackwardsCompatibleParser.parseExpression(expression, allowEscape);
187            if (answer == null) {
188                // use the new parser
189                SimpleExpressionParser parser = new SimpleExpressionParser(expression, allowEscape, cacheExpression);
190                answer = parser.parseExpression();
191            }
192            if (cacheExpression != null && answer != null) {
193                cacheExpression.put(expression, answer);
194            }
195        }
196
197        return answer;
198    }
199
200    /**
201     * Creates a new {@link Expression}.
202     * <p/>
203     * <b>Important:</b> If you need to use a predicate (function to return true|false) then use
204     * {@link #predicate(String)} instead.
205     */
206    public static Expression simple(String expression) {
207        return expression(expression);
208    }
209
210    /**
211     * Creates a new {@link Expression} (or {@link Predicate}
212     * if the resultType is a <tt>Boolean</tt>, or <tt>boolean</tt> type).
213     */
214    public static Expression simple(String expression, Class<?> resultType) {
215        return new SimpleLanguage().createExpression(expression, resultType);
216    }
217
218    public Expression createExpression(String expression, Class<?> resultType) {
219        if (resultType == Boolean.class || resultType == boolean.class) {
220            // if its a boolean as result then its a predicate
221            Predicate predicate = createPredicate(expression);
222            return PredicateToExpressionAdapter.toExpression(predicate);
223        } else {
224            Expression exp = createExpression(expression);
225            if (resultType != null) {
226                exp = ExpressionBuilder.convertToExpression(exp, resultType);
227            }
228            return exp;
229        }
230    }
231
232    /**
233     * Creates a new {@link Expression}.
234     * <p/>
235     * <b>Important:</b> If you need to use a predicate (function to return true|false) then use
236     * {@link #predicate(String)} instead.
237     */
238    public static Expression expression(String expression) {
239        return SIMPLE.createExpression(expression);
240    }
241
242    /**
243     * Creates a new {@link Predicate}.
244     */
245    public static Predicate predicate(String predicate) {
246        return SIMPLE.createPredicate(predicate);
247    }
248
249    /**
250     * Does the expression include a simple function.
251     *
252     * @param expression the expression
253     * @return <tt>true</tt> if one or more simple function is included in the expression
254     */
255    public static boolean hasSimpleFunction(String expression) {
256        return SimpleTokenizer.hasFunctionStartToken(expression);
257    }
258
259    /**
260     * Change the start tokens used for functions.
261     * <p/>
262     * This can be used to alter the function tokens to avoid clashes with other
263     * frameworks etc.
264     * <p/>
265     * The default start tokens is <tt>${</tt> and <tt>$simple{</tt>.
266     *
267     * @param startToken new start token(s) to be used for functions
268     */
269    public static void changeFunctionStartToken(String... startToken) {
270        SimpleTokenizer.changeFunctionStartToken(startToken);
271    }
272    
273    /**
274     * Change the end tokens used for functions.
275     * <p/>
276     * This can be used to alter the function tokens to avoid clashes with other
277     * frameworks etc.
278     * <p/>
279     * The default end token is <tt>}</tt>
280     *
281     * @param endToken new end token(s) to be used for functions
282     */
283    public static void changeFunctionEndToken(String... endToken) {
284        SimpleTokenizer.changeFunctionEndToken(endToken);
285    }
286
287    /**
288     * Change the start token used for functions.
289     * <p/>
290     * This can be used to alter the function tokens to avoid clashes with other
291     * frameworks etc.
292     * <p/>
293     * The default start tokens is <tt>${</tt> and <tt>$simple{</tt>.
294     *
295     * @param startToken new start token to be used for functions
296     */
297    public void setFunctionStartToken(String startToken) {
298        changeFunctionStartToken(startToken);
299    }
300
301    /**
302     * Change the end token used for functions.
303     * <p/>
304     * This can be used to alter the function tokens to avoid clashes with other
305     * frameworks etc.
306     * <p/>
307     * The default end token is <tt>}</tt>
308     *
309     * @param endToken new end token to be used for functions
310     */
311    public void setFunctionEndToken(String endToken) {
312        changeFunctionEndToken(endToken);
313    }
314
315}