KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > util > patterns > flow > ConfigurationLoader


1 package org.mr.core.util.patterns.flow;
2
3 import org.mr.core.util.exceptions.CreationException;
4 import org.w3c.dom.Document JavaDoc;
5 import org.w3c.dom.Element JavaDoc;
6 import org.w3c.dom.NodeList JavaDoc;
7
8 import javax.xml.parsers.DocumentBuilder JavaDoc;
9 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 /*
14  * Copyright 2002 by
15  * <a HREF="http://www.coridan.com">Coridan</a>
16  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
17  *
18  * The contents of this file are subject to the Mozilla Public License Version
19  * 1.1 (the "License"); you may not use this file except in compliance with the
20  * License. You may obtain a copy of the License at
21  * http://www.mozilla.org/MPL/
22  *
23  * Software distributed under the License is distributed on an "AS IS" basis,
24  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
25  * for the specific language governing rights and limitations under the
26  * License.
27  *
28  * The Original Code is "MantaRay" (TM).
29  *
30  * The Initial Developer of the Original Code is Coridan.
31  * Portions created by the Initial Developer are Copyright (C) 2006
32  * Coridan Inc. All Rights Reserved.
33  *
34  * Contributor(s): all the names of the contributors are added in the source
35  * code where applicable.
36  *
37  * Alternatively, the contents of this file may be used under the terms of the
38  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
39  * provisions of LGPL are applicable instead of those above. If you wish to
40  * allow use of your version of this file only under the terms of the LGPL
41  * License and not to allow others to use your version of this file under
42  * the MPL, indicate your decision by deleting the provisions above and
43  * replace them with the notice and other provisions required by the LGPL.
44  * If you do not delete the provisions above, a recipient may use your version
45  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
46  
47  *
48  * This library is free software; you can redistribute it and/or modify it
49  * under the terms of the MPL as stated above or under the terms of the GNU
50  * Lesser General Public License as published by the Free Software Foundation;
51  * either version 2.1 of the License, or any later version.
52  *
53  * This library is distributed in the hope that it will be useful, but WITHOUT
54  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
55  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
56  * License for more details.
57  */

58
59  /**
60  * User: Moti Tal
61  * Date: Mar 13, 2005
62  * Time: 12:57:06 PM
63  *
64  * This Class parses the configuration file and sets the FlowFramework.
65  */

