KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > container > ContainerConfig


1 /*
2  * $Id: ContainerConfig.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 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  */

25 package org.ofbiz.base.container;
26
27 import java.io.IOException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35
36 import org.ofbiz.base.util.UtilURL;
37 import org.ofbiz.base.util.UtilValidate;
38 import org.ofbiz.base.util.UtilXml;
39
40 import org.apache.commons.collections.map.LinkedMap;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Element JavaDoc;
43 import org.xml.sax.SAXException JavaDoc;
44
45 /**
46  * ContainerConfig - Container configuration for ofbiz.xml
47  *
48  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
49  * @version $Rev: 5462 $
50  * @since 3.0
51  */

52 public class ContainerConfig {
53     
54     public static final String JavaDoc module = ContainerConfig.class.getName();
55     
56     protected static Map JavaDoc containers = new LinkedMap();
57     
58     public static Container getContainer(String JavaDoc containerName, String JavaDoc configFile) throws ContainerException {
59         Container container = (Container) containers.get(containerName);
60         if (container == null) {
61             synchronized (ContainerConfig.class) {
62                 container = (Container) containers.get(containerName);
63                 if (container == null) {
64                     if (configFile == null) {
65                         throw new ContainerException("Container config file cannot be null");
66                     }
67                     new ContainerConfig(configFile);
68                     container = (Container) containers.get(containerName);
69                 }
70             }
71             if (container == null) {
72                 throw new ContainerException("No container found with the name : " + containerName);
73             }
74         }
75         return container;
76     }
77     
78     public static Collection JavaDoc getContainers(String JavaDoc configFile) throws ContainerException {
79         if (containers.size() == 0) {
80             synchronized (ContainerConfig.class) {
81                 if (containers.size() == 0) {
82                     if (configFile == null) {
83                         throw new ContainerException("Container config file cannot be null");
84                     }
85                     new ContainerConfig(configFile);
86                 }
87             }
88             if (containers.size() == 0) {
89                 throw new ContainerException("No containers loaded; problem with configuration");
90             }
91         }
92         return containers.values();
93     }
94
95     public static String JavaDoc getPropertyValue(ContainerConfig.Container parentProp, String JavaDoc name, String JavaDoc defaultValue) {
96         ContainerConfig.Container.Property prop = parentProp.getProperty(name);
97         if (prop == null || UtilValidate.isEmpty(prop.value)) {
98             return defaultValue;
99         } else {
100             return prop.value;
101         }
102     }
103
104     public static int getPropertyValue(ContainerConfig.Container parentProp, String JavaDoc name, int defaultValue) {
105         ContainerConfig.Container.Property prop = parentProp.getProperty(name);
106         if (prop == null || UtilValidate.isEmpty(prop.value)) {
107             return defaultValue;
108         } else {
109             int num = defaultValue;
110             try {
111                 num = Integer.parseInt(prop.value);
112             } catch (Exception JavaDoc e) {
113                 return defaultValue;
114             }
115             return num;
116         }
117     }
118
119     public static boolean getPropertyValue(ContainerConfig.Container parentProp, String JavaDoc name, boolean defaultValue) {
120         ContainerConfig.Container.Property prop = parentProp.getProperty(name);
121         if (prop == null || UtilValidate.isEmpty(prop.value)) {
122             return defaultValue;
123         } else {
124             return "true".equalsIgnoreCase(prop.value);
125         }
126     }
127
128     public static String JavaDoc getPropertyValue(ContainerConfig.Container.Property parentProp, String JavaDoc name, String JavaDoc defaultValue) {
129         ContainerConfig.Container.Property prop = parentProp.getProperty(name);
130         if (prop == null || UtilValidate.isEmpty(prop.value)) {
131             return defaultValue;
132         } else {
133             return prop.value;
134         }
135     }
136
137     public static int getPropertyValue(ContainerConfig.Container.Property parentProp, String JavaDoc name, int defaultValue) {
138         ContainerConfig.Container.Property prop = parentProp.getProperty(name);
139         if (prop == null || UtilValidate.isEmpty(prop.value)) {
140             return defaultValue;
141         } else {
142             int num = defaultValue;
143             try {
144                 num = Integer.parseInt(prop.value);
145             } catch (Exception JavaDoc e) {
146                 return defaultValue;
147             }
148             return num;
149         }
150     }
151
152     public static boolean getPropertyValue(ContainerConfig.Container.Property parentProp, String JavaDoc name, boolean defaultValue) {
153         ContainerConfig.Container.Property prop = parentProp.getProperty(name);
154         if (prop == null || UtilValidate.isEmpty(prop.value)) {
155             return defaultValue;
156         } else {
157             return "true".equalsIgnoreCase(prop.value);
158         }
159     }
160
161     protected ContainerConfig() {}
162     
163     protected ContainerConfig(String JavaDoc configFileLocation) throws ContainerException {
164         // load the config file
165
URL JavaDoc xmlUrl = UtilURL.fromResource(configFileLocation);
166         if (xmlUrl == null) {
167             throw new ContainerException("Could not find " + configFileLocation + " master OFBiz container configuration");
168         }
169         
170         // read the document
171
Document JavaDoc containerDocument = null;
172         try {
173             containerDocument = UtilXml.readXmlDocument(xmlUrl, true);
174         } catch (SAXException JavaDoc e) {
175             throw new ContainerException("Error reading the container config file: " + xmlUrl, e);
176         } catch (ParserConfigurationException JavaDoc e) {
177             throw new ContainerException("Error reading the container config file: " + xmlUrl, e);
178         } catch (IOException JavaDoc e) {
179             throw new ContainerException("Error reading the container config file: " + xmlUrl, e);
180         }
181         
182         // root element
183
Element JavaDoc root = containerDocument.getDocumentElement();
184           
185         // containers
186
Iterator JavaDoc elementIter = UtilXml.childElementList(root, "container").iterator();
187         while (elementIter.hasNext()) {
188             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
189             Container container = new Container(curElement);
190             containers.put(container.name, container);
191         }
192     }
193     
194     public static class Container {
195         public String JavaDoc name;
196         public String JavaDoc className;
197         public Map JavaDoc properties;
198         
199         public Container(Element JavaDoc element) {
200             this.name = element.getAttribute("name");
201             this.className = element.getAttribute("class");
202             
203             properties = new LinkedMap();
204             Iterator JavaDoc elementIter = UtilXml.childElementList(element, "property").iterator();
205             while (elementIter.hasNext()) {
206                 Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
207                 Property property = new Property(curElement);
208                 properties.put(property.name, property);
209             }
210         }
211         
212         public Property getProperty(String JavaDoc name) {
213             return (Property) properties.get(name);
214         }
215
216         public List JavaDoc getPropertiesWithValue(String JavaDoc value) {
217             List JavaDoc props = new LinkedList JavaDoc();
218             if (properties != null && properties.size() > 0) {
219                 Iterator JavaDoc i = properties.entrySet().iterator();
220                 while (i.hasNext()) {
221                     Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
222                     Property p = (Property) e.getValue();
223                     if (p != null && value.equals(p.value)) {
224                         props.add(p);
225                     }
226                 }
227             }
228             return props;
229         }
230
231         public static class Property {
232             public String JavaDoc name;
233             public String JavaDoc value;
234             public Map JavaDoc properties;
235             
236             public Property(Element JavaDoc element) {
237                 this.name = element.getAttribute("name");
238                 this.value = element.getAttribute("value");
239                 if (UtilValidate.isEmpty(this.value)) {
240                     this.value = UtilXml.childElementValue(element, "property-value");
241                 }
242
243                 properties = new LinkedMap();
244                 Iterator JavaDoc elementIter = UtilXml.childElementList(element, "property").iterator();
245                 while (elementIter.hasNext()) {
246                     Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
247                     Property property = new Property(curElement);
248                     properties.put(property.name, property);
249                 }
250             }
251             
252             public Property getProperty(String JavaDoc name) {
253                 return (Property) properties.get(name);
254             }
255
256             public List JavaDoc getPropertiesWithValue(String JavaDoc value) {
257                 List JavaDoc props = new LinkedList JavaDoc();
258                 if (properties != null && properties.size() > 0) {
259                     Iterator JavaDoc i = properties.entrySet().iterator();
260                     while (i.hasNext()) {
261                         Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
262                         Property p = (Property) e.getValue();
263                         if (p != null && value.equals(p.value)) {
264                             props.add(p);
265                         }
266                     }
267                 }
268                 return props;
269             }
270         }
271     }
272 }
Popular Tags