1 25 package org.ofbiz.base.component; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.net.URL ; 31 import java.util.Collection ; 32 import java.util.Comparator ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Map ; 36 import java.util.TreeMap ; 37 import javax.xml.parsers.ParserConfigurationException ; 38 39 import javolution.util.FastList; 40 import javolution.util.FastMap; 41 import org.ofbiz.base.util.Debug; 42 import org.ofbiz.base.util.UtilURL; 43 import org.ofbiz.base.util.UtilValidate; 44 import org.ofbiz.base.util.UtilXml; 45 import org.w3c.dom.Document ; 46 import org.w3c.dom.Element ; 47 import org.xml.sax.SAXException ; 48 49 57 public class ComponentConfig { 58 59 public static final String module = ComponentConfig.class.getName(); 60 public static final String OFBIZ_COMPONENT_XML_FILENAME = "ofbiz-component.xml"; 61 62 protected static Map componentConfigs = FastMap.newInstance(); 64 protected static Map serverWebApps = FastMap.newInstance(); 65 66 public static ComponentConfig getComponentConfig(String globalName) throws ComponentException { 67 return getComponentConfig(globalName, null); 69 } 70 71 public static ComponentConfig getComponentConfig(String globalName, String rootLocation) throws ComponentException { 72 ComponentConfig componentConfig = null; 73 if (UtilValidate.isNotEmpty(globalName)) { 74 componentConfig = (ComponentConfig) componentConfigs.get(globalName); 75 } 76 if (componentConfig == null) { 77 if (rootLocation != null) { 78 synchronized (ComponentConfig.class) { 79 if (UtilValidate.isNotEmpty(globalName)) { 80 componentConfig = (ComponentConfig) componentConfigs.get(globalName); 81 } 82 if (componentConfig == null) { 83 componentConfig = new ComponentConfig(globalName, rootLocation); 84 if (componentConfigs.containsKey(componentConfig.getGlobalName())) { 85 Debug.logWarning("WARNING: Loading ofbiz-component using a global name that already exists, will over-write: " + componentConfig.getGlobalName(), module); 86 } 87 if (componentConfig.enabled()) { 88 componentConfigs.put(componentConfig.getGlobalName(), componentConfig); 89 } 90 } 91 } 92 } else { 93 throw new ComponentException("No component found named : " + globalName); 94 } 95 } 96 return componentConfig; 97 } 98 99 public static Collection getAllComponents() { 100 Collection values = componentConfigs.values(); 101 if (values != null) { 102 return values; 103 } else { 104 Debug.logWarning("No components were found, something is probably missing or incorrect in the component-load setup.", module); 105 return FastList.newInstance(); 106 } 107 } 108 109 public static List getAllClasspathInfos() { 110 List classpaths = FastList.newInstance(); 111 Iterator i = getAllComponents().iterator(); 112 while (i.hasNext()) { 113 ComponentConfig cc = (ComponentConfig) i.next(); 114 classpaths.addAll(cc.getClasspathInfos()); 115 } 116 return classpaths; 117 } 118 119 public static List getAllEntityResourceInfos(String type) { 120 List entityInfos = FastList.newInstance(); 121 Iterator i = getAllComponents().iterator(); 122 while (i.hasNext()) { 123 ComponentConfig cc = (ComponentConfig) i.next(); 124 List ccEntityInfoList = cc.getEntityResourceInfos(); 125 if (UtilValidate.isEmpty(type)) { 126 entityInfos.addAll(ccEntityInfoList); 127 } else { 128 Iterator ccEntityInfoIter = ccEntityInfoList.iterator(); 129 while (ccEntityInfoIter.hasNext()) { 130 EntityResourceInfo entityResourceInfo = (EntityResourceInfo) ccEntityInfoIter.next(); 131 if (type.equals(entityResourceInfo.type)) { 132 entityInfos.add(entityResourceInfo); 133 } 134 } 135 } 136 } 137 return entityInfos; 138 } 139 140 public static List getAllServiceResourceInfos(String type) { 141 List serviceInfos = FastList.newInstance(); 142 Iterator i = getAllComponents().iterator(); 143 while (i.hasNext()) { 144 ComponentConfig cc = (ComponentConfig) i.next(); 145 List ccServiceInfoList = cc.getServiceResourceInfos(); 146 if (UtilValidate.isEmpty(type)) { 147 serviceInfos.addAll(ccServiceInfoList); 148 } else { 149 Iterator ccServiceInfoIter = ccServiceInfoList.iterator(); 150 while (ccServiceInfoIter.hasNext()) { 151 ServiceResourceInfo serviceResourceInfo = (ServiceResourceInfo) ccServiceInfoIter.next(); 152 if (type.equals(serviceResourceInfo.type)) { 153 serviceInfos.add(serviceResourceInfo); 154 } 155 } 156 } 157 } 158 return serviceInfos; 159 } 160 161 public static List getAllWebappResourceInfos() { 162 List webappInfos = FastList.newInstance(); 163 Iterator i = getAllComponents().iterator(); 164 while (i.hasNext()) { 165 ComponentConfig cc = (ComponentConfig) i.next(); 166 webappInfos.addAll(cc.getWebappInfos()); 167 } 168 return webappInfos; 169 170 } 171 172 public static boolean isFileResourceLoader(String componentName, String resourceLoaderName) throws ComponentException { 173 ComponentConfig cc = ComponentConfig.getComponentConfig(componentName); 174 if (cc == null) { 175 throw new ComponentException("Could not find component with name: " + componentName); 176 } 177 return cc.isFileResourceLoader(resourceLoaderName); 178 } 179 180 public static InputStream getStream(String componentName, String resourceLoaderName, String location) throws ComponentException { 181 ComponentConfig cc = ComponentConfig.getComponentConfig(componentName); 182 if (cc == null) { 183 throw new ComponentException("Could not find component with name: " + componentName); 184 } 185 return cc.getStream(resourceLoaderName, location); 186 } 187 188 public static URL getURL(String componentName, String resourceLoaderName, String location) throws ComponentException { 189 ComponentConfig cc = ComponentConfig.getComponentConfig(componentName); 190 if (cc == null) { 191 throw new ComponentException("Could not find component with name: " + componentName); 192 } 193 return cc.getURL(resourceLoaderName, location); 194 } 195 196 public static String getFullLocation(String componentName, String resourceLoaderName, String location) throws ComponentException { 197 ComponentConfig cc = ComponentConfig.getComponentConfig(componentName); 198 if (cc == null) { 199 throw new ComponentException("Could not find component with name: " + componentName); 200 } 201 return cc.getFullLocation(resourceLoaderName, location); 202 } 203 204 public static String getRootLocation(String componentName) throws ComponentException { 205 ComponentConfig cc = ComponentConfig.getComponentConfig(componentName); 206 if (cc == null) { 207 throw new ComponentException("Could not find component with name: " + componentName); 208 } 209 return cc.getRootLocation(); 210 } 211 212 public static List getAppBarWebInfos(String serverName) { 213 return ComponentConfig.getAppBarWebInfos(serverName, null); 214 } 215 216 public static List getAppBarWebInfos(String serverName, Comparator comp) { 217 List webInfos = (List ) serverWebApps.get(serverName); 218 if (webInfos == null) { 219 synchronized (ComponentConfig.class) { 220 if (webInfos == null) { 221 Map tm = null; 222 Iterator i = getAllComponents().iterator(); 223 224 if (comp != null) { 226 tm = new TreeMap (comp); 227 } else { 228 tm = new TreeMap (); 229 } 230 231 while (i.hasNext()) { 232 ComponentConfig cc = (ComponentConfig) i.next(); 233 Iterator wi = cc.getWebappInfos().iterator(); 234 while (wi.hasNext()) { 235 ComponentConfig.WebappInfo wInfo = (ComponentConfig.WebappInfo) wi.next(); 236 if (serverName.equals(wInfo.server) && wInfo.appBarDisplay) { 237 tm.put(wInfo.title, wInfo); 238 } 239 } 240 } 241 List webInfoList = FastList.newInstance(); 242 webInfoList.addAll(tm.values()); 243 serverWebApps.put(serverName, webInfoList); 244 return webInfoList; 245 } 246 } 247 } 248 return webInfos; 249 } 250 251 public static WebappInfo getWebAppInfo(String serverName, String contextRoot) { 252 ComponentConfig.WebappInfo info = null; 253 if (serverName == null || contextRoot == null) { 254 return info; 255 } 256 257 Iterator i = getAllComponents().iterator(); 258 while (i.hasNext() && info == null) { 259 ComponentConfig cc = (ComponentConfig) i.next(); 260 Iterator wi = cc.getWebappInfos().iterator(); 261 while (wi.hasNext()) { 262 ComponentConfig.WebappInfo wInfo = (ComponentConfig.WebappInfo) wi.next(); 263 if (serverName.equals(wInfo.server) && contextRoot.equals(wInfo.getContextRoot())) { 264 info = wInfo; 265 } 266 } 267 } 268 return info; 269 } 270 271 protected String globalName = null; 273 protected String rootLocation = null; 274 protected String componentName = null; 275 protected boolean enabled = true; 276 277 protected Map resourceLoaderInfos = FastMap.newInstance(); 278 protected List classpathInfos = FastList.newInstance(); 279 protected List entityResourceInfos = FastList.newInstance(); 280 protected List serviceResourceInfos = FastList.newInstance(); 281 protected List webappInfos = FastList.newInstance(); 282 283 protected ComponentConfig() {} 284 285 protected ComponentConfig(String globalName, String rootLocation) throws ComponentException { 286 this.globalName = globalName; 287 if (!rootLocation.endsWith("/")) { 288 rootLocation = rootLocation + "/"; 289 } 290 this.rootLocation = rootLocation.replace('\\', '/'); 291 292 File rootLocationDir = new File (rootLocation); 293 if (rootLocationDir == null) { 294 throw new ComponentException("The given component root location is does not exist: " + rootLocation); 295 } 296 if (!rootLocationDir.isDirectory()) { 297 throw new ComponentException("The given component root location is not a directory: " + rootLocation); 298 } 299 300 String xmlFilename = rootLocation + "/" + OFBIZ_COMPONENT_XML_FILENAME; 301 URL xmlUrl = UtilURL.fromFilename(xmlFilename); 302 if (xmlUrl == null) { 303 throw new ComponentException("Could not find the " + OFBIZ_COMPONENT_XML_FILENAME + " configuration file in the component root location: " + rootLocation); 304 } 305 306 Document ofbizComponentDocument = null; 307 try { 308 ofbizComponentDocument = UtilXml.readXmlDocument(xmlUrl, true); 309 } catch (SAXException e) { 310 throw new ComponentException("Error reading the component config file: " + xmlUrl, e); 311 } catch (ParserConfigurationException e) { 312 throw new ComponentException("Error reading the component config file: " + xmlUrl, e); 313 } catch (IOException e) { 314 throw new ComponentException("Error reading the component config file: " + xmlUrl, e); 315 } 316 317 Element ofbizComponentElement = ofbizComponentDocument.getDocumentElement(); 318 this.componentName = ofbizComponentElement.getAttribute("name"); 319 this.enabled = "true".equalsIgnoreCase(ofbizComponentElement.getAttribute("enabled")); 320 if (UtilValidate.isEmpty(this.globalName)) { 321 this.globalName = this.componentName; 322 } 323 Iterator elementIter = null; 324 325 elementIter = UtilXml.childElementList(ofbizComponentElement, "resource-loader").iterator(); 327 while (elementIter.hasNext()) { 328 Element curElement = (Element ) elementIter.next(); 329 ResourceLoaderInfo resourceLoaderInfo = new ResourceLoaderInfo(curElement); 330 this.resourceLoaderInfos.put(resourceLoaderInfo.name, resourceLoaderInfo); 331 } 332 333 elementIter = UtilXml.childElementList(ofbizComponentElement, "classpath").iterator(); 335 while (elementIter.hasNext()) { 336 Element curElement = (Element ) elementIter.next(); 337 ClasspathInfo classpathInfo = new ClasspathInfo(this, curElement); 338 this.classpathInfos.add(classpathInfo); 339 } 340 341 elementIter = UtilXml.childElementList(ofbizComponentElement, "entity-resource").iterator(); 343 while (elementIter.hasNext()) { 344 Element curElement = (Element ) elementIter.next(); 345 EntityResourceInfo entityResourceInfo = new EntityResourceInfo(this, curElement); 346 this.entityResourceInfos.add(entityResourceInfo); 347 } 348 349 elementIter = UtilXml.childElementList(ofbizComponentElement, "service-resource").iterator(); 351 while (elementIter.hasNext()) { 352 Element curElement = (Element ) elementIter.next(); 353 ServiceResourceInfo serviceResourceInfo = new ServiceResourceInfo(this, curElement); 354 this.serviceResourceInfos.add(serviceResourceInfo); 355 } 356 357 elementIter = UtilXml.childElementList(ofbizComponentElement, "webapp").iterator(); 359 while (elementIter.hasNext()) { 360 Element curElement = (Element ) elementIter.next(); 361 WebappInfo webappInfo = new WebappInfo(this, curElement); 362 this.webappInfos.add(webappInfo); 363 } 364 365 if (Debug.verboseOn()) Debug.logVerbose("Read component config : [" + rootLocation + "]", module); 366 } 367 368 public boolean isFileResource(ResourceInfo resourceInfo) throws ComponentException { 369 return isFileResourceLoader(resourceInfo.loader); 370 } 371 public boolean isFileResourceLoader(String resourceLoaderName) throws ComponentException { 372 ResourceLoaderInfo resourceLoaderInfo = (ResourceLoaderInfo) resourceLoaderInfos.get(resourceLoaderName); 373 if (resourceLoaderInfo == null) { 374 throw new ComponentException("Could not find resource-loader named: " + resourceLoaderName); 375 } 376 return "file".equals(resourceLoaderInfo.type) || "component".equals(resourceLoaderInfo.type); 377 } 378 379 public InputStream getStream(String resourceLoaderName, String location) throws ComponentException { 380 URL url = getURL(resourceLoaderName, location); 381 try { 382 return url.openStream(); 383 } catch (java.io.IOException e) { 384 throw new ComponentException("Error opening resource at location [" + url.toExternalForm() + "]", e); 385 } 386 } 387 388 public URL getURL(String resourceLoaderName, String location) throws ComponentException { 389 ResourceLoaderInfo resourceLoaderInfo = (ResourceLoaderInfo) resourceLoaderInfos.get(resourceLoaderName); 390 if (resourceLoaderInfo == null) { 391 throw new ComponentException("Could not find resource-loader named: " + resourceLoaderName); 392 } 393 394 if ("component".equals(resourceLoaderInfo.type) || "file".equals(resourceLoaderInfo.type)) { 395 String fullLocation = getFullLocation(resourceLoaderName, location); 396 URL fileUrl = UtilURL.fromFilename(fullLocation); 397 if (fileUrl == null) { 398 throw new ComponentException("File Resource not found: " + fullLocation); 399 } 400 return fileUrl; 401 } else if ("classpath".equals(resourceLoaderInfo.type)) { 402 String fullLocation = getFullLocation(resourceLoaderName, location); 403 URL url = UtilURL.fromResource(fullLocation); 404 if (url == null) { 405 throw new ComponentException("Classpath Resource not found: " + fullLocation); 406 } 407 return url; 408 } else if ("url".equals(resourceLoaderInfo.type)) { 409 String fullLocation = getFullLocation(resourceLoaderName, location); 410 URL url = null; 411 try { 412 url = new URL (fullLocation); 413 } catch (java.net.MalformedURLException e) { 414 throw new ComponentException("Error with malformed URL while trying to load URL resource at location [" + fullLocation + "]", e); 415 } 416 if (url == null) { 417 throw new ComponentException("URL Resource not found: " + fullLocation); 418 } 419 return url; 420 } else { 421 throw new ComponentException("The resource-loader type is not recognized: " + resourceLoaderInfo.type); 422 } 423 } 424 425 public String getFullLocation(String resourceLoaderName, String location) throws ComponentException { 426 ResourceLoaderInfo resourceLoaderInfo = (ResourceLoaderInfo) resourceLoaderInfos.get(resourceLoaderName); 427 if (resourceLoaderInfo == null) { 428 throw new ComponentException("Could not find resource-loader named: " + resourceLoaderName); 429 } 430 431 StringBuffer buf = new StringBuffer (); 432 433 if ("component".equals(resourceLoaderInfo.type)) { 435 buf.append(rootLocation); 436 } 437 438 if (resourceLoaderInfo.prependEnv != null && resourceLoaderInfo.prependEnv.length() > 0) { 439 String propValue = System.getProperty(resourceLoaderInfo.prependEnv); 440 if (propValue == null) { 441 String errMsg = "The Java environment (-Dxxx=yyy) variable with name " + resourceLoaderInfo.prependEnv + " is not set, cannot load resource."; 442 Debug.logError(errMsg, module); 443 throw new IllegalArgumentException (errMsg); 444 } 445 buf.append(propValue); 446 } 447 if (resourceLoaderInfo.prefix != null && resourceLoaderInfo.prefix.length() > 0) { 448 buf.append(resourceLoaderInfo.prefix); 449 } 450 buf.append(location); 451 return buf.toString(); 452 } 453 454 public List getClasspathInfos() { 455 return classpathInfos; 456 } 457 458 public String getComponentName() { 459 return componentName; 460 } 461 462 public List getEntityResourceInfos() { 463 return entityResourceInfos; 464 } 465 466 public String getGlobalName() { 467 return globalName; 468 } 469 470 public Map getResourceLoaderInfos() { 471 return resourceLoaderInfos; 472 } 473 474 public String getRootLocation() { 475 return rootLocation; 476 } 477 478 public List getServiceResourceInfos() { 479 return serviceResourceInfos; 480 } 481 482 public List getWebappInfos() { 483 return webappInfos; 484 } 485 486 public boolean enabled() { 487 return enabled; 488 } 489 490 public static class ResourceLoaderInfo { 491 public String name; 492 public String type; 493 public String prependEnv; 494 public String prefix; 495 496 public ResourceLoaderInfo(Element element) { 497 this.name = element.getAttribute("name"); 498 this.type = element.getAttribute("type"); 499 this.prependEnv = element.getAttribute("prepend-env"); 500 this.prefix = element.getAttribute("prefix"); 501 } 502 } 503 504 public static class ResourceInfo { 505 public ComponentConfig componentConfig; 506 public String loader; 507 public String location; 508 509 public ResourceInfo(ComponentConfig componentConfig, Element element) { 510 this.componentConfig = componentConfig; 511 this.loader = element.getAttribute("loader"); 512 this.location = element.getAttribute("location"); 513 } 514 515 public ComponentResourceHandler createResourceHandler() { 516 return new ComponentResourceHandler(componentConfig.getGlobalName(), loader, location); 517 } 518 } 519 520 public static class ClasspathInfo { 521 public ComponentConfig componentConfig; 522 public String type; 523 public String location; 524 525 public ClasspathInfo(ComponentConfig componentConfig, Element element) { 526 this.componentConfig = componentConfig; 527 this.type = element.getAttribute("type"); 528 this.location = element.getAttribute("location"); 529 } 530 } 531 532 public static class EntityResourceInfo extends ResourceInfo { 533 public String type; 534 public String readerName; 535 536 public EntityResourceInfo(ComponentConfig componentConfig, Element element) { 537 super(componentConfig, element); 538 this.type = element.getAttribute("type"); 539 this.readerName = element.getAttribute("reader-name"); 540 } 541 } 542 543 public static class ServiceResourceInfo extends ResourceInfo { 544 public String type; 545 546 public ServiceResourceInfo(ComponentConfig componentConfig, Element element) { 547 super(componentConfig, element); 548 this.type = element.getAttribute("type"); 549 } 550 } 551 552 public static class WebappInfo { 553 public ComponentConfig componentConfig; 554 public List virtualHosts; 555 public Map initParameters; 556 public String name; 557 public String title; 558 public String server; 559 public String mountPoint; 560 public String location; 561 public String [] basePermission; 562 public boolean appBarDisplay; 563 564 public WebappInfo(ComponentConfig componentConfig, Element element) { 565 this.virtualHosts = FastList.newInstance(); 566 this.initParameters = FastMap.newInstance(); 567 this.componentConfig = componentConfig; 568 this.name = element.getAttribute("name"); 569 this.title = element.getAttribute("title"); 570 this.server = element.getAttribute("server"); 571 this.mountPoint = element.getAttribute("mount-point"); 572 this.location = element.getAttribute("location"); 573 this.appBarDisplay = !"false".equals(element.getAttribute("app-bar-display")); 574 String basePermStr = element.getAttribute("base-permission"); 575 if (UtilValidate.isNotEmpty(basePermStr)) { 576 this.basePermission = basePermStr.split(","); 577 } else { 578 this.basePermission = new String [] { "NONE" }; 580 } 581 582 for (int i = 0; i < this.basePermission.length; i++) { 584 this.basePermission[i] = this.basePermission[i].trim(); 585 if (this.basePermission[i].indexOf('_') != -1) { 586 this.basePermission[i] = this.basePermission[i].substring(0, this.basePermission[i].indexOf('_')); 587 } 588 } 589 590 if (UtilValidate.isEmpty(this.title)) { 592 this.title = Character.toUpperCase(name.charAt(0)) + name.substring(1).toLowerCase(); 593 } 594 595 if (UtilValidate.isEmpty(this.mountPoint)) { 597 this.mountPoint = this.name; 598 } 599 600 if (!"/".equals(this.mountPoint)) { 602 if (!this.mountPoint.startsWith("/")) { 603 this.mountPoint = "/" + this.mountPoint; 604 } 605 if (!this.mountPoint.endsWith("/*")) { 606 if (!this.mountPoint.endsWith("/")) { 607 this.mountPoint = this.mountPoint + "/"; 608 } 609 this.mountPoint = this.mountPoint + "*"; 610 } 611 } 612 613 List virtHostList = UtilXml.childElementList(element, "virtual-host"); 615 if (virtHostList != null && virtHostList.size() > 0) { 616 Iterator elementIter = virtHostList.iterator(); 617 while (elementIter.hasNext()) { 618 Element e = (Element ) elementIter.next(); 619 virtualHosts.add(e.getAttribute("host-name")); 620 } 621 } 622 623 List initParamList = UtilXml.childElementList(element, "init-param"); 625 if (initParamList != null && initParamList.size() > 0) { 626 Iterator elementIter = initParamList.iterator(); 627 while (elementIter.hasNext()) { 628 Element e = (Element ) elementIter.next(); 629 this.initParameters.put(e.getAttribute("name"), e.getAttribute("value")); 630 } 631 } 632 } 633 634 public String getContextRoot() { 635 if (mountPoint.endsWith("/*")) { 636 return mountPoint.substring(0, mountPoint.length() - 2); 637 } 638 return mountPoint; 639 } 640 641 public String [] getBasePermission() { 642 return this.basePermission; 643 } 644 645 public String getName() { 646 return name; 647 } 648 649 public String getLocation() { 650 return componentConfig.getRootLocation() + location; 651 } 652 653 public String getTitle() { 654 return title; 655 } 656 657 public List getVirtualHosts() { 658 return virtualHosts; 659 } 660 661 public Map getInitParameters() { 662 return initParameters; 663 } 664 } 665 } 666 | Popular Tags |