Search in sources :

Example 1 with BeanShellInterpreter

use of org.apache.jmeter.util.BeanShellInterpreter in project jmeter by apache.

the class JMeter method startOptionalServers.

/**
     *
     */
private void startOptionalServers() {
    // $NON-NLS-1$
    int bshport = JMeterUtils.getPropDefault("beanshell.server.port", 0);
    // $NON-NLS-1$ $NON-NLS-2$
    String bshfile = JMeterUtils.getPropDefault("beanshell.server.file", "");
    if (bshport > 0) {
        log.info("Starting Beanshell server ({},{})", bshport, bshfile);
        Runnable t = new BeanShellServer(bshport, bshfile);
        // NOSONAR we just evaluate some code here
        t.run();
    }
    // Should we run a beanshell script on startup?
    // $NON-NLS-1$
    String bshinit = JMeterUtils.getProperty("beanshell.init.file");
    if (bshinit != null) {
        log.info("Run Beanshell on file: {}", bshinit);
        try {
            BeanShellInterpreter bsi = new BeanShellInterpreter();
            bsi.source(bshinit);
        } catch (ClassNotFoundException e) {
            if (log.isWarnEnabled()) {
                log.warn("Could not start Beanshell: {}", e.getLocalizedMessage());
            }
        } catch (JMeterException e) {
            if (log.isWarnEnabled()) {
                log.warn("Could not process Beanshell file: {}", e.getLocalizedMessage());
            }
        }
    }
    // $NON-NLS-1$
    int mirrorPort = JMeterUtils.getPropDefault("mirror.server.port", 0);
    if (mirrorPort > 0) {
        log.info("Starting Mirror server ({})", mirrorPort);
        try {
            Object instance = ClassTools.construct(// $NON-NLS-1$
            "org.apache.jmeter.protocol.http.control.HttpMirrorControl", mirrorPort);
            ClassTools.invoke(instance, "startHttpMirror");
        } catch (JMeterException e) {
            log.warn("Could not start Mirror server", e);
        }
    }
}
Also used : JMeterException(org.apache.jorphan.util.JMeterException) BeanShellServer(org.apache.jmeter.util.BeanShellServer) BeanShellInterpreter(org.apache.jmeter.util.BeanShellInterpreter)

Example 2 with BeanShellInterpreter

use of org.apache.jmeter.util.BeanShellInterpreter in project jmeter by apache.

the class BeanShellAssertion method getResult.

/**
     * {@inheritDoc}
     */
@Override
public AssertionResult getResult(SampleResult response) {
    AssertionResult result = new AssertionResult(getName());
    final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
    if (bshInterpreter == null) {
        result.setFailure(true);
        result.setError(true);
        result.setFailureMessage("BeanShell Interpreter not found");
        return result;
    }
    try {
        // Add SamplerData for consistency with BeanShell Sampler
        //$NON-NLS-1$
        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());
        // The following are used to set the Result details on return from
        // the script:
        //$NON-NLS-1$ //$NON-NLS-2$
        bshInterpreter.set("FailureMessage", "");
        //$NON-NLS-1$
        bshInterpreter.set("Failure", false);
        processFileOrScript(bshInterpreter);
        //$NON-NLS-1$
        result.setFailureMessage(bshInterpreter.get("FailureMessage").toString());
        result.setFailure(Boolean.parseBoolean(//$NON-NLS-1$
        bshInterpreter.get("Failure").toString()));
        result.setError(false);
    } catch (NoClassDefFoundError ex) {
        // NOSONAR explicitly trap this error to make tests work better
        log.error("BeanShell Jar missing? " + ex.toString());
        result.setError(true);
        result.setFailureMessage("BeanShell Jar missing? " + ex.toString());
        // No point continuing
        response.setStopThread(true);
    } catch (// Mainly for bsh.EvalError
    Exception ex) {
        result.setError(true);
        result.setFailureMessage(ex.toString());
        if (log.isWarnEnabled()) {
            log.warn(ex.toString());
        }
    }
    return result;
}
Also used : BeanShellInterpreter(org.apache.jmeter.util.BeanShellInterpreter)

Example 3 with BeanShellInterpreter

use of org.apache.jmeter.util.BeanShellInterpreter in project jmeter by apache.

the class BeanShellPreProcessor method process.

@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
        //$NON-NLS-1$
        bshInterpreter.set("sampler", sam);
        processFileOrScript(bshInterpreter);
    } catch (JMeterException e) {
        if (log.isWarnEnabled()) {
            log.warn("Problem in BeanShell script. {}", e.toString());
        }
    }
}
Also used : JMeterException(org.apache.jorphan.util.JMeterException) JMeterContext(org.apache.jmeter.threads.JMeterContext) Sampler(org.apache.jmeter.samplers.Sampler) BeanShellInterpreter(org.apache.jmeter.util.BeanShellInterpreter)

