KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: BSFEngine.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.cache.UtilCache;
33 import org.ofbiz.base.util.UtilURL;
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 com.ibm.bsf.BSFException;
40 import com.ibm.bsf.BSFManager;
41
42 /**
43  * BSF Service Engine
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 2.1
48  */

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

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

68     public Map JavaDoc runSync(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
69         Object JavaDoc result = serviceInvoker(localName, modelService, context);
70
71         if (result == null || !(result instanceof Map JavaDoc))
72             throw new GenericServiceException("Service did not return expected result");
73         return (Map JavaDoc) result;
74     }
75     
76     // Invoke the BSF Script.
77
private Object JavaDoc serviceInvoker(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
78         if (modelService.location == null || modelService.invoke == null)
79             throw new GenericServiceException("Cannot locate service to invoke");
80
81         // get the DispatchContext from the localName and ServiceDispatcher
82
DispatchContext dctx = dispatcher.getLocalContext(localName);
83         
84         // get the classloader to use
85
ClassLoader JavaDoc cl = null;
86
87         if (dctx == null) {
88             cl = this.getClass().getClassLoader();
89         } else {
90             cl = dctx.getClassLoader();
91         }
92
93         String JavaDoc location = this.getLocation(modelService);
94
95         // create the manager object and set the classloader
96
BSFManager mgr = new BSFManager();
97         mgr.setClassLoader(cl);
98
99         mgr.registerBean("dctx", dctx);
100         mgr.registerBean("context", context);
101         
102         // pre-load the engine to make sure we were called right
103
com.ibm.bsf.BSFEngine bsfEngine = null;
104         try {
105             bsfEngine = mgr.loadScriptingEngine(modelService.engineName);
106         } catch (BSFException e) {
107             throw new GenericServiceException("Problems loading com.ibm.bsf.BSFEngine: " + modelService.engineName, e);
108         }
109         
110         // source the script into a string
111
String JavaDoc script = (String JavaDoc) scriptCache.get(localName + "_" + location);
112
113         if (script == null) {
114             synchronized (this) {
115                 script = (String JavaDoc) scriptCache.get(localName + "_" + location);
116                 if (script == null) {
117                     URL JavaDoc scriptUrl = UtilURL.fromResource(location, cl);
118
119                     if (scriptUrl != null) {
120                         try {
121                             HttpClient http = new HttpClient(scriptUrl);
122                             script = http.get();
123                         } catch (HttpClientException e) {
124                             throw new GenericServiceException("Cannot read script from resource", e);
125                         }
126                     } else {
127                         throw new GenericServiceException("Cannot read script, resource [" + location + "] not found");
128                     }
129                     if (script == null || script.length() < 2) {
130                         throw new GenericServiceException("Null or empty script");
131                     }
132                     scriptCache.put(localName + "_" + location, script);
133                 }
134             }
135         }
136         
137         // now invoke the script
138
try {
139             bsfEngine.exec(location, 0, 0, script);
140         } catch (BSFException e) {
141             throw new GenericServiceException("Script invocation error", e);
142         }
143         
144         return mgr.lookupBean("response");
145     }
146 }
147
Popular Tags