Author: sebb
Date: Sun Jul  1 15:44:30 2007
New Revision: 552369

URL: http://svn.apache.org/viewvc?view=rev&rev=552369
Log:
BSF Sampler passes additional variables to the script

Modified:
    
jakarta/jmeter/branches/rel-2-2/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java
    jakarta/jmeter/branches/rel-2-2/xdocs/changes.xml
    jakarta/jmeter/branches/rel-2-2/xdocs/usermanual/component_reference.xml

Modified: 
jakarta/jmeter/branches/rel-2-2/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/branches/rel-2-2/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java?view=diff&rev=552369&r1=552368&r2=552369
==============================================================================
--- 
jakarta/jmeter/branches/rel-2-2/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java
 (original)
+++ 
jakarta/jmeter/branches/rel-2-2/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java
 Sun Jul  1 15:44:30 2007
@@ -18,18 +18,24 @@
 
 package org.apache.jmeter.protocol.java.sampler;
 
+import java.io.FileInputStream;
+
 import org.apache.bsf.BSFEngine;
 import org.apache.bsf.BSFManager;
+import org.apache.commons.io.IOUtils;
 import org.apache.jmeter.samplers.AbstractSampler;
 import org.apache.jmeter.samplers.Entry;
 import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.threads.JMeterContext;
+import org.apache.jmeter.threads.JMeterContextService;
+import org.apache.jmeter.threads.JMeterVariables;
 import org.apache.jorphan.logging.LoggingManager;
+import org.apache.jorphan.util.JOrphanUtils;
 import org.apache.log.Logger;
 
 /**
  * A sampler which understands BSF
  * 
- * @version $Revision$ Updated on: $Date$
  */
 public class BSFSampler extends AbstractSampler {
 
@@ -45,18 +51,8 @@
 
        private transient BSFManager mgr;
 
-       private transient BSFEngine bsfEngine;
-
        public BSFSampler() {
-               try {
-                       // register beanshell with the BSF framework
-                       mgr = new BSFManager();
-                       BSFManager.registerScriptingEngine("beanshell", 
"bsh.util.BeanShellBSFEngine", new String[] { "bsh" });
-               } catch (NoClassDefFoundError e) {
-               }
-
-               // TODO: register other scripting languages ...
-
+               mgr = new BSFManager();
        }
 
        public String getFilename() {
@@ -103,41 +99,73 @@
 
        public SampleResult sample(Entry e)// Entry tends to be ignored ...
        {
-               log.info(getLabel() + " " + getFilename());
+               final String label = getLabel();
+               log.info(label + " " + getFilename());
                SampleResult res = new SampleResult();
-               boolean isSuccessful = false;
-               res.setSampleLabel(getLabel());
+               res.setSampleLabel(label);
+               FileInputStream is = null;
+               
                res.sampleStart();
                try {
-                       String request = getScript();
-                       res.setSamplerData(request);
-
-                       mgr.registerBean("Label", getLabel());
-                       mgr.registerBean("Name", getFilename());
-
-                       bsfEngine = 
mgr.loadScriptingEngine(getScriptLanguage());
-
-                       Object bsfOut = bsfEngine.eval("Sampler", 0, 0, 
request);
-
-                       res.setResponseData(bsfOut.toString().getBytes());
-                       res.setDataType(SampleResult.TEXT);
-                       res.setResponseCode("200");// TODO set from script
-                       res.setResponseMessage("OK");// TODO set from script
-                       isSuccessful = true;// TODO set from script
+                       final String request = getScript();
+                       final String fileName = getFilename();
+                       
+                       mgr.declareBean("log", log, log.getClass()); // 
$NON-NLS-1$
+                       mgr.declareBean("Label",label, String.class); // 
$NON-NLS-1$
+                       mgr.declareBean("FileName",fileName, String.class); // 
$NON-NLS-1$
+                       mgr.declareBean("Parameters", getParameters(), 
String.class); // $NON-NLS-1$
+                       String [] args=JOrphanUtils.split(getParameters(), " 
");//$NON-NLS-1$
+                       
mgr.declareBean("args",args,args.getClass());//$NON-NLS-1$
+                       mgr.declareBean("SampleResult", res, res.getClass()); 
// $NON-NLS-1$
+                       
+                       // TODO: find out how to retrieve these from the script
+                       // At present the script has to use SampleResult 
methods to set them.
+                       res.setResponseCode("200"); // $NON-NLS-1$
+                       res.setResponseMessage("OK"); // $NON-NLS-1$
+                       res.setSuccessful(true);
+
+                       // These are not useful yet, as have not found how to 
get updated values back
+                       //mgr.declareBean("ResponseCode", "200", String.class); 
// $NON-NLS-1$
+                       //mgr.declareBean("ResponseMessage", "OK", 
String.class); // $NON-NLS-1$
+                       //mgr.declareBean("IsSuccess", Boolean.TRUE, 
Boolean.class); // $NON-NLS-1$
+
+                       res.setDataType(SampleResult.TEXT); // Default (can be 
overridden by the script)
+
+                       // Add variables for access to context and variables
+                       JMeterContext jmctx = JMeterContextService.getContext();
+                       JMeterVariables vars = jmctx.getVariables();
+                       mgr.declareBean("ctx", jmctx, jmctx.getClass()); // 
$NON-NLS-1$
+                       mgr.declareBean("vars", vars, vars.getClass()); // 
$NON-NLS-1$
+
+                       BSFEngine bsfEngine = 
mgr.loadScriptingEngine(getScriptLanguage());
+
+                       Object bsfOut = null;
+                       if (fileName.length()>0) {
+                               res.setSamplerData("File: "+fileName);
+                               is = new FileInputStream(fileName);
+                               bsfOut = bsfEngine.eval(fileName, 0, 0, 
IOUtils.toString(is));
+                       } else {
+                               res.setSamplerData("[script]");
+                           bsfOut = bsfEngine.eval("script", 0, 0, request);
+                       }
+
+                       if (bsfOut != null) {
+                           res.setResponseData(bsfOut.toString().getBytes());
+                       }
                } catch (NoClassDefFoundError ex) {
                        log.warn("", ex);
-                       res.setResponseCode("500");
+                       res.setSuccessful(false);
+                       res.setResponseCode("500"); // $NON-NLS-1$
                        res.setResponseMessage(ex.toString());
                } catch (Exception ex) {
                        log.warn("", ex);
-                       res.setResponseCode("500");
+                       res.setSuccessful(false);
+                       res.setResponseCode("500"); // $NON-NLS-1$
                        res.setResponseMessage(ex.toString());
+               } finally {
+                       res.sampleEnd();
+                       IOUtils.closeQuietly(is);
                }
-
-               res.sampleEnd();
-
-               // Set if we were successful or not
-               res.setSuccessful(isSuccessful);
 
                return res;
        }