66 public class ConfigurationLoader {
67
68     public final static String JavaDoc FLOW_STEP_CONDITION_TAG_NAME = "condition";
69     public final static String JavaDoc FLOW_STEP_CONDITIONS_TAG_NAME = "conditions";
70     public final static String JavaDoc FLOW_STEP_CONDITION_PARAM_ATTR_NAME = "variable";
71     public final static String JavaDoc FLOW_STEP_CONDITION_PARAM_VALUE_ATTR_NAME = "value";
72     public final static String JavaDoc FLOW_STEP_CONDITION_CLASS_ATTR_NAME = "class";
73
74     //configuration file path
75
String JavaDoc m_filePath = null;
76
77     //holds the frameworks after parsing
78
private static HashMap JavaDoc s_framworks = new HashMap JavaDoc();
79
80     //use for overwrite tag name
81
//TODO SHOULD be replaced by common XML format using XSLT.
82
HashMap JavaDoc m_tagsAndAttributesNames = new HashMap JavaDoc();
83
84     public ConfigurationLoader(String JavaDoc i_filePath, HashMap JavaDoc i_tagsAndAttributesNames) {
85         m_filePath = i_filePath;
86         if(i_tagsAndAttributesNames != null)
87             m_tagsAndAttributesNames.putAll(i_tagsAndAttributesNames);
88     }
89
90     /**
91      * Use as hook to get an empty FlowFramework
92       * @return a FlowFramework
93      */

94     protected FlowFramework generateEmptyFramework(){
95         return new DefaultFlowFramework();
96     }
97
98     /**
99      * Use as hook to get an empty Flow
100      * @return a Flow
101      */

102     protected Flow generateEmptyFlow(){
103         return new DefaultFlow();
104     }
105
106     protected Condition generateConditionSet(Element JavaDoc i_element) throws CreationException{
107         Conditional conditional = new Conditional();
108         conditional.configure(i_element);
109         NodeList JavaDoc conditions = i_element.getElementsByTagName(FLOW_STEP_CONDITION_TAG_NAME);
110         for(int j = 0; j < conditions.getLength() ; j++){
111             Condition condition = generateCondition((Element JavaDoc)conditions.item(j));
112             conditional.addCondition(condition);
113         }
114         conditions = i_element.getElementsByTagName(FLOW_STEP_CONDITIONS_TAG_NAME);
115         for(int j = 0; j < conditions.getLength() ; j++){
116             Condition condition = generateConditionSet((Element JavaDoc)conditions.item(j));
117             conditional.addCondition(condition);
118         }
119         return conditional;
120     }
121
122     protected Condition generateCondition(Element JavaDoc i_element) throws CreationException{
123         Condition condition = null;
124         try {
125
126             String JavaDoc className = i_element.getAttribute(FLOW_STEP_CONDITION_CLASS_ATTR_NAME);
127             if(className != null && className.length() > 0){
128                 condition = (Condition)loadClass(className).newInstance();
129             }else{
130                 condition = new StringCondition();
131             }
132             condition.configure(i_element);
133         } catch (Exception JavaDoc e) {
134             e.printStackTrace();
135             return null;
136         }
137         return condition;
138     }
139
140     /**
141      * Use as hook to overwrite configurable tag names.
142      * @param i_name the default name
143      * @return The default name or the applicative name if configured.
144      */

145     protected String JavaDoc getConfigurationName(String JavaDoc i_name){
146         String JavaDoc name = (String JavaDoc)m_tagsAndAttributesNames.get(i_name);
147         if(name == null)
148             return i_name;
149         return name;
150     }
151
152     /**
153      * a hook to set additional information to the Decleration
154      * @param i_declaration the current Decleration
155      */

156     protected void configureDeclaration(Declaration i_declaration){
157     }
158
159     /**
160      * Loads the FlowFramework
161      * @return a FlowFramework
162      * @throws Exception When parse configuration failed
163      */

164     public FlowFramework loadFramework() throws Exception JavaDoc{
165         //Check if the flow parsed already.
166
FlowFramework framework = (FlowFramework)s_framworks.get(m_filePath);
167         if(framework!= null)
168             return framework;
169
170         //call a hook to get an empty framework
171
framework = generateEmptyFramework();
172
173         DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
174         DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
175
176         // Read the entire document into memory
177
Document JavaDoc document = db.parse(m_filePath);
178
179         //add declarations
180
HashMap JavaDoc declarations = new HashMap JavaDoc();
181         NodeList JavaDoc list = document.getElementsByTagName(getConfigurationName(Declaration.DECLARATION_TAG_NAME));
182         for (int i = 0 ; i < list.getLength() ; i++) {
183             Element JavaDoc dec = (Element JavaDoc)list.item(i);
184             //declaration
185
String JavaDoc className = dec.getAttribute(getConfigurationName(Declaration.DECLARATION_CLASS_ATTR_NAME));
186             String JavaDoc name = dec.getAttribute(getConfigurationName(Declaration.DECLARATION_NAME_ATTR_NAME));
187 // String entryPoint = panel.getAttribute(Declaration.DECLARATION_ENTRYPOINT_ATTR_NAME);
188
Declaration declaration = (Declaration)loadClass(className).newInstance();
189             declaration.configure(dec);
190 // declaration.setEntryPoint("true".equals(entryPoint));
191
configureDeclaration(declaration);
192             declarations.put(name, declaration);
193 // if(!panel.hasChildNodes())
194
// continue;
195
// //add input/output parameters for each panel
196
// NodeList directions = panel.getChildNodes();
197
// Map inputs = null, outputs = null;
198
// for (int j = 0 ; j < directions.getLength() ; j++) {
199
// Node node = directions.item(j);
200
// Map set = new HashMap();
201
// if(node.hasChildNodes()){
202
// NodeList params = panel.getChildNodes();
203
// for (int y = 0 ; y < params.getLength() ; y++) {
204
// Element input = (Element)params.item(y);
205
// set.put(input.getAttribute(Declaration.INPUT_AOUTPUT_PARAM_ATTR_NAME), null);
206
// }
207
// }
208
// if(XUIPanel.XUI_PANEL_INPUTS_TAG_NAME.equals(node.getNodeName())){
209
// inputs = new HashMap(set);
210
// }else if(Declaration.OUTPUTS_TAG_NAME.equals(node.getNodeName())){
211
// outputs = new HashMap(set);
212
// }
213
// declaration.configure(inputs,outputs);
214
// }
215
}
216
217         //add flows
218
list = document.getElementsByTagName(getConfigurationName(Flow.FLOW_TAG_NAME));
219         Flow flow = generateEmptyFlow();
220 // FlowCondition[] conditionArray = null;
221
if(list != null && list.getLength() == 1){
222             Element JavaDoc element = (Element JavaDoc)list.item(0);
223             if(element.hasChildNodes()){
224                 list = element.getElementsByTagName(getConfigurationName(Flow.FLOW_STEP_TAG_NAME));
225                  for (int i = 0 ; i < list.getLength() ; i++) {
226                     Element JavaDoc step = (Element JavaDoc)list.item(i);
227                     NodeList JavaDoc conditionsTag = step.getElementsByTagName(getConfigurationName(FLOW_STEP_CONDITIONS_TAG_NAME));
228                     Condition condition = null;
229                     if(conditionsTag != null && conditionsTag.getLength() == 1){
230                         Element JavaDoc cons = (Element JavaDoc)conditionsTag.item(0);
231 // NodeList conditions = cons.getElementsByTagName(getConfigurationName(FLOW_STEP_CONDITION_TAG_NAME));
232
// conditionArray = new FlowCondition[conditions.getLength()];
233
// for (int j = 0 ; j < conditions.getLength() ; j++) {
234
// Element condition = (Element)conditions.item(j);
235
// NamedNodeMap nodeMap = condition.getAttributes();
236
// Map map = new HashMap();
237
// for(int z = 0 ;z < nodeMap.getLength() ; z++){
238
// Node node = nodeMap.item(z);
239
// map.put(node.getNodeName(),node.getNodeValue());
240
// }
241
// conditionArray[j] = generateCondition(condition);
242
// }
243
condition = generateConditionSet(cons);
244                     }
245
246                     flow.addStep(step.getAttribute(getConfigurationName(Flow.FLOW_STEP_ID_ATTR_NAME)),
247                                  (Declaration)declarations.get(step.getAttribute(getConfigurationName(Flow.FLOW_STEP_FROM_ATTR_NAME))),
248                                  (Declaration)declarations.get(step.getAttribute(getConfigurationName(Flow.FLOW_STEP_TO_ATTR_NAME))),
249                                  condition);
250                  }
251             }
252         }
253         framework.setFlow(flow);
254         s_framworks.put(m_filePath,framework);
255         return framework;
256     }
257
258     /**
259      * A default condition.
260      * This condition checks 2 strings to met the condition.
261      */

262     private class StringCondition implements Condition{
263         String JavaDoc m_name;
264         String JavaDoc m_value;
265
266         public void configure(Element JavaDoc i_object) throws CreationException {
267             m_name = i_object.getAttribute(getConfigurationName(FLOW_STEP_CONDITION_PARAM_ATTR_NAME));
268             m_value = i_object.getAttribute(getConfigurationName(FLOW_STEP_CONDITION_PARAM_VALUE_ATTR_NAME));
269         }
270
271         public boolean metCondition(Object JavaDoc i_param) {
272             String JavaDoc value = (String JavaDoc)((Map JavaDoc)i_param).get(m_name);
273             return m_value.equals(value);
274         }
275     }
276
277     public static void main(String JavaDoc[] args) {
278         try {
279             HashMap JavaDoc hashMap = new HashMap JavaDoc();
280             hashMap.put(Declaration.DECLARATION_TAG_NAME, "plugin");
281             hashMap.put(Flow.FLOW_TAG_NAME, "flow");
282             ConfigurationLoader defaultLoader = new ConfigurationLoader("conf.xml", hashMap);
283             FlowFramework framwork = defaultLoader.loadFramework();
284 // framwork.execute(new ActivationDataObject());
285
} catch (Exception JavaDoc e) {
286             e.printStackTrace();
287         }
288     }
289
290     public Class JavaDoc loadClass(String JavaDoc i_name)throws Exception JavaDoc{
291         try {
292             return Thread.currentThread().getContextClassLoader().loadClass(i_name);
293         } catch (NullPointerException JavaDoc e) { //native call patch
294
return Class.forName(i_name);
295         }
296     }
297 }
298
Popular Tags