001: /*
002: * $Id: ContainerConfig.java,v 1.5 2003/09/02 02:17:15 ajzeneski 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.container;
026:
027: import java.io.IOException;
028: import java.net.URL;
029: import java.util.Collection;
030: import java.util.Iterator;
031: import java.util.Map;
032:
033: import javax.xml.parsers.ParserConfigurationException;
034:
035: import org.ofbiz.base.util.OrderedMap;
036: import org.ofbiz.base.util.UtilURL;
037: import org.ofbiz.base.util.UtilXml;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Element;
040: import org.xml.sax.SAXException;
041:
042: /**
043: * ContainerConfig - Container configuration for ofbiz.xml
044: *
045: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
046: * @version $Revision: 1.5 $
047: * @since 3.0
048: */
049: public class ContainerConfig {
050:
051: public static final String module = ContainerConfig.class.getName();
052:
053: protected static Map containers = new OrderedMap();
054:
055: public static Container getContainer(String containerName,
056: String configFile) throws ContainerException {
057: Container container = (Container) containers.get(containerName);
058: if (container == null) {
059: synchronized (ContainerConfig.class) {
060: container = (Container) containers.get(containerName);
061: if (container == null) {
062: if (configFile == null) {
063: throw new ContainerException(
064: "Container config file cannot be null");
065: }
066: new ContainerConfig(configFile);
067: container = (Container) containers
068: .get(containerName);
069: }
070: }
071: if (container == null) {
072: throw new ContainerException(
073: "No container found with the name : "
074: + containerName);
075: }
076: }
077: return container;
078: }
079:
080: public static Collection getContainers(String configFile)
081: throws ContainerException {
082: if (containers.size() == 0) {
083: synchronized (ContainerConfig.class) {
084: if (containers.size() == 0) {
085: if (configFile == null) {
086: throw new ContainerException(
087: "Container config file cannot be null");
088: }
089: new ContainerConfig(configFile);
090: }
091: }
092: if (containers.size() == 0) {
093: throw new ContainerException(
094: "No contaners loaded; problem with configuration");
095: }
096: }
097: return containers.values();
098: }
099:
100: protected ContainerConfig() {
101: }
102:
103: protected ContainerConfig(String configFileLocation)
104: throws ContainerException {
105: // load the config file
106: URL xmlUrl = UtilURL.fromFilename(configFileLocation);
107: if (xmlUrl == null) {
108: throw new ContainerException("Could not find "
109: + configFileLocation
110: + " master OFBiz container configuration");
111: }
112:
113: // read the document
114: Document containerDocument = null;
115: try {
116: containerDocument = UtilXml.readXmlDocument(xmlUrl, true);
117: } catch (SAXException e) {
118: throw new ContainerException(
119: "Error reading the container config file: "
120: + xmlUrl, e);
121: } catch (ParserConfigurationException e) {
122: throw new ContainerException(
123: "Error reading the container config file: "
124: + xmlUrl, e);
125: } catch (IOException e) {
126: throw new ContainerException(
127: "Error reading the container config file: "
128: + xmlUrl, e);
129: }
130:
131: // root element
132: Element root = containerDocument.getDocumentElement();
133:
134: // containers
135: Iterator elementIter = UtilXml.childElementList(root,
136: "container").iterator();
137: while (elementIter.hasNext()) {
138: Element curElement = (Element) elementIter.next();
139: Container container = new Container(curElement);
140: containers.put(container.name, container);
141: }
142: }
143:
144: public static class Container {
145: public String name;
146: public String className;
147: public Map properties;
148:
149: public Container(Element element) {
150: this .name = element.getAttribute("name");
151: this .className = element.getAttribute("class");
152:
153: properties = new OrderedMap();
154: Iterator elementIter = UtilXml.childElementList(element,
155: "property").iterator();
156: while (elementIter.hasNext()) {
157: Element curElement = (Element) elementIter.next();
158: Property property = new Property(curElement);
159: properties.put(property.name, property);
160: }
161: }
162:
163: public Property getProperty(String name) {
164: return (Property) properties.get(name);
165: }
166:
167: public static class Property {
168: public String name;
169: public String value;
170: public Map properties;
171:
172: public Property(Element element) {
173: this .name = element.getAttribute("name");
174: this .value = element.getAttribute("value");
175:
176: properties = new OrderedMap();
177: Iterator elementIter = UtilXml.childElementList(
178: element, "property").iterator();
179: while (elementIter.hasNext()) {
180: Element curElement = (Element) elementIter.next();
181: Property property = new Property(curElement);
182: properties.put(property.name, property);
183: }
184: }
185:
186: public Property getProperty(String name) {
187: return (Property) properties.get(name);
188: }
189: }
190: }
191: }
|