1 18 package org.apache.beehive.netui.script; 19 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.apache.beehive.netui.util.config.ConfigUtil; 24 import org.apache.beehive.netui.util.config.bean.NetuiConfigDocument.NetuiConfig; 25 import org.apache.beehive.netui.util.config.bean.ExpressionLanguages; 26 import org.apache.beehive.netui.util.config.bean.ExpressionLanguages.ExpressionLanguage; 27 import org.apache.beehive.netui.util.logging.Logger; 28 29 32 public class ExpressionEvaluatorFactory { 33 34 private static final Logger _logger = Logger.getInstance(ExpressionEvaluatorFactory.class); 35 36 private static final HashMap FACTORY_MAP = new HashMap (); 37 private static ExpressionEngineFactory DEFAULT_FACTORY; 38 39 static { 40 try { 41 DEFAULT_FACTORY = initialize(FACTORY_MAP); 42 } catch(Exception e) { 43 DEFAULT_FACTORY = null; 44 45 if(_logger.isErrorEnabled()) 46 _logger.error("An exception occurred loading the expression evaluator configuration. Cause: " + e, e); 47 } 48 } 49 50 55 public final static ExpressionEvaluator getInstance() { 56 return getInstance(null); 57 } 58 59 65 public final static ExpressionEvaluator getInstance(String name) { 66 assert DEFAULT_FACTORY != null; 67 assert FACTORY_MAP != null; 68 69 if(name == null) 70 return DEFAULT_FACTORY.getInstance(); 71 else if(FACTORY_MAP.containsKey(name)) 72 return ((ExpressionEngineFactory)FACTORY_MAP.get(name)).getInstance(); 73 74 String msg = "An ExpressionEvaluator named \"" + name + "\" is not available."; 75 if(_logger.isErrorEnabled()) 76 _logger.error(msg); 77 78 throw new IllegalArgumentException (msg); 79 } 80 81 private static final ExpressionEngineFactory initialize(Map factoryMap) { 82 assert factoryMap != null; 83 84 NetuiConfig config = ConfigUtil.getConfig(); 85 assert config != null; 86 87 ExpressionLanguages languages = config.getExpressionLanguages(); 88 assert languages != null; 89 90 ExpressionLanguage[] array = languages.getExpressionLanguageArray(); 91 assert array != null; 92 93 for(int i = 0; i < array.length; i++) { 94 String name = array[i].getName(); 95 String className = array[i].getFactoryClass(); 96 97 ExpressionEngineFactory factory = null; 98 try { 99 Class type = Class.forName(className); 100 factory = (ExpressionEngineFactory)type.newInstance(); 101 } catch(ClassNotFoundException cnf) { 102 if(_logger.isWarnEnabled()) 103 _logger.warn("Could not create an ExpressionEngineFactory for type \"" + className + 104 "\" because the implementation class could not be found."); 105 106 continue; 107 } catch(Exception ex) { 108 assert ex instanceof IllegalAccessException || ex instanceof InstantiationException ; 109 110 if(_logger.isWarnEnabled()) 111 _logger.warn("Could not create an ExpressionEngineFactory for type \"" + className + 112 "\" because an error occurred creating the factory. Cause: " + ex, ex); 113 continue; 114 } 115 116 if(factoryMap.containsKey(name)) 117 if(_logger.isWarnEnabled()) 118 _logger.warn("Overwriting a previously defined ExpressionEngineFactory named \"" + name + 119 "\" with a new ExpressionEngineFactory of type \"" + className + "\""); 120 else 121 _logger.info("Adding an ExpressionEngineFactory named \"" + name + "\" with implementation \"" + className + "\""); 122 123 factoryMap.put(name, factory); 124 } 125 126 ExpressionEngineFactory defaultEngineFactory = null; 127 String defaultLanguage = languages.getDefaultLanguage(); 128 if(defaultLanguage != null) { 129 defaultEngineFactory = (ExpressionEngineFactory)factoryMap.get(defaultLanguage); 130 if(defaultEngineFactory != null) { 131 if(_logger.isInfoEnabled()) 132 _logger.info("Using a default expression evaluator of type \"" + factoryMap.get(defaultLanguage).getClass().getName() + "\""); 133 } else { 134 String msg = 135 "The default ExpressionEvaluator named \"" + defaultLanguage + "\" was specified, but the ExpressionEngineFactory could not be found."; 136 if(_logger.isWarnEnabled()) _logger.warn(msg); 137 138 throw new RuntimeException (msg); 139 } 140 } else { 141 String msg = "There is no default expression engine specified."; 142 if(_logger.isErrorEnabled()) _logger.error(msg); 143 throw new RuntimeException (msg); 144 } 145 146 return defaultEngineFactory; 147 } 148 } 149 | Popular Tags |