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: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.script;
020:
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import org.apache.beehive.netui.util.config.ConfigUtil;
025: import org.apache.beehive.netui.util.config.bean.NetUIConfig;
026: import org.apache.beehive.netui.util.config.bean.ExpressionLanguagesConfig;
027: import org.apache.beehive.netui.util.config.bean.ExpressionLanguageConfig;
028: import org.apache.beehive.netui.util.logging.Logger;
029:
030: /**
031: * Factory used to obtain {@link ExpressionEvaluator} instances.
032: */
033: public class ExpressionEvaluatorFactory {
034:
035: private static final Logger LOGGER = Logger
036: .getInstance(ExpressionEvaluatorFactory.class);
037:
038: /**
039: * {@link Map} that maps String keys naming a type of {@link ExpressionEvaluator} to a
040: * {@link ExpressionEngineFactory} that creates an instance of an {@link ExpressionEvaluator}.
041: */
042: private static final HashMap FACTORY_MAP = new HashMap();
043: private static ExpressionEngineFactory DEFAULT_FACTORY;
044:
045: static {
046: /* todo: add default beahvrior */
047: try {
048: DEFAULT_FACTORY = initialize(FACTORY_MAP);
049: } catch (Exception e) {
050: DEFAULT_FACTORY = null;
051: LOGGER
052: .error(
053: "An exception occurred loading the expression evaluator configuration. Cause: "
054: + e, e);
055: }
056: }
057:
058: /**
059: * Get the default instance of an expression evaluator.
060: *
061: * @return an {@link ExpressionEvaluator}
062: */
063: public static ExpressionEvaluator getInstance() {
064: return getInstance(null);
065: }
066:
067: /**
068: * Get an {@link ExpressionEvaluator} named <code>name</code>.
069: *
070: * @param name the name of the {@link ExpressionEvaluator} to obtain.
071: * @return an {@link ExpressionEvaluator} matching the given name.
072: * @throws IllegalArgumentException if an {@link ExpressionEvaluator} matching the name is not found
073: */
074: public static ExpressionEvaluator getInstance(String name) {
075: assert DEFAULT_FACTORY != null;
076: assert FACTORY_MAP != null;
077:
078: if (name == null)
079: return DEFAULT_FACTORY.getInstance();
080: else if (FACTORY_MAP.containsKey(name))
081: return ((ExpressionEngineFactory) FACTORY_MAP.get(name))
082: .getInstance();
083:
084: String msg = "An ExpressionEvaluator named \"" + name
085: + "\" is not available.";
086: LOGGER.error(msg);
087: throw new IllegalArgumentException(msg);
088: }
089:
090: private static ExpressionEngineFactory initialize(Map factoryMap) {
091: assert factoryMap != null;
092:
093: NetUIConfig config = ConfigUtil.getConfig();
094:
095: ExpressionLanguagesConfig elConfig = config
096: .getExpressionLanguages();
097: assert elConfig != null;
098:
099: ExpressionLanguageConfig[] els = elConfig
100: .getExpressionLanguages();
101: assert els != null;
102:
103: if (els != null) {
104: for (int i = 0; i < els.length; i++) {
105: String name = els[i].getName();
106: String className = els[i].getFactoryClass();
107:
108: ExpressionEngineFactory factory = null;
109: try {
110: Class type = Class.forName(className);
111: factory = (ExpressionEngineFactory) type
112: .newInstance();
113: } catch (ClassNotFoundException cnf) {
114: LOGGER
115: .warn("Could not create an ExpressionEngineFactory for type \""
116: + className
117: + "\" because the implementation class could not be found.");
118: continue;
119: } catch (Exception ex) {
120: assert ex instanceof IllegalAccessException
121: || ex instanceof InstantiationException;
122: LOGGER
123: .warn(
124: "Could not create an ExpressionEngineFactory for type \""
125: + className
126: + "\" because an error occurred creating the factory. Cause: "
127: + ex, ex);
128: continue;
129: }
130:
131: if (factoryMap.containsKey(name))
132: if (LOGGER.isWarnEnabled())
133: LOGGER
134: .warn("Overwriting a previously defined ExpressionEngineFactory named \""
135: + name
136: + "\" with a new ExpressionEngineFactory of type \""
137: + className + "\"");
138: else
139: LOGGER
140: .info("Adding an ExpressionEngineFactory named \""
141: + name
142: + "\" with implementation \""
143: + className + "\"");
144:
145: factoryMap.put(name, factory);
146: }
147: }
148:
149: ExpressionEngineFactory defaultEngineFactory = null;
150: String defaultLanguage = elConfig.getDefaultLanguage();
151:
152: if (defaultLanguage != null) {
153: defaultEngineFactory = (ExpressionEngineFactory) factoryMap
154: .get(defaultLanguage);
155: if (defaultEngineFactory != null) {
156: LOGGER
157: .info("Using a default expression evaluator of type \""
158: + factoryMap.get(defaultLanguage)
159: .getClass().getName() + "\"");
160: } else {
161: String msg = "The default ExpressionEvaluator named \""
162: + defaultLanguage
163: + "\" was specified, but the ExpressionEngineFactory could not be found.";
164: LOGGER.warn(msg);
165:
166: throw new RuntimeException(msg);
167: }
168: } else {
169: String msg = "There is no default expression engine specified.";
170: LOGGER.error(msg);
171: throw new RuntimeException(msg);
172: }
173:
174: return defaultEngineFactory;
175: }
176: }
|