KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > engine > AbstractEngine


1 /*
2  * $Id: AbstractEngine.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004-2005 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.engine;
26
27 import java.util.Map JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import javolution.util.FastMap;
32
33 import org.ofbiz.service.ServiceDispatcher;
34 import org.ofbiz.service.ModelService;
35 import org.ofbiz.service.GenericServiceException;
36 import org.ofbiz.service.GenericServiceCallback;
37 import org.ofbiz.service.config.ServiceConfigUtil;
38 import org.ofbiz.base.config.GenericConfigException;
39 import org.ofbiz.base.util.Debug;
40 import org.ofbiz.base.util.UtilXml;
41
42 import org.w3c.dom.Element JavaDoc;
43
44 /**
45  * Abstract Service Engine
46  *
47  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
48  * @version $Rev: 5462 $
49  * @since 3.1
50  */

51 public abstract class AbstractEngine implements GenericEngine {
52
53     public static final String JavaDoc module = AbstractEngine.class.getName();
54     protected static Map JavaDoc locationMap = null;
55
56     protected ServiceDispatcher dispatcher = null;
57
58     protected AbstractEngine(ServiceDispatcher dispatcher) {
59         this.dispatcher = dispatcher;
60         initLocations();
61     }
62
63     // creates the location alias map
64
protected synchronized void initLocations() {
65         if (locationMap == null) {
66             locationMap = FastMap.newInstance();
67
68             Element JavaDoc root = null;
69             try {
70                 root = ServiceConfigUtil.getXmlRootElement();
71             } catch (GenericConfigException e) {
72                 Debug.logError(e, module);
73             }
74
75             if (root != null) {
76                 List JavaDoc locationElements = UtilXml.childElementList(root, "service-location");
77                 if (locationElements != null) {
78                     Iterator JavaDoc i = locationElements.iterator();
79                     while (i.hasNext()) {
80                         Element JavaDoc e = (Element JavaDoc) i.next();
81                         locationMap.put(e.getAttribute("name"), e.getAttribute("location"));
82                     }
83                 }
84             }
85             Debug.logInfo("Loaded Service Locations : " + locationMap, module);
86         }
87     }
88
89     // uses the lookup map to determin if the location has been aliased in serviceconfig.xml
90
protected String JavaDoc getLocation(ModelService model) {
91         if (locationMap.containsKey(model.location)) {
92             return (String JavaDoc) locationMap.get(model.location);
93         } else {
94             return model.location;
95         }
96     }
97
98     /**
99      * @see org.ofbiz.service.engine.GenericEngine#sendCallbacks(org.ofbiz.service.ModelService, java.util.Map, java.lang.Object, int)
100      */

101     public void sendCallbacks(ModelService model, Map JavaDoc context, Object JavaDoc cbObj, int mode) throws GenericServiceException {
102         List JavaDoc callbacks = dispatcher.getCallbacks(model.name);
103         if (callbacks != null) {
104             Iterator JavaDoc i = callbacks.iterator();
105             while (i.hasNext()) {
106                 GenericServiceCallback gsc = (GenericServiceCallback) i.next();
107                 if (gsc.isEnabled()) {
108                     if (cbObj == null) {
109                         gsc.receiveEvent(context);
110                     } else if (cbObj instanceof Throwable JavaDoc) {
111                         gsc.receiveEvent(context, (Throwable JavaDoc) cbObj);
112                     } else if (cbObj instanceof Map JavaDoc) {
113                         gsc.receiveEvent(context, (Map JavaDoc) cbObj);
114                     } else {
115                         throw new GenericServiceException("Callback object is not Throwable or Map");
116                     }
117                 } else {
118                     i.remove();
119                 }
120             }
121         }
122     }
123 }
124
Popular Tags