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.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 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 60 public void runSyncIgnore(String localName, ModelService modelService, Map context) throws GenericServiceException { 61 Map result = runSync(localName, modelService, context); 62 } 63 64 67 public Map runSync(String localName, ModelService modelService, Map context) throws GenericServiceException { 68 Object result = serviceInvoker(localName, modelService, context); 69 70 if (result == null || !(result instanceof Map )) 71 throw new GenericServiceException("Service did not return expected result"); 72 return (Map ) result; 73 } 74 75 private Object serviceInvoker(String localName, ModelService modelService, Map context) throws GenericServiceException { 77 if (modelService.location == null || modelService.invoke == null) 78 throw new GenericServiceException("Cannot locate service to invoke"); 79 80 DispatchContext dctx = dispatcher.getLocalContext(localName); 82 83 ClassLoader cl = null; 85 86 if (dctx == null) { 87 cl = this.getClass().getClassLoader(); 88 } else { 89 cl = dctx.getClassLoader(); 90 } 91 92 String location = this.getLocation(modelService); 93 94 String script = (String ) scriptCache.get(localName + "_" + location); 96 97 if (script == null) { 98 synchronized (this) { 99 script = (String ) scriptCache.get(localName + "_" + location); 100 if (script == null) { 101 URL 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 result = null; 124 125 try { 126 bsh.set("dctx", dctx); bsh.set("context", context); bsh.eval(script); 129 Object bshResult = bsh.get("result"); 130 131 if ((bshResult != null) && (bshResult instanceof Map )) 132 context.putAll((Map ) 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 |