KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > control > ConfigXMLReader


1 /*
2  * $Id: ConfigXMLReader.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.webapp.control;
25
26 import java.io.File JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import javolution.util.FastList;
35 import javolution.util.FastMap;
36
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.base.util.UtilValidate;
39 import org.ofbiz.base.util.UtilXml;
40 import org.ofbiz.base.util.cache.UtilCache;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Element JavaDoc;
43
44 /**
45  * ConfigXMLReader.java - Reads and parses the XML site config files.
46  *
47  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
48  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
49  * @version $Rev: 5462 $
50  * @since 2.0
51  */

52 public class ConfigXMLReader {
53
54     public static final String JavaDoc module = ConfigXMLReader.class.getName();
55
56     public static ControllerConfig getControllerConfig(URL JavaDoc url) {
57         ControllerConfig controllerConfig = (ControllerConfig) controllerCache.get(url);
58         if (controllerConfig == null) { // don't want to block here
59
synchronized (ConfigXMLReader.class) {
60                 // must check if null again as one of the blocked threads can still enter
61
controllerConfig = (ControllerConfig) controllerCache.get(url);
62                 if (controllerConfig == null) {
63                     controllerConfig = new ControllerConfig(url);
64                     controllerCache.put(url, controllerConfig);
65                 }
66             }
67         }
68         return controllerConfig;
69     }
70     
71     public static UtilCache controllerCache = new UtilCache("webapp.ControllerConfig");
72     
73     public static class ControllerConfig {
74         public URL JavaDoc url;
75         
76         public Map JavaDoc configMap = FastMap.newInstance();
77         public Map JavaDoc handlerMap = FastMap.newInstance();
78         public Map JavaDoc requestMap = FastMap.newInstance();
79         public Map JavaDoc viewMap = FastMap.newInstance();
80
81         public ControllerConfig(URL JavaDoc url) {
82             this.url = url;
83             
84             Element JavaDoc rootElement = loadDocument(url);
85             if (rootElement != null) {
86                 this.configMap = loadConfigMap(rootElement, url);
87                 this.handlerMap = loadHandlerMap(rootElement, url);
88                 this.requestMap = loadRequestMap(rootElement, url);
89                 this.viewMap = loadViewMap(rootElement, url);
90             }
91         }
92     }
93
94     /** Site Config Variables */
95     public static final String JavaDoc DEFAULT_ERROR_PAGE = "errorpage";
96     public static final String JavaDoc SITE_OWNER = "owner";
97     public static final String JavaDoc SECURITY_CLASS = "security-class";
98     public static final String JavaDoc FIRSTVISIT = "firstvisit";
99     public static final String JavaDoc PREPROCESSOR = "preprocessor";
100     public static final String JavaDoc POSTPROCESSOR = "postprocessor";
101
102     /** URI Config Variables */
103     public static final String JavaDoc INCLUDE = "include";
104     public static final String JavaDoc INCLUDE_FILE = "file";
105     public static final String JavaDoc INCLUDE_URL = "url";
106
107     public static final String JavaDoc REQUEST_MAPPING = "request-map";
108     public static final String JavaDoc REQUEST_URI = "uri";
109     public static final String JavaDoc REQUEST_EDIT = "edit";
110
111     public static final String JavaDoc REQUEST_DESCRIPTION = "description";
112     public static final String JavaDoc ERROR_PAGE = "error";
113     public static final String JavaDoc NEXT_PAGE = "success";
114
115     public static final String JavaDoc SECURITY = "security";
116     public static final String JavaDoc SECURITY_HTTPS = "https";
117     public static final String JavaDoc SECURITY_AUTH = "auth";
118     public static final String JavaDoc SECURITY_EXTVIEW = "external-view";
119     public static final String JavaDoc SECURITY_DIRECT = "direct-request";
120
121     public static final String JavaDoc EVENT = "event";
122     public static final String JavaDoc EVENT_PATH = "path";
123     public static final String JavaDoc EVENT_TYPE = "type";
124     public static final String JavaDoc EVENT_METHOD = "invoke";
125     public static final String JavaDoc EVENT_GLOBAL_TRANSACTION = "global-transaction";
126
127     public static final String JavaDoc RESPONSE = "response";
128     public static final String JavaDoc RESPONSE_NAME = "name";
129     public static final String JavaDoc RESPONSE_TYPE = "type";
130     public static final String JavaDoc RESPONSE_VALUE = "value";
131
132     /** View Config Variables */
133     public static final String JavaDoc VIEW_MAPPING = "view-map";
134     public static final String JavaDoc VIEW_NAME = "name";
135     public static final String JavaDoc VIEW_PAGE = "page";
136     public static final String JavaDoc VIEW_TYPE = "type";
137     public static final String JavaDoc VIEW_INFO = "info";
138     public static final String JavaDoc VIEW_CONTENT_TYPE = "content-type";
139     public static final String JavaDoc VIEW_ENCODING = "encoding";
140     public static final String JavaDoc VIEW_DESCRIPTION = "description";
141
142     /** Handler Config Variables */
143     public static final String JavaDoc HANDLER = "handler";
144     public static final String JavaDoc HANDLER_NAME = "name";
145     public static final String JavaDoc HANDLER_TYPE = "type";
146     public static final String JavaDoc HANDLER_CLASS = "class";
147
148     /** Loads the XML file and returns the root element */
149     public static Element JavaDoc loadDocument(URL JavaDoc location) {
150         Document JavaDoc document = null;
151         try {
152             document = UtilXml.readXmlDocument(location, true);
153             Element JavaDoc rootElement = document.getDocumentElement();
154             // rootElement.normalize();
155
if (Debug.verboseOn()) Debug.logVerbose("Loaded XML Config - " + location, module);
156             return rootElement;
157         } catch (Exception JavaDoc e) {
158             Debug.logError(e, module);
159         }
160         return null;
161     }
162
163     /** Gets a Map of request mappings. */
164     public static Map JavaDoc getRequestMap(URL JavaDoc xml) {
165         ControllerConfig controllerConfig = getControllerConfig(xml);
166         return controllerConfig != null ? controllerConfig.requestMap : null;
167     }
168
169     /** Gets a FastMap of request mappings. */
170     public static Map JavaDoc loadRequestMap(Element JavaDoc root, URL JavaDoc xml) {
171         long startTime = System.currentTimeMillis();
172         FastMap map = FastMap.newInstance();
173         if (root == null) {
174             root = loadDocument(xml);
175         }
176
177         if (root == null) return map;
178
179         List JavaDoc includeElementList = UtilXml.childElementList(root, INCLUDE);
180         Iterator JavaDoc includeElementIter = includeElementList.iterator();
181         while (includeElementIter.hasNext()) {
182             Element JavaDoc includeElement = (Element JavaDoc) includeElementIter.next();
183             String JavaDoc includeFile = includeElement.getAttribute(INCLUDE_FILE);
184
185             if ((includeFile != null) && (includeFile.length() > 0)) {
186                 File JavaDoc oldFile = new File JavaDoc(xml.getFile());
187                 File JavaDoc newFile = new java.io.File JavaDoc("" + oldFile.getParent() + java.io.File.separator + includeFile);
188
189                 try {
190                     Map JavaDoc subMap = loadRequestMap(null, newFile.toURL());
191
192                     map.putAll(subMap);
193                 } catch (MalformedURLException JavaDoc mue) {
194                     mue.printStackTrace();
195                 }
196             }
197
198             String JavaDoc includeURL = includeElement.getAttribute(INCLUDE_URL);
199             if ((includeURL != null) && (includeURL.length() > 0)) {
200                 try {
201                     Map JavaDoc subMap = loadRequestMap(null, new URL JavaDoc(includeURL));
202                     map.putAll(subMap);
203                 } catch (MalformedURLException JavaDoc mue) {
204                     mue.printStackTrace();
205                 }
206             }
207         }
208
209         List JavaDoc requestMapElementList = UtilXml.childElementList(root, REQUEST_MAPPING);
210         Iterator JavaDoc requestMapElementIter = requestMapElementList.iterator();
211         while (requestMapElementIter.hasNext()) {
212             Element JavaDoc requestMapElement = (Element JavaDoc) requestMapElementIter.next();
213             
214             // Create a URI-MAP for each element found.
215
FastMap uriMap = FastMap.newInstance();
216
217             // Get the URI info.
218
String JavaDoc uri = requestMapElement.getAttribute(REQUEST_URI);
219             String JavaDoc edit = requestMapElement.getAttribute(REQUEST_EDIT);
220
221             if (edit == null || edit.equals(""))
222                 edit = "true";
223             if (uri != null) {
224                 uriMap.put(REQUEST_URI, uri);
225                 uriMap.put(REQUEST_EDIT, edit);
226             }
227
228             // Check for security.
229
Element JavaDoc securityElement = UtilXml.firstChildElement(requestMapElement, SECURITY);
230             if (securityElement != null) {
231                 String JavaDoc securityHttps = securityElement.getAttribute(SECURITY_HTTPS);
232                 String JavaDoc securityAuth = securityElement.getAttribute(SECURITY_AUTH);
233                 String JavaDoc securityExtView = securityElement.getAttribute(SECURITY_EXTVIEW);
234                 String JavaDoc securityDirectRequest = securityElement.getAttribute(SECURITY_DIRECT);
235                 uriMap.put(SECURITY_HTTPS, securityHttps);
236                 uriMap.put(SECURITY_AUTH, securityAuth);
237                 uriMap.put(SECURITY_EXTVIEW, securityExtView);
238                 uriMap.put(SECURITY_DIRECT, securityDirectRequest);
239             }
240
241             // Check for an event.
242
Element JavaDoc eventElement = UtilXml.firstChildElement(requestMapElement, EVENT);
243             if (eventElement != null) {
244                 String JavaDoc type = eventElement.getAttribute(EVENT_TYPE);
245                 String JavaDoc path = eventElement.getAttribute(EVENT_PATH);
246                 String JavaDoc invoke = eventElement.getAttribute(EVENT_METHOD);
247
248                 uriMap.put(EVENT_TYPE, type);
249                 uriMap.put(EVENT_PATH, path);
250                 uriMap.put(EVENT_METHOD, invoke);
251                 
252                 // Check for a global-transaction attribute - default to true
253
uriMap.put(EVENT_GLOBAL_TRANSACTION, eventElement.hasAttribute(EVENT_GLOBAL_TRANSACTION) ? eventElement.getAttribute(EVENT_GLOBAL_TRANSACTION) : "true");
254             }
255
256             // Check for a description.
257
String JavaDoc description = UtilXml.childElementValue(requestMapElement, REQUEST_DESCRIPTION);
258             uriMap.put(REQUEST_DESCRIPTION, UtilValidate.isNotEmpty(description) ? description : "");
259
260             // Get the response(s).
261
List JavaDoc responseElementList = UtilXml.childElementList(requestMapElement, RESPONSE);
262             Iterator JavaDoc responseElementIter = responseElementList.iterator();
263             while (responseElementIter.hasNext()) {
264                 Element JavaDoc responseElement = (Element JavaDoc) responseElementIter.next();
265                 String JavaDoc name = responseElement.getAttribute(RESPONSE_NAME);
266                 String JavaDoc type = responseElement.getAttribute(RESPONSE_TYPE);
267                 String JavaDoc value = responseElement.getAttribute(RESPONSE_VALUE);
268                 uriMap.put(name, type + ":" + value);
269             }
270
271             if (uri != null) {
272                 map.put(uri, uriMap);
273             }
274         }
275
276         /* Debugging */
277         if (Debug.verboseOn()) {
278             Debug.logVerbose("-------- Request Mappings --------", module);
279             FastMap debugMap = map;
280             Set JavaDoc debugSet = debugMap.keySet();
281             Iterator JavaDoc i = debugSet.iterator();
282
283             while (i.hasNext()) {
284                 Object JavaDoc o = i.next();
285                 String JavaDoc request = (String JavaDoc) o;
286                 FastMap thisURI = (FastMap) debugMap.get(o);
287
288
289                 StringBuffer JavaDoc verboseMessageBuffer = verboseMessageBuffer = new StringBuffer JavaDoc();
290
291                 Iterator JavaDoc debugIter = ((Set JavaDoc) thisURI.keySet()).iterator();
292                 while (debugIter.hasNext()) {
293                     Object JavaDoc lo = debugIter.next();
294                     String JavaDoc name = (String JavaDoc) lo;
295                     String JavaDoc value = (String JavaDoc) thisURI.get(lo);
296
297                     verboseMessageBuffer.append("[" + name + "=>" + value + "]");
298                 }
299                 Debug.logVerbose(request + " :: " + verboseMessageBuffer.toString(), module);
300             }
301             Debug.logVerbose("------ End Request Mappings ------", module);
302         }
303         /* End Debugging */
304
305         double totalSeconds = (System.currentTimeMillis() - startTime)/1000.0;
306         if (Debug.infoOn()) Debug.logInfo("RequestMap Created: (" + map.size() + ") records in " + totalSeconds + "s", module);
307         return map;
308     }
309
310     /** Gets a FastMap of view mappings. */
311     public static Map JavaDoc getViewMap(URL JavaDoc xml) {
312         ControllerConfig controllerConfig = getControllerConfig(xml);
313         return controllerConfig != null ? controllerConfig.viewMap : null;
314     }
315
316     /** Gets a FastMap of view mappings. */
317     public static Map JavaDoc loadViewMap(Element JavaDoc root, URL JavaDoc xml) {
318         long startTime = System.currentTimeMillis();
319         FastMap map = FastMap.newInstance();
320         if (root == null) {
321             root = loadDocument(xml);
322         }
323
324         if (root == null) {
325             return map;
326         }
327
328         List JavaDoc includeElementList = UtilXml.childElementList(root, INCLUDE);
329         Iterator JavaDoc includeElementIter = includeElementList.iterator();
330         while (includeElementIter.hasNext()) {
331             Element JavaDoc includeElement = (Element JavaDoc) includeElementIter.next();
332             String JavaDoc includeFile = includeElement.getAttribute(INCLUDE_FILE);
333
334             if ((includeFile != null) && (includeFile.length() > 0)) {
335                 File JavaDoc oldFile = new File JavaDoc(xml.getFile());
336                 File JavaDoc newFile = new java.io.File JavaDoc("" + oldFile.getParent() + java.io.File.separator + includeFile);
337
338                 try {
339                     Map JavaDoc subMap = loadRequestMap(null, newFile.toURL());
340
341                     map.putAll(subMap);
342                 } catch (MalformedURLException JavaDoc mue) {
343                     mue.printStackTrace();
344                 }
345             }
346
347             String JavaDoc includeURL = includeElement.getAttribute(INCLUDE_URL);
348             if ((includeURL != null) && (includeURL.length() > 0)) {
349                 try {
350                     Map JavaDoc subMap = loadRequestMap(null, new URL JavaDoc(includeURL));
351                     map.putAll(subMap);
352                 } catch (MalformedURLException JavaDoc mue) {
353                     mue.printStackTrace();
354                 }
355             }
356         }
357
358         List JavaDoc viewMapElementList = UtilXml.childElementList(root, VIEW_MAPPING);
359         Iterator JavaDoc viewMapElementIter = viewMapElementList.iterator();
360         while (viewMapElementIter.hasNext()) {
361             Element JavaDoc viewMapElement = (Element JavaDoc) viewMapElementIter.next();
362             // Create a URI-MAP for each element found.
363
FastMap uriMap = FastMap.newInstance();
364
365             // Get the view info.
366
String JavaDoc name = viewMapElement.getAttribute(VIEW_NAME);
367             String JavaDoc page = viewMapElement.getAttribute(VIEW_PAGE);
368             if (page == null || page.length() == 0) {
369                 page = name;
370             }
371
372             uriMap.put(VIEW_NAME, name);
373             uriMap.put(VIEW_PAGE, page);
374             uriMap.put(VIEW_TYPE, viewMapElement.getAttribute(VIEW_TYPE));
375             uriMap.put(VIEW_INFO, viewMapElement.getAttribute(VIEW_INFO));
376             uriMap.put(VIEW_CONTENT_TYPE, viewMapElement.getAttribute(VIEW_CONTENT_TYPE));
377             uriMap.put(VIEW_ENCODING, viewMapElement.getAttribute(VIEW_ENCODING));
378
379             // Check for a description.
380
String JavaDoc description = UtilXml.childElementValue(viewMapElement, VIEW_DESCRIPTION);
381             uriMap.put(VIEW_DESCRIPTION, UtilValidate.isNotEmpty(description) ? description : "");
382
383             if (name != null) map.put(name, uriMap);
384         }
385
386         /* Debugging */
387         if (Debug.verboseOn()) {
388             Debug.logVerbose("-------- View Mappings --------", module);
389             FastMap debugMap = map;
390             Set JavaDoc debugSet = debugMap.keySet();
391             Iterator JavaDoc i = debugSet.iterator();
392     
393             while (i.hasNext()) {
394                 Object JavaDoc o = i.next();
395                 String JavaDoc request = (String JavaDoc) o;
396                 FastMap thisURI = (FastMap) debugMap.get(o);
397     
398                 StringBuffer JavaDoc verboseMessageBuffer = verboseMessageBuffer = new StringBuffer JavaDoc();
399     
400                 Iterator JavaDoc debugIter = ((Set JavaDoc) thisURI.keySet()).iterator();
401                 while (debugIter.hasNext()) {
402                     Object JavaDoc lo = debugIter.next();
403                     String JavaDoc name = (String JavaDoc) lo;
404                     String JavaDoc value = (String JavaDoc) thisURI.get(lo);
405     
406                     verboseMessageBuffer.append("[" + name + "=>" + value + "]");
407                 }
408                 Debug.logVerbose(request + " :: " + verboseMessageBuffer.toString(), module);
409             }
410             Debug.logVerbose("------ End View Mappings ------", module);
411         }
412         /* End Debugging */
413
414         double totalSeconds = (System.currentTimeMillis() - startTime)/1000.0;
415         if (Debug.infoOn()) Debug.logInfo("ViewMap Created: (" + map.size() + ") records in " + totalSeconds + "s", module);
416         return map;
417     }
418
419     /** Gets a FastMap of site configuration variables. */
420     public static Map JavaDoc getConfigMap(URL JavaDoc xml) {
421         ControllerConfig controllerConfig = getControllerConfig(xml);
422         return controllerConfig != null ? controllerConfig.configMap : null;
423     }
424
425     /** Gets a FastMap of site configuration variables. */
426     public static Map JavaDoc loadConfigMap(Element JavaDoc root, URL JavaDoc xml) {
427         long startTime = System.currentTimeMillis();
428         FastMap map = FastMap.newInstance();
429         if (root == null) {
430             root = loadDocument(xml);
431         }
432
433         if (root != null) {
434             // default error page
435
String JavaDoc errorpage = UtilXml.childElementValue(root, DEFAULT_ERROR_PAGE);
436             if (UtilValidate.isNotEmpty(errorpage)) map.put(DEFAULT_ERROR_PAGE, errorpage);
437
438             // site owner
439
String JavaDoc owner = UtilXml.childElementValue(root, SITE_OWNER);
440             if (UtilValidate.isNotEmpty(owner)) map.put(SITE_OWNER, owner);
441
442             // security class
443
String JavaDoc securityClass = UtilXml.childElementValue(root, SECURITY_CLASS);
444             if (UtilValidate.isNotEmpty(securityClass)) map.put(SECURITY_CLASS, securityClass);
445
446             // first visit event
447
Element JavaDoc firstvisitElement = UtilXml.firstChildElement(root, FIRSTVISIT);
448             if (firstvisitElement != null) {
449                 List JavaDoc eventList = FastList.newInstance();
450                 List JavaDoc eventElementList = UtilXml.childElementList(firstvisitElement, EVENT);
451                 Iterator JavaDoc eventElementIter = eventElementList.iterator();
452                 while (eventElementIter.hasNext()) {
453                     Element JavaDoc eventElement = (Element JavaDoc) eventElementIter.next();
454                     FastMap eventMap = FastMap.newInstance();
455                     eventMap.put(EVENT_TYPE, eventElement.getAttribute(EVENT_TYPE));
456                     eventMap.put(EVENT_PATH, eventElement.getAttribute(EVENT_PATH));
457                     eventMap.put(EVENT_METHOD, eventElement.getAttribute(EVENT_METHOD));
458                 
459                     // Check for a global-transaction attribute - default to true
460
eventMap.put(EVENT_GLOBAL_TRANSACTION, eventElement.hasAttribute(EVENT_GLOBAL_TRANSACTION) ? eventElement.getAttribute(EVENT_GLOBAL_TRANSACTION) : "true");
461                     eventList.add(eventMap);
462                 }
463                 map.put(FIRSTVISIT, eventList);
464             }
465
466             // preprocessor events
467
Element JavaDoc preprocessorElement = UtilXml.firstChildElement(root, PREPROCESSOR);
468             if (preprocessorElement != null) {
469                 List JavaDoc eventList = FastList.newInstance();
470                 List JavaDoc eventElementList = UtilXml.childElementList(preprocessorElement, EVENT);
471                 Iterator JavaDoc eventElementIter = eventElementList.iterator();
472                 while (eventElementIter.hasNext()) {
473                     Element JavaDoc eventElement = (Element JavaDoc) eventElementIter.next();
474                     FastMap eventMap = FastMap.newInstance();
475                     eventMap.put(EVENT_TYPE, eventElement.getAttribute(EVENT_TYPE));
476                     eventMap.put(EVENT_PATH, eventElement.getAttribute(EVENT_PATH));
477                     eventMap.put(EVENT_METHOD, eventElement.getAttribute(EVENT_METHOD));
478                 
479                     // Check for a global-transaction attribute - default to true
480
eventMap.put(EVENT_GLOBAL_TRANSACTION, eventElement.hasAttribute(EVENT_GLOBAL_TRANSACTION) ? eventElement.getAttribute(EVENT_GLOBAL_TRANSACTION) : "true");
481                     eventList.add(eventMap);
482                 }
483                 map.put(PREPROCESSOR, eventList);
484             }
485
486             // postprocessor events
487
Element JavaDoc postprocessorElement = UtilXml.firstChildElement(root, POSTPROCESSOR);
488             if (postprocessorElement != null) {
489                 List JavaDoc eventList = FastList.newInstance();
490                 List JavaDoc eventElementList = UtilXml.childElementList(postprocessorElement, EVENT);
491                 Iterator JavaDoc eventElementIter = eventElementList.iterator();
492                 while (eventElementIter.hasNext()) {
493                     Element JavaDoc eventElement = (Element JavaDoc) eventElementIter.next();
494                     FastMap eventMap = FastMap.newInstance();
495                     eventMap.put(EVENT_TYPE, eventElement.getAttribute(EVENT_TYPE));
496                     eventMap.put(EVENT_PATH, eventElement.getAttribute(EVENT_PATH));
497                     eventMap.put(EVENT_METHOD, eventElement.getAttribute(EVENT_METHOD));
498                 
499                     // Check for a global-transaction attribute - default to true
500
eventMap.put(EVENT_GLOBAL_TRANSACTION, eventElement.hasAttribute(EVENT_GLOBAL_TRANSACTION) ? eventElement.getAttribute(EVENT_GLOBAL_TRANSACTION) : "true");
501                     eventList.add(eventMap);
502                 }
503                 map.put(POSTPROCESSOR, eventList);
504             }
505
506             // after-login events
507
Element JavaDoc afterLoginElement = UtilXml.firstChildElement(root, "after-login");
508             if (afterLoginElement != null) {
509                 List JavaDoc eventList = FastList.newInstance();
510                 List JavaDoc eventElementList = UtilXml.childElementList(afterLoginElement, EVENT);
511                 Iterator JavaDoc eventElementIter = eventElementList.iterator();
512                 while (eventElementIter.hasNext()) {
513                     Element JavaDoc eventElement = (Element JavaDoc) eventElementIter.next();
514                     FastMap eventMap = FastMap.newInstance();
515                     eventMap.put(EVENT_TYPE, eventElement.getAttribute(EVENT_TYPE));
516                     eventMap.put(EVENT_PATH, eventElement.getAttribute(EVENT_PATH));
517                     eventMap.put(EVENT_METHOD, eventElement.getAttribute(EVENT_METHOD));
518                 
519                     // Check for a global-transaction attribute - default to true
520
eventMap.put(EVENT_GLOBAL_TRANSACTION, eventElement.hasAttribute(EVENT_GLOBAL_TRANSACTION) ? eventElement.getAttribute(EVENT_GLOBAL_TRANSACTION) : "true");
521                     eventList.add(eventMap);
522                 }
523                 map.put("after-login", eventList);
524             }
525
526             // before-logout events
527
Element JavaDoc beforeLogoutElement = UtilXml.firstChildElement(root, "before-logout");
528             if (beforeLogoutElement != null) {
529                 List JavaDoc eventList = FastList.newInstance();
530                 List JavaDoc eventElementList = UtilXml.childElementList(beforeLogoutElement, EVENT);
531                 Iterator JavaDoc eventElementIter = eventElementList.iterator();
532                 while (eventElementIter.hasNext()) {
533                     Element JavaDoc eventElement = (Element JavaDoc) eventElementIter.next();
534                     FastMap eventMap = FastMap.newInstance();
535                     eventMap.put(EVENT_TYPE, eventElement.getAttribute(EVENT_TYPE));
536                     eventMap.put(EVENT_PATH, eventElement.getAttribute(EVENT_PATH));
537                     eventMap.put(EVENT_METHOD, eventElement.getAttribute(EVENT_METHOD));
538                 
539                     // Check for a global-transaction attribute - default to true
540
eventMap.put(EVENT_GLOBAL_TRANSACTION, eventElement.hasAttribute(EVENT_GLOBAL_TRANSACTION) ? eventElement.getAttribute(EVENT_GLOBAL_TRANSACTION) : "true");
541                     eventList.add(eventMap);
542                 }
543                 map.put("before-logout", eventList);
544             }
545         }
546
547         /* Debugging */
548         /*
549          Debug.logVerbose("-------- Config Mappings --------", module);
550          FastMap debugMap = map;
551          Set debugSet = debugMap.keySet();
552          Iterator i = debugSet.iterator();
553          while (i.hasNext()) {
554          Object o = i.next();
555          String request = (String) o;
556          FastMap thisURI = (FastMap) debugMap.get(o);
557          Debug.logVerbose(request, module);
558          Iterator debugIter = ((Set) thisURI.keySet()).iterator();
559          while (debugIter.hasNext()) {
560          Object lo = debugIter.next();
561          String name = (String) lo;
562          String value = (String) thisURI.get(lo);
563          if (Debug.verboseOn()) Debug.logVerbose("\t" + name + " -> " + value, module);
564          }
565          }
566          Debug.logVerbose("------ End Config Mappings ------", module);
567          */

568         /* End Debugging */
569
570         double totalSeconds = (System.currentTimeMillis() - startTime)/1000.0;
571         if (Debug.infoOn()) Debug.logInfo("ConfigMap Created: (" + map.size() + ") records in " + totalSeconds + "s", module);
572         return map;
573     }
574
575     /** Gets a FastMap of handler mappings. */
576     public static Map JavaDoc getHandlerMap(URL JavaDoc xml) {
577         ControllerConfig controllerConfig = getControllerConfig(xml);
578         return controllerConfig != null ? controllerConfig.handlerMap : null;
579     }
580
581     public static Map JavaDoc loadHandlerMap(Element JavaDoc root, URL JavaDoc xml) {
582         long startTime = System.currentTimeMillis();
583         FastMap map = FastMap.newInstance();
584         if (root == null) {
585             root = loadDocument(xml);
586         }
587
588         if (root != null) {
589             Map JavaDoc rMap = FastMap.newInstance();
590             Map JavaDoc vMap = FastMap.newInstance();
591
592             List JavaDoc handlerElementList = UtilXml.childElementList(root, HANDLER);
593             Iterator JavaDoc handlerElementIter = handlerElementList.iterator();
594             while (handlerElementIter.hasNext()) {
595                 Element JavaDoc handlerElement = (Element JavaDoc) handlerElementIter.next();
596                 String JavaDoc hName = checkEmpty(handlerElement.getAttribute(HANDLER_NAME));
597                 String JavaDoc hClass = checkEmpty(handlerElement.getAttribute(HANDLER_CLASS));
598                 String JavaDoc hType = checkEmpty(handlerElement.getAttribute(HANDLER_TYPE));
599                 if (hType.equals("view")) {
600                     vMap.put(hName, hClass);
601                 } else {
602                     rMap.put(hName, hClass);
603                 }
604             }
605             map.put("view", vMap);
606             map.put("event", rMap);
607         }
608
609         /* Debugging */
610         if (Debug.verboseOn()) {
611             Debug.logVerbose("-------- Handler Mappings --------", module);
612             Map JavaDoc debugMap = (Map JavaDoc) map.get("event");
613
614             if (debugMap != null && debugMap.size() > 0) {
615                 Debug.logVerbose("-------------- EVENT -------------", module);
616                 Set JavaDoc debugSet = debugMap.keySet();
617                 Iterator JavaDoc i = debugSet.iterator();
618                 while (i.hasNext()) {
619                     Object JavaDoc o = i.next();
620                     String JavaDoc handlerName = (String JavaDoc) o;
621                     String JavaDoc className = (String JavaDoc) debugMap.get(o);
622                     Debug.logVerbose("[EH] : " + handlerName + " => " + className, module);
623                 }
624             }
625             debugMap = (Map JavaDoc) map.get("view");
626             if (debugMap != null && debugMap.size() > 0) {
627                 Debug.logVerbose("-------------- VIEW --------------", module);
628                 Set JavaDoc debugSet = debugMap.keySet();
629                 Iterator JavaDoc i = debugSet.iterator();
630                 while (i.hasNext()) {
631                     Object JavaDoc o = i.next();
632                     String JavaDoc handlerName = (String JavaDoc) o;
633                     String JavaDoc className = (String JavaDoc) debugMap.get(o);
634                     Debug.logVerbose("[VH] : " + handlerName + " => " + className, module);
635                 }
636             }
637             Debug.logVerbose("------ End Handler Mappings ------", module);
638         }
639
640         double totalSeconds = (System.currentTimeMillis() - startTime)/1000.0;
641         if (Debug.infoOn()) Debug.logInfo("HandlerMap Created: (" + map.size() + ") records in " + totalSeconds + "s", module);
642         return map;
643     }
644
645     private static String JavaDoc checkEmpty(String JavaDoc string) {
646         if (string != null && string.length() > 0)
647             return string;
648         else
649             return "";
650     }
651
652     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
653         /** Debugging */
654         if (args[0] == null) {
655             System.out.println("Please give a path to the config file you wish to test.");
656             return;
657         }
658         System.out.println("----------------------------------");
659         System.out.println("Request Mappings:");
660         System.out.println("----------------------------------");
661         Map JavaDoc debugMap = getRequestMap(new URL JavaDoc(args[0]));
662         Set JavaDoc debugSet = debugMap.keySet();
663         Iterator JavaDoc i = debugSet.iterator();
664         while (i.hasNext()) {
665             Object JavaDoc o = i.next();
666             String JavaDoc request = (String JavaDoc) o;
667             FastMap thisURI = (FastMap) debugMap.get(o);
668
669             System.out.println(request);
670             Iterator JavaDoc list = ((java.util.Set JavaDoc) thisURI.keySet()).iterator();
671             while (list.hasNext()) {
672                 Object JavaDoc lo = list.next();
673                 String JavaDoc name = (String JavaDoc) lo;
674                 String JavaDoc value = (String JavaDoc) thisURI.get(lo);
675                 System.out.println("\t" + name + " -> " + value);
676             }
677         }
678         System.out.println("----------------------------------");
679         System.out.println("End Request Mappings.");
680         System.out.println("----------------------------------");
681         /** End Debugging */
682     }
683 }
684
Popular Tags