KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > SimpleServiceEngine


1 /*
2  * $Id: SimpleServiceEngine.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 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.minilang;
26
27 import java.util.Map JavaDoc;
28
29 import org.ofbiz.service.DispatchContext;
30 import org.ofbiz.service.GenericServiceException;
31 import org.ofbiz.service.ModelService;
32 import org.ofbiz.service.ServiceDispatcher;
33 import org.ofbiz.service.engine.GenericAsyncEngine;
34
35 /**
36  * Standard Java Static Method Service Engine
37  *
38  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
39  * @version $Rev: 5462 $
40  * @since 2.0
41  */

42 public final class SimpleServiceEngine extends GenericAsyncEngine {
43
44     /** Creates new Engine */
45     public SimpleServiceEngine(ServiceDispatcher dispatcher) {
46         super(dispatcher);
47     }
48
49     /** Run the service synchronously and IGNORE the result
50      * @param context Map of name, value pairs composing the context
51      */

52     public void runSyncIgnore(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
53         Map JavaDoc result = runSync(localName, modelService, context);
54     }
55
56     /** Run the service synchronously and return the result
57      * @param context Map of name, value pairs composing the context
58      * @return Map of name, value pairs composing the result
59      */

60     public Map JavaDoc runSync(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
61         Object JavaDoc result = serviceInvoker(localName, modelService, context);
62         if (result == null || !(result instanceof Map JavaDoc))
63             throw new GenericServiceException("Service did not return expected result");
64         return (Map JavaDoc) result;
65     }
66
67     // Invoke the simple method from a service context
68
private Object JavaDoc serviceInvoker(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
69         // static java service methods should be: public Map methodName(DispatchContext dctx, Map context)
70
DispatchContext dctx = dispatcher.getLocalContext(localName);
71
72         // check the package and method names
73
if (modelService.location == null || modelService.invoke == null)
74             throw new GenericServiceException("Cannot locate service to invoke (location or invoke name missing)");
75
76         // get the classloader to use
77
ClassLoader JavaDoc classLoader = null;
78
79         if (dctx != null)
80             classLoader = dctx.getClassLoader();
81
82         // if the classLoader is null, no big deal, SimpleMethod will use the
83
// current thread's ClassLoader by default if null passed in
84

85         try {
86             return SimpleMethod.runSimpleService(this.getLocation(modelService), modelService.invoke, dctx, context, classLoader);
87         } catch (MiniLangException e) {
88             throw new GenericServiceException("Error running simple method [" + modelService.invoke +
89                     "] in XML file [" + modelService.location + "]: ", e);
90         }
91     }
92 }
93
Popular Tags