KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: StandardJavaEngine.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.service.engine;
26
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.ofbiz.service.DispatchContext;
32 import org.ofbiz.service.GenericServiceException;
33 import org.ofbiz.service.ModelService;
34 import org.ofbiz.service.ServiceDispatcher;
35 import org.ofbiz.base.util.Debug;
36
37 /**
38  * Standard Java Static Method Service Engine
39  *
40  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
41  * @version $Rev: 5462 $
42  * @since 2.0
43  */

44 public final class StandardJavaEngine extends GenericAsyncEngine {
45
46     public static final String JavaDoc module = StandardJavaEngine.class.getName();
47
48     public StandardJavaEngine(ServiceDispatcher dispatcher) {
49         super(dispatcher);
50     }
51  
52     /**
53      * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
54      */

55     public void runSyncIgnore(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
56         Map JavaDoc result = runSync(localName, modelService, context);
57     }
58
59     /**
60      * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
61      */

62     public Map JavaDoc runSync(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
63         Object JavaDoc result = serviceInvoker(localName, modelService, context);
64
65         if (result == null || !(result instanceof Map JavaDoc))
66             throw new GenericServiceException("Service did not return expected result");
67         return (Map JavaDoc) result;
68     }
69
70     // Invoke the static java method service.
71
private Object JavaDoc serviceInvoker(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
72         // static java service methods should be: public Map methodName(DispatchContext dctx, Map context)
73
DispatchContext dctx = dispatcher.getLocalContext(localName);
74
75         if (modelService == null)
76             Debug.logError("ERROR: Null Model Service.", module);
77         if (dctx == null)
78             Debug.logError("ERROR: Null DispatchContext.", module);
79         if (context == null)
80             Debug.logError("ERROR: Null Service Context.", module);
81
82         Class JavaDoc[] paramTypes = new Class JavaDoc[] {DispatchContext.class, Map JavaDoc.class};
83         Object JavaDoc[] params = new Object JavaDoc[] {dctx, context};
84         Object JavaDoc result = null;
85
86         // check the package and method names
87
if (modelService.location == null || modelService.invoke == null)
88             throw new GenericServiceException("Cannot locate service to invoke (location or invoke name missing)");
89
90         // get the classloader to use
91
ClassLoader JavaDoc cl = null;
92
93         if (dctx == null)
94             cl = this.getClass().getClassLoader();
95         else
96             cl = dctx.getClassLoader();
97
98         try {
99             Class JavaDoc c = cl.loadClass(this.getLocation(modelService));
100             Method JavaDoc m = c.getMethod(modelService.invoke, paramTypes);
101             result = m.invoke(null, params);
102         } catch (ClassNotFoundException JavaDoc cnfe) {
103             throw new GenericServiceException("Cannot find service location", cnfe);
104         } catch (NoSuchMethodException JavaDoc nsme) {
105             throw new GenericServiceException("Service method does not exist", nsme);
106         } catch (SecurityException JavaDoc se) {
107             throw new GenericServiceException("Access denied", se);
108         } catch (IllegalAccessException JavaDoc iae) {
109             throw new GenericServiceException("Method not accessible", iae);
110         } catch (IllegalArgumentException JavaDoc iarge) {
111             throw new GenericServiceException("Invalid parameter match", iarge);
112         } catch (InvocationTargetException JavaDoc ite) {
113             throw new GenericServiceException("Service target threw an unexpected exception", ite.getTargetException());
114         } catch (NullPointerException JavaDoc npe) {
115             throw new GenericServiceException("Specified object is null", npe);
116         } catch (ExceptionInInitializerError JavaDoc eie) {
117             throw new GenericServiceException("Initialization failed", eie);
118         } catch (Throwable JavaDoc th) {
119             throw new GenericServiceException("Error or nknown exception", th);
120         }
121
122         return result;
123     }
124 }
125
126
Popular Tags