1 18 19 package org.apache.jmeter.protocol.java.sampler; 20 21 import java.io.IOException ; 22 23 import org.apache.jmeter.samplers.AbstractSampler; 24 import org.apache.jmeter.samplers.Entry; 25 import org.apache.jmeter.samplers.SampleResult; 26 import org.apache.jmeter.util.BeanShellInterpreter; 27 import org.apache.jmeter.util.JMeterUtils; 28 import org.apache.jorphan.logging.LoggingManager; 29 import org.apache.jorphan.util.JMeterException; 30 import org.apache.jorphan.util.JOrphanUtils; 31 import org.apache.log.Logger; 32 33 38 public class BeanShellSampler extends AbstractSampler 39 { 40 private static final Logger log = LoggingManager.getLoggerForClass(); 41 42 public static final String FILENAME = "BeanShellSampler.filename"; public static final String SCRIPT = "BeanShellSampler.query"; public static final String PARAMETERS = "BeanShellSampler.parameters"; public static final String INIT_FILE = "beanshell.sampler.init"; 47 transient private BeanShellInterpreter bshInterpreter; 48 49 public BeanShellSampler() 50 { 51 try { 52 bshInterpreter = new BeanShellInterpreter(); 53 String init = JMeterUtils.getProperty(INIT_FILE); 54 try { 55 bshInterpreter.init(init,log); 56 } catch (IOException e) { 57 log.warn("Could not initialise interpreter",e); 58 } catch (JMeterException e) { 59 log.warn("Could not initialise interpreter",e); 60 } 61 } catch (ClassNotFoundException e) { 62 log.error("Could not establish BeanShellInterpreter: "+e); 63 } 64 } 65 66 71 72 public String getLabel() 73 { 74 return getName(); 75 } 76 77 public String getScript() 78 { 79 return this.getPropertyAsString(SCRIPT); 80 } 81 82 public String getFilename() 83 { 84 return getPropertyAsString(FILENAME); 85 } 86 87 public String getParameters() 88 { 89 return getPropertyAsString(PARAMETERS); 90 } 91 92 public SampleResult sample(Entry e) { 94 SampleResult res = new SampleResult(); 96 boolean isSuccessful = false; 97 res.setSampleLabel(getLabel()); 98 res.sampleStart(); 99 if (bshInterpreter == null){ 100 res.sampleEnd(); 101 res.setResponseCode("503"); res.setResponseMessage("BeanShell Interpreter not found"); 103 res.setSuccessful(false); 104 return res; 105 } 106 try 107 { 108 String request=getScript(); 109 String fileName=getFilename(); 110 if (fileName.length() == 0) { 111 res.setSamplerData(request); 112 } else { 113 res.setSamplerData(fileName); 114 } 115 116 bshInterpreter.set("Label",getLabel()); bshInterpreter.set("FileName",getFilename()); bshInterpreter.set("SampleResult",res); bshInterpreter.set("Parameters",getParameters()); bshInterpreter.set("bsh.args",JOrphanUtils.split(getParameters()," ")); 121 122 bshInterpreter.set("ResponseCode","200"); bshInterpreter.set("ResponseMessage","OK"); bshInterpreter.set("IsSuccess",true); 127 Object bshOut; 128 129 if (fileName.length() == 0){ 130 bshOut = bshInterpreter.eval(request); 131 } else { 132 bshOut = bshInterpreter.source(fileName); 133 } 134 135 String out; 136 if (bshOut == null) { out=""; 138 } else { 139 out = bshOut.toString(); 140 } 141 res.setResponseData(out.getBytes()); 142 res.setDataType(SampleResult.TEXT); 143 res.setResponseCode(bshInterpreter.get("ResponseCode").toString()); res.setResponseMessage(bshInterpreter.get("ResponseMessage").toString()); isSuccessful = Boolean.valueOf(bshInterpreter.get("IsSuccess") .toString()).booleanValue(); 147 } 148 158 catch(NoClassDefFoundError ex){ 160 log.error("BeanShell Jar missing? "+ex.toString()); 161 res.setResponseCode("501"); res.setResponseMessage(ex.toString()); 163 res.setStopThread(true); } 165 catch (Exception ex) { 167 log.warn(ex.toString()); 168 res.setResponseCode("500"); res.setResponseMessage(ex.toString()); 170 } 171 172 res.sampleEnd(); 173 174 res.setSuccessful(isSuccessful); 176 177 return res; 178 } 179 } | Popular Tags |