1 25 package org.ofbiz.base.util; 26 27 import java.io.IOException ; 28 import java.io.InputStreamReader ; 29 import java.io.Reader ; 30 import java.io.StringReader ; 31 import java.net.MalformedURLException ; 32 import java.net.URL ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 import java.util.Set ; 37 38 import org.ofbiz.base.location.FlexibleLocation; 39 import org.ofbiz.base.util.cache.UtilCache; 40 41 import bsh.BshClassManager; 42 import bsh.EvalError; 43 import bsh.Interpreter; 44 import bsh.NameSpace; 45 import bsh.ParseException; 46 47 56 public final class BshUtil { 57 58 public static final String module = BshUtil.class.getName(); 59 60 protected static Map masterClassManagers = new HashMap (); 61 public static UtilCache parsedScripts = new UtilCache("script.BshLocationParsedCache", 0, 0, false); 62 63 70 public static final Object eval(String expression, Map context) throws EvalError { 71 Object o = null; 72 if (expression == null || expression.equals("")) { 73 Debug.logError("BSH Evaluation error. Empty expression", module); 74 return null; 75 } 76 77 if (Debug.verboseOn()) 78 Debug.logVerbose("Evaluating -- " + expression, module); 79 if (Debug.verboseOn()) 80 Debug.logVerbose("Using Context -- " + context, module); 81 82 try { 83 Interpreter bsh = makeInterpreter(context); 84 o = bsh.eval(expression); 86 if (Debug.verboseOn()) 87 Debug.logVerbose("Evaluated to -- " + o, module); 88 89 NameSpace ns = bsh.getNameSpace(); 91 String [] varNames = ns.getVariableNames(); 92 for (int x = 0; x < varNames.length; x++) { 93 context.put(varNames[x], bsh.get(varNames[x])); 94 } 95 } catch (EvalError e) { 96 Debug.logError(e, "BSH Evaluation error.", module); 97 throw e; 98 } 99 return o; 100 } 101 102 public static Interpreter makeInterpreter(Map context) throws EvalError { 103 Interpreter bsh = getMasterInterpreter(null); 104 if (context != null) { 106 Set keySet = context.keySet(); 107 Iterator i = keySet.iterator(); 108 while (i.hasNext()) { 109 Object key = i.next(); 110 Object value = context.get(key); 111 bsh.set((String ) key, value); 112 } 113 114 bsh.set("context", context); 116 } 117 118 return bsh; 119 } 120 121 public static Interpreter getMasterInterpreter(ClassLoader classLoader) { 122 if (classLoader == null) { 123 classLoader = Thread.currentThread().getContextClassLoader(); 124 } 125 126 BshClassManager master = (BshClassManager) BshUtil.masterClassManagers.get(classLoader); 128 if (master == null) { 129 synchronized (OfbizBshBsfEngine.class) { 130 master = (BshClassManager) BshUtil.masterClassManagers.get(classLoader); 131 if (master == null) { 132 master = BshClassManager.createClassManager(); 133 master.setClassLoader(classLoader); 134 BshUtil.masterClassManagers.put(classLoader, master); 135 } 136 } 137 } 138 139 if (master != null) { 140 Interpreter interpreter = new Interpreter(new StringReader (""), System.out, System.err, 141 false, new NameSpace(master, "global"), null, null); 142 return interpreter; 143 } else { 144 Interpreter interpreter = new Interpreter(); 145 interpreter.setClassLoader(classLoader); 146 return interpreter; 147 } 148 } 149 150 public static Object runBshAtLocation(String location, Map context) throws GeneralException { 151 try { 152 Interpreter interpreter = makeInterpreter(context); 153 154 Interpreter.ParsedScript script = null; 155 script = (Interpreter.ParsedScript) parsedScripts.get(location); 156 if (script == null) { 157 synchronized (OfbizBshBsfEngine.class) { 158 script = (Interpreter.ParsedScript) parsedScripts.get(location); 159 if (script == null) { 160 URL scriptUrl = FlexibleLocation.resolveLocation(location); 161 Reader scriptReader = new InputStreamReader (scriptUrl.openStream()); 162 script = interpreter.parseScript(location, scriptReader); 163 if (Debug.verboseOn()) Debug.logVerbose("Caching BSH script at: " + location, module); 164 parsedScripts.put(location, script); 165 } 166 } 167 } 168 169 return interpreter.evalParsedScript(script); 170 } catch (MalformedURLException e) { 171 String errMsg = "Error loading BSH script at [" + location + "]: " + e.toString(); 172 Debug.logError(e, errMsg, module); 173 throw new GeneralException(errMsg, e); 174 } catch (ParseException e) { 175 String errMsg = "Error parsing BSH script at [" + location + "]: " + e.toString(); 176 Debug.logError(e, errMsg, module); 177 throw new GeneralException(errMsg, e); 178 } catch (IOException e) { 179 String errMsg = "Error loading BSH script at [" + location + "]: " + e.toString(); 180 Debug.logError(e, errMsg, module); 181 throw new GeneralException(errMsg, e); 182 } catch (EvalError ee) { 183 Throwable t = ee.getCause(); 184 if (t == null) { 185 Debug.logWarning(ee, "WARNING: no cause (from getCause) found for BSH EvalError: " + ee.toString(), module); 186 t = ee; 187 } else { 188 Debug.logError(t, "ERROR: Got cause (from getCause) for BSH EvalError: " + ee.toString(), module); 189 } 190 191 String errMsg = "Error running BSH script at [" + location + "], line [" + ee.getErrorLineNumber() + "]: " + t.toString(); 192 throw new GeneralException(errMsg, t); 194 } 195 } 196 } 197 | Popular Tags |