KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > group > ServiceGroupReader


1 /*
2  * $Id: ServiceGroupReader.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 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 package org.ofbiz.service.group;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.ofbiz.base.component.ComponentConfig;
30 import org.ofbiz.base.config.GenericConfigException;
31 import org.ofbiz.base.config.MainResourceHandler;
32 import org.ofbiz.base.config.ResourceHandler;
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.base.util.UtilXml;
35 import org.ofbiz.base.util.cache.UtilCache;
36 import org.ofbiz.service.config.ServiceConfigUtil;
37 import org.w3c.dom.Element JavaDoc;
38
39 /**
40  * ServiceGroupReader.java
41  *
42  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
43  * @version $Rev: 5462 $
44  * @since 2.0
45  */

46 public class ServiceGroupReader {
47     
48     public static final String JavaDoc module = ServiceGroupReader.class.getName();
49
50     public static UtilCache groupsCache = new UtilCache("service.ServiceGroups", 0, 0, false);
51     
52     public static void readConfig() {
53         Element JavaDoc rootElement = null;
54
55         try {
56             rootElement = ServiceConfigUtil.getXmlRootElement();
57         } catch (GenericConfigException e) {
58             Debug.logError(e, "Error getting Service Engine XML root element", module);
59             return;
60         }
61
62         List JavaDoc serviceGroupElements = UtilXml.childElementList(rootElement, "service-groups");
63         Iterator JavaDoc groupsIter = serviceGroupElements.iterator();
64         while (groupsIter.hasNext()) {
65             Element JavaDoc serviceGroupElement = (Element JavaDoc) groupsIter.next();
66             ResourceHandler handler = new MainResourceHandler(ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME, serviceGroupElement);
67             addGroupDefinitions(handler);
68         }
69
70         // get all of the component resource group stuff, ie specified in each ofbiz-component.xml file
71
List JavaDoc componentResourceInfos = ComponentConfig.getAllServiceResourceInfos("group");
72         Iterator JavaDoc componentResourceInfoIter = componentResourceInfos.iterator();
73         while (componentResourceInfoIter.hasNext()) {
74             ComponentConfig.ServiceResourceInfo componentResourceInfo = (ComponentConfig.ServiceResourceInfo) componentResourceInfoIter.next();
75             addGroupDefinitions(componentResourceInfo.createResourceHandler());
76         }
77     }
78     
79     public static void addGroupDefinitions(ResourceHandler handler) {
80         Element JavaDoc rootElement = null;
81
82         try {
83             rootElement = handler.getDocument().getDocumentElement();
84         } catch (GenericConfigException e) {
85             Debug.logError(e, module);
86             return;
87         }
88         List JavaDoc groupList = UtilXml.childElementList(rootElement, "group");
89         Iterator JavaDoc groupIt = groupList.iterator();
90         int numDefs = 0;
91
92         while (groupIt.hasNext()) {
93             Element JavaDoc group = (Element JavaDoc) groupIt.next();
94             String JavaDoc groupName = group.getAttribute("name");
95             groupsCache.put(groupName, new GroupModel(group));
96             numDefs++;
97         }
98         if (Debug.importantOn()) {
99             String JavaDoc resourceLocation = handler.getLocation();
100             try {
101                 resourceLocation = handler.getURL().toExternalForm();
102             } catch (GenericConfigException e) {
103                 Debug.logError(e, "Could not get resource URL", module);
104             }
105             Debug.logImportant("Loaded " + numDefs + " Group definitions from " + resourceLocation, module);
106         }
107     }
108
109     public static GroupModel getGroupModel(String JavaDoc serviceName) {
110         if (groupsCache.size() == 0) {
111             ServiceGroupReader.readConfig();
112         }
113         return (GroupModel) groupsCache.get(serviceName);
114     }
115 }
116
Popular Tags