KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > script > ExpressionEvaluatorFactory


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.script;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
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 /**
30  * Get an expression evaluator.
31  */

32 public class ExpressionEvaluatorFactory {
33
34     private static final Logger _logger = Logger.getInstance(ExpressionEvaluatorFactory.class);
35
36     private static final HashMap JavaDoc FACTORY_MAP = new HashMap JavaDoc();
37     private static ExpressionEngineFactory DEFAULT_FACTORY;
38
39     static {
40         try {
41             DEFAULT_FACTORY = initialize(FACTORY_MAP);
42         } catch(Exception JavaDoc 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     /**
51      * Get the default instance of an expression evaluator.
52      *
53      * @return an {@link ExpressionEvaluator}
54      */

55     public final static ExpressionEvaluator getInstance() {
56         return getInstance(null);
57     }
58
59     /**
60      * Get an {@link ExpressionEvaluator} named <code>name</code>.
61      *
62      * @return an ExpressionEvaluator matching the given name.
63      * @throws IllegalArgumentException if an ExpressionEvaluator matching the name is not found
64      */

65     public final static ExpressionEvaluator getInstance(String JavaDoc 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 JavaDoc msg = "An ExpressionEvaluator named \"" + name + "\" is not available.";
75         if(_logger.isErrorEnabled())
76             _logger.error(msg);
77
78         throw new IllegalArgumentException JavaDoc(msg);
79     }
80
81     private static final ExpressionEngineFactory initialize(Map JavaDoc 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 JavaDoc name = array[i].getName();
95             String JavaDoc className = array[i].getFactoryClass();
96
97             ExpressionEngineFactory factory = null;
98             try {
99                 Class JavaDoc type = Class.forName(className);
100                 factory = (ExpressionEngineFactory)type.newInstance();
101             } catch(ClassNotFoundException JavaDoc 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 JavaDoc ex) {
108                 assert ex instanceof IllegalAccessException JavaDoc || ex instanceof InstantiationException JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc(msg);
139             }
140         } else {
141             String JavaDoc msg = "There is no default expression engine specified.";
142             if(_logger.isErrorEnabled()) _logger.error(msg);
143             throw new RuntimeException JavaDoc(msg);
144         }
145
146         return defaultEngineFactory;
147     }
148 }
149
Popular Tags