001: /*
002: * $Id: ServiceGroupReader.java,v 1.4 2003/09/02 18:41:32 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.service.group;
025:
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import org.ofbiz.base.component.ComponentConfig;
030: import org.ofbiz.base.config.GenericConfigException;
031: import org.ofbiz.base.config.MainResourceHandler;
032: import org.ofbiz.base.config.ResourceHandler;
033: import org.ofbiz.base.util.Debug;
034: import org.ofbiz.base.util.UtilCache;
035: import org.ofbiz.base.util.UtilXml;
036: import org.ofbiz.service.config.ServiceConfigUtil;
037: import org.w3c.dom.Element;
038:
039: /**
040: * ServiceGroupReader.java
041: *
042: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
043: * @version $Revision: 1.4 $
044: * @since 2.0
045: */
046: public class ServiceGroupReader {
047:
048: public static final String module = ServiceGroupReader.class
049: .getName();
050:
051: public static UtilCache groupsCache = new UtilCache(
052: "service.ServiceGroups", 0, 0, false);
053:
054: public static void readConfig() {
055: Element rootElement = null;
056:
057: try {
058: rootElement = ServiceConfigUtil.getXmlRootElement();
059: } catch (GenericConfigException e) {
060: Debug.logError(e,
061: "Error getting Service Engine XML root element",
062: module);
063: return;
064: }
065:
066: List serviceGroupElements = UtilXml.childElementList(
067: rootElement, "service-groups");
068: Iterator groupsIter = serviceGroupElements.iterator();
069: while (groupsIter.hasNext()) {
070: Element serviceGroupElement = (Element) groupsIter.next();
071: ResourceHandler handler = new MainResourceHandler(
072: ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME,
073: serviceGroupElement);
074: addGroupDefinitions(handler);
075: }
076:
077: // get all of the component resource group stuff, ie specified in each ofbiz-component.xml file
078: List componentResourceInfos = ComponentConfig
079: .getAllServiceResourceInfos("group");
080: Iterator componentResourceInfoIter = componentResourceInfos
081: .iterator();
082: while (componentResourceInfoIter.hasNext()) {
083: ComponentConfig.ServiceResourceInfo componentResourceInfo = (ComponentConfig.ServiceResourceInfo) componentResourceInfoIter
084: .next();
085: addGroupDefinitions(componentResourceInfo
086: .createResourceHandler());
087: }
088: }
089:
090: public static void addGroupDefinitions(ResourceHandler handler) {
091: Element rootElement = null;
092:
093: try {
094: rootElement = handler.getDocument().getDocumentElement();
095: } catch (GenericConfigException e) {
096: Debug.logError(e, module);
097: return;
098: }
099: List groupList = UtilXml.childElementList(rootElement, "group");
100: Iterator groupIt = groupList.iterator();
101: int numDefs = 0;
102:
103: while (groupIt.hasNext()) {
104: Element group = (Element) groupIt.next();
105: String groupName = group.getAttribute("name");
106: groupsCache.put(groupName, new GroupModel(group));
107: numDefs++;
108: }
109: if (Debug.importantOn()) {
110: String resourceLocation = handler.getLocation();
111: try {
112: resourceLocation = handler.getURL().toExternalForm();
113: } catch (GenericConfigException e) {
114: Debug.logError(e, "Could not get resource URL", module);
115: }
116: Debug.logImportant("Loaded " + numDefs
117: + " Group definitions from " + resourceLocation,
118: module);
119: }
120: }
121:
122: public static GroupModel getGroupModel(String serviceName) {
123: if (groupsCache.size() == 0)
124: ServiceGroupReader.readConfig();
125: return (GroupModel) groupsCache.get(serviceName);
126: }
127: }
|