001: /*
002: * $Id: ServiceEcaUtil.java,v 1.3 2003/09/02 04:29:28 jonesde Exp $
003: *
004: * Copyright (c) 2002-2003 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: */
025: package org.ofbiz.service.eca;
026:
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.LinkedList;
030: import java.util.List;
031: import java.util.Map;
032: import java.util.Set;
033: import java.util.TreeSet;
034:
035: import org.ofbiz.base.component.ComponentConfig;
036: import org.ofbiz.base.config.GenericConfigException;
037: import org.ofbiz.base.config.MainResourceHandler;
038: import org.ofbiz.base.config.ResourceHandler;
039: import org.ofbiz.service.DispatchContext;
040: import org.ofbiz.service.GenericServiceException;
041: import org.ofbiz.service.config.ServiceConfigUtil;
042: import org.ofbiz.base.util.Debug;
043: import org.ofbiz.base.util.UtilXml;
044: import org.w3c.dom.Element;
045:
046: /**
047: * ServiceEcaUtil
048: *
049: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
050: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
051: * @version $Revision: 1.3 $
052: * @since 2.0
053: */
054: public class ServiceEcaUtil {
055:
056: public static final String module = ServiceEcaUtil.class.getName();
057:
058: //NOTE: the Service ECA cache is a Map instead of a UtilCache because there is no way to
059: // reload ecas for inidividual services if their cache line is cleared, leading to potential
060: // problems; the symantics of UtilCaches dictate that the user of the cache should know how
061: // to reload ANY cleared cache line because a user could muck up the system pretty bad otherwise
062: public static Map ecaCache = null;
063:
064: //public static UtilCache ecaCache = new UtilCache("service.ServiceECAs", 0, 0, false);
065:
066: public static void reloadConfig() {
067: ServiceEcaUtil.ecaCache = null;
068: readConfig();
069: }
070:
071: public static void readConfig() {
072: ServiceEcaUtil.ecaCache = new HashMap();
073:
074: Element rootElement = null;
075: try {
076: rootElement = ServiceConfigUtil.getXmlRootElement();
077: } catch (GenericConfigException e) {
078: Debug.logError(e,
079: "Error getting Service Engine XML root element",
080: module);
081: return;
082: }
083:
084: List serviceEcasElements = UtilXml.childElementList(
085: rootElement, "service-ecas");
086: Iterator secasIter = serviceEcasElements.iterator();
087: while (secasIter.hasNext()) {
088: Element serviceEcasElement = (Element) secasIter.next();
089: ResourceHandler handler = new MainResourceHandler(
090: ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME,
091: serviceEcasElement);
092: addEcaDefinitions(handler);
093: }
094:
095: // get all of the component resource eca stuff, ie specified in each ofbiz-component.xml file
096: List componentResourceInfos = ComponentConfig
097: .getAllServiceResourceInfos("eca");
098: Iterator componentResourceInfoIter = componentResourceInfos
099: .iterator();
100: while (componentResourceInfoIter.hasNext()) {
101: ComponentConfig.ServiceResourceInfo componentResourceInfo = (ComponentConfig.ServiceResourceInfo) componentResourceInfoIter
102: .next();
103: addEcaDefinitions(componentResourceInfo
104: .createResourceHandler());
105: }
106: }
107:
108: public static void addEcaDefinitions(ResourceHandler handler) {
109: Element rootElement = null;
110: try {
111: rootElement = handler.getDocument().getDocumentElement();
112: } catch (GenericConfigException e) {
113: Debug.logError(e, module);
114: return;
115: }
116:
117: List ecaList = UtilXml.childElementList(rootElement, "eca");
118: Iterator ecaIt = ecaList.iterator();
119: int numDefs = 0;
120: while (ecaIt.hasNext()) {
121: Element e = (Element) ecaIt.next();
122: String serviceName = e.getAttribute("service");
123: String eventName = e.getAttribute("event");
124: Map eventMap = (Map) ecaCache.get(serviceName);
125: List rules = null;
126:
127: if (eventMap == null) {
128: eventMap = new HashMap();
129: rules = new LinkedList();
130: ecaCache.put(serviceName, eventMap);
131: eventMap.put(eventName, rules);
132: } else {
133: rules = (List) eventMap.get(eventName);
134: if (rules == null) {
135: rules = new LinkedList();
136: eventMap.put(eventName, rules);
137: }
138: }
139: rules.add(new ServiceEcaRule(e));
140: numDefs++;
141: }
142: if (Debug.importantOn()) {
143: String resourceLocation = handler.getLocation();
144: try {
145: resourceLocation = handler.getURL().toExternalForm();
146: } catch (GenericConfigException e) {
147: Debug.logError(e, "Could not get resource URL", module);
148: }
149: Debug.logImportant("Loaded " + numDefs
150: + " Service ECA definitions from "
151: + resourceLocation, module);
152: }
153: }
154:
155: public static Map getServiceEventMap(String serviceName) {
156: if (ServiceEcaUtil.ecaCache == null)
157: ServiceEcaUtil.readConfig();
158: return (Map) ServiceEcaUtil.ecaCache.get(serviceName);
159: }
160:
161: public static void evalRules(String serviceName, Map eventMap,
162: String event, DispatchContext dctx, Map context,
163: Map result, boolean isError) 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: actionsRun);
184: }
185: }
186: }
|