View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2013 Sebastian
3    * Thomschke.
4    *
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    *
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.expression;
14  
15  import java.util.Map;
16  
17  import net.sf.oval.Validator;
18  import net.sf.oval.exception.ExpressionLanguageNotAvailableException;
19  import net.sf.oval.internal.Log;
20  import net.sf.oval.internal.util.Assert;
21  import net.sf.oval.internal.util.ReflectionUtils;
22  
23  /**
24   * @author Sebastian Thomschke
25   */
26  public class ExpressionLanguageRegistry
27  {
28  	private static final Log LOG = Log.getLog(ExpressionLanguageRegistry.class);
29  
30  	private final Map<String, ExpressionLanguage> elcache = Validator.getCollectionFactory().createMap(4);
31  
32  	private ExpressionLanguage _initializeDefaultEL(final String languageId)
33  	{
34  		// JavaScript support
35  		if (("javascript".equals(languageId) || "js".equals(languageId))
36  				&& ReflectionUtils.isClassPresent("org.mozilla.javascript.Context"))
37  			return registerExpressionLanguage("js", registerExpressionLanguage("javascript", new ExpressionLanguageJavaScriptImpl()));
38  
39  		// Groovy support
40  		if ("groovy".equals(languageId) && ReflectionUtils.isClassPresent("groovy.lang.Binding"))
41  			return registerExpressionLanguage("groovy", new ExpressionLanguageGroovyImpl());
42  
43  		// BeanShell support
44  		if (("beanshell".equals(languageId) || "bsh".equals(languageId)) && ReflectionUtils.isClassPresent("bsh.Interpreter"))
45  			return registerExpressionLanguage("beanshell", registerExpressionLanguage("bsh", new ExpressionLanguageBeanShellImpl()));
46  
47  		// OGNL support
48  		if ("ognl".equals(languageId) && ReflectionUtils.isClassPresent("ognl.Ognl"))
49  			return registerExpressionLanguage("ognl", new ExpressionLanguageOGNLImpl());
50  
51  		// MVEL2 support
52  		if ("mvel".equals(languageId) && ReflectionUtils.isClassPresent("org.mvel2.MVEL"))
53  			return registerExpressionLanguage("mvel", new ExpressionLanguageMVELImpl());
54  
55  		// JRuby support
56  		else if (("jruby".equals(languageId) || "ruby".equals(languageId)) && ReflectionUtils.isClassPresent("org.jruby.Ruby"))
57  			return registerExpressionLanguage("jruby", registerExpressionLanguage("ruby", new ExpressionLanguageJRubyImpl()));
58  
59  		// JEXL2 support
60  		if ("jexl".equals(languageId) && ReflectionUtils.isClassPresent("org.apache.commons.jexl2.JexlEngine"))
61  			return registerExpressionLanguage("jexl", new ExpressionLanguageJEXLImpl());
62  
63  		// scripting support via JSR223
64  		if (ReflectionUtils.isClassPresent("javax.script.ScriptEngineManager"))
65  		{
66  			final ExpressionLanguage el = ExpressionLanguageScriptEngineImpl.get(languageId);
67  			if (el != null) return registerExpressionLanguage(languageId, el);
68  		}
69  
70  		return null;
71  	}
72  
73  	/**
74  	 *
75  	 * @param languageId the id of the language, cannot be null
76  	 *
77  	 * @throws IllegalArgumentException if <code>languageName == null</code>
78  	 * @throws ExpressionLanguageNotAvailableException
79  	 */
80  	public ExpressionLanguage getExpressionLanguage(final String languageId) throws IllegalArgumentException,
81  			ExpressionLanguageNotAvailableException
82  	{
83  		Assert.argumentNotNull("languageId", languageId);
84  
85  		ExpressionLanguage el = elcache.get(languageId);
86  
87  		if (el == null) el = _initializeDefaultEL(languageId);
88  
89  		if (el == null) throw new ExpressionLanguageNotAvailableException(languageId);
90  
91  		return el;
92  	}
93  
94  	/**
95  	 *
96  	 * @param languageId the expression language identifier
97  	 * @param impl the expression language implementation
98  	 * @throws IllegalArgumentException if <code>languageId == null || expressionLanguage == null</code>
99  	 */
100 	public ExpressionLanguage registerExpressionLanguage(final String languageId, final ExpressionLanguage impl)
101 			throws IllegalArgumentException
102 	{
103 		Assert.argumentNotNull("languageId", languageId);
104 		Assert.argumentNotNull("impl", impl);
105 
106 		LOG.info("Expression language '{1}' registered: {2}", languageId, impl);
107 		elcache.put(languageId, impl);
108 		return impl;
109 	}
110 }