1 25 package org.ofbiz.service.engine; 26 27 import java.net.URL ; 28 import java.util.Map ; 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 49 public class BSFEngine extends GenericAsyncEngine { 50 51 public static final String 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 61 public void runSyncIgnore(String localName, ModelService modelService, Map context) throws GenericServiceException { 62 Map result = runSync(localName, modelService, context); 63 } 64 65 68 public Map runSync(String localName, ModelService modelService, Map context) throws GenericServiceException { 69 Object result = serviceInvoker(localName, modelService, context); 70 71 if (result == null || !(result instanceof Map )) 72 throw new GenericServiceException("Service did not return expected result"); 73 return (Map ) result; 74 } 75 76 private Object serviceInvoker(String localName, ModelService modelService, Map context) throws GenericServiceException { 78 if (modelService.location == null || modelService.invoke == null) 79 throw new GenericServiceException("Cannot locate service to invoke"); 80 81 DispatchContext dctx = dispatcher.getLocalContext(localName); 83 84 ClassLoader cl = null; 86 87 if (dctx == null) { 88 cl = this.getClass().getClassLoader(); 89 } else { 90 cl = dctx.getClassLoader(); 91 } 92 93 String location = this.getLocation(modelService); 94 95 BSFManager mgr = new BSFManager(); 97 mgr.setClassLoader(cl); 98 99 mgr.registerBean("dctx", dctx); 100 mgr.registerBean("context", context); 101 102 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 String script = (String ) scriptCache.get(localName + "_" + location); 112 113 if (script == null) { 114 synchronized (this) { 115 script = (String ) scriptCache.get(localName + "_" + location); 116 if (script == null) { 117 URL 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 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 |