1 24 package org.ofbiz.minilang.method.callops; 25 26 import java.io.*; 27 import java.util.*; 28 29 import org.w3c.dom.*; 30 import org.ofbiz.base.util.*; 31 import org.ofbiz.minilang.*; 32 import org.ofbiz.minilang.method.*; 33 34 import bsh.*; 35 36 43 public class CallBsh extends MethodOperation { 44 45 public static final String module = CallBsh.class.getName(); 46 47 public static final int bufferLength = 4096; 48 49 String inline = null; 50 String resource = null; 51 ContextAccessor errorListAcsr; 52 53 public CallBsh(Element element, SimpleMethod simpleMethod) { 54 super(element, simpleMethod); 55 inline = UtilXml.elementValue(element); 56 resource = element.getAttribute("resource"); 57 errorListAcsr = new ContextAccessor(element.getAttribute("error-list-name"), "error_list"); 58 59 if (inline != null && inline.length() > 0) { } 61 } 62 63 public boolean exec(MethodContext methodContext) { 64 List messages = (List) errorListAcsr.get(methodContext); 65 66 if (messages == null) { 67 messages = new LinkedList(); 68 errorListAcsr.put(methodContext, messages); 69 } 70 71 Interpreter bsh = new Interpreter(); 72 bsh.setClassLoader(methodContext.getLoader()); 73 74 try { 75 Iterator envEntries = methodContext.getEnvEntryIterator(); 77 78 while (envEntries.hasNext()) { 79 Map.Entry entry = (Map.Entry) envEntries.next(); 80 bsh.set((String ) entry.getKey(), entry.getValue()); 81 } 82 83 if (resource != null && resource.length() > 0) { 85 String resource = methodContext.expandString(this.resource); 86 InputStream is = methodContext.getLoader().getResourceAsStream(resource); 87 88 if (is == null) { 89 messages.add("Could not find bsh resource: " + resource); 90 } else { 91 try { 92 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 93 StringBuffer outSb = new StringBuffer (); 94 95 String tempStr = null; 96 97 while ((tempStr = reader.readLine()) != null) { 98 outSb.append(tempStr); 99 outSb.append('\n'); 100 } 101 102 Object resourceResult = bsh.eval(outSb.toString()); 103 104 if ((resourceResult != null) && (resourceResult instanceof Map)) { 106 methodContext.putAllEnv((Map) resourceResult); 107 } 108 } catch (IOException e) { 109 messages.add("IO error loading bsh resource: " + e.getMessage()); 110 } 111 } 112 } 113 114 if (Debug.verboseOn()) Debug.logVerbose("Running inline BSH script: " + inline, module); 115 Object inlineResult = bsh.eval(inline); 117 if (Debug.verboseOn()) Debug.logVerbose("Result of inline BSH script: " + inlineResult, module); 118 119 if ((inlineResult != null) && (inlineResult instanceof Map)) { 121 methodContext.putAllEnv((Map) inlineResult); 122 } 123 } catch (EvalError e) { 124 Debug.logError(e, "BeanShell execution caused an error", module); 125 messages.add("BeanShell execution caused an error: " + e.getMessage()); 126 } 127 128 return true; 130 } 131 132 public String rawString() { 133 return "<call-bsh/>"; 135 } 136 public String expandedString(MethodContext methodContext) { 137 return this.rawString(); 139 } 140 } 141 | Popular Tags |