Index: src/components/org/apache/jmeter/visualizers/BeanShellListener.java =================================================================== --- src/components/org/apache/jmeter/visualizers/BeanShellListener.java (revision 488388) +++ src/components/org/apache/jmeter/visualizers/BeanShellListener.java (working copy) @@ -17,6 +17,7 @@ package org.apache.jmeter.visualizers; +import java.io.IOException; import java.io.Serializable; import org.apache.jmeter.gui.UnsharedComponent; @@ -59,7 +60,11 @@ bshInterpreter = new BeanShellInterpreter(JMeterUtils.getProperty(INIT_FILE),log); } catch (ClassNotFoundException e) { log.error(e.getLocalizedMessage()); - } + } catch (JMeterException e) { + log.error(e.getLocalizedMessage()); + } catch (IOException e) { + log.error(e.getLocalizedMessage()); + } } private Object readResolve() { @@ -90,6 +95,10 @@ bshInterpreter.eval(script); } catch (JMeterException e) { log.warn("Problem in BeanShell script "+e); + } catch (ClassNotFoundException e) { + log.warn("Problem in BeanShell script "+e); + } catch (IOException e) { + log.warn("Problem in BeanShell script "+e); } } Index: src/components/org/apache/jmeter/modifiers/BeanShellPreProcessor.java =================================================================== --- src/components/org/apache/jmeter/modifiers/BeanShellPreProcessor.java (revision 488388) +++ src/components/org/apache/jmeter/modifiers/BeanShellPreProcessor.java (working copy) @@ -17,6 +17,7 @@ package org.apache.jmeter.modifiers; +import java.io.IOException; import java.io.Serializable; import org.apache.jmeter.processor.PreProcessor; @@ -55,7 +56,11 @@ bshInterpreter = new BeanShellInterpreter(JMeterUtils.getProperty(INIT_FILE),log); } catch (ClassNotFoundException e) { log.error("Cannot find BeanShell: "+e.getLocalizedMessage()); - } + } catch (JMeterException e) { + log.error("Cannot find BeanShell: "+e.getLocalizedMessage()); + } catch (IOException e) { + log.error("Cannot find BeanShell: "+e.getLocalizedMessage()); + } } private Object readResolve() { @@ -81,6 +86,10 @@ bshInterpreter.eval(script); } catch (JMeterException e) { log.warn("Problem in BeanShell script "+e); + } catch (ClassNotFoundException e) { + log.warn("Problem in BeanShell script "+e); + } catch (IOException e) { + log.warn("Problem in BeanShell script "+e); } } Index: src/components/org/apache/jmeter/timers/BeanShellTimer.java =================================================================== --- src/components/org/apache/jmeter/timers/BeanShellTimer.java (revision 488388) +++ src/components/org/apache/jmeter/timers/BeanShellTimer.java (working copy) @@ -17,6 +17,7 @@ package org.apache.jmeter.timers; +import java.io.IOException; import java.io.Serializable; import org.apache.jmeter.testbeans.TestBean; @@ -52,7 +53,11 @@ bshInterpreter = new BeanShellInterpreter(JMeterUtils.getProperty(INIT_FILE),log); } catch (ClassNotFoundException e) { log.error(e.getLocalizedMessage()); - } + } catch (JMeterException e) { + log.error(e.getLocalizedMessage()); + } catch (IOException e) { + log.error(e.getLocalizedMessage()); + } } private Object readResolve() { @@ -77,6 +82,10 @@ if (o != null) ret=o.toString(); } catch (JMeterException e) { log.warn("Problem in BeanShell script "+e); + } catch (ClassNotFoundException e) { + log.warn("Problem in BeanShell script "+e); + } catch (IOException e) { + log.warn("Problem in BeanShell script "+e); } try { return Long.decode(ret).longValue(); Index: src/components/org/apache/jmeter/extractor/BeanShellPostProcessor.java =================================================================== --- src/components/org/apache/jmeter/extractor/BeanShellPostProcessor.java (revision 488388) +++ src/components/org/apache/jmeter/extractor/BeanShellPostProcessor.java (working copy) @@ -17,6 +17,7 @@ package org.apache.jmeter.extractor; +import java.io.IOException; import java.io.Serializable; import org.apache.jmeter.processor.PostProcessor; @@ -54,7 +55,11 @@ bshInterpreter = new BeanShellInterpreter(JMeterUtils.getProperty(INIT_FILE),log); } catch (ClassNotFoundException e) { log.error("Cannot find BeanShell: "+e.getLocalizedMessage()); - } + } catch (JMeterException e) { + log.error("Cannot find BeanShell: "+e.getLocalizedMessage()); + } catch (IOException e) { + log.error("Cannot find BeanShell: "+e.getLocalizedMessage()); + } } private Object readResolve() { @@ -80,6 +85,10 @@ bshInterpreter.eval(script); } catch (JMeterException e) { log.warn("Problem in BeanShell script "+e); + } catch (ClassNotFoundException e) { + log.warn("Problem in BeanShell script "+e); + } catch (IOException e) { + log.warn("Problem in BeanShell script "+e); } } Index: src/core/org/apache/jmeter/JMeter.java =================================================================== --- src/core/org/apache/jmeter/JMeter.java (revision 488388) +++ src/core/org/apache/jmeter/JMeter.java (working copy) @@ -361,6 +361,8 @@ log.warn("Could not start Beanshell: "+e.getLocalizedMessage()); } catch (JMeterException e) { log.warn("Could not process Beanshell file: "+e.getLocalizedMessage()); + } catch (IOException e) { + log.warn("Could not process Beanshell file: "+e.getLocalizedMessage()); } } } Index: src/core/org/apache/jmeter/util/BeanShellInterpreter.java =================================================================== --- src/core/org/apache/jmeter/util/BeanShellInterpreter.java (revision 488388) +++ src/core/org/apache/jmeter/util/BeanShellInterpreter.java (working copy) @@ -22,6 +22,9 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; import org.apache.jorphan.logging.LoggingManager; import org.apache.jorphan.util.JMeterError; import org.apache.jorphan.util.JMeterException; @@ -50,28 +53,43 @@ private static final Method bshSource; + private static final Method bshGetNameSpace; + private static final Class bshClass; + private static final Method bshNameSpaceGetVariableNames; + private static final String BSH_INTERPRETER = "bsh.Interpreter"; //$NON-NLS-1$ static { // Temporary copies, so can set the final ones - Method get = null, eval = null, set = null, source = null; - Class clazz = null; + Method get = null, eval = null, set = null, source = null, getNameSpace = null; + Class interpreterClass = null; + + Method variableNames = null; + Class nameSpaceClass = null; + ClassLoader loader = Thread.currentThread().getContextClassLoader(); try { - clazz = loader.loadClass(BSH_INTERPRETER); + interpreterClass = loader.loadClass(BSH_INTERPRETER); Class string = String.class; Class object = Object.class; - get = clazz.getMethod("get", //$NON-NLS-1$ + get = interpreterClass.getMethod("get", //$NON-NLS-1$ new Class[] { string }); - eval = clazz.getMethod("eval", //$NON-NLS-1$ + eval = interpreterClass.getMethod("eval", //$NON-NLS-1$ new Class[] { string }); - set = clazz.getMethod("set", //$NON-NLS-1$ + set = interpreterClass.getMethod("set", //$NON-NLS-1$ new Class[] { string, object }); - source = clazz.getMethod("source", //$NON-NLS-1$ + source = interpreterClass.getMethod("source", //$NON-NLS-1$ new Class[] { string }); + getNameSpace = interpreterClass.getMethod("getNameSpace", + new Class[] {}); + + nameSpaceClass = loader.loadClass("bsh.NameSpace"); + + variableNames = nameSpaceClass.getMethod("getVariableNames", + new Class[] {}); } catch (ClassNotFoundException e) { log.error("Beanshell Interpreter not found"); } catch (SecurityException e) { @@ -83,48 +101,22 @@ bshGet = get; bshSet = set; bshSource = source; - bshClass = clazz; + bshClass = interpreterClass; + bshGetNameSpace = getNameSpace; + + bshNameSpaceGetVariableNames = variableNames; } } // This class is not serialised - private Object bshInstance = null; // The interpreter instance for this class + private final Map interpreterVariables = new HashMap(); - public BeanShellInterpreter() throws ClassNotFoundException { - if (bshClass == null) { - throw new ClassNotFoundException(BSH_INTERPRETER); - } - try { - bshInstance = bshClass.newInstance(); - } catch (InstantiationException e) { - log.error("Can't instantiate BeanShell", e); - throw new ClassNotFoundException("Can't instantiate BeanShell", e); - } catch (IllegalAccessException e) { - log.error("Can't instantiate BeanShell", e); - throw new ClassNotFoundException("Can't instantiate BeanShell", e); - } - } + public BeanShellInterpreter() { + } - public void init(String initFile, Object logger) throws IOException, JMeterException { - if (logger != null) {// Do this before starting the script - try { - set("log", logger);//$NON-NLS-1$ - } catch (JMeterException e) { - log.error("Can't set logger variable", e); - throw e; - } - } - if (initFile != null && initFile.length() > 0) { - // Check file so we can distinguish file error from script error - File in = new File(initFile); - if (!in.exists()) { - throw new FileNotFoundException(initFile); - } - if (!in.canRead()) { - throw new IOException("Cannot read" + initFile); - } - source(initFile); - } + public void init(String initFile, Object logger) throws IOException, JMeterException, ClassNotFoundException { + set("log", logger); + this.source(initFile); } /** @@ -132,21 +124,15 @@ * @param init initialisation file * @param log logger to pass to interpreter; also used to log errors in this method */ - public BeanShellInterpreter(String init, Logger _log) throws ClassNotFoundException { + public BeanShellInterpreter(String init, Logger _log) throws ClassNotFoundException, JMeterException, IOException { this(); - try { - this.init(init, _log); - } catch (IOException e) { - _log.warn("Could not initialise interpreter", e); - } catch (JMeterException e) { - _log.warn("Could not initialise interpreter", e); - } + this.init(init, _log); } - private Object bshInvoke(Method m, String s) throws JMeterException { + private Object bshInvoke(Object targetObject, Method m, Object[] args) throws JMeterException { Object r = null; try { - r = m.invoke(bshInstance, new Object[] { s }); + r = m.invoke(targetObject, args); } catch (IllegalArgumentException e) { // Programming error log.error("Error invoking bsh method " + m.getName() + "\n", e); throw new JMeterError("Error invoking bsh method " + m.getName(), e); @@ -161,43 +147,88 @@ } return r; } + + private Object executeBeanShell(String script, String file) throws JMeterException, ClassNotFoundException, IOException { + //create the interpreter + Object bshInstance; + if (bshClass == null) { + throw new ClassNotFoundException(BSH_INTERPRETER);//$NON-NLS-1$ + } + try { + bshInstance = bshClass.newInstance(); + } + catch (InstantiationException e) { + log.error("Can't instantiate BeanShell", e); + throw new ClassNotFoundException("Can't instantiate BeanShell", e); + } + catch (IllegalAccessException e) { + log.error("Can't instantiate BeanShell", e); + throw new ClassNotFoundException("Can't instantiate BeanShell", e); + } + + //Set all of the variables on the interpreter + for (final Iterator entryItr = this.interpreterVariables.entrySet().iterator(); entryItr.hasNext();) { + final Map.Entry entry = (Map.Entry)entryItr.next(); + final String name = (String)entry.getKey(); + final Object value = entry.getValue(); + + bshInvoke(bshInstance, bshSet, new Object[] { name, value }); + } + + Object evalResults = null; + //If using a source file execute it + if (file != null && file.length() > 0) { + // Check file so we can distinguish file error from script error + File in = new File(file); + if (!in.exists()) { + throw new FileNotFoundException(file); + } + if (!in.canRead()) { + throw new IOException("Cannot read" + file); + } - private Object bshInvoke(Method m, String s, Object o) throws JMeterException { - Object r = null; - try { - r = m.invoke(bshInstance, new Object[] { s, o }); - } catch (IllegalArgumentException e) { // Programming error - log.error("Error invoking bsh method " + m.getName() + "\n", e); - throw new JMeterError("Error invoking bsh method " + m.getName(), e); - } catch (IllegalAccessException e) { // Also programming error - log.error("Error invoking bsh method " + m.getName() + "\n", e); - throw new JMeterError("Error invoking bsh method " + m.getName(), e); - } catch (InvocationTargetException e) { // Can occur at run-time - // could be caused by the bsh Exceptions: - // EvalError, ParseException or TargetError - log.error("Error invoking bsh method " + m.getName() + "\n", e); - throw new JMeterException("Error invoking bsh method " + m.getName(), e); - } - return r; + evalResults = bshInvoke(bshInstance, bshSource, new Object[] { file }); + } + + //If using a script execute it + if (script != null) { + evalResults = bshInvoke(bshInstance, bshEval, new Object[] { script }); + } + + //Get the NameSpace for the Interpreter to get the set of variables from + final Object bshNamespace = bshInvoke(bshInstance, bshGetNameSpace, new Object[] { }); + + //Get all variables names for the NameSpace + final String[] varNames = (String[])bshInvoke(bshNamespace, bshNameSpaceGetVariableNames, new Object[] { }); + + //Dump the Variables into the local map + for (int index = 0; index < varNames.length; index++) { + final String name = varNames[index]; + final Object value = bshInvoke(bshInstance, bshGet, new Object[] { name }); + this.interpreterVariables.put(name, value); + } + + return evalResults; } - public Object eval(String s) throws JMeterException { - return bshInvoke(bshEval, s); + public Object eval(String s) throws JMeterException, ClassNotFoundException, IOException { + return this.executeBeanShell(s, null); } - public Object set(String s, Object o) throws JMeterException { - return bshInvoke(bshSet, s, o); + public Object set(String s, Object o) { + this.interpreterVariables.put(s, o); + return null; } - public Object set(String s, boolean b) throws JMeterException { - return bshInvoke(bshSet, s, Boolean.valueOf(b)); + public Object set(String s, boolean b) { + return this.set(s, Boolean.valueOf(b)); } - public Object source(String s) throws JMeterException { - return bshInvoke(bshSource, s); + public Object source(String s) throws JMeterException, ClassNotFoundException, IOException { + return this.executeBeanShell(null, s); } - public Object get(String s) throws JMeterException { - return bshInvoke(bshGet, s); + public Object get(String s) { + return this.interpreterVariables.get(s); } }