1 24 package org.ofbiz.service.group; 25 26 import java.util.Iterator ; 27 import java.util.List ; 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 ; 38 39 46 public class ServiceGroupReader { 47 48 public static final String 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 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 serviceGroupElements = UtilXml.childElementList(rootElement, "service-groups"); 63 Iterator groupsIter = serviceGroupElements.iterator(); 64 while (groupsIter.hasNext()) { 65 Element serviceGroupElement = (Element ) groupsIter.next(); 66 ResourceHandler handler = new MainResourceHandler(ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME, serviceGroupElement); 67 addGroupDefinitions(handler); 68 } 69 70 List componentResourceInfos = ComponentConfig.getAllServiceResourceInfos("group"); 72 Iterator 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 rootElement = null; 81 82 try { 83 rootElement = handler.getDocument().getDocumentElement(); 84 } catch (GenericConfigException e) { 85 Debug.logError(e, module); 86 return; 87 } 88 List groupList = UtilXml.childElementList(rootElement, "group"); 89 Iterator groupIt = groupList.iterator(); 90 int numDefs = 0; 91 92 while (groupIt.hasNext()) { 93 Element group = (Element ) groupIt.next(); 94 String groupName = group.getAttribute("name"); 95 groupsCache.put(groupName, new GroupModel(group)); 96 numDefs++; 97 } 98 if (Debug.importantOn()) { 99 String 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 serviceName) { 110 if (groupsCache.size() == 0) { 111 ServiceGroupReader.readConfig(); 112 } 113 return (GroupModel) groupsCache.get(serviceName); 114 } 115 } 116 | Popular Tags |