public Object source(String s) throws JMeterException { return bshInvoke(bshSource, new Object[] { s }, true); }
/** * Resets the BeanShell interpreter. * * @throws ClassNotFoundException if interpreter cannot be instantiated */ public void reset() throws ClassNotFoundException { init(); }
/** * Process the file or script from the test element. * <p> * Sets the following script variables: * <ul> * <li>FileName</li> * <li>Parameters</li> * <li>bsh.args</li> * </ul> * @param bsh the interpreter, not {@code null} * @return the result of the script, may be {@code null} * * @throws JMeterException when working with the bsh fails */ protected Object processFileOrScript(BeanShellInterpreter bsh) throws JMeterException{ String fileName = getFilename(); String params = getParameters(); bsh.set("FileName", fileName);//$NON-NLS-1$ // Set params as a single line bsh.set("Parameters", params); // $NON-NLS-1$ // and set as an array bsh.set("bsh.args",//$NON-NLS-1$ JOrphanUtils.split(params, " "));//$NON-NLS-1$ if (fileName.length() == 0) { return bsh.eval(getScript()); } return bsh.source(fileName); }
bshInterpreter.set("SampleResult", response); //$NON-NLS-1$ bshInterpreter.set("Response", response); //$NON-NLS-1$ bshInterpreter.set("ResponseData", response.getResponseData());//$NON-NLS-1$ bshInterpreter.set("ResponseCode", response.getResponseCode());//$NON-NLS-1$ bshInterpreter.set("ResponseMessage", response.getResponseMessage());//$NON-NLS-1$ bshInterpreter.set("ResponseHeaders", response.getResponseHeaders());//$NON-NLS-1$ bshInterpreter.set("RequestHeaders", response.getRequestHeaders());//$NON-NLS-1$ bshInterpreter.set("SampleLabel", response.getSampleLabel());//$NON-NLS-1$ bshInterpreter.set("SamplerData", response.getSamplerData());//$NON-NLS-1$ bshInterpreter.set("Successful", response.isSuccessful());//$NON-NLS-1$ bshInterpreter.set("FailureMessage", "");//$NON-NLS-1$ //$NON-NLS-2$ bshInterpreter.set("Failure", false);//$NON-NLS-1$ result.setFailureMessage(bshInterpreter.get("FailureMessage").toString());//$NON-NLS-1$ result.setFailure(Boolean.parseBoolean(bshInterpreter.get("Failure") //$NON-NLS-1$ .toString())); result.setError(false);
bshInterpreter.set("Sampler", currentSampler); //$NON-NLS-1$ bshInterpreter.set("SampleResult", previousResult); //$NON-NLS-1$ bshInterpreter.set("ctx", jmctx); //$NON-NLS-1$ bshInterpreter.set("vars", vars); //$NON-NLS-1$ bshInterpreter.set("props", JMeterUtils.getJMeterProperties()); //$NON-NLS-1$ bshInterpreter.set("threadName", Thread.currentThread().getName()); //$NON-NLS-1$ Object bshOut = bshInterpreter.eval(script); if (bshOut != null) { resultStr = bshOut.toString();
@Override public void sampleOccurred(SampleEvent se) { final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter(); if (bshInterpreter == null) { log.error("BeanShell not found"); return; } SampleResult samp=se.getResult(); try { bshInterpreter.set("sampleEvent", se);//$NON-NLS-1$ bshInterpreter.set("sampleResult", samp);//$NON-NLS-1$ processFileOrScript(bshInterpreter); } catch (JMeterException e) { if (log.isWarnEnabled()) { log.warn("Problem in BeanShell script. {}", e.toString()); } } }
if (isResetInterpreter()) { try { bshInterpreter.reset(); } catch (ClassNotFoundException e) { log.error("Cannot reset BeanShell: "+e.toString()); bshInterpreter.set("ctx", jmctx);//$NON-NLS-1$ bshInterpreter.set("Label", getName()); //$NON-NLS-1$ bshInterpreter.set("prev", jmctx.getPreviousResult());//$NON-NLS-1$ bshInterpreter.set("props", JMeterUtils.getJMeterProperties()); bshInterpreter.set("vars", vars);//$NON-NLS-1$ } catch (JMeterException e) { log.warn("Problem setting one or more BeanShell variables "+e);
@Override public void threadStarted() { if (bshInterpreter == null || !hasInitFile) { return; } try { bshInterpreter.evalNoLog("threadStarted()"); // $NON-NLS-1$ } catch (JMeterException ignored) { log.debug(getClass().getName() + " : " + ignored.getLocalizedMessage()); // $NON-NLS-1$ } }
log.info("Running Beanshell on file: {}", bshinit); try { BeanShellInterpreter bsi = new BeanShellInterpreter(); bsi.source(bshinit); } catch (ClassNotFoundException|JMeterException e) { if (log.isWarnEnabled()) {
private void init() { // WARNING: called from ctor so must not be overridden (i.e. must be private or final) parameters=""; // ensure variables are not null filename=""; script=""; try { String initFileName = JMeterUtils.getProperty(getInitFileProperty()); hasInitFile = initFileName != null; bshInterpreter = new BeanShellInterpreter(initFileName, log); } catch (ClassNotFoundException e) { log.error("Cannot find BeanShell: "+e.toString()); } }
@Override public void testEnded(String host) { if (bshInterpreter == null || !hasInitFile) { return; } try { bshInterpreter.eval((new StringBuilder("testEnded(\"")) // $NON-NLS-1$ .append(host) .append("\")") // $NON-NLS-1$ .toString()); // $NON-NLS-1$ } catch (JMeterException ignored) { log.debug(getClass().getName() + " : " + ignored.getLocalizedMessage()); // $NON-NLS-1$ } }
bshInterpreter.set("SampleResult", res); //$NON-NLS-1$ bshInterpreter.set("ResponseCode", "200"); //$NON-NLS-1$ bshInterpreter.set("ResponseMessage", "OK");//$NON-NLS-1$ bshInterpreter.set("IsSuccess", true);//$NON-NLS-1$ res.setResponseCode(bshInterpreter.get("ResponseCode").toString());//$NON-NLS-1$ res.setResponseMessage(bshInterpreter.get("ResponseMessage").toString());//$NON-NLS-1$ isSuccessful = Boolean.valueOf(bshInterpreter.get("IsSuccess") //$NON-NLS-1$ .toString()).booleanValue();
@Override public void process(){ final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter(); if (bshInterpreter == null) { log.error("BeanShell not found"); return; } JMeterContext jmctx = JMeterContextService.getContext(); Sampler sam = jmctx.getCurrentSampler(); try { // Add variables for access to context and variables bshInterpreter.set("sampler", sam);//$NON-NLS-1$ processFileOrScript(bshInterpreter); } catch (JMeterException e) { if (log.isWarnEnabled()) { log.warn("Problem in BeanShell script. {}", e.toString()); } } }
@Override public void testStarted() { if (bshInterpreter == null || !hasInitFile) { return; } try { bshInterpreter.evalNoLog("testStarted()"); // $NON-NLS-1$ } catch (JMeterException ignored) { log.debug(getClass().getName() + " : " + ignored.getLocalizedMessage()); // $NON-NLS-1$ } }
/** {@inheritDoc} */ @Override public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException { checkParameterCount(parameters, 1, 2); values = parameters.toArray(); try { bshInterpreter = new BeanShellInterpreter(JMeterUtils.getProperty(INIT_FILE), log); } catch (ClassNotFoundException e) { throw new InvalidVariableException("BeanShell not found", e); } }
@Override public void testStarted(String host) { if (bshInterpreter == null || !hasInitFile) { return; } try { bshInterpreter.eval((new StringBuilder("testStarted(\"")) // $NON-NLS-1$ .append(host) .append("\")") // $NON-NLS-1$ .toString()); // $NON-NLS-1$ } catch (JMeterException ignored) { log.debug(getClass().getName() + " : " + ignored.getLocalizedMessage()); // $NON-NLS-1$ } }
@Override public void process() { JMeterContext jmctx = JMeterContextService.getContext(); SampleResult prev = jmctx.getPreviousResult(); if (prev == null) { return; // TODO - should we skip processing here? } final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter(); if (bshInterpreter == null) { log.error("BeanShell not found"); return; } try { // Add variables for access to context and variables bshInterpreter.set("data", prev.getResponseData());//$NON-NLS-1$ processFileOrScript(bshInterpreter); } catch (JMeterException e) { if (log.isWarnEnabled()) { log.warn("Problem in BeanShell script: {}", e.toString()); } } }
@Override public void testEnded() { if (bshInterpreter == null || !hasInitFile) { return; } try { bshInterpreter.evalNoLog("testEnded()"); // $NON-NLS-1$ } catch (JMeterException ignored) { log.debug(getClass().getName() + " : " + ignored.getLocalizedMessage()); // $NON-NLS-1$ } }
public Object evalNoLog(String s) throws JMeterException { return bshInvoke(bshEval, new Object[] { s }, false); }