1 25 package org.ofbiz.service.eca; 26 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import java.util.TreeSet ; 34 import java.util.Collection ; 35 36 import org.ofbiz.base.component.ComponentConfig; 37 import org.ofbiz.base.config.GenericConfigException; 38 import org.ofbiz.base.config.MainResourceHandler; 39 import org.ofbiz.base.config.ResourceHandler; 40 import org.ofbiz.service.DispatchContext; 41 import org.ofbiz.service.GenericServiceException; 42 import org.ofbiz.service.config.ServiceConfigUtil; 43 import org.ofbiz.base.util.Debug; 44 import org.ofbiz.base.util.UtilXml; 45 import org.ofbiz.base.util.cache.UtilCache; 46 47 import org.w3c.dom.Element ; 48 49 57 public class ServiceEcaUtil { 58 59 public static final String module = ServiceEcaUtil.class.getName(); 60 61 public static UtilCache ecaCache = new UtilCache("service.ServiceECAs", 0, 0, false); 62 63 public static void reloadConfig() { 64 ecaCache.clear(); 65 readConfig(); 66 } 67 68 public static void readConfig() { 69 Element rootElement = null; 70 try { 71 rootElement = ServiceConfigUtil.getXmlRootElement(); 72 } catch (GenericConfigException e) { 73 Debug.logError(e, "Error getting Service Engine XML root element", module); 74 return; 75 } 76 77 List serviceEcasElements = UtilXml.childElementList(rootElement, "service-ecas"); 78 Iterator secasIter = serviceEcasElements.iterator(); 79 while (secasIter.hasNext()) { 80 Element serviceEcasElement = (Element ) secasIter.next(); 81 ResourceHandler handler = new MainResourceHandler(ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME, serviceEcasElement); 82 addEcaDefinitions(handler); 83 } 84 85 List componentResourceInfos = ComponentConfig.getAllServiceResourceInfos("eca"); 87 Iterator componentResourceInfoIter = componentResourceInfos.iterator(); 88 while (componentResourceInfoIter.hasNext()) { 89 ComponentConfig.ServiceResourceInfo componentResourceInfo = (ComponentConfig.ServiceResourceInfo) componentResourceInfoIter.next(); 90 addEcaDefinitions(componentResourceInfo.createResourceHandler()); 91 } 92 } 93 94 public static void addEcaDefinitions(ResourceHandler handler) { 95 Element rootElement = null; 96 try { 97 rootElement = handler.getDocument().getDocumentElement(); 98 } catch (GenericConfigException e) { 99 Debug.logError(e, module); 100 return; 101 } 102 103 List ecaList = UtilXml.childElementList(rootElement, "eca"); 104 Iterator ecaIt = ecaList.iterator(); 105 int numDefs = 0; 106 while (ecaIt.hasNext()) { 107 Element e = (Element ) ecaIt.next(); 108 String serviceName = e.getAttribute("service"); 109 String eventName = e.getAttribute("event"); 110 Map eventMap = (Map ) ecaCache.get(serviceName); 111 List rules = null; 112 113 if (eventMap == null) { 114 eventMap = new HashMap (); 115 rules = new LinkedList (); 116 ecaCache.put(serviceName, eventMap); 117 eventMap.put(eventName, rules); 118 } else { 119 rules = (List ) eventMap.get(eventName); 120 if (rules == null) { 121 rules = new LinkedList (); 122 eventMap.put(eventName, rules); 123 } 124 } 125 rules.add(new ServiceEcaRule(e)); 126 numDefs++; 127 } 128 if (Debug.importantOn()) { 129 String resourceLocation = handler.getLocation(); 130 try { 131 resourceLocation = handler.getURL().toExternalForm(); 132 } catch (GenericConfigException e) { 133 Debug.logError(e, "Could not get resource URL", module); 134 } 135 Debug.logImportant("Loaded " + numDefs + " Service ECA definitions from " + resourceLocation, module); 136 } 137 } 138 139 public static Map getServiceEventMap(String serviceName) { 140 if (ServiceEcaUtil.ecaCache == null) ServiceEcaUtil.readConfig(); 141 return (Map ) ServiceEcaUtil.ecaCache.get(serviceName); 142 } 143 144 public static Collection getServiceEventRules(String serviceName, String event) { 145 Map eventMap = getServiceEventMap(serviceName); 146 if (eventMap != null) { 147 if (event != null) { 148 return (Collection ) eventMap.get(event); 149 } else { 150 return eventMap.values(); 151 } 152 } 153 return null; 154 } 155 156 public static void evalRules(String serviceName, Map eventMap, String event, DispatchContext dctx, Map context, Map result, boolean isError, boolean isFailure) throws GenericServiceException { 157 if (eventMap == null) eventMap = getServiceEventMap(serviceName); 159 if (eventMap == null || eventMap.size() == 0) { 160 return; 161 } 162 163 List rules = (List ) eventMap.get(event); 164 if (rules == null || rules.size() == 0) { 165 return; 166 } 167 168 Iterator i = rules.iterator(); 169 if (i.hasNext() && Debug.verboseOn()) Debug.logVerbose("Running ECA (" + event + ").", module); 170 Set actionsRun = new TreeSet (); 171 while (i.hasNext()) { 172 ServiceEcaRule eca = (ServiceEcaRule) i.next(); 173 eca.eval(serviceName, dctx, context, result, isError, isFailure, actionsRun); 174 } 175 } 176 } 177 | Popular Tags |