001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.service.group;
019:
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javolution.util.FastMap;
025:
026: import org.ofbiz.base.component.ComponentConfig;
027: import org.ofbiz.base.config.GenericConfigException;
028: import org.ofbiz.base.config.MainResourceHandler;
029: import org.ofbiz.base.config.ResourceHandler;
030: import org.ofbiz.base.util.Debug;
031: import org.ofbiz.base.util.UtilXml;
032: import org.ofbiz.service.config.ServiceConfigUtil;
033: import org.w3c.dom.Element;
034:
035: /**
036: * ServiceGroupReader.java
037: */
038: public class ServiceGroupReader {
039:
040: public static final String module = ServiceGroupReader.class
041: .getName();
042:
043: // using a cache is dangerous here because if someone clears it the groups won't work at all: public static UtilCache groupsCache = new UtilCache("service.ServiceGroups", 0, 0, false);
044: public static Map groupsCache = FastMap.newInstance();
045:
046: public static void readConfig() {
047: Element rootElement = null;
048:
049: try {
050: rootElement = ServiceConfigUtil.getXmlRootElement();
051: } catch (GenericConfigException e) {
052: Debug.logError(e,
053: "Error getting Service Engine XML root element",
054: module);
055: return;
056: }
057:
058: List serviceGroupElements = UtilXml.childElementList(
059: rootElement, "service-groups");
060: Iterator groupsIter = serviceGroupElements.iterator();
061: while (groupsIter.hasNext()) {
062: Element serviceGroupElement = (Element) groupsIter.next();
063: ResourceHandler handler = new MainResourceHandler(
064: ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME,
065: serviceGroupElement);
066: addGroupDefinitions(handler);
067: }
068:
069: // get all of the component resource group stuff, ie specified in each ofbiz-component.xml file
070: List componentResourceInfos = ComponentConfig
071: .getAllServiceResourceInfos("group");
072: Iterator componentResourceInfoIter = componentResourceInfos
073: .iterator();
074: while (componentResourceInfoIter.hasNext()) {
075: ComponentConfig.ServiceResourceInfo componentResourceInfo = (ComponentConfig.ServiceResourceInfo) componentResourceInfoIter
076: .next();
077: addGroupDefinitions(componentResourceInfo
078: .createResourceHandler());
079: }
080: }
081:
082: public static void addGroupDefinitions(ResourceHandler handler) {
083: Element rootElement = null;
084:
085: try {
086: rootElement = handler.getDocument().getDocumentElement();
087: } catch (GenericConfigException e) {
088: Debug.logError(e, module);
089: return;
090: }
091: List groupList = UtilXml.childElementList(rootElement, "group");
092: Iterator groupIt = groupList.iterator();
093: int numDefs = 0;
094:
095: while (groupIt.hasNext()) {
096: Element group = (Element) groupIt.next();
097: String groupName = group.getAttribute("name");
098: groupsCache.put(groupName, new GroupModel(group));
099: numDefs++;
100: }
101: if (Debug.importantOn()) {
102: String resourceLocation = handler.getLocation();
103: try {
104: resourceLocation = handler.getURL().toExternalForm();
105: } catch (GenericConfigException e) {
106: Debug.logError(e, "Could not get resource URL", module);
107: }
108: Debug.logImportant("Loaded " + numDefs
109: + " Group definitions from " + resourceLocation,
110: module);
111: }
112: }
113:
114: public static GroupModel getGroupModel(String serviceName) {
115: if (groupsCache.size() == 0) {
116: ServiceGroupReader.readConfig();
117: }
118: return (GroupModel) groupsCache.get(serviceName);
119: }
120: }
|