001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.callops;
019:
020: import java.io.*;
021: import java.util.*;
022:
023: import org.w3c.dom.*;
024: import org.ofbiz.base.util.*;
025: import org.ofbiz.minilang.*;
026: import org.ofbiz.minilang.method.*;
027:
028: import bsh.*;
029:
030: /**
031: * Simple class to wrap messages that come either from a straight string or a properties file
032: */
033: public class CallBsh extends MethodOperation {
034:
035: public static final String module = CallBsh.class.getName();
036:
037: public static final int bufferLength = 4096;
038:
039: String inline = null;
040: String resource = null;
041: ContextAccessor errorListAcsr;
042:
043: public CallBsh(Element element, SimpleMethod simpleMethod) {
044: super (element, simpleMethod);
045: inline = UtilXml.elementValue(element);
046: resource = element.getAttribute("resource");
047: errorListAcsr = new ContextAccessor(element
048: .getAttribute("error-list-name"), "error_list");
049:
050: if (inline != null && inline.length() > 0) {// pre-parse/compile inlined bsh, only accessed here
051: }
052: }
053:
054: public boolean exec(MethodContext methodContext) {
055: List messages = (List) errorListAcsr.get(methodContext);
056:
057: if (messages == null) {
058: messages = new LinkedList();
059: errorListAcsr.put(methodContext, messages);
060: }
061:
062: Interpreter bsh = new Interpreter();
063: bsh.setClassLoader(methodContext.getLoader());
064:
065: try {
066: // setup environment
067: Iterator envEntries = methodContext.getEnvEntryIterator();
068:
069: while (envEntries.hasNext()) {
070: Map.Entry entry = (Map.Entry) envEntries.next();
071: bsh.set((String) entry.getKey(), entry.getValue());
072: }
073:
074: // run external, from resource, first if resource specified
075: if (resource != null && resource.length() > 0) {
076: String resource = methodContext
077: .expandString(this .resource);
078: InputStream is = methodContext.getLoader()
079: .getResourceAsStream(resource);
080:
081: if (is == null) {
082: messages.add("Could not find bsh resource: "
083: + resource);
084: } else {
085: try {
086: BufferedReader reader = new BufferedReader(
087: new InputStreamReader(is));
088: StringBuffer outSb = new StringBuffer();
089:
090: String tempStr = null;
091:
092: while ((tempStr = reader.readLine()) != null) {
093: outSb.append(tempStr);
094: outSb.append('\n');
095: }
096:
097: Object resourceResult = bsh.eval(outSb
098: .toString());
099:
100: // if map is returned, copy values into env
101: if ((resourceResult != null)
102: && (resourceResult instanceof Map)) {
103: methodContext
104: .putAllEnv((Map) resourceResult);
105: }
106: } catch (IOException e) {
107: messages.add("IO error loading bsh resource: "
108: + e.getMessage());
109: }
110: }
111: }
112:
113: if (Debug.verboseOn())
114: Debug.logVerbose(
115: "Running inline BSH script: " + inline, module);
116: // run inlined second to it can override the one from the property
117: Object inlineResult = bsh.eval(inline);
118: if (Debug.verboseOn())
119: Debug.logVerbose("Result of inline BSH script: "
120: + inlineResult, module);
121:
122: // if map is returned, copy values into env
123: if ((inlineResult != null) && (inlineResult instanceof Map)) {
124: methodContext.putAllEnv((Map) inlineResult);
125: }
126: } catch (EvalError e) {
127: Debug.logError(e, "BeanShell execution caused an error",
128: module);
129: messages.add("BeanShell execution caused an error: "
130: + e.getMessage());
131: }
132:
133: // always return true, error messages just go on the error list
134: return true;
135: }
136:
137: public String rawString() {
138: // TODO: something more than the empty tag
139: return "<call-bsh/>";
140: }
141:
142: public String expandedString(MethodContext methodContext) {
143: // TODO: something more than a stub/dummy
144: return this.rawString();
145: }
146: }
|