KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > callops > CallBsh


1 /*
2  * $Id: CallBsh.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

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 /**
37  * Simple class to wrap messages that come either from a straight string or a properties file
38  *
39  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
40  * @version $Rev: 5462 $
41  * @since 2.0
42  */

43 public class CallBsh extends MethodOperation {
44     
45     public static final String JavaDoc module = CallBsh.class.getName();
46     
47     public static final int bufferLength = 4096;
48
49     String JavaDoc inline = null;
50     String JavaDoc 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) {// pre-parse/compile inlined bsh, only accessed here
60
}
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             // setup environment
76
Iterator envEntries = methodContext.getEnvEntryIterator();
77
78             while (envEntries.hasNext()) {
79                 Map.Entry entry = (Map.Entry) envEntries.next();
80                 bsh.set((String JavaDoc) entry.getKey(), entry.getValue());
81             }
82
83             // run external, from resource, first if resource specified
84
if (resource != null && resource.length() > 0) {
85                 String JavaDoc 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 JavaDoc outSb = new StringBuffer JavaDoc();
94
95                         String JavaDoc tempStr = null;
96
97                         while ((tempStr = reader.readLine()) != null) {
98                             outSb.append(tempStr);
99                             outSb.append('\n');
100                         }
101
102                         Object JavaDoc resourceResult = bsh.eval(outSb.toString());
103
104                         // if map is returned, copy values into env
105
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             // run inlined second to it can override the one from the property
116
Object JavaDoc inlineResult = bsh.eval(inline);
117             if (Debug.verboseOn()) Debug.logVerbose("Result of inline BSH script: " + inlineResult, module);
118
119             // if map is returned, copy values into env
120
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         // always return true, error messages just go on the error list
129
return true;
130     }
131
132     public String JavaDoc rawString() {
133         // TODO: something more than the empty tag
134
return "<call-bsh/>";
135     }
136     public String JavaDoc expandedString(MethodContext methodContext) {
137         // TODO: something more than a stub/dummy
138
return this.rawString();
139     }
140 }
141
Popular Tags