001: /*
002: * $Id: CallBsh.java,v 1.1 2003/08/17 06:06:13 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: package org.ofbiz.minilang.method.callops;
025:
026: import java.io.*;
027: import java.util.*;
028:
029: import org.w3c.dom.*;
030: import org.ofbiz.base.util.*;
031: import org.ofbiz.minilang.*;
032: import org.ofbiz.minilang.method.*;
033:
034: import bsh.*;
035:
036: /**
037: * Simple class to wrap messages that come either from a straight string or a properties file
038: *
039: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
040: * @version $Revision: 1.1 $
041: * @since 2.0
042: */
043: public class CallBsh extends MethodOperation {
044:
045: public static final String module = CallBsh.class.getName();
046:
047: public static final int bufferLength = 4096;
048:
049: String inline = null;
050: String resource = null;
051: ContextAccessor errorListAcsr;
052:
053: public CallBsh(Element element, SimpleMethod simpleMethod) {
054: super (element, simpleMethod);
055: inline = UtilXml.elementValue(element);
056: resource = element.getAttribute("resource");
057: errorListAcsr = new ContextAccessor(element
058: .getAttribute("error-list-name"), "error_list");
059:
060: if (inline != null && inline.length() > 0) {// pre-parse/compile inlined bsh, only accessed here
061: }
062: }
063:
064: public boolean exec(MethodContext methodContext) {
065: List messages = (List) errorListAcsr.get(methodContext);
066:
067: if (messages == null) {
068: messages = new LinkedList();
069: errorListAcsr.put(methodContext, messages);
070: }
071:
072: Interpreter bsh = new Interpreter();
073: bsh.setClassLoader(methodContext.getLoader());
074:
075: try {
076: // setup environment
077: Iterator envEntries = methodContext.getEnvEntryIterator();
078:
079: while (envEntries.hasNext()) {
080: Map.Entry entry = (Map.Entry) envEntries.next();
081: bsh.set((String) entry.getKey(), entry.getValue());
082: }
083:
084: // run external, from resource, first if resource specified
085: if (resource != null && resource.length() > 0) {
086: String resource = methodContext
087: .expandString(this .resource);
088: InputStream is = methodContext.getLoader()
089: .getResourceAsStream(resource);
090:
091: if (is == null) {
092: messages.add("Could not find bsh resource: "
093: + resource);
094: } else {
095: try {
096: BufferedReader reader = new BufferedReader(
097: new InputStreamReader(is));
098: StringBuffer outSb = new StringBuffer();
099:
100: String tempStr = null;
101:
102: while ((tempStr = reader.readLine()) != null) {
103: outSb.append(tempStr);
104: outSb.append('\n');
105: }
106:
107: Object resourceResult = bsh.eval(outSb
108: .toString());
109:
110: // if map is returned, copy values into env
111: if ((resourceResult != null)
112: && (resourceResult instanceof Map)) {
113: methodContext
114: .putAllEnv((Map) resourceResult);
115: }
116: } catch (IOException e) {
117: messages.add("IO error loading bsh resource: "
118: + e.getMessage());
119: }
120: }
121: }
122:
123: if (Debug.verboseOn())
124: Debug.logVerbose(
125: "Running inline BSH script: " + inline, module);
126: // run inlined second to it can override the one from the property
127: Object inlineResult = bsh.eval(inline);
128: if (Debug.verboseOn())
129: Debug.logVerbose("Result of inline BSH script: "
130: + inlineResult, module);
131:
132: // if map is returned, copy values into env
133: if ((inlineResult != null) && (inlineResult instanceof Map)) {
134: methodContext.putAllEnv((Map) inlineResult);
135: }
136: } catch (EvalError e) {
137: Debug.logError(e, "BeanShell execution caused an error",
138: module);
139: messages.add("BeanShell execution caused an error: "
140: + e.getMessage());
141: }
142:
143: // always return true, error messages just go on the error list
144: return true;
145: }
146: }
|