Example 4 with BeanShellInterpreter

use of org.apache.jmeter.util.BeanShellInterpreter in project jmeter by apache.

the class BeanShellTimer method delay.

/**
     * {@inheritDoc}
     */
@Override
public long delay() {
    String ret = "0";
    final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
    if (bshInterpreter == null) {
        log.error("BeanShell not found");
        return 0;
    }
    try {
        Object o = processFileOrScript(bshInterpreter);
        if (o != null) {
            ret = o.toString();
        }
    } catch (JMeterException e) {
        if (log.isWarnEnabled()) {
            log.warn("Problem in BeanShell script. {}", e.toString());
        }
    }
    try {
        return Long.decode(ret).longValue();
    } catch (NumberFormatException e) {
        log.warn("Number format exception while decoding number: '{}'", ret);
        return 0;
    }
}
Also used : JMeterException(org.apache.jorphan.util.JMeterException) BeanShellInterpreter(org.apache.jmeter.util.BeanShellInterpreter)

Example 5 with BeanShellInterpreter

use of org.apache.jmeter.util.BeanShellInterpreter in project jmeter by apache.

the class BeanShellSampler method sample.

@Override
public // Entry tends to be ignored ...
SampleResult sample(// Entry tends to be ignored ...
Entry e) {
    SampleResult res = new SampleResult();
    boolean isSuccessful = false;
    res.setSampleLabel(getName());
    res.sampleStart();
    final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
    if (bshInterpreter == null) {
        res.sampleEnd();
        //$NON-NLS-1$
        res.setResponseCode("503");
        res.setResponseMessage("BeanShell Interpreter not found");
        res.setSuccessful(false);
        return res;
    }
    try {
        String request = getScript();
        String fileName = getFilename();
        if (fileName.length() == 0) {
            res.setSamplerData(request);
        } else {
            res.setSamplerData(fileName);
        }
        //$NON-NLS-1$
        bshInterpreter.set("SampleResult", res);
        // Set default values
        //$NON-NLS-1$
        bshInterpreter.set("ResponseCode", "200");
        //$NON-NLS-1$
        bshInterpreter.set("ResponseMessage", "OK");
        //$NON-NLS-1$
        bshInterpreter.set("IsSuccess", true);
        // assume text output - script can override if necessary
        res.setDataType(SampleResult.TEXT);
        savedBsh = bshInterpreter;
        Object bshOut = processFileOrScript(bshInterpreter);
        savedBsh = null;
        if (bshOut != null) {
            // Set response data
            String out = bshOut.toString();
            res.setResponseData(out, null);
        }
        // script can also use setResponseData() so long as it returns null
        //$NON-NLS-1$
        res.setResponseCode(bshInterpreter.get("ResponseCode").toString());
        //$NON-NLS-1$
        res.setResponseMessage(bshInterpreter.get("ResponseMessage").toString());
        isSuccessful = Boolean.valueOf(//$NON-NLS-1$
        bshInterpreter.get("IsSuccess").toString()).booleanValue();
    }// but we do trap this error to make tests work better
     catch (NoClassDefFoundError ex) {
        log.error("BeanShell Jar missing? {}", ex.toString());
        //$NON-NLS-1$
        res.setResponseCode("501");
        res.setResponseMessage(ex.toString());
        // No point continuing
        res.setStopThread(true);
    } catch (// Mainly for bsh.EvalError
    Exception ex) {
        if (log.isWarnEnabled()) {
            log.warn("Exception executing script. {}", ex.toString());
        }
        //$NON-NLS-1$
        res.setResponseCode("500");
        res.setResponseMessage(ex.toString());
    } finally {
        savedBsh = null;
    }
    res.sampleEnd();
    // Set if we were successful or not
    res.setSuccessful(isSuccessful);
    return res;
}
Also used : SampleResult(org.apache.jmeter.samplers.SampleResult) BeanShellInterpreter(org.apache.jmeter.util.BeanShellInterpreter) JMeterException(org.apache.jorphan.util.JMeterException)

Aggregations

BeanShellInterpreter (org.apache.jmeter.util.BeanShellInterpreter)8 JMeterException (org.apache.jorphan.util.JMeterException)6 SampleResult (org.apache.jmeter.samplers.SampleResult)3 JMeterContext (org.apache.jmeter.threads.JMeterContext)2 Sampler (org.apache.jmeter.samplers.Sampler)1 BeanShellServer (org.apache.jmeter.util.BeanShellServer)1