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.eca;
019:
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026: import java.util.TreeSet;
027: import java.util.Collection;
028:
029: import javolution.util.FastMap;
030:
031: import org.ofbiz.base.component.ComponentConfig;
032: import org.ofbiz.base.config.GenericConfigException;
033: import org.ofbiz.base.config.MainResourceHandler;
034: import org.ofbiz.base.config.ResourceHandler;
035: import org.ofbiz.service.DispatchContext;
036: import org.ofbiz.service.GenericServiceException;
037: import org.ofbiz.service.config.ServiceConfigUtil;
038: import org.ofbiz.base.util.Debug;
039: import org.ofbiz.base.util.UtilXml;
040: import org.ofbiz.base.util.cache.UtilCache;
041:
042: import org.w3c.dom.Element;
043:
044: /**
045: * ServiceEcaUtil
046: */
047: public class ServiceEcaUtil {
048:
049: public static final String module = ServiceEcaUtil.class.getName();
050:
051: // using a cache is dangerous here because if someone clears it the ECAs won't run: public static UtilCache ecaCache = new UtilCache("service.ServiceECAs", 0, 0, false);
052: public static Map ecaCache = FastMap.newInstance();
053:
054: public static void reloadConfig() {
055: ecaCache.clear();
056: readConfig();
057: }
058:
059: public static void readConfig() {
060: Element rootElement = null;
061: try {
062: rootElement = ServiceConfigUtil.getXmlRootElement();
063: } catch (GenericConfigException e) {
064: Debug.logError(e,
065: "Error getting Service Engine XML root element",
066: module);
067: return;
068: }
069:
070: List serviceEcasElements = UtilXml.childElementList(
071: rootElement, "service-ecas");
072: Iterator secasIter = serviceEcasElements.iterator();
073: while (secasIter.hasNext()) {
074: Element serviceEcasElement = (Element) secasIter.next();
075: ResourceHandler handler = new MainResourceHandler(
076: ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME,
077: serviceEcasElement);
078: addEcaDefinitions(handler);
079: }
080:
081: // get all of the component resource eca stuff, ie specified in each ofbiz-component.xml file
082: List componentResourceInfos = ComponentConfig
083: .getAllServiceResourceInfos("eca");
084: Iterator componentResourceInfoIter = componentResourceInfos
085: .iterator();
086: while (componentResourceInfoIter.hasNext()) {
087: ComponentConfig.ServiceResourceInfo componentResourceInfo = (ComponentConfig.ServiceResourceInfo) componentResourceInfoIter
088: .next();
089: addEcaDefinitions(componentResourceInfo
090: .createResourceHandler());
091: }
092: }
093:
094: public static void addEcaDefinitions(ResourceHandler handler) {
095: Element rootElement = null;
096: try {
097: rootElement = handler.getDocument().getDocumentElement();
098: } catch (GenericConfigException e) {
099: 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
136: + " Service ECA definitions from "
137: + resourceLocation, module);
138: }
139: }
140:
141: public static Map getServiceEventMap(String serviceName) {
142: if (ServiceEcaUtil.ecaCache == null)
143: ServiceEcaUtil.readConfig();
144: return (Map) ServiceEcaUtil.ecaCache.get(serviceName);
145: }
146:
147: public static Collection getServiceEventRules(String serviceName,
148: String event) {
149: Map eventMap = getServiceEventMap(serviceName);
150: if (eventMap != null) {
151: if (event != null) {
152: return (Collection) eventMap.get(event);
153: } else {
154: return eventMap.values();
155: }
156: }
157: return null;
158: }
159:
160: public static void evalRules(String serviceName, Map eventMap,
161: String event, DispatchContext dctx, Map context,
162: Map result, boolean isError, boolean isFailure)
163: throws GenericServiceException {
164: // if the eventMap is passed we save a HashMap lookup, but if not that's okay we'll just look it up now
165: if (eventMap == null)
166: eventMap = getServiceEventMap(serviceName);
167: if (eventMap == null || eventMap.size() == 0) {
168: return;
169: }
170:
171: List rules = (List) eventMap.get(event);
172: if (rules == null || rules.size() == 0) {
173: return;
174: }
175:
176: Iterator i = rules.iterator();
177: if (i.hasNext() && Debug.verboseOn())
178: Debug.logVerbose("Running ECA (" + event + ").", module);
179: Set actionsRun = new TreeSet();
180: while (i.hasNext()) {
181: ServiceEcaRule eca = (ServiceEcaRule) i.next();
182: eca.eval(serviceName, dctx, context, result, isError,
183: isFailure, actionsRun);
184: }
185: }
186: }
|