KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > eca > ServiceEcaUtil


1 /*
2  * $Id: ServiceEcaUtil.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.service.eca;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.TreeSet JavaDoc;
34 import java.util.Collection JavaDoc;
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 JavaDoc;
48
49 /**
50  * ServiceEcaUtil
51  *
52  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
53  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
54  * @version $Rev: 5462 $
55  * @since 2.0
56  */

57 public class ServiceEcaUtil {
58
59     public static final String JavaDoc 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 JavaDoc 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 JavaDoc serviceEcasElements = UtilXml.childElementList(rootElement, "service-ecas");
78         Iterator JavaDoc secasIter = serviceEcasElements.iterator();
79         while (secasIter.hasNext()) {
80             Element JavaDoc serviceEcasElement = (Element JavaDoc) secasIter.next();
81             ResourceHandler handler = new MainResourceHandler(ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME, serviceEcasElement);
82             addEcaDefinitions(handler);
83         }
84
85         // get all of the component resource eca stuff, ie specified in each ofbiz-component.xml file
86
List JavaDoc componentResourceInfos = ComponentConfig.getAllServiceResourceInfos("eca");
87         Iterator JavaDoc 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 JavaDoc rootElement = null;
96         try {
97             rootElement = handler.getDocument().getDocumentElement();
98         } catch (GenericConfigException e) {
99             Debug.logError(e, module);
100             return;
101         }
102
103         List JavaDoc ecaList = UtilXml.childElementList(rootElement, "eca");
104         Iterator JavaDoc ecaIt = ecaList.iterator();
105         int numDefs = 0;
106         while (ecaIt.hasNext()) {
107             Element JavaDoc e = (Element JavaDoc) ecaIt.next();
108             String JavaDoc serviceName = e.getAttribute("service");
109             String JavaDoc eventName = e.getAttribute("event");
110             Map JavaDoc eventMap = (Map JavaDoc) ecaCache.get(serviceName);
111             List JavaDoc rules = null;
112
113             if (eventMap == null) {
114                 eventMap = new HashMap JavaDoc();
115                 rules = new LinkedList JavaDoc();
116                 ecaCache.put(serviceName, eventMap);
117                 eventMap.put(eventName, rules);
118             } else {
119                 rules = (List JavaDoc) eventMap.get(eventName);
120                 if (rules == null) {
121                     rules = new LinkedList JavaDoc();
122                     eventMap.put(eventName, rules);
123                 }
124             }
125             rules.add(new ServiceEcaRule(e));
126             numDefs++;
127         }
128         if (Debug.importantOn()) {
129             String JavaDoc 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 JavaDoc getServiceEventMap(String JavaDoc serviceName) {
140         if (ServiceEcaUtil.ecaCache == null) ServiceEcaUtil.readConfig();
141         return (Map JavaDoc) ServiceEcaUtil.ecaCache.get(serviceName);
142     }
143
144     public static Collection JavaDoc getServiceEventRules(String JavaDoc serviceName, String JavaDoc event) {
145         Map JavaDoc eventMap = getServiceEventMap(serviceName);
146         if (eventMap != null) {
147             if (event != null) {
148                 return (Collection JavaDoc) eventMap.get(event);
149             } else {
150                 return eventMap.values();
151             }
152         }
153         return null;
154     }
155
156     public static void evalRules(String JavaDoc serviceName, Map JavaDoc eventMap, String JavaDoc event, DispatchContext dctx, Map JavaDoc context, Map JavaDoc result, boolean isError, boolean isFailure) throws GenericServiceException {
157         // if the eventMap is passed we save a HashMap lookup, but if not that's okay we'll just look it up now
158
if (eventMap == null) eventMap = getServiceEventMap(serviceName);
159         if (eventMap == null || eventMap.size() == 0) {
160             return;
161         }
162
163         List JavaDoc rules = (List JavaDoc) eventMap.get(event);
164         if (rules == null || rules.size() == 0) {
165             return;
166         }
167
168         Iterator JavaDoc i = rules.iterator();
169         if (i.hasNext() && Debug.verboseOn()) Debug.logVerbose("Running ECA (" + event + ").", module);
170         Set JavaDoc actionsRun = new TreeSet JavaDoc();
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