|
18 | 18 | package org.apache.jmeter.util;
|
19 | 19 |
|
20 | 20 | import java.io.File;
|
21 |
| -import java.lang.reflect.InvocationTargetException; |
22 |
| -import java.lang.reflect.Method; |
| 21 | +import java.io.IOException; |
23 | 22 |
|
24 | 23 | import org.apache.commons.lang3.StringUtils;
|
25 |
| -import org.apache.jorphan.util.JMeterError; |
26 | 24 | import org.apache.jorphan.util.JMeterException;
|
27 | 25 | import org.slf4j.Logger;
|
28 | 26 | import org.slf4j.LoggerFactory;
|
29 | 27 |
|
| 28 | +import bsh.EvalError; |
| 29 | +import bsh.Interpreter; |
| 30 | + |
30 | 31 | /**
|
31 | 32 | * BeanShell setup function - encapsulates all the access to the BeanShell
|
32 | 33 | * Interpreter in a single class.
|
33 | 34 | *
|
34 |
| - * The class uses dynamic class loading to access BeanShell, which means that |
35 |
| - * all the source files can be built without needing access to the bsh jar. |
36 |
| - * |
37 |
| - * If the beanshell jar is not present at run-time, an error will be logged |
| 35 | + * This class wraps a BeanShell instance. |
38 | 36 | *
|
| 37 | + * Note that reflection-based dynamic class loading has been removed, so the |
| 38 | + * bsh jar must be available at compile-time and runtime. |
39 | 39 | */
|
40 |
| - |
41 | 40 | public class BeanShellInterpreter {
|
42 | 41 | private static final Logger log = LoggerFactory.getLogger(BeanShellInterpreter.class);
|
43 | 42 |
|
44 |
| - private static final Method bshGet; |
45 |
| - |
46 |
| - private static final Method bshSet; |
47 |
| - |
48 |
| - private static final Method bshEval; |
49 |
| - |
50 |
| - private static final Method bshSource; |
51 |
| - |
52 |
| - private static final Class<?> bshClass; |
53 |
| - |
54 |
| - private static final String BSH_INTERPRETER = "bsh.Interpreter"; //$NON-NLS-1$ |
55 |
| - |
56 |
| - static { |
57 |
| - // Temporary copies, so can set the final ones |
58 |
| - Method get = null; |
59 |
| - Method eval = null; |
60 |
| - Method set = null; |
61 |
| - Method source = null; |
62 |
| - Class<?> clazz = null; |
63 |
| - ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
64 |
| - try { |
65 |
| - clazz = loader.loadClass(BSH_INTERPRETER); |
66 |
| - Class<String> string = String.class; |
67 |
| - Class<Object> object = Object.class; |
68 |
| - |
69 |
| - get = clazz.getMethod("get", string); //$NON-NLS-1$ |
70 |
| - eval = clazz.getMethod("eval", string); //$NON-NLS-1$ |
71 |
| - set = clazz.getMethod("set", string, object); //$NON-NLS-1$ |
72 |
| - source = clazz.getMethod("source", string); //$NON-NLS-1$ |
73 |
| - } catch (ClassNotFoundException|SecurityException | NoSuchMethodException e) { |
74 |
| - log.error("Beanshell Interpreter not found", e); |
75 |
| - } finally { |
76 |
| - bshEval = eval; |
77 |
| - bshGet = get; |
78 |
| - bshSet = set; |
79 |
| - bshSource = source; |
80 |
| - bshClass = clazz; |
81 |
| - } |
82 |
| - } |
83 |
| - |
84 | 43 | // This class is not serialised
|
85 |
| - private Object bshInstance = null; // The interpreter instance for this class |
| 44 | + private Interpreter bshInstance = null; // The interpreter instance for this class |
86 | 45 |
|
87 | 46 | private final String initFile; // Script file to initialize the Interpreter with
|
88 | 47 |
|
89 | 48 | private final Logger logger; // Logger to use during initialization and script run
|
90 | 49 |
|
91 |
| - public BeanShellInterpreter() throws ClassNotFoundException { |
| 50 | + private static final String BSH_ERROR_TEMPLATE = "Error invoking bsh method: %s"; |
| 51 | + |
| 52 | + public BeanShellInterpreter() { |
92 | 53 | this(null, null);
|
93 | 54 | }
|
94 | 55 |
|
95 | 56 | /**
|
96 | 57 | *
|
97 | 58 | * @param init initialisation file
|
98 | 59 | * @param log logger to pass to interpreter
|
99 |
| - * @throws ClassNotFoundException when beanshell can not be instantiated |
100 | 60 | */
|
101 |
| - public BeanShellInterpreter(String init, Logger log) throws ClassNotFoundException { |
| 61 | + public BeanShellInterpreter(String init, Logger log) { |
102 | 62 | initFile = init;
|
103 | 63 | logger = log;
|
104 | 64 | init();
|
105 | 65 | }
|
106 | 66 |
|
107 | 67 | // Called from ctor, so must be private (or final, but it does not seem useful elsewhere)
|
108 |
| - private void init() throws ClassNotFoundException { |
109 |
| - if (bshClass == null) { |
110 |
| - throw new ClassNotFoundException(BSH_INTERPRETER); |
111 |
| - } |
112 |
| - try { |
113 |
| - bshInstance = bshClass.getDeclaredConstructor().newInstance(); |
114 |
| - } catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) { |
115 |
| - log.error("Can't instantiate BeanShell", e); |
116 |
| - throw new ClassNotFoundException("Can't instantiate BeanShell", e); |
117 |
| - } |
| 68 | + private void init() { |
| 69 | + bshInstance = new Interpreter(); |
118 | 70 | if (logger != null) {// Do this before starting the script
|
119 | 71 | try {
|
120 | 72 | set("log", logger);//$NON-NLS-1$
|
@@ -148,65 +100,111 @@ private void init() throws ClassNotFoundException {
|
148 | 100 |
|
149 | 101 | /**
|
150 | 102 | * Resets the BeanShell interpreter.
|
151 |
| - * |
152 |
| - * @throws ClassNotFoundException if interpreter cannot be instantiated |
153 | 103 | */
|
154 |
| - public void reset() throws ClassNotFoundException { |
| 104 | + public void reset() { |
155 | 105 | init();
|
156 | 106 | }
|
157 | 107 |
|
158 |
| - private Object bshInvoke(Method m, Object[] o, boolean shouldLog) throws JMeterException { |
| 108 | + public Object eval(String s) throws JMeterException { |
159 | 109 | Object r = null;
|
160 |
| - final String errorString = "Error invoking bsh method: "; |
161 | 110 | try {
|
162 |
| - r = m.invoke(bshInstance, o); |
163 |
| - } catch (IllegalArgumentException | IllegalAccessException e) { // Programming error |
164 |
| - final String message = errorString + m.getName(); |
165 |
| - log.error(message); |
166 |
| - throw new JMeterError(message, e); |
167 |
| - } catch (InvocationTargetException e) { // Can occur at run-time |
168 |
| - // could be caused by the bsh Exceptions: |
169 |
| - // EvalError, ParseException or TargetError |
170 |
| - String message = errorString + m.getName(); |
| 111 | + r = bshInstance.eval(s); |
| 112 | + } catch (EvalError e) { |
| 113 | + String message = String.format(BSH_ERROR_TEMPLATE, "eval"); |
171 | 114 | Throwable cause = e.getCause();
|
172 | 115 | if (cause != null) {
|
173 | 116 | message += "\t" + cause.getLocalizedMessage();
|
174 | 117 | }
|
175 |
| - |
176 |
| - if (shouldLog) { |
177 |
| - log.error(message); |
178 |
| - } |
| 118 | + log.error(message); |
179 | 119 | throw new JMeterException(message, e);
|
180 | 120 | }
|
181 | 121 | return r;
|
182 | 122 | }
|
183 | 123 |
|
184 |
| - public Object eval(String s) throws JMeterException { |
185 |
| - return bshInvoke(bshEval, new Object[] { s }, true); |
186 |
| - } |
187 |
| - |
188 | 124 | public Object evalNoLog(String s) throws JMeterException {
|
189 |
| - return bshInvoke(bshEval, new Object[] { s }, false); |
| 125 | + Object r = null; |
| 126 | + try { |
| 127 | + r = bshInstance.eval(s); |
| 128 | + } catch (EvalError e) { |
| 129 | + String message = String.format(BSH_ERROR_TEMPLATE, "eval"); |
| 130 | + Throwable cause = e.getCause(); |
| 131 | + if (cause != null) { |
| 132 | + message += "\t" + cause.getLocalizedMessage(); |
| 133 | + } |
| 134 | + throw new JMeterException(message, e); |
| 135 | + } |
| 136 | + return r; |
190 | 137 | }
|
191 | 138 |
|
192 |
| - public Object set(String s, Object o) throws JMeterException { |
193 |
| - return bshInvoke(bshSet, new Object[] { s, o }, true); |
| 139 | + public void set(String s, Object o) throws JMeterException { |
| 140 | + try { |
| 141 | + bshInstance.set(s, o); |
| 142 | + } catch (EvalError e) { |
| 143 | + String message = String.format(BSH_ERROR_TEMPLATE, "set"); |
| 144 | + Throwable cause = e.getCause(); |
| 145 | + if (cause != null) { |
| 146 | + message += "\t" + cause.getLocalizedMessage(); |
| 147 | + } |
| 148 | + log.error(message); |
| 149 | + throw new JMeterException(message, e); |
| 150 | + } |
194 | 151 | }
|
195 | 152 |
|
196 |
| - public Object set(String s, boolean b) throws JMeterException { |
197 |
| - return bshInvoke(bshSet, new Object[] { s, b}, true); |
| 153 | + public void set(String s, boolean b) throws JMeterException { |
| 154 | + try { |
| 155 | + bshInstance.set(s, b); |
| 156 | + } catch (EvalError e) { |
| 157 | + String message = String.format(BSH_ERROR_TEMPLATE, "set"); |
| 158 | + Throwable cause = e.getCause(); |
| 159 | + if (cause != null) { |
| 160 | + message += "\t" + cause.getLocalizedMessage(); |
| 161 | + } |
| 162 | + log.error(message); |
| 163 | + throw new JMeterException(message, e); |
| 164 | + } |
198 | 165 | }
|
199 | 166 |
|
200 | 167 | public Object source(String s) throws JMeterException {
|
201 |
| - return bshInvoke(bshSource, new Object[] { s }, true); |
| 168 | + Object r = null; |
| 169 | + try { |
| 170 | + r = bshInstance.source(s); |
| 171 | + } catch (EvalError | IOException e) { |
| 172 | + String message = String.format(BSH_ERROR_TEMPLATE, "source"); |
| 173 | + Throwable cause = e.getCause(); |
| 174 | + if (cause != null) { |
| 175 | + message += "\t" + cause.getLocalizedMessage(); |
| 176 | + } |
| 177 | + log.error(message); |
| 178 | + throw new JMeterException(message, e); |
| 179 | + } |
| 180 | + return r; |
202 | 181 | }
|
203 | 182 |
|
204 | 183 | public Object get(String s) throws JMeterException {
|
205 |
| - return bshInvoke(bshGet, new Object[] { s }, true); |
| 184 | + Object r = null; |
| 185 | + try { |
| 186 | + r = bshInstance.get(s); |
| 187 | + } catch (EvalError e) { |
| 188 | + String message = String.format(BSH_ERROR_TEMPLATE, "get"); |
| 189 | + Throwable cause = e.getCause(); |
| 190 | + if (cause != null) { |
| 191 | + message += "\t" + cause.getLocalizedMessage(); |
| 192 | + } |
| 193 | + log.error(message); |
| 194 | + throw new JMeterException(message, e); |
| 195 | + } |
| 196 | + return r; |
206 | 197 | }
|
207 | 198 |
|
208 | 199 | // For use by Unit Tests
|
209 |
| - public static boolean isInterpreterPresent(){ |
| 200 | + public static boolean isInterpreterPresent() { |
| 201 | + Class<?> bshClass = null; |
| 202 | + ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
| 203 | + try { |
| 204 | + bshClass = loader.loadClass("bsh.Interpreter"); |
| 205 | + } catch (ClassNotFoundException e) { |
| 206 | + log.error("Beanshell Interpreter not found", e); |
| 207 | + } |
210 | 208 | return bshClass != null;
|
211 | 209 | }
|
212 | 210 | }
|
0 commit comments