Modified: jakarta/jmeter/branches/rel-2-2/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/branches/rel-2-2/xdocs/changes.xml?view=diff&rev=552369&r1=552368&r2=552369
==============================================================================
--- jakarta/jmeter/branches/rel-2-2/xdocs/changes.xml (original)
+++ jakarta/jmeter/branches/rel-2-2/xdocs/changes.xml Sun Jul  1 15:44:30 2007
@@ -180,6 +180,7 @@
 <li>Bug 42582 - JSON pretty printing in Tree View Listener</li>
 <li>Http Autoredirects are now enabled by default when creating new 
samplers</li>
 <li>Bug 42674 - default to pre-emptive authorisation if not specified</li>
+<li>BSF Sampler passes additional variables to the script</li>
 </ul>
 
 <h4>Non-functional improvements:</h4>

Modified: 
jakarta/jmeter/branches/rel-2-2/xdocs/usermanual/component_reference.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/branches/rel-2-2/xdocs/usermanual/component_reference.xml?view=diff&rev=552369&r1=552368&r2=552369
==============================================================================
--- jakarta/jmeter/branches/rel-2-2/xdocs/usermanual/component_reference.xml 
(original)
+++ jakarta/jmeter/branches/rel-2-2/xdocs/usermanual/component_reference.xml 
Sun Jul  1 15:44:30 2007
@@ -763,12 +763,55 @@
                </p>
        </description>
 <properties>
-       <property name="Name" required="no">Descriptive name for this 
controller that is shown in the tree.</property>
-       <property name="Parameters" required="no">List of parameters to be 
passed to the script file or the script.</property>
-       <property name="Script File" required="no">Name of a file to be used as 
a BeanShell script</property>
-       <property name="Script" required="no">Script to be passed to 
BeanShell</property>
+       <property name="Name" required="No">Descriptive name for this 
controller that is shown in the tree.</property>
+       <property name="Scripting Language" required="Yes">Name of the BSF 
scripting language to be used.</property>
+       <property name="Script File" required="No">Name of a file to be used as 
a BSF script</property>
+       <property name="Parameters" required="No">List of parameters to be 
passed to the script file or the script.</property>
+       <property name="Script" required="Yes (unless script file is 
provided)">Script to be passed to BSF language</property>
 </properties>
-TBC
+<p>
+If a script file is supplied, that will be used, otherwise the script will be 
used.</p>
+<p>
+Before invoking the script, some variables are set up:
+</p>
+<p>
+The contents of the Parameters field is put into the variable "Parameters".
+The string is also split into separate tokens using a single space as the 
separator, and the resulting list
+is stored in the String array args.
+</p>
+<p>
+The full list of variables that is set up is as follows:</p>
+<ul>
+<li>log - the Logger</li>
+<li>Label - the Sampler label</li>
+<li>FileName - the file name, if any</li>
+<li>Parameters - text from the Parameters field</li>
+<li>args - the parameters, split as described above</li>
+<li>SampleResult - pointer to the current SampleResult</li>
+<li>ctx - JMeterContext</li>
+<li>vars - JMeterVariables  - e.g. vars.get("VAR1"); vars.put("VAR2","value"); 
vars.remove("VAR3");</li>
+</ul>
+<p>
+The Sampler ResponseData is set from the return value of the script.
+If the script returns null, it can set the response directly, by using the 
method 
+SampleResponse.setResponseData(data), where data is either a String or a byte 
array.
+The data type defaults to "text", but can be set to binary by using the method
+SampleResponse.setDataType(SampleResponse.BINARY).
+</p>
+<p>
+The SampleResult variable gives the script full access to all the fields and
+methods in the SampleResult. For example, the script has access to the methods
+setStopThread(boolean) and setStopTest(boolean).
+</p>
+<p>
+Unlike the Beanshell Sampler, the BSF Sampler does not set the ResponseCode, 
ResponseMessage and sample status via script variables.
+Currently the only way to changes these is via the SampleResponse methods:
+<ul>
+<li>SampleResponse.setSuccessful(true/false)</li>
+<li>SampleResponse.setResponseCode("code")</li>
+<li>SampleResponse.setResponseMessage("message")</li>
+</ul>
+</p>
 </component>
 
 <component name="TCP Sampler" index="&sect-num;.1.12"  width="631" 
height="305" screenshot="tcpsampler.png">



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to