1 25 package org.ofbiz.base.container; 26 27 import java.io.IOException ; 28 import java.net.URL ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Map ; 34 import javax.xml.parsers.ParserConfigurationException ; 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 ; 42 import org.w3c.dom.Element ; 43 import org.xml.sax.SAXException ; 44 45 52 public class ContainerConfig { 53 54 public static final String module = ContainerConfig.class.getName(); 55 56 protected static Map containers = new LinkedMap(); 57 58 public static Container getContainer(String containerName, String 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 getContainers(String 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 getPropertyValue(ContainerConfig.Container parentProp, String name, String 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 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 e) { 113 return defaultValue; 114 } 115 return num; 116 } 117 } 118 119 public static boolean getPropertyValue(ContainerConfig.Container parentProp, String 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 getPropertyValue(ContainerConfig.Container.Property parentProp, String name, String 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 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 e) { 146 return defaultValue; 147 } 148 return num; 149 } 150 } 151 152 public static boolean getPropertyValue(ContainerConfig.Container.Property parentProp, String 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 configFileLocation) throws ContainerException { 164 URL xmlUrl = UtilURL.fromResource(configFileLocation); 166 if (xmlUrl == null) { 167 throw new ContainerException("Could not find " + configFileLocation + " master OFBiz container configuration"); 168 } 169 170 Document containerDocument = null; 172 try { 173 containerDocument = UtilXml.readXmlDocument(xmlUrl, true); 174 } catch (SAXException e) { 175 throw new ContainerException("Error reading the container config file: " + xmlUrl, e); 176 } catch (ParserConfigurationException e) { 177 throw new ContainerException("Error reading the container config file: " + xmlUrl, e); 178 } catch (IOException e) { 179 throw new ContainerException("Error reading the container config file: " + xmlUrl, e); 180 } 181 182 Element root = containerDocument.getDocumentElement(); 184 185 Iterator elementIter = UtilXml.childElementList(root, "container").iterator(); 187 while (elementIter.hasNext()) { 188 Element curElement = (Element ) 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 name; 196 public String className; 197 public Map properties; 198 199 public Container(Element element) { 200 this.name = element.getAttribute("name"); 201 this.className = element.getAttribute("class"); 202 203 properties = new LinkedMap(); 204 Iterator elementIter = UtilXml.childElementList(element, "property").iterator(); 205 while (elementIter.hasNext()) { 206 Element curElement = (Element ) elementIter.next(); 207 Property property = new Property(curElement); 208 properties.put(property.name, property); 209 } 210 } 211 212 public Property getProperty(String name) { 213 return (Property) properties.get(name); 214 } 215 216 public List getPropertiesWithValue(String value) { 217 List props = new LinkedList (); 218 if (properties != null && properties.size() > 0) { 219 Iterator i = properties.entrySet().iterator(); 220 while (i.hasNext()) { 221 Map.Entry e = (Map.Entry ) 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 name; 233 public String value; 234 public Map properties; 235 236 public Property(Element 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 elementIter = UtilXml.childElementList(element, "property").iterator(); 245 while (elementIter.hasNext()) { 246 Element curElement = (Element ) elementIter.next(); 247 Property property = new Property(curElement); 248 properties.put(property.name, property); 249 } 250 } 251 252 public Property getProperty(String name) { 253 return (Property) properties.get(name); 254 } 255 256 public List getPropertiesWithValue(String value) { 257 List props = new LinkedList (); 258 if (properties != null && properties.size() > 0) { 259 Iterator i = properties.entrySet().iterator(); 260 while (i.hasNext()) { 261 Map.Entry e = (Map.Entry ) 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 |