1 18 19 package org.apache.jmeter.protocol.java.sampler; 20 21 import org.apache.bsf.BSFEngine; 22 import org.apache.bsf.BSFManager; 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.jorphan.logging.LoggingManager; 27 import org.apache.log.Logger; 28 29 34 public class BSFSampler extends AbstractSampler 35 { 36 37 private static final Logger log = LoggingManager.getLoggerForClass(); 38 39 public static final String FILENAME = "BSFSampler.filename"; public static final String SCRIPT = "BSFSampler.query"; public static final String LANGUAGE = "BSFSampler.language"; public static final String PARAMETERS = "BSFSampler.parameters"; 44 private transient BSFManager mgr; 45 private BSFEngine bsfEngine; 46 47 public BSFSampler() 48 { 49 try { 50 BSFManager.registerScriptingEngine("beanshell", 52 "bsh.util.BeanShellBSFEngine", new String [] { "bsh"} ); 53 } catch (NoClassDefFoundError e){ 54 } 55 56 58 } 59 60 public String getFilename() 61 { 62 return getPropertyAsString(FILENAME); 63 } 64 public void setFilename(String newFilename) 65 { 66 this.setProperty(FILENAME, newFilename); 67 } 68 public String getScript() 69 { 70 return this.getPropertyAsString(SCRIPT); 71 } 72 public void setScript(String newScript) 73 { 74 this.setProperty(SCRIPT, newScript); 75 } 76 public String getParameters() 77 { 78 return this.getPropertyAsString(PARAMETERS); 79 } 80 public void setParameters(String newScript) 81 { 82 this.setProperty(PARAMETERS, newScript); 83 } 84 public String getScriptLanguage() 85 { 86 return this.getPropertyAsString(LANGUAGE); 87 } 88 public void setScriptLanguage(String lang) 89 { 90 this.setProperty(LANGUAGE,lang); 91 } 92 93 98 99 public String getLabel() 100 { 101 return getName(); 102 } 103 104 105 public SampleResult sample(Entry e) { 107 log.info(getLabel()+" "+getFilename()); 108 SampleResult res = new SampleResult(); 109 boolean isSuccessful = false; 110 res.setSampleLabel(getLabel()); 111 res.sampleStart(); 112 try 113 { 114 String request=getScript(); 115 res.setSamplerData(request); 116 117 mgr.registerBean("Label",getLabel()); 118 mgr.registerBean("Name",getFilename()); 119 120 bsfEngine = mgr.loadScriptingEngine(getScriptLanguage()); 121 122 Object bsfOut = bsfEngine.eval("Sampler",0,0,request); 123 124 res.setResponseData(bsfOut.toString().getBytes()); 125 res.setDataType(SampleResult.TEXT); 126 res.setResponseCode("200"); res.setResponseMessage("OK"); isSuccessful = true; } 130 catch (NoClassDefFoundError ex) 131 { 132 log.warn("",ex); 133 res.setResponseCode("500"); 134 res.setResponseMessage(ex.toString()); 135 } 136 catch (Exception ex) 137 { 138 log.warn("",ex); 139 res.setResponseCode("500"); 140 res.setResponseMessage(ex.toString()); 141 } 142 143 res.sampleEnd(); 144 145 res.setSuccessful(isSuccessful); 147 148 return res; 149 } 150 } 151 | Popular Tags |