KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > component > ComponentLoaderConfig


1 /*
2  * $Id: ComponentLoaderConfig.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.component;
26
27 import java.io.IOException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35
36 import org.ofbiz.base.util.UtilURL;
37 import org.ofbiz.base.util.UtilXml;
38 import org.ofbiz.base.util.string.FlexibleStringExpander;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42
43 /**
44  * ComponentLoaderConfig - Component Loader configuration
45  *
46  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
47  * @version $Rev: 5462 $
48  * @since 3.0
49  */

50 public class ComponentLoaderConfig {
51     
52     public static final String JavaDoc module = ComponentLoaderConfig.class.getName();
53     public static final String JavaDoc COMPONENT_LOAD_XML_FILENAME = "component-load.xml";
54     
55     public static final int SINGLE_COMPONENT = 0;
56     public static final int COMPONENT_DIRECTORY = 1;
57     
58     protected static List JavaDoc componentsToLoad = null;
59     
60     public static List JavaDoc getRootComponents(String JavaDoc configFile) throws ComponentException {
61         if (componentsToLoad == null) {
62             synchronized (ComponentLoaderConfig.class) {
63                 if (componentsToLoad == null) {
64                     if (configFile == null) {
65                         configFile = COMPONENT_LOAD_XML_FILENAME;
66                     }
67                     URL JavaDoc xmlUrl = UtilURL.fromResource(configFile);
68                     ComponentLoaderConfig.componentsToLoad = ComponentLoaderConfig.getComponentsFromConfig(xmlUrl);
69                 }
70             }
71         }
72         return componentsToLoad;
73     }
74
75     public static List JavaDoc getComponentsFromConfig(URL JavaDoc configUrl) throws ComponentException {
76         if (configUrl == null) {
77             throw new ComponentException("Component config file does not exist: " + configUrl);
78         }
79
80         List JavaDoc componentsFromConfig = null;
81         Document JavaDoc document = null;
82         try {
83             document = UtilXml.readXmlDocument(configUrl, true);
84         } catch (SAXException JavaDoc e) {
85             throw new ComponentException("Error reading the component config file: " + configUrl, e);
86         } catch (ParserConfigurationException JavaDoc e) {
87             throw new ComponentException("Error reading the component config file: " + configUrl, e);
88         } catch (IOException JavaDoc e) {
89             throw new ComponentException("Error reading the component config file: " + configUrl, e);
90         }
91
92         Element JavaDoc root = document.getDocumentElement();
93         List JavaDoc toLoad = UtilXml.childElementList(root);
94         if (toLoad != null && toLoad.size() > 0) {
95             componentsFromConfig = new LinkedList JavaDoc();
96             Iterator JavaDoc i = toLoad.iterator();
97             while (i.hasNext()) {
98                 Element JavaDoc element = (Element JavaDoc) i.next();
99                 componentsFromConfig.add(new ComponentDef(element));
100             }
101         }
102         return componentsFromConfig;
103     }
104
105     public static class ComponentDef {
106         public String JavaDoc name;
107         public String JavaDoc location;
108         public int type = -1;
109         
110         public ComponentDef(Element JavaDoc element) {
111             Properties JavaDoc systemProps = System.getProperties();
112             if ("load-component".equals(element.getNodeName())) {
113                 name = element.getAttribute("component-name");
114                 location = FlexibleStringExpander.expandString(element.getAttribute("component-location"), systemProps);
115                 type = SINGLE_COMPONENT;
116             } else if ("load-components".equals(element.getNodeName())) {
117                 name = null;
118                 location = FlexibleStringExpander.expandString(element.getAttribute("parent-directory"), systemProps);
119                 type = COMPONENT_DIRECTORY;
120             }
121         }
122     }
123 }
124
Popular Tags