001: /*
002: * $Id: ComponentLoaderConfig.java,v 1.5 2003/08/27 18:12:56 jonesde Exp $
003: *
004: * Copyright (c) 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: */
025: package org.ofbiz.base.component;
026:
027: import java.io.IOException;
028: import java.net.URL;
029: import java.util.Iterator;
030: import java.util.LinkedList;
031: import java.util.List;
032: import java.util.Properties;
033:
034: import javax.xml.parsers.ParserConfigurationException;
035:
036: import org.ofbiz.base.util.FlexibleStringExpander;
037: import org.ofbiz.base.util.UtilURL;
038: import org.ofbiz.base.util.UtilXml;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041: import org.xml.sax.SAXException;
042:
043: /**
044: * ComponentLoaderConfig - Component Loader configuration
045: *
046: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
047: * @version $Revision: 1.5 $
048: * @since 3.0
049: */
050: public class ComponentLoaderConfig {
051:
052: public static final String module = ComponentLoaderConfig.class
053: .getName();
054: public static final String COMPONENT_LOAD_XML_FILENAME = "component-load.xml";
055:
056: public static final int SINGLE_COMPONENT = 0;
057: public static final int COMPONENT_DIRECTORY = 1;
058:
059: protected static List componentsToLoad = null;
060:
061: public static List getComponentsToLoad(String configFile)
062: throws ComponentException {
063: if (componentsToLoad == null) {
064: synchronized (ComponentLoaderConfig.class) {
065: if (componentsToLoad == null) {
066: ComponentLoaderConfig.componentsToLoad = new LinkedList();
067: if (configFile == null) {
068: configFile = COMPONENT_LOAD_XML_FILENAME;
069: }
070:
071: URL xmlUrl = UtilURL.fromResource(configFile);
072: Document document = null;
073: try {
074: document = UtilXml
075: .readXmlDocument(xmlUrl, true);
076: } catch (SAXException e) {
077: throw new ComponentException(
078: "Error reading the component config file: "
079: + xmlUrl, e);
080: } catch (ParserConfigurationException e) {
081: throw new ComponentException(
082: "Error reading the component config file: "
083: + xmlUrl, e);
084: } catch (IOException e) {
085: throw new ComponentException(
086: "Error reading the component config file: "
087: + xmlUrl, e);
088: }
089:
090: Element root = document.getDocumentElement();
091: List toLoad = UtilXml.childElementList(root, null);
092: if (toLoad != null && toLoad.size() > 0) {
093: Iterator i = toLoad.iterator();
094: while (i.hasNext()) {
095: Element element = (Element) i.next();
096: componentsToLoad.add(new ComponentDef(
097: element));
098: }
099: }
100: }
101: }
102: }
103: return componentsToLoad;
104: }
105:
106: public static class ComponentDef {
107: public String name;
108: public String location;
109: public int type = -1;
110:
111: public ComponentDef(Element element) {
112: Properties systemProps = System.getProperties();
113: if ("load-component".equals(element.getNodeName())) {
114: name = element.getAttribute("component-name");
115: location = FlexibleStringExpander.expandString(element
116: .getAttribute("component-location"),
117: systemProps);
118: type = SINGLE_COMPONENT;
119: } else if ("load-components".equals(element.getNodeName())) {
120: name = null;
121: location = FlexibleStringExpander.expandString(element
122: .getAttribute("parent-directory"), systemProps);
123: type = COMPONENT_DIRECTORY;
124: }
125: }
126: }
127: }
|