001: /*
002: * $Id: ConfigXMLReader.java,v 1.2 2003/09/14 05:36:47 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.content.webapp.control;
025:
026: import java.io.File;
027: import java.net.MalformedURLException;
028: import java.net.URL;
029: import java.util.ArrayList;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.Set;
034:
035: import org.ofbiz.base.util.Debug;
036: import org.ofbiz.base.util.UtilCache;
037: import org.ofbiz.base.util.UtilXml;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Element;
040: import org.w3c.dom.Node;
041: import org.w3c.dom.NodeList;
042:
043: /**
044: * ConfigXMLReader.java - Reads and parses the XML site config files.
045: *
046: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
047: * @version $Revision: 1.2 $
048: * @since 2.0
049: */
050: public class ConfigXMLReader {
051:
052: public static final String module = ConfigXMLReader.class.getName();
053:
054: public static UtilCache requestCache = new UtilCache(
055: "webapp.ConfigXMLReader.Request");
056: public static UtilCache viewCache = new UtilCache(
057: "webapp.ConfigXMLReader.View");
058: public static UtilCache headCache = new UtilCache(
059: "webapp.ConfigXMLReader.Config");
060: public static UtilCache handlerCache = new UtilCache(
061: "webapp.ConfigXMLReader.Handler");
062:
063: /** Site Config Variables */
064: public static final String DEFAULT_ERROR_PAGE = "errorpage";
065: public static final String SITE_OWNER = "owner";
066: public static final String SECURITY_CLASS = "security-class";
067: public static final String FIRSTVISIT = "firstvisit";
068: public static final String PREPROCESSOR = "preprocessor";
069: public static final String POSTPROCESSOR = "postprocessor";
070:
071: /** URI Config Variables */
072: public static final String INCLUDE = "include";
073: public static final String INCLUDE_FILE = "file";
074: public static final String INCLUDE_URL = "url";
075:
076: public static final String REQUEST_MAPPING = "request-map";
077: public static final String REQUEST_URI = "uri";
078: public static final String REQUEST_EDIT = "edit";
079:
080: public static final String REQUEST_DESCRIPTION = "description";
081: public static final String ERROR_PAGE = "error";
082: public static final String NEXT_PAGE = "success";
083:
084: public static final String SECURITY = "security";
085: public static final String SECURITY_HTTPS = "https";
086: public static final String SECURITY_AUTH = "auth";
087: public static final String SECURITY_EXTVIEW = "external-view";
088: public static final String SECURITY_DIRECT = "direct-request";
089:
090: public static final String EVENT = "event";
091: public static final String EVENT_PATH = "path";
092: public static final String EVENT_TYPE = "type";
093: public static final String EVENT_METHOD = "invoke";
094:
095: public static final String RESPONSE = "response";
096: public static final String RESPONSE_NAME = "name";
097: public static final String RESPONSE_TYPE = "type";
098: public static final String RESPONSE_VALUE = "value";
099:
100: /** View Config Variables */
101: public static final String VIEW_MAPPING = "view-map";
102: public static final String VIEW_NAME = "name";
103: public static final String VIEW_PAGE = "page";
104: public static final String VIEW_TYPE = "type";
105: public static final String VIEW_INFO = "info";
106: public static final String VIEW_CONTENT_TYPE = "content-type";
107: public static final String VIEW_ENCODING = "encoding";
108: public static final String VIEW_DESCRIPTION = "description";
109:
110: /** Handler Config Variables */
111: public static final String HANDLER = "handler";
112: public static final String HANDLER_NAME = "name";
113: public static final String HANDLER_TYPE = "type";
114: public static final String HANDLER_CLASS = "class";
115:
116: /** Loads the XML file and returns the root element */
117: public static Element loadDocument(URL location) {
118: Document document = null;
119:
120: try {
121: document = UtilXml.readXmlDocument(location, true);
122:
123: Element rootElement = document.getDocumentElement();
124:
125: // rootElement.normalize();
126: if (Debug.verboseOn())
127: Debug.logVerbose("Loaded XML Config - " + location,
128: module);
129: return rootElement;
130: } catch (Exception e) {
131: Debug.logError(e, module);
132: }
133:
134: return null;
135: }
136:
137: /** Gets a HashMap of request mappings. */
138: public static HashMap getRequestMap(URL xml) {
139: HashMap requestMap = (HashMap) requestCache.get(xml);
140:
141: if (requestMap == null) // don't want to block here
142: {
143: synchronized (ConfigXMLReader.class) {
144: // must check if null again as one of the blocked threads can still enter
145: requestMap = (HashMap) requestCache.get(xml);
146: if (requestMap == null) {
147: requestMap = loadRequestMap(xml);
148: requestCache.put(xml, requestMap);
149: }
150: }
151: }
152: // never return null, just an empty map...
153: if (requestMap == null)
154: requestMap = new HashMap();
155: return requestMap;
156: }
157:
158: /** Gets a HashMap of request mappings. */
159: public static HashMap loadRequestMap(URL xml) {
160: HashMap map = new HashMap();
161: Element root = loadDocument(xml);
162:
163: if (root == null)
164: return map;
165:
166: NodeList list = root.getElementsByTagName(INCLUDE);
167:
168: for (int rootCount = 0; rootCount < list.getLength(); rootCount++) {
169: Node node = list.item(rootCount);
170:
171: // Make sure we are an element.
172: if (node instanceof Element) {
173: // Get the file to include
174: Element mapping = (Element) node;
175: String includeFile = mapping.getAttribute(INCLUDE_FILE);
176:
177: if ((includeFile != null) && (includeFile.length() > 0)) {
178: File oldFile = new File(xml.getFile());
179: File newFile = new java.io.File(""
180: + oldFile.getParent()
181: + java.io.File.separator + includeFile);
182:
183: try {
184: HashMap subMap = loadRequestMap(newFile.toURL());
185:
186: map.putAll(subMap);
187: } catch (MalformedURLException mue) {
188: mue.printStackTrace();
189: }
190: }
191:
192: String includeURL = mapping.getAttribute(INCLUDE_URL);
193:
194: if ((includeURL != null) && (includeURL.length() > 0)) {
195: try {
196: HashMap subMap = loadRequestMap(new URL(
197: includeURL));
198:
199: map.putAll(subMap);
200: } catch (MalformedURLException mue) {
201: mue.printStackTrace();
202: }
203: }
204:
205: }
206: }
207:
208: list = root.getElementsByTagName(REQUEST_MAPPING);
209: for (int rootCount = 0; rootCount < list.getLength(); rootCount++) {
210: // Create a URI-MAP for each element found.
211: HashMap uriMap = new HashMap();
212: // Get the node.
213: Node node = list.item(rootCount);
214:
215: // Make sure we are an element.
216: if (node instanceof Element) {
217: // Get the URI info.
218: Element mapping = (Element) node;
219: String uri = mapping.getAttribute(REQUEST_URI);
220: String edit = mapping.getAttribute(REQUEST_EDIT);
221:
222: if (edit == null || edit.equals(""))
223: edit = "true";
224: if (uri != null) {
225: uriMap.put(REQUEST_URI, uri);
226: uriMap.put(REQUEST_EDIT, edit);
227: }
228:
229: // Check for security.
230: NodeList securityList = mapping
231: .getElementsByTagName(SECURITY);
232:
233: if (securityList.getLength() > 0) {
234: Node securityNode = securityList.item(0); // There should be only one.
235:
236: if (securityNode instanceof Element) { // We must be an element.
237: Element security = (Element) securityNode;
238: String securityHttps = security
239: .getAttribute(SECURITY_HTTPS);
240: String securityAuth = security
241: .getAttribute(SECURITY_AUTH);
242: String securityExtView = security
243: .getAttribute(SECURITY_EXTVIEW);
244: String securityDirectRequest = security
245: .getAttribute(SECURITY_DIRECT);
246:
247: uriMap.put(SECURITY_HTTPS, securityHttps);
248: uriMap.put(SECURITY_AUTH, securityAuth);
249: uriMap.put(SECURITY_EXTVIEW, securityExtView);
250: uriMap.put(SECURITY_DIRECT,
251: securityDirectRequest);
252: }
253: }
254:
255: // Check for an event.
256: NodeList eventList = mapping
257: .getElementsByTagName(EVENT);
258:
259: if (eventList.getLength() > 0) {
260: Node eventNode = eventList.item(0); // There should be only one.
261:
262: if (eventNode instanceof Element) { // We must be an element.
263: Element event = (Element) eventNode;
264: String type = event.getAttribute(EVENT_TYPE);
265: String path = event.getAttribute(EVENT_PATH);
266: String invoke = event
267: .getAttribute(EVENT_METHOD);
268:
269: uriMap.put(EVENT_TYPE, type);
270: uriMap.put(EVENT_PATH, path);
271: uriMap.put(EVENT_METHOD, invoke);
272: }
273: }
274:
275: // Check for a description.
276: NodeList descList = mapping
277: .getElementsByTagName(REQUEST_DESCRIPTION);
278:
279: if (descList.getLength() > 0) {
280: Node descNode = descList.item(0); // There should be only one.
281:
282: if (descNode instanceof Element) { // We must be an element.
283: NodeList children = descNode.getChildNodes();
284:
285: if (children.getLength() > 0) {
286: Node cdata = children.item(0); // Just get the first one.
287: String description = cdata.getNodeValue();
288:
289: if (description != null)
290: description = description.trim();
291: else
292: description = "";
293: uriMap
294: .put(REQUEST_DESCRIPTION,
295: description);
296: }
297: }
298: } else {
299: uriMap.put(REQUEST_DESCRIPTION, "");
300: }
301:
302: // Get the response(s).
303: NodeList respList = mapping
304: .getElementsByTagName(RESPONSE);
305:
306: for (int respCount = 0; respCount < respList
307: .getLength(); respCount++) {
308: Node responseNode = respList.item(respCount);
309:
310: if (responseNode instanceof Element) {
311: Element response = (Element) responseNode;
312: String name = response
313: .getAttribute(RESPONSE_NAME);
314: String type = response
315: .getAttribute(RESPONSE_TYPE);
316: String value = response
317: .getAttribute(RESPONSE_VALUE);
318:
319: uriMap.put(name, type + ":" + value);
320: }
321: }
322:
323: if (uri != null)
324: map.put(uri, uriMap);
325: }
326:
327: }
328:
329: /* Debugging */
330: if (Debug.verboseOn())
331: Debug.logVerbose("-------- Request Mappings --------",
332: module);
333: HashMap debugMap = map;
334: Set debugSet = debugMap.keySet();
335: Iterator i = debugSet.iterator();
336:
337: while (i.hasNext()) {
338: Object o = i.next();
339: String request = (String) o;
340: HashMap this URI = (HashMap) debugMap.get(o);
341:
342: if (Debug.verboseOn())
343: Debug.logVerbose(request, module);
344: Iterator debugIter = ((Set) this URI.keySet()).iterator();
345:
346: while (debugIter.hasNext()) {
347: Object lo = debugIter.next();
348: String name = (String) lo;
349: String value = (String) this URI.get(lo);
350:
351: if (Debug.verboseOn())
352: Debug.logVerbose("\t" + name + " -> " + value,
353: module);
354: }
355: }
356: if (Debug.verboseOn())
357: Debug.logVerbose("------ End Request Mappings ------",
358: module);
359:
360: /* End Debugging */
361:
362: if (Debug.infoOn())
363: Debug.logInfo("RequestMap Created: (" + map.size()
364: + ") records.", module);
365: return map;
366: }
367:
368: /** Gets a HashMap of view mappings. */
369: public static Map getViewMap(URL xml) {
370: Map viewMap = (Map) viewCache.get(xml);
371:
372: if (viewMap == null) // don't want to block here
373: {
374: synchronized (ConfigXMLReader.class) {
375: // must check if null again as one of the blocked threads can still enter
376: viewMap = (HashMap) viewCache.get(xml);
377: if (viewMap == null) {
378: viewMap = loadViewMap(xml);
379: viewCache.put(xml, viewMap);
380: }
381: }
382: }
383: // never return null, just an empty map...
384: if (viewMap == null)
385: viewMap = new HashMap();
386: return viewMap;
387: }
388:
389: /** Gets a HashMap of view mappings. */
390: public static Map loadViewMap(URL xml) {
391: HashMap map = new HashMap();
392: Element root = loadDocument(xml);
393:
394: if (root == null)
395: return map;
396:
397: NodeList list = root.getElementsByTagName(INCLUDE);
398:
399: for (int rootCount = 0; rootCount < list.getLength(); rootCount++) {
400: Node node = list.item(rootCount);
401:
402: // Make sure we are an element.
403: if (node instanceof Element) {
404: // Get the file to include
405: Element mapping = (Element) node;
406: String includeFile = mapping.getAttribute(INCLUDE_FILE);
407:
408: if ((includeFile != null) && (includeFile.length() > 0)) {
409: File oldFile = new File(xml.getFile());
410: File newFile = new java.io.File(""
411: + oldFile.getParent()
412: + java.io.File.separator + includeFile);
413:
414: try {
415: Map subMap = loadViewMap(newFile.toURL());
416:
417: map.putAll(subMap);
418: } catch (MalformedURLException mue) {
419: mue.printStackTrace();
420: }
421: }
422:
423: String includeURL = mapping.getAttribute(INCLUDE_URL);
424:
425: if ((includeURL != null) && (includeURL.length() > 0)) {
426: try {
427: Map subMap = loadViewMap(new URL(includeURL));
428:
429: map.putAll(subMap);
430: } catch (MalformedURLException mue) {
431: mue.printStackTrace();
432: }
433: }
434:
435: }
436: }
437:
438: list = root.getElementsByTagName(VIEW_MAPPING);
439: for (int rootCount = 0; rootCount < list.getLength(); rootCount++) {
440: // Create a URI-MAP for each element found.
441: HashMap uriMap = new HashMap();
442: // Get the node.
443: Node node = list.item(rootCount);
444:
445: // Make sure we are an element.
446: if (node instanceof Element) {
447: // Get the view info.
448: Element mapping = (Element) node;
449: String name = mapping.getAttribute(VIEW_NAME);
450: String page = mapping.getAttribute(VIEW_PAGE);
451: String type = mapping.getAttribute(VIEW_TYPE);
452:
453: if (page == null || page.length() == 0) {
454: page = name;
455: }
456:
457: uriMap.put(VIEW_NAME, name);
458: uriMap.put(VIEW_PAGE, page);
459: uriMap.put(VIEW_TYPE, type);
460: uriMap.put(VIEW_INFO, mapping.getAttribute(VIEW_INFO));
461: uriMap.put(VIEW_CONTENT_TYPE, mapping
462: .getAttribute(VIEW_CONTENT_TYPE));
463: uriMap.put(VIEW_ENCODING, mapping
464: .getAttribute(VIEW_ENCODING));
465:
466: // Check for a description.
467: NodeList descList = mapping
468: .getElementsByTagName(VIEW_DESCRIPTION);
469:
470: if (descList.getLength() > 0) {
471: Node descNode = descList.item(0); // There should be only one.
472:
473: if (descNode instanceof Element) { // We must be an element.
474: NodeList children = descNode.getChildNodes();
475:
476: if (children.getLength() > 0) {
477: Node cdata = children.item(0); // Just get the first one.
478: String description = cdata.getNodeValue();
479:
480: if (description != null)
481: description = description.trim();
482: else
483: description = "";
484: uriMap.put(VIEW_DESCRIPTION, description);
485: }
486: }
487: } else {
488: uriMap.put(VIEW_DESCRIPTION, "");
489: }
490:
491: if (name != null)
492: map.put(name, uriMap);
493: }
494: }
495:
496: /* Debugging */
497: Debug.logVerbose("-------- View Mappings --------", module);
498: HashMap debugMap = map;
499: Set debugSet = debugMap.keySet();
500: Iterator i = debugSet.iterator();
501:
502: while (i.hasNext()) {
503: Object o = i.next();
504: String request = (String) o;
505: HashMap this URI = (HashMap) debugMap.get(o);
506:
507: Debug.logVerbose(request, module);
508: Iterator debugIter = ((Set) this URI.keySet()).iterator();
509:
510: while (debugIter.hasNext()) {
511: Object lo = debugIter.next();
512: String name = (String) lo;
513: String value = (String) this URI.get(lo);
514:
515: if (Debug.verboseOn())
516: Debug.logVerbose("\t" + name + " -> " + value,
517: module);
518: }
519: }
520: Debug.logVerbose("------ End View Mappings ------", module);
521:
522: /* End Debugging */
523:
524: if (Debug.infoOn())
525: Debug.logInfo("ViewMap Created: (" + map.size()
526: + ") records.", module);
527: return map;
528: }
529:
530: /** Gets a HashMap of site configuration variables. */
531: public static Map getConfigMap(URL xml) {
532: Map configMap = (Map) headCache.get(xml);
533:
534: if (configMap == null) // don't want to block here
535: {
536: synchronized (ConfigXMLReader.class) {
537: // must check if null again as one of the blocked threads can still enter
538: configMap = (HashMap) headCache.get(xml);
539: if (configMap == null) {
540: configMap = loadConfigMap(xml);
541: headCache.put(xml, configMap);
542: }
543: }
544: }
545: // never return null, just an empty map...
546: if (configMap == null)
547: configMap = new HashMap();
548: return configMap;
549: }
550:
551: /** Gets a HashMap of site configuration variables. */
552: public static Map loadConfigMap(URL xml) {
553: HashMap map = new HashMap();
554: Element root = loadDocument(xml);
555: NodeList list = null;
556:
557: if (root != null) {
558: // default error page
559: list = root.getElementsByTagName(DEFAULT_ERROR_PAGE);
560: if (list.getLength() > 0) {
561: Node node = list.item(0);
562: NodeList children = node.getChildNodes();
563: Node child = children.item(0);
564:
565: if (child.getNodeName() != null)
566: map.put(DEFAULT_ERROR_PAGE, child.getNodeValue());
567: }
568: list = null;
569: // site owner
570: list = root.getElementsByTagName(SITE_OWNER);
571: if (list.getLength() > 0) {
572: Node node = list.item(0);
573: NodeList children = node.getChildNodes();
574: Node child = children.item(0);
575:
576: if (child.getNodeName() != null)
577: map.put(SITE_OWNER, child.getNodeValue());
578: }
579: list = null;
580: // security class
581: list = root.getElementsByTagName(SECURITY_CLASS);
582: if (list.getLength() > 0) {
583: Node node = list.item(0);
584: NodeList children = node.getChildNodes();
585: Node child = children.item(0);
586:
587: if (child.getNodeName() != null)
588: map.put(SECURITY_CLASS, child.getNodeValue());
589: }
590: list = null;
591: // first visit events
592: list = root.getElementsByTagName(FIRSTVISIT);
593: if (list.getLength() > 0) {
594: ArrayList eventList = new ArrayList();
595: Node node = list.item(0);
596:
597: if (node instanceof Element) {
598: Element nodeElement = (Element) node;
599: NodeList procEvents = nodeElement
600: .getElementsByTagName(EVENT);
601:
602: for (int procCount = 0; procCount < procEvents
603: .getLength(); procCount++) {
604: Node eventNode = procEvents.item(procCount);
605:
606: if (eventNode instanceof Element) {
607: Element event = (Element) eventNode;
608: String type = event
609: .getAttribute(EVENT_TYPE);
610: String path = event
611: .getAttribute(EVENT_PATH);
612: String invoke = event
613: .getAttribute(EVENT_METHOD);
614:
615: HashMap eventMap = new HashMap();
616:
617: eventMap.put(EVENT_TYPE, type);
618: eventMap.put(EVENT_PATH, path);
619: eventMap.put(EVENT_METHOD, invoke);
620: eventList.add(eventMap);
621: }
622: }
623: }
624: map.put(FIRSTVISIT, eventList);
625: }
626: list = null;
627: // preprocessor events
628: list = root.getElementsByTagName(PREPROCESSOR);
629: if (list.getLength() > 0) {
630: ArrayList eventList = new ArrayList();
631: Node node = list.item(0);
632:
633: if (node instanceof Element) {
634: Element nodeElement = (Element) node;
635: NodeList procEvents = nodeElement
636: .getElementsByTagName(EVENT);
637:
638: for (int procCount = 0; procCount < procEvents
639: .getLength(); procCount++) {
640: Node eventNode = procEvents.item(procCount);
641:
642: if (eventNode instanceof Element) {
643: Element event = (Element) eventNode;
644: String type = event
645: .getAttribute(EVENT_TYPE);
646: String path = event
647: .getAttribute(EVENT_PATH);
648: String invoke = event
649: .getAttribute(EVENT_METHOD);
650:
651: HashMap eventMap = new HashMap();
652:
653: eventMap.put(EVENT_TYPE, type);
654: eventMap.put(EVENT_PATH, path);
655: eventMap.put(EVENT_METHOD, invoke);
656: eventList.add(eventMap);
657: }
658: }
659: }
660: map.put(PREPROCESSOR, eventList);
661: }
662: list = null;
663: // postprocessor events
664: list = root.getElementsByTagName(POSTPROCESSOR);
665: if (list.getLength() > 0) {
666: ArrayList eventList = new ArrayList();
667: Node node = list.item(0);
668:
669: if (node instanceof Element) {
670: Element nodeElement = (Element) node;
671: NodeList procEvents = nodeElement
672: .getElementsByTagName(EVENT);
673:
674: for (int procCount = 0; procCount < procEvents
675: .getLength(); procCount++) {
676: Node eventNode = procEvents.item(procCount);
677:
678: if (eventNode instanceof Element) {
679: Element event = (Element) eventNode;
680: String type = event
681: .getAttribute(EVENT_TYPE);
682: String path = event
683: .getAttribute(EVENT_PATH);
684: String invoke = event
685: .getAttribute(EVENT_METHOD);
686:
687: HashMap eventMap = new HashMap();
688:
689: eventMap.put(EVENT_TYPE, type);
690: eventMap.put(EVENT_PATH, path);
691: eventMap.put(EVENT_METHOD, invoke);
692: eventList.add(eventMap);
693: }
694: }
695: }
696: map.put(POSTPROCESSOR, eventList);
697: }
698: list = null;
699: }
700:
701: /* Debugging */
702:
703: /*
704: Debug.logVerbose("-------- Config Mappings --------", module);
705: HashMap debugMap = map;
706: Set debugSet = debugMap.keySet();
707: Iterator i = debugSet.iterator();
708: while (i.hasNext()) {
709: Object o = i.next();
710: String request = (String) o;
711: HashMap thisURI = (HashMap) debugMap.get(o);
712: Debug.logVerbose(request, module);
713: Iterator debugIter = ((Set) thisURI.keySet()).iterator();
714: while (debugIter.hasNext()) {
715: Object lo = debugIter.next();
716: String name = (String) lo;
717: String value = (String) thisURI.get(lo);
718: if (Debug.verboseOn()) Debug.logVerbose("\t" + name + " -> " + value, module);
719: }
720: }
721: Debug.logVerbose("------ End Config Mappings ------", module);
722: */
723:
724: /* End Debugging */
725:
726: if (Debug.infoOn())
727: Debug.logInfo("ConfigMap Created: (" + map.size()
728: + ") records.", module);
729: return map;
730: }
731:
732: /** Gets a HashMap of handler mappings. */
733: public static Map getHandlerMap(URL xml) {
734: Map handlerMap = (Map) handlerCache.get(xml);
735:
736: if (handlerMap == null) // don't want to block here
737: {
738: synchronized (ConfigXMLReader.class) {
739: // must check if null again as one of the blocked threads can still enter
740: handlerMap = (HashMap) handlerCache.get(xml);
741: if (handlerMap == null) {
742: handlerMap = loadHandlerMap(xml);
743: handlerCache.put(xml, handlerMap);
744: }
745: }
746: }
747: // never return null, just an empty map...
748: if (handlerMap == null)
749: handlerMap = new HashMap();
750: return handlerMap;
751: }
752:
753: public static Map loadHandlerMap(URL xml) {
754: HashMap map = new HashMap();
755: Element root = loadDocument(xml);
756: NodeList list = null;
757:
758: if (root != null) {
759: Map rMap = new HashMap();
760: Map vMap = new HashMap();
761:
762: list = root.getElementsByTagName(HANDLER);
763: for (int i = 0; i < list.getLength(); i++) {
764: Element handler = (Element) list.item(i);
765: String hName = checkEmpty(handler
766: .getAttribute(HANDLER_NAME));
767: String hClass = checkEmpty(handler
768: .getAttribute(HANDLER_CLASS));
769: String hType = checkEmpty(handler
770: .getAttribute(HANDLER_TYPE));
771:
772: if (hType.equals("view"))
773: vMap.put(hName, hClass);
774: else
775: rMap.put(hName, hClass);
776: }
777: map.put("view", vMap);
778: map.put("event", rMap);
779: }
780:
781: /* Debugging */
782: Debug.logVerbose("-------- Handler Mappings --------", module);
783: Map debugMap = (Map) map.get("event");
784:
785: if (debugMap != null && debugMap.size() > 0) {
786: Debug.logVerbose("-------------- EVENT -------------",
787: module);
788: Set debugSet = debugMap.keySet();
789: Iterator i = debugSet.iterator();
790:
791: while (i.hasNext()) {
792: Object o = i.next();
793: String handlerName = (String) o;
794: String className = (String) debugMap.get(o);
795:
796: if (Debug.verboseOn())
797: Debug.logVerbose("[EH] : " + handlerName + " => "
798: + className, module);
799: }
800: }
801: debugMap = (Map) map.get("view");
802: if (debugMap != null && debugMap.size() > 0) {
803: Debug.logVerbose("-------------- VIEW --------------",
804: module);
805: Set debugSet = debugMap.keySet();
806: Iterator i = debugSet.iterator();
807:
808: while (i.hasNext()) {
809: Object o = i.next();
810: String handlerName = (String) o;
811: String className = (String) debugMap.get(o);
812:
813: if (Debug.verboseOn())
814: Debug.logVerbose("[VH] : " + handlerName + " => "
815: + className, module);
816: }
817: }
818: Debug.logVerbose("------ End Handler Mappings ------", module);
819:
820: /* End Debugging */
821:
822: if (Debug.infoOn())
823: Debug.logInfo("HandlerMap Created: (" + map.size()
824: + ") records.", module);
825: return map;
826: }
827:
828: private static String checkEmpty(String string) {
829: if (string != null && string.length() > 0)
830: return string;
831: else
832: return "";
833: }
834:
835: /** Not used right now */
836: public static String getSubTagValue(Node node, String subTagName) {
837: String returnString = "";
838:
839: if (node != null) {
840: NodeList children = node.getChildNodes();
841:
842: for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
843: Node child = children.item(innerLoop);
844:
845: if ((child != null) && (child.getNodeName() != null)
846: && child.getNodeName().equals(subTagName)) {
847: Node grandChild = child.getFirstChild();
848:
849: if (grandChild.getNodeValue() != null)
850: return grandChild.getNodeValue();
851: }
852: }
853: }
854: return returnString;
855: }
856:
857: public static void main(String args[]) throws Exception {
858:
859: /** Debugging */
860: if (args[0] == null) {
861: System.out
862: .println("Please give a path to the config file you wish to test.");
863: return;
864: }
865: System.out.println("----------------------------------");
866: System.out.println("Request Mappings:");
867: System.out.println("----------------------------------");
868: java.util.HashMap debugMap = getRequestMap(new URL(args[0]));
869: java.util.Set debugSet = debugMap.keySet();
870: java.util.Iterator i = debugSet.iterator();
871:
872: while (i.hasNext()) {
873: Object o = i.next();
874: String request = (String) o;
875: HashMap this URI = (java.util.HashMap) debugMap.get(o);
876:
877: System.out.println(request);
878: java.util.Iterator list = ((java.util.Set) this URI.keySet())
879: .iterator();
880:
881: while (list.hasNext()) {
882: Object lo = list.next();
883: String name = (String) lo;
884: String value = (String) this URI.get(lo);
885:
886: System.out.println("\t" + name + " -> " + value);
887: }
888: }
889: System.out.println("----------------------------------");
890: System.out.println("End Request Mappings.");
891: System.out.println("----------------------------------");
892:
893: /** End Debugging */
894: }
895:
896: }
|