KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: BeanShellEngine.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.net.URL JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.base.util.HttpClient;
31 import org.ofbiz.base.util.HttpClientException;
32 import org.ofbiz.base.util.UtilURL;
33 import org.ofbiz.base.util.cache.UtilCache;
34 import org.ofbiz.service.DispatchContext;
35 import org.ofbiz.service.GenericServiceException;
36 import org.ofbiz.service.ModelService;
37 import org.ofbiz.service.ServiceDispatcher;
38
39 import bsh.EvalError;
40 import bsh.Interpreter;
41
42 /**
43  * BeanShell Script Service Engine
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 2.0
48  */

49 public final class BeanShellEngine extends GenericAsyncEngine {
50
51     public static UtilCache scriptCache = new UtilCache("BeanShellScripts", 0, 0);
52
53     public BeanShellEngine(ServiceDispatcher dispatcher) {
54         super(dispatcher);
55     }
56     
57     /**
58      * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
59      */

60     public void runSyncIgnore(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
61         Map JavaDoc result = runSync(localName, modelService, context);
62     }
63   
64     /**
65      * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
66      */

67     public Map JavaDoc runSync(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
68         Object JavaDoc result = serviceInvoker(localName, modelService, context);
69
70         if (result == null || !(result instanceof Map JavaDoc))
71             throw new GenericServiceException("Service did not return expected result");
72         return (Map JavaDoc) result;
73     }
74
75     // Invoke the BeanShell Script.
76
private Object JavaDoc serviceInvoker(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
77         if (modelService.location == null || modelService.invoke == null)
78             throw new GenericServiceException("Cannot locate service to invoke");
79
80         // get the DispatchContext from the localName and ServiceDispatcher
81
DispatchContext dctx = dispatcher.getLocalContext(localName);
82         
83         // get the classloader to use
84
ClassLoader JavaDoc cl = null;
85
86         if (dctx == null) {
87             cl = this.getClass().getClassLoader();
88         } else {
89             cl = dctx.getClassLoader();
90         }
91
92         String JavaDoc location = this.getLocation(modelService);
93
94         // source the script into a string
95
String JavaDoc script = (String JavaDoc) scriptCache.get(localName + "_" + location);
96
97         if (script == null) {
98             synchronized (this) {
99                 script = (String JavaDoc) scriptCache.get(localName + "_" + location);
100                 if (script == null) {
101                     URL JavaDoc scriptUrl = UtilURL.fromResource(location, cl);
102
103                     if (scriptUrl != null) {
104                         try {
105                             HttpClient http = new HttpClient(scriptUrl);
106                             script = http.get();
107                         } catch (HttpClientException e) {
108                             throw new GenericServiceException("Cannot read script from resource", e);
109                         }
110                     } else {
111                         throw new GenericServiceException("Cannot read script, resource [" + location + "] not found");
112                     }
113                     if (script == null || script.length() < 2) {
114                         throw new GenericServiceException("Null or empty script");
115                     }
116                     scriptCache.put(localName + "_" + location, script);
117                 }
118             }
119         }
120
121         Interpreter bsh = new Interpreter();
122
123         Map JavaDoc result = null;
124
125         try {
126             bsh.set("dctx", dctx); // set the dispatch context
127
bsh.set("context", context); // set the parameter context used for both IN and OUT
128
bsh.eval(script);
129             Object JavaDoc bshResult = bsh.get("result");
130
131             if ((bshResult != null) && (bshResult instanceof Map JavaDoc))
132                 context.putAll((Map JavaDoc) bshResult);
133             result = modelService.makeValid(context, ModelService.OUT_PARAM);
134         } catch (EvalError e) {
135             throw new GenericServiceException("BeanShell script threw an exception", e);
136         }
137         return result;
138     }
139
140 }
141
142
Popular Tags