001: /*
002: * $Id: BSFEngine.java,v 1.2 2003/12/18 02:13:03 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.service.engine;
026:
027: import java.net.URL;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Map;
031: import java.util.Set;
032:
033: import org.ofbiz.service.DispatchContext;
034: import org.ofbiz.service.GenericServiceException;
035: import org.ofbiz.service.ModelService;
036: import org.ofbiz.service.ServiceDispatcher;
037: import org.ofbiz.base.util.HttpClient;
038: import org.ofbiz.base.util.HttpClientException;
039: import org.ofbiz.base.util.UtilCache;
040: import org.ofbiz.base.util.UtilURL;
041:
042: import com.ibm.bsf.BSFException;
043: import com.ibm.bsf.BSFManager;
044:
045: /**
046: * BSF Service Engine
047: *
048: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
049: * @version $Revision: 1.2 $
050: * @since 2.1
051: */
052: public class BSFEngine extends GenericAsyncEngine {
053:
054: public static final String module = BSFEngine.class.getName();
055: public static UtilCache scriptCache = new UtilCache("BSFScripts",
056: 0, 0);
057:
058: public BSFEngine(ServiceDispatcher dispatcher) {
059: super (dispatcher);
060: }
061:
062: /**
063: * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
064: */
065: public void runSyncIgnore(String localName,
066: ModelService modelService, Map context)
067: throws GenericServiceException {
068: Map result = runSync(localName, modelService, context);
069: }
070:
071: /**
072: * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
073: */
074: public Map runSync(String localName, ModelService modelService,
075: Map context) throws GenericServiceException {
076: Object result = serviceInvoker(localName, modelService, context);
077:
078: if (result == null || !(result instanceof Map))
079: throw new GenericServiceException(
080: "Service did not return expected result");
081: return (Map) result;
082: }
083:
084: // Invoke the BSF Script.
085: private Object serviceInvoker(String localName,
086: ModelService modelService, Map context)
087: throws GenericServiceException {
088: if (modelService.location == null
089: || modelService.invoke == null)
090: throw new GenericServiceException(
091: "Cannot locate service to invoke");
092:
093: // get the DispatchContext from the localName and ServiceDispatcher
094: DispatchContext dctx = dispatcher.getLocalContext(localName);
095:
096: // get the classloader to use
097: ClassLoader cl = null;
098:
099: if (dctx == null)
100: cl = this .getClass().getClassLoader();
101: else
102: cl = dctx.getClassLoader();
103:
104: // create the manager object and set the classloader
105: BSFManager mgr = new BSFManager();
106: mgr.setClassLoader(cl);
107:
108: mgr.registerBean("dctx", dctx);
109: mgr.registerBean("context", context);
110:
111: // pre-load the engine to make sure we were called right
112: com.ibm.bsf.BSFEngine bsfEngine = null;
113: try {
114: bsfEngine = mgr
115: .loadScriptingEngine(modelService.engineName);
116: } catch (BSFException e) {
117: throw new GenericServiceException(
118: "Problems loading com.ibm.bsf.BSFEngine: "
119: + modelService.engineName, e);
120: }
121:
122: // source the script into a string
123: String script = (String) scriptCache.get(localName + "_"
124: + modelService.location);
125:
126: if (script == null) {
127: synchronized (this ) {
128: script = (String) scriptCache.get(localName + "_"
129: + modelService.location);
130: if (script == null) {
131: URL scriptUrl = UtilURL.fromResource(
132: modelService.location, cl);
133:
134: if (scriptUrl != null) {
135: try {
136: HttpClient http = new HttpClient(scriptUrl);
137: script = http.get();
138: } catch (HttpClientException e) {
139: throw new GenericServiceException(
140: "Cannot read script from resource",
141: e);
142: }
143: } else {
144: throw new GenericServiceException(
145: "Cannot read script, resource ["
146: + modelService.location
147: + "] not found");
148: }
149: if (script == null || script.length() < 2) {
150: throw new GenericServiceException(
151: "Null or empty script");
152: }
153: scriptCache.put(localName + "_"
154: + modelService.location, script);
155: }
156: }
157: }
158:
159: // now invoke the script
160: try {
161: bsfEngine.exec(modelService.location, 0, 0, script);
162: } catch (BSFException e) {
163: throw new GenericServiceException(
164: "Script invocation error", e);
165: }
166:
167: return mgr.lookupBean("response");
168: }
169: }
|