You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by se...@apache.org on 2008/05/03 22:10:41 UTC

svn commit: r653124 - in /jakarta/jmeter/trunk: docs/changes.html src/protocol/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java xdocs/changes.xml

Author: sebb
Date: Sat May  3 13:10:41 2008
New Revision: 653124

URL: http://svn.apache.org/viewvc?rev=653124&view=rev
Log:
BSF Sampler now works properly with Javascript

Modified:
    jakarta/jmeter/trunk/docs/changes.html
    jakarta/jmeter/trunk/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java
    jakarta/jmeter/trunk/xdocs/changes.xml

Modified: jakarta/jmeter/trunk/docs/changes.html
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/docs/changes.html?rev=653124&r1=653123&r2=653124&view=diff
==============================================================================
--- jakarta/jmeter/trunk/docs/changes.html (original)
+++ jakarta/jmeter/trunk/docs/changes.html Sat May  3 13:10:41 2008
@@ -398,6 +398,11 @@
 						</li>
 									
 
+												<li	>
+								BSF Sampler now works properly with Javascript
+						</li>
+									
+
 						</ul>
 							  									 				<h4	>
 								Improvements

Modified: jakarta/jmeter/trunk/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java?rev=653124&r1=653123&r2=653124&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java (original)
+++ jakarta/jmeter/trunk/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java Sat May  3 13:10:41 2008
@@ -21,6 +21,7 @@
 import java.io.FileInputStream;
 
 import org.apache.bsf.BSFEngine;
+import org.apache.bsf.BSFException;
 import org.apache.bsf.BSFManager;
 import org.apache.commons.io.IOUtils;
 import org.apache.jmeter.samplers.AbstractSampler;
@@ -51,10 +52,7 @@
 	private static final String PARAMETERS = "BSFSampler.parameters"; //$NON-NLS-1$
 	//- JMX file attributes
 
-	private transient BSFManager mgr;
-
 	public BSFSampler() {
-		mgr = new BSFManager();
 	}
 
 	public String getFilename() {
@@ -102,15 +100,29 @@
 	public SampleResult sample(Entry e)// Entry tends to be ignored ...
 	{
 		final String label = getLabel();
-		log.info(label + " " + getFilename());
+        final String request = getScript();
+        final String fileName = getFilename();
+		log.debug(label + " " + fileName);
 		SampleResult res = new SampleResult();
 		res.setSampleLabel(label);
 		FileInputStream is = null;
-		
-		res.sampleStart();
+		BSFEngine bsfEngine = null;
+		// There's little point saving the manager between invocations
+		// as we need to reset most of the beans anyway
+        BSFManager mgr = new BSFManager();
+
+        // 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);
+        res.setDataType(SampleResult.TEXT); // Default (can be overridden by the script)
+
+        JMeterContext jmctx = JMeterContextService.getContext();
+        JMeterVariables vars = jmctx.getVariables();
+
+        res.sampleStart();
 		try {
-			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$
@@ -120,26 +132,18 @@
 			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());
+			// N.B. some engines (e.g. Javascript) cannot handle certain declareBean() calls
+			// after the engine has been initialised, so create the engine last
+			bsfEngine = mgr.loadScriptingEngine(getScriptLanguage());
 
 			Object bsfOut = null;
 			if (fileName.length()>0) {
@@ -154,19 +158,23 @@
 			if (bsfOut != null) {
 			    res.setResponseData(bsfOut.toString().getBytes());
 			}
-		} catch (NoClassDefFoundError ex) {
-			log.warn("", ex);
-			res.setSuccessful(false);
-			res.setResponseCode("500"); // $NON-NLS-1$
-			res.setResponseMessage(ex.toString());
-		} catch (Exception ex) {
-			log.warn("", ex);
+        } catch (BSFException ex) {
+            log.warn("BSF error", ex);
+            res.setSuccessful(false);
+            res.setResponseCode("500"); // $NON-NLS-1$
+            res.setResponseMessage(ex.toString());
+		} catch (Exception ex) {// Catch evaluation errors
+			log.warn("Problem evaluating the script", ex);
 			res.setSuccessful(false);
 			res.setResponseCode("500"); // $NON-NLS-1$
 			res.setResponseMessage(ex.toString());
 		} finally {
 			res.sampleEnd();
 			IOUtils.closeQuietly(is);
+			if (bsfEngine != null) {
+			    bsfEngine.terminate();
+			}
+	        mgr.terminate();
 		}
 
 		return res;

Modified: jakarta/jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=653124&r1=653123&r2=653124&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/changes.xml (original)
+++ jakarta/jmeter/trunk/xdocs/changes.xml Sat May  3 13:10:41 2008
@@ -131,6 +131,7 @@
 <li>Bug 44912 - Filter not working in Log Parser</li>
 <li>The BeanShell and BSF component documentation made some incorrect references to the "SampleResponse" object;
 this has been corrected to "SampleResult"</li>
+<li>BSF Sampler now works properly with Javascript</li>
 </ul>
 
 <h4>Improvements</h4>



---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org