1 25 package org.ofbiz.base.component; 26 27 import java.io.IOException ; 28 import java.net.URL ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Properties ; 33 34 import javax.xml.parsers.ParserConfigurationException ; 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 ; 40 import org.w3c.dom.Element ; 41 import org.xml.sax.SAXException ; 42 43 50 public class ComponentLoaderConfig { 51 52 public static final String module = ComponentLoaderConfig.class.getName(); 53 public static final String 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 componentsToLoad = null; 59 60 public static List getRootComponents(String 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 xmlUrl = UtilURL.fromResource(configFile); 68 ComponentLoaderConfig.componentsToLoad = ComponentLoaderConfig.getComponentsFromConfig(xmlUrl); 69 } 70 } 71 } 72 return componentsToLoad; 73 } 74 75 public static List getComponentsFromConfig(URL configUrl) throws ComponentException { 76 if (configUrl == null) { 77 throw new ComponentException("Component config file does not exist: " + configUrl); 78 } 79 80 List componentsFromConfig = null; 81 Document document = null; 82 try { 83 document = UtilXml.readXmlDocument(configUrl, true); 84 } catch (SAXException e) { 85 throw new ComponentException("Error reading the component config file: " + configUrl, e); 86 } catch (ParserConfigurationException e) { 87 throw new ComponentException("Error reading the component config file: " + configUrl, e); 88 } catch (IOException e) { 89 throw new ComponentException("Error reading the component config file: " + configUrl, e); 90 } 91 92 Element root = document.getDocumentElement(); 93 List toLoad = UtilXml.childElementList(root); 94 if (toLoad != null && toLoad.size() > 0) { 95 componentsFromConfig = new LinkedList (); 96 Iterator i = toLoad.iterator(); 97 while (i.hasNext()) { 98 Element element = (Element ) i.next(); 99 componentsFromConfig.add(new ComponentDef(element)); 100 } 101 } 102 return componentsFromConfig; 103 } 104 105 public static class ComponentDef { 106 public String name; 107 public String location; 108 public int type = -1; 109 110 public ComponentDef(Element element) { 111 Properties 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 |