KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ComponentConfig.java 5596 2005-08-26 01:36:06Z jaz $
3  *
4  * Copyright (c) 2003-2005 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.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Comparator JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.TreeMap JavaDoc;
37 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
46 import org.w3c.dom.Element JavaDoc;
47 import org.xml.sax.SAXException JavaDoc;
48
49 /**
50  * ComponentConfig - Component configuration class for ofbiz-container.xml
51  *
52  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
53  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
54  * @version $Rev: 5596 $
55  * @since 3.0
56  */

57 public class ComponentConfig {
58
59     public static final String JavaDoc module = ComponentConfig.class.getName();
60     public static final String JavaDoc OFBIZ_COMPONENT_XML_FILENAME = "ofbiz-component.xml";
61
62     // this is not a UtilCache because reloading may cause problems
63
protected static Map JavaDoc componentConfigs = FastMap.newInstance();
64     protected static Map JavaDoc serverWebApps = FastMap.newInstance();
65
66     public static ComponentConfig getComponentConfig(String JavaDoc globalName) throws ComponentException {
67         // TODO: we need to look up the rootLocation from the container config, or this will blow up
68
return getComponentConfig(globalName, null);
69     }
70
71     public static ComponentConfig getComponentConfig(String JavaDoc globalName, String JavaDoc 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 JavaDoc getAllComponents() {
100         Collection JavaDoc 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 JavaDoc getAllClasspathInfos() {
110         List JavaDoc classpaths = FastList.newInstance();
111         Iterator JavaDoc 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 JavaDoc getAllEntityResourceInfos(String JavaDoc type) {
120         List JavaDoc entityInfos = FastList.newInstance();
121         Iterator JavaDoc i = getAllComponents().iterator();
122         while (i.hasNext()) {
123             ComponentConfig cc = (ComponentConfig) i.next();
124             List JavaDoc ccEntityInfoList = cc.getEntityResourceInfos();
125             if (UtilValidate.isEmpty(type)) {
126                 entityInfos.addAll(ccEntityInfoList);
127             } else {
128                 Iterator JavaDoc 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 JavaDoc getAllServiceResourceInfos(String JavaDoc type) {
141         List JavaDoc serviceInfos = FastList.newInstance();
142         Iterator JavaDoc i = getAllComponents().iterator();
143         while (i.hasNext()) {
144             ComponentConfig cc = (ComponentConfig) i.next();
145             List JavaDoc ccServiceInfoList = cc.getServiceResourceInfos();
146             if (UtilValidate.isEmpty(type)) {
147                 serviceInfos.addAll(ccServiceInfoList);
148             } else {
149                 Iterator JavaDoc 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 JavaDoc getAllWebappResourceInfos() {
162         List JavaDoc webappInfos = FastList.newInstance();
163         Iterator JavaDoc 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 JavaDoc componentName, String JavaDoc 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 JavaDoc getStream(String JavaDoc componentName, String JavaDoc resourceLoaderName, String JavaDoc 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 JavaDoc getURL(String JavaDoc componentName, String JavaDoc resourceLoaderName, String JavaDoc 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 JavaDoc getFullLocation(String JavaDoc componentName, String JavaDoc resourceLoaderName, String JavaDoc 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 JavaDoc getRootLocation(String JavaDoc 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 JavaDoc getAppBarWebInfos(String JavaDoc serverName) {
213         return ComponentConfig.getAppBarWebInfos(serverName, null);
214     }
215
216     public static List JavaDoc getAppBarWebInfos(String JavaDoc serverName, Comparator JavaDoc comp) {
217         List JavaDoc webInfos = (List JavaDoc) serverWebApps.get(serverName);
218         if (webInfos == null) {
219             synchronized (ComponentConfig.class) {
220                 if (webInfos == null) {
221                     Map JavaDoc tm = null;
222                     Iterator JavaDoc i = getAllComponents().iterator();
223
224                     // use a TreeMap to sort the components alpha by title
225
if (comp != null) {
226                         tm = new TreeMap JavaDoc(comp);
227                     } else {
228                         tm = new TreeMap JavaDoc();
229                     }
230
231                     while (i.hasNext()) {
232                         ComponentConfig cc = (ComponentConfig) i.next();
233                         Iterator JavaDoc 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 JavaDoc 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 JavaDoc serverName, String JavaDoc contextRoot) {
252         ComponentConfig.WebappInfo info = null;
253         if (serverName == null || contextRoot == null) {
254             return info;
255         }
256
257         Iterator JavaDoc i = getAllComponents().iterator();
258         while (i.hasNext() && info == null) {
259             ComponentConfig cc = (ComponentConfig) i.next();
260             Iterator JavaDoc 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     // ========== component info fields ==========
272
protected String JavaDoc globalName = null;
273     protected String JavaDoc rootLocation = null;
274     protected String JavaDoc componentName = null;
275     protected boolean enabled = true;
276
277     protected Map JavaDoc resourceLoaderInfos = FastMap.newInstance();
278     protected List JavaDoc classpathInfos = FastList.newInstance();
279     protected List JavaDoc entityResourceInfos = FastList.newInstance();
280     protected List JavaDoc serviceResourceInfos = FastList.newInstance();
281     protected List JavaDoc webappInfos = FastList.newInstance();
282
283     protected ComponentConfig() {}
284
285     protected ComponentConfig(String JavaDoc globalName, String JavaDoc rootLocation) throws ComponentException {
286         this.globalName = globalName;
287         if (!rootLocation.endsWith("/")) {
288             rootLocation = rootLocation + "/";
289         }
290         this.rootLocation = rootLocation.replace('\\', '/');
291
292         File JavaDoc rootLocationDir = new File JavaDoc(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 JavaDoc xmlFilename = rootLocation + "/" + OFBIZ_COMPONENT_XML_FILENAME;
301         URL JavaDoc 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 JavaDoc ofbizComponentDocument = null;
307         try {
308             ofbizComponentDocument = UtilXml.readXmlDocument(xmlUrl, true);
309         } catch (SAXException JavaDoc e) {
310             throw new ComponentException("Error reading the component config file: " + xmlUrl, e);
311         } catch (ParserConfigurationException JavaDoc e) {
312             throw new ComponentException("Error reading the component config file: " + xmlUrl, e);
313         } catch (IOException JavaDoc e) {
314             throw new ComponentException("Error reading the component config file: " + xmlUrl, e);
315         }
316
317         Element JavaDoc 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 JavaDoc elementIter = null;
324
325         // resource-loader - resourceLoaderInfos
326
elementIter = UtilXml.childElementList(ofbizComponentElement, "resource-loader").iterator();
327         while (elementIter.hasNext()) {
328             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
329             ResourceLoaderInfo resourceLoaderInfo = new ResourceLoaderInfo(curElement);
330             this.resourceLoaderInfos.put(resourceLoaderInfo.name, resourceLoaderInfo);
331         }
332
333         // classpath - classpathInfos
334
elementIter = UtilXml.childElementList(ofbizComponentElement, "classpath").iterator();
335         while (elementIter.hasNext()) {
336             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
337             ClasspathInfo classpathInfo = new ClasspathInfo(this, curElement);
338             this.classpathInfos.add(classpathInfo);
339         }
340
341         // entity-resource - entityResourceInfos
342
elementIter = UtilXml.childElementList(ofbizComponentElement, "entity-resource").iterator();
343         while (elementIter.hasNext()) {
344             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
345             EntityResourceInfo entityResourceInfo = new EntityResourceInfo(this, curElement);
346             this.entityResourceInfos.add(entityResourceInfo);
347         }
348
349         // service-resource - serviceResourceInfos
350
elementIter = UtilXml.childElementList(ofbizComponentElement, "service-resource").iterator();
351         while (elementIter.hasNext()) {
352             Element JavaDoc curElement = (Element JavaDoc) elementIter.next();
353             ServiceResourceInfo serviceResourceInfo = new ServiceResourceInfo(this, curElement);
354             this.serviceResourceInfos.add(serviceResourceInfo);
355         }
356
357         // webapp - webappInfos
358
elementIter = UtilXml.childElementList(ofbizComponentElement, "webapp").iterator();
359         while (elementIter.hasNext()) {
360             Element JavaDoc curElement = (Element JavaDoc) 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 JavaDoc 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 JavaDoc getStream(String JavaDoc resourceLoaderName, String JavaDoc location) throws ComponentException {
380         URL JavaDoc url = getURL(resourceLoaderName, location);
381         try {
382             return url.openStream();
383         } catch (java.io.IOException JavaDoc e) {
384             throw new ComponentException("Error opening resource at location [" + url.toExternalForm() + "]", e);
385         }
386     }
387
388     public URL JavaDoc getURL(String JavaDoc resourceLoaderName, String JavaDoc 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 JavaDoc fullLocation = getFullLocation(resourceLoaderName, location);
396             URL JavaDoc 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 JavaDoc fullLocation = getFullLocation(resourceLoaderName, location);
403             URL JavaDoc 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 JavaDoc fullLocation = getFullLocation(resourceLoaderName, location);
410             URL JavaDoc url = null;
411             try {
412                 url = new URL JavaDoc(fullLocation);
413             } catch (java.net.MalformedURLException JavaDoc 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 JavaDoc getFullLocation(String JavaDoc resourceLoaderName, String JavaDoc 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 JavaDoc buf = new StringBuffer JavaDoc();
432
433         // pre-pend component root location if this is a type component resource-loader
434
if ("component".equals(resourceLoaderInfo.type)) {
435             buf.append(rootLocation);
436         }
437
438         if (resourceLoaderInfo.prependEnv != null && resourceLoaderInfo.prependEnv.length() > 0) {
439             String JavaDoc propValue = System.getProperty(resourceLoaderInfo.prependEnv);
440             if (propValue == null) {
441                 String JavaDoc 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 JavaDoc(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 JavaDoc getClasspathInfos() {
455         return classpathInfos;
456     }
457
458     public String JavaDoc getComponentName() {
459         return componentName;
460     }
461
462     public List JavaDoc getEntityResourceInfos() {
463         return entityResourceInfos;
464     }
465
466     public String JavaDoc getGlobalName() {
467         return globalName;
468     }
469
470     public Map JavaDoc getResourceLoaderInfos() {
471         return resourceLoaderInfos;
472     }
473
474     public String JavaDoc getRootLocation() {
475         return rootLocation;
476     }
477
478     public List JavaDoc getServiceResourceInfos() {
479         return serviceResourceInfos;
480     }
481
482     public List JavaDoc getWebappInfos() {
483         return webappInfos;
484     }
485
486     public boolean enabled() {
487         return enabled;
488     }
489
490     public static class ResourceLoaderInfo {
491         public String JavaDoc name;
492         public String JavaDoc type;
493         public String JavaDoc prependEnv;
494         public String JavaDoc prefix;
495
496         public ResourceLoaderInfo(Element JavaDoc 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 JavaDoc loader;
507         public String JavaDoc location;
508
509         public ResourceInfo(ComponentConfig componentConfig, Element JavaDoc 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 JavaDoc type;
523         public String JavaDoc location;
524
525         public ClasspathInfo(ComponentConfig componentConfig, Element JavaDoc 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 JavaDoc type;
534         public String JavaDoc readerName;
535
536         public EntityResourceInfo(ComponentConfig componentConfig, Element JavaDoc 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 JavaDoc type;
545
546         public ServiceResourceInfo(ComponentConfig componentConfig, Element JavaDoc 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 JavaDoc virtualHosts;
555         public Map JavaDoc initParameters;
556         public String JavaDoc name;
557         public String JavaDoc title;
558         public String JavaDoc server;
559         public String JavaDoc mountPoint;
560         public String JavaDoc location;
561         public String JavaDoc[] basePermission;
562         public boolean appBarDisplay;
563
564         public WebappInfo(ComponentConfig componentConfig, Element JavaDoc 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 JavaDoc basePermStr = element.getAttribute("base-permission");
575             if (UtilValidate.isNotEmpty(basePermStr)) {
576                 this.basePermission = basePermStr.split(",");
577             } else {
578                 // default base permission is NONE
579
this.basePermission = new String JavaDoc[] { "NONE" };
580             }
581
582             // trim the permussions (remove spaces)
583
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             // default title is name w/ upper-cased first letter
591
if (UtilValidate.isEmpty(this.title)) {
592                 this.title = Character.toUpperCase(name.charAt(0)) + name.substring(1).toLowerCase();
593             }
594
595             // default mount point is name if none specified
596
if (UtilValidate.isEmpty(this.mountPoint)) {
597                 this.mountPoint = this.name;
598             }
599
600             // check the mount point and make sure it is properly formatted
601
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             // load the virtual hosts
614
List JavaDoc virtHostList = UtilXml.childElementList(element, "virtual-host");
615             if (virtHostList != null && virtHostList.size() > 0) {
616                 Iterator JavaDoc elementIter = virtHostList.iterator();
617                 while (elementIter.hasNext()) {
618                     Element JavaDoc e = (Element JavaDoc) elementIter.next();
619                     virtualHosts.add(e.getAttribute("host-name"));
620                 }
621             }
622
623             // load the init parameters
624
List JavaDoc initParamList = UtilXml.childElementList(element, "init-param");
625             if (initParamList != null && initParamList.size() > 0) {
626                 Iterator JavaDoc elementIter = initParamList.iterator();
627                 while (elementIter.hasNext()) {
628                     Element JavaDoc e = (Element JavaDoc) elementIter.next();
629                     this.initParameters.put(e.getAttribute("name"), e.getAttribute("value"));
630                 }
631             }
632         }
633
634         public String JavaDoc getContextRoot() {
635             if (mountPoint.endsWith("/*")) {
636                 return mountPoint.substring(0, mountPoint.length() - 2);
637             }
638             return mountPoint;
639         }
640
641         public String JavaDoc[] getBasePermission() {
642             return this.basePermission;
643         }
644
645         public String JavaDoc getName() {
646             return name;
647         }
648
649         public String JavaDoc getLocation() {
650             return componentConfig.getRootLocation() + location;
651         }
652
653         public String JavaDoc getTitle() {
654             return title;
655         }
656
657         public List JavaDoc getVirtualHosts() {
658             return virtualHosts;
659         }
660
661         public Map JavaDoc getInitParameters() {
662             return initParameters;
663         }
664     }
665 }
666
Popular Tags