[CJ-dev] other-projects/marmalade/src/main/java/org/marmalade MarmaladeFactory.java,1.1,1.2
Brought to you by:
johnqueso
From: John C. <joh...@co...> - 2004-03-27 04:35:39
|
Update of /cvsroot/commonjava/other-projects/marmalade/src/main/java/org/marmalade In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7841/src/main/java/org/marmalade Modified Files: MarmaladeFactory.java Log Message: added test for ScopedMap (it works!) and added OGNL implementation of ExpressionEvaluator (not tested yet). Index: MarmaladeFactory.java =================================================================== RCS file: /cvsroot/commonjava/other-projects/marmalade/src/main/java/org/marmalade/MarmaladeFactory.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- MarmaladeFactory.java 26 Mar 2004 05:13:35 -0000 1.1 +++ MarmaladeFactory.java 27 Mar 2004 04:24:34 -0000 1.2 @@ -1,7 +1,14 @@ /* Created on Mar 24, 2004 */ package org.marmalade; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.UndeclaredThrowableException; +import java.util.HashMap; import java.util.Map; +import java.util.WeakHashMap; import org.codehaus.tagalog.Attributes; import org.commonjava.reflection.FinderException; @@ -17,39 +24,81 @@ private static final String DEFAULT_EL_TYPE = "ognl"; - private static final String EL_LOCK = "EL-LOCK".intern(); - - private static ExpressionEvaluator el; + private static Map evaluators = new WeakHashMap(); /** Factory; deny construction. */ private MarmaladeFactory() { } + /** Return the default expression evaluator implementation. + */ public static ExpressionEvaluator getDefaultExpressionEvaluator() throws ConfigurationException { return getExpressionEvaluator(DEFAULT_EL_TYPE); } + /** Return the expression evaluator implementation for the specified EL type. + */ public static ExpressionEvaluator getExpressionEvaluator(String type) throws ConfigurationException { - synchronized(EL_LOCK){ - if(el == null){ - try { - el = (ExpressionEvaluator)ImplFinder.findImplementation( - ExpressionEvaluator.class, new Object[]{} - ); - } - catch (FinderException e) { + // lock only for the specified type of EL...let everything else happen. + synchronized(type.intern()){ + ExpressionEvaluator evaluator = (ExpressionEvaluator)evaluators.get(type); + if(evaluator == null){ + String elResource = "META-INF/el/" + type; + ClassLoader cloader = MarmaladeFactory.class.getClassLoader(); + + InputStream res = cloader.getResourceAsStream(elResource); + if(res == null){ throw new ConfigurationException( - "Error retrieving ExpressionEvaluator implementation.", e + "No ExpressionEvaluator implementation found for EL type: " + type ); } + else{ + String className = null; + + try{ + BufferedReader in = new BufferedReader(new InputStreamReader(res)); + in.readLine(); + in.close(); + } + catch(IOException e){ + throw new ConfigurationException( + "Error reading Expression Evaluator implementation class name for type: \'" + + type + "\' from \'" + elResource + "\'.", + e + ); + } + + try { + Class elClass = cloader.loadClass(className); + evaluator = (ExpressionEvaluator)elClass.newInstance(); + evaluators.put(type, evaluator); + } + catch (ClassNotFoundException e) { + throw new ConfigurationException( + "Expression evaluator class: \'" + className + "\' for type: \'" + + type + "\' not found.", + e + ); + } + catch (InstantiationException e) { + throw new ConfigurationException("Error instantiating expression evaluator.", e); + } + catch (IllegalAccessException e) { + throw new ConfigurationException("Error instantiating expression evaluator.", e); + } + catch(UndeclaredThrowableException e){ + Throwable t = e.getUndeclaredThrowable(); + throw new ConfigurationException("Error instantiating expression evaluator.", t); + } + } } - return el; + return evaluator; } } |