KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > DispatchContext


1 /*
2  * $Id: DispatchContext.java 5720 2005-09-13 03:10:59Z jonesde $
3  *
4  * Copyright (c) 2001-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;
26
27 import java.io.Serializable JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.TreeSet JavaDoc;
35 import javax.wsdl.WSDLException;
36
37 import javolution.util.FastMap;
38
39 import org.ofbiz.base.component.ComponentConfig;
40 import org.ofbiz.base.config.GenericConfigException;
41 import org.ofbiz.base.config.MainResourceHandler;
42 import org.ofbiz.base.config.ResourceHandler;
43 import org.ofbiz.base.util.Debug;
44 import org.ofbiz.base.util.UtilXml;
45 import org.ofbiz.base.util.cache.UtilCache;
46 import org.ofbiz.entity.GenericDelegator;
47 import org.ofbiz.security.Security;
48 import org.ofbiz.service.config.ServiceConfigUtil;
49 import org.ofbiz.service.eca.ServiceEcaUtil;
50
51 import org.w3c.dom.Document JavaDoc;
52 import org.w3c.dom.Element JavaDoc;
53
54 /**
55  * Dispatcher Context
56  *
57  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
58  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
59  * @version $Rev: 5720 $
60  * @since 2.0
61  */

62 public class DispatchContext implements Serializable JavaDoc {
63
64     public static final String JavaDoc module = DispatchContext.class.getName();
65
66     protected static final String JavaDoc GLOBAL_KEY = "global.services";
67     protected static UtilCache modelService = new UtilCache("service.ModelServices", 0, 0, false);
68
69     protected transient LocalDispatcher dispatcher;
70     protected transient ClassLoader JavaDoc loader;
71     protected Collection JavaDoc readers;
72     protected Map JavaDoc attributes;
73     protected String JavaDoc name;
74
75     /**
76      * Creates new DispatchContext
77      * @param readers a collection of reader URLs
78      * @param loader the classloader to use for dispatched services
79      */

80     public DispatchContext(String JavaDoc name, Collection JavaDoc readers, ClassLoader JavaDoc loader, LocalDispatcher dispatcher) {
81         this.name = name;
82         this.readers = readers;
83         this.loader = loader;
84         this.dispatcher = dispatcher;
85         this.attributes = FastMap.newInstance();
86     }
87
88     public void loadReaders() {
89         Map JavaDoc localService = addReaders(readers);
90         if (localService != null) {
91             modelService.put(name, localService);
92         }
93         
94         Map JavaDoc globalService = addGlobal();
95         if (globalService != null) {
96             modelService.put(GLOBAL_KEY, globalService);
97         }
98     }
99
100     /**
101      * Returns the service attribute for the given name, or null if there is no attribute by that name.
102      * @param name a String specifying the name of the attribute
103      * @return an Object conatining the value of the attribute, or null if there is no attribute by that name.
104      */

105     public Object JavaDoc getAttribute(String JavaDoc name) {
106         if (attributes.containsKey(name))
107             return attributes.get(name);
108         return null;
109     }
110
111     /**
112      * Binds an object to a given attribute name in this context.
113      * @param name a String specifying the name of the attribute
114      * @param object an Object representing the attribute to be bound.
115      */

116     public void setAttribute(String JavaDoc name, Object JavaDoc object) {
117         attributes.put(name, object);
118     }
119
120     /**
121      * Gets the classloader of this context
122      * @return ClassLoader of the context
123      */

124     public ClassLoader JavaDoc getClassLoader() {
125         return this.loader;
126     }
127
128     /**
129      * Gets the collection of readers associated with this context
130      * @return Collection of reader URLs
131      */

132     public Collection JavaDoc getReaders() {
133         return readers;
134     }
135
136     /**
137      * Gets the name of the local dispatcher
138      * @return String name of the LocalDispatcher object
139      */

140     public String JavaDoc getName() {
141         return name;
142     }
143     
144     /**
145      * Uses an existing map of name value pairs and extracts the keys which are used in serviceName
146      * Note: This goes not guarantee the context will be 100% valid, there may be missing fields
147      * @param serviceName The name of the service to obtain parameters for
148      * @param mode The mode to use for building the new map (i.e. can be IN or OUT)
149      * @param context The initial set of values to pull from
150      * @return Map contains any valid values
151      * @throws GenericServiceException
152      */

153     public Map JavaDoc makeValidContext(String JavaDoc serviceName, String JavaDoc mode, Map JavaDoc context) throws GenericServiceException {
154         ModelService model = this.getModelService(serviceName);
155         return makeValidContext(model, mode, context);
156         
157     }
158     
159     /**
160      * Uses an existing map of name value pairs and extracts the keys which are used in serviceName
161      * Note: This goes not guarantee the context will be 100% valid, there may be missing fields
162      * @param model The ModelService object of the service to obtain parameters for
163      * @param mode The mode to use for building the new map (i.e. can be IN or OUT)
164      * @param context The initial set of values to pull from
165      * @return Map contains any valid values
166      * @throws GenericServiceException
167      */

168     public Map JavaDoc makeValidContext(ModelService model, String JavaDoc mode, Map JavaDoc context) throws GenericServiceException {
169         Map JavaDoc newContext = null;
170         
171         int modeInt = 0;
172         if (mode.equalsIgnoreCase("in")) {
173             modeInt = 1;
174         } else if (mode.equalsIgnoreCase("out")) {
175             modeInt = 2;
176         }
177                         
178         if (model == null) {
179             throw new GenericServiceException("Model service is null! Should never happen.");
180         } else {
181             switch (modeInt) {
182                 case 2:
183                     newContext = model.makeValid(context, ModelService.OUT_PARAM, true, null);
184                     break;
185                 case 1:
186                     newContext = model.makeValid(context, ModelService.IN_PARAM, true, null);
187                     break;
188                 default:
189                     throw new GenericServiceException("Invalid mode, should be either IN or OUT");
190             }
191             return newContext;
192         }
193     }
194
195     /**
196      * Gets the ModelService instance that corresponds to given the name
197      * @param serviceName Name of the service
198      * @return GenericServiceModel that corresponds to the serviceName
199      */

200     public ModelService getModelService(String JavaDoc serviceName) throws GenericServiceException {
201         ModelService retVal = getLocalModelService(serviceName);
202         if (retVal == null) {
203             retVal = getGlobalModelService(serviceName);
204         }
205         
206         if (retVal == null) {
207             throw new GenericServiceException("Cannot locate service by name (" + serviceName + ")");
208         }
209         
210         return retVal;
211     }
212     
213     private ModelService getLocalModelService(String JavaDoc serviceName) throws GenericServiceException {
214         Map JavaDoc serviceMap = (Map JavaDoc) modelService.get(name);
215         if (serviceMap == null) {
216             synchronized (this) {
217                 serviceMap = (Map JavaDoc) modelService.get(name);
218                 if (serviceMap == null) {
219                     serviceMap = addReaders(readers);
220                     if (serviceMap != null) {
221                         modelService.put(name, serviceMap);
222                         ServiceEcaUtil.reloadConfig();
223                     }
224                 }
225             }
226         }
227         
228         ModelService retVal = null;
229         if (serviceMap != null) {
230             retVal = (ModelService) serviceMap.get(serviceName);
231             if (retVal != null && !retVal.inheritedParameters()) {
232                 retVal.interfaceUpdate(this);
233             }
234         }
235         
236         return retVal;
237     }
238
239     private ModelService getGlobalModelService(String JavaDoc serviceName) throws GenericServiceException {
240         Map JavaDoc serviceMap = (Map JavaDoc) modelService.get(GLOBAL_KEY);
241         if (serviceMap == null) {
242             synchronized (this) {
243                 serviceMap = (Map JavaDoc) modelService.get(GLOBAL_KEY);
244                 if (serviceMap == null) {
245                     serviceMap = addGlobal();
246                     if (serviceMap != null) {
247                         modelService.put(GLOBAL_KEY, serviceMap);
248                         ServiceEcaUtil.reloadConfig();
249                     }
250                 }
251             }
252         }
253
254         ModelService retVal = null;
255         if (serviceMap != null) {
256             retVal = (ModelService) serviceMap.get(serviceName);
257             if (retVal != null && !retVal.inheritedParameters()) {
258                 retVal.interfaceUpdate(this);
259             }
260         }
261         
262         return retVal;
263     }
264
265     /**
266      * Gets the LocalDispatcher used with this context
267      * @return LocalDispatcher that was used to create this context
268      */

269     public LocalDispatcher getDispatcher() {
270         return this.dispatcher;
271     }
272
273     /**
274      * Sets the LocalDispatcher used with this context
275      * @param dispatcher The LocalDispatcher to re-assign to this context
276      */

277     public void setDispatcher(LocalDispatcher dispatcher) {
278         this.dispatcher = dispatcher;
279     }
280
281     /**
282      * Gets the GenericDelegator associated with this context/dispatcher
283      * @return GenericDelegator associated with this context
284      */

285     public GenericDelegator getDelegator() {
286         return dispatcher.getDelegator();
287     }
288
289     /**
290      * Gets the Security object associated with this dispatcher
291      * @return Security object associated with this dispatcher
292      */

293     public Security getSecurity() {
294         return dispatcher.getSecurity();
295     }
296
297     private Map JavaDoc addReaders(Collection JavaDoc readerURLs) {
298         Map JavaDoc serviceMap = FastMap.newInstance();
299
300         if (readerURLs == null)
301             return null;
302         Iterator JavaDoc urlIter = readerURLs.iterator();
303
304         while (urlIter.hasNext()) {
305             URL JavaDoc readerURL = (URL JavaDoc) urlIter.next();
306
307             serviceMap.putAll(addReader(readerURL));
308         }
309         return serviceMap;
310     }
311
312     private Map JavaDoc addReader(URL JavaDoc readerURL) {
313         if (readerURL == null) {
314             Debug.logError("Cannot add reader with a null reader URL", module);
315             return null;
316         }
317
318         ModelServiceReader reader = ModelServiceReader.getModelServiceReader(readerURL, this);
319
320         if (reader == null) {
321             Debug.logError("Could not load the reader for the reader URL " + readerURL, module);
322             return null;
323         }
324
325         Map JavaDoc serviceMap = reader.getModelServices();
326
327         return serviceMap;
328     }
329
330     private Map JavaDoc addReader(ResourceHandler handler) {
331         ModelServiceReader reader = ModelServiceReader.getModelServiceReader(handler, this);
332
333         if (reader == null) {
334             Debug.logError("Could not load the reader for " + handler, module);
335             return null;
336         }
337
338         Map JavaDoc serviceMap = reader.getModelServices();
339
340         return serviceMap;
341     }
342
343     private Map JavaDoc addGlobal() {
344         Map JavaDoc globalMap = FastMap.newInstance();
345
346         Element JavaDoc rootElement = null;
347
348         try {
349             rootElement = ServiceConfigUtil.getXmlRootElement();
350         } catch (GenericConfigException e) {
351             Debug.logError(e, "Error getting Service Engine XML root element", module);
352             return null;
353         }
354
355         List JavaDoc globalServicesElements = UtilXml.childElementList(rootElement, "global-services");
356         Iterator JavaDoc gseIter = globalServicesElements.iterator();
357         while (gseIter.hasNext()) {
358             Element JavaDoc globalServicesElement = (Element JavaDoc) gseIter.next();
359             ResourceHandler handler = new MainResourceHandler(
360                     ServiceConfigUtil.SERVICE_ENGINE_XML_FILENAME, globalServicesElement);
361
362             Map JavaDoc servicesMap = addReader(handler);
363             if (servicesMap != null) {
364                 globalMap.putAll(servicesMap);
365             }
366         }
367         
368         // get all of the component resource model stuff, ie specified in each ofbiz-component.xml file
369
List JavaDoc componentResourceInfos = ComponentConfig.getAllServiceResourceInfos("model");
370         Iterator JavaDoc componentResourceInfoIter = componentResourceInfos.iterator();
371         while (componentResourceInfoIter.hasNext()) {
372             ComponentConfig.ServiceResourceInfo componentResourceInfo = (ComponentConfig.ServiceResourceInfo) componentResourceInfoIter.next();
373             Map JavaDoc servicesMap = addReader(componentResourceInfo.createResourceHandler());
374             if (servicesMap != null) {
375                 globalMap.putAll(servicesMap);
376             }
377         }
378
379         return globalMap;
380     }
381
382     public Set JavaDoc getAllServiceNames() {
383         Set JavaDoc serviceNames = new TreeSet JavaDoc();
384
385         Map JavaDoc globalServices = (Map JavaDoc) modelService.get(GLOBAL_KEY);
386         Map JavaDoc localServices = (Map JavaDoc) modelService.get(name);
387         if (globalServices != null) {
388             serviceNames.addAll(globalServices.keySet());
389         }
390         if (localServices != null) {
391             serviceNames.addAll(localServices.keySet());
392         }
393         return serviceNames;
394     }
395
396     public Document JavaDoc getWSDL(String JavaDoc serviceName, String JavaDoc locationURI) throws GenericServiceException, WSDLException {
397         ModelService model = this.getModelService(serviceName);
398         return model.toWSDL(locationURI);
399     }
400 }
Popular Tags