001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.base.component;
019:
020: import java.io.IOException;
021: import java.net.URL;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Properties;
026:
027: import javax.xml.parsers.ParserConfigurationException;
028:
029: import org.ofbiz.base.util.UtilURL;
030: import org.ofbiz.base.util.UtilXml;
031: import org.ofbiz.base.util.string.FlexibleStringExpander;
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Element;
034: import org.xml.sax.SAXException;
035:
036: /**
037: * ComponentLoaderConfig - Component Loader configuration
038: *
039: */
040: public class ComponentLoaderConfig {
041:
042: public static final String module = ComponentLoaderConfig.class
043: .getName();
044: public static final String COMPONENT_LOAD_XML_FILENAME = "component-load.xml";
045:
046: public static final int SINGLE_COMPONENT = 0;
047: public static final int COMPONENT_DIRECTORY = 1;
048:
049: protected static List componentsToLoad = null;
050:
051: public static List getRootComponents(String configFile)
052: throws ComponentException {
053: if (componentsToLoad == null) {
054: synchronized (ComponentLoaderConfig.class) {
055: if (componentsToLoad == null) {
056: if (configFile == null) {
057: configFile = COMPONENT_LOAD_XML_FILENAME;
058: }
059: URL xmlUrl = UtilURL.fromResource(configFile);
060: ComponentLoaderConfig.componentsToLoad = ComponentLoaderConfig
061: .getComponentsFromConfig(xmlUrl);
062: }
063: }
064: }
065: return componentsToLoad;
066: }
067:
068: public static List getComponentsFromConfig(URL configUrl)
069: throws ComponentException {
070: if (configUrl == null) {
071: throw new ComponentException(
072: "Component config file does not exist: "
073: + configUrl);
074: }
075:
076: List componentsFromConfig = null;
077: Document document = null;
078: try {
079: document = UtilXml.readXmlDocument(configUrl, true);
080: } catch (SAXException e) {
081: throw new ComponentException(
082: "Error reading the component config file: "
083: + configUrl, e);
084: } catch (ParserConfigurationException e) {
085: throw new ComponentException(
086: "Error reading the component config file: "
087: + configUrl, e);
088: } catch (IOException e) {
089: throw new ComponentException(
090: "Error reading the component config file: "
091: + configUrl, e);
092: }
093:
094: Element root = document.getDocumentElement();
095: List toLoad = UtilXml.childElementList(root);
096: if (toLoad != null && toLoad.size() > 0) {
097: componentsFromConfig = new LinkedList();
098: Iterator i = toLoad.iterator();
099: while (i.hasNext()) {
100: Element element = (Element) i.next();
101: componentsFromConfig.add(new ComponentDef(element));
102: }
103: }
104: return componentsFromConfig;
105: }
106:
107: public static class ComponentDef {
108: public String name;
109: public String location;
110: public int type = -1;
111:
112: public ComponentDef(Element element) {
113: Properties systemProps = System.getProperties();
114: if ("load-component".equals(element.getNodeName())) {
115: name = element.getAttribute("component-name");
116: location = FlexibleStringExpander.expandString(element
117: .getAttribute("component-location"),
118: systemProps);
119: type = SINGLE_COMPONENT;
120: } else if ("load-components".equals(element.getNodeName())) {
121: name = null;
122: location = FlexibleStringExpander.expandString(element
123: .getAttribute("parent-directory"), systemProps);
124: type = COMPONENT_DIRECTORY;
125: }
126: }
127: }
128: }
|