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 2005/03/07 01:26:57 UTC

cvs commit: jakarta-jmeter/src/protocol/java/org/apache/jmeter/protocol/java/sampler BeanShellSampler.java

sebb        2005/03/06 16:26:57

  Modified:    src/components/org/apache/jmeter/assertions Tag: rel-2_0
                        BeanShellAssertion.java
               src/functions/org/apache/jmeter/functions Tag: rel-2_0
                        BeanShell.java
               src/protocol/java/org/apache/jmeter/protocol/java/sampler
                        Tag: rel-2_0 BeanShellSampler.java
  Log:
  Use new BeanShellInterpreter class so can build without BeanShell
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.3.2.6   +22 -31    jakarta-jmeter/src/components/org/apache/jmeter/assertions/BeanShellAssertion.java
  
  Index: BeanShellAssertion.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/BeanShellAssertion.java,v
  retrieving revision 1.3.2.5
  retrieving revision 1.3.2.6
  diff -u -r1.3.2.5 -r1.3.2.6
  --- BeanShellAssertion.java	6 Mar 2005 17:33:17 -0000	1.3.2.5
  +++ BeanShellAssertion.java	7 Mar 2005 00:26:56 -0000	1.3.2.6
  @@ -21,13 +21,12 @@
   import java.io.IOException;
   import java.io.Serializable;
   
  -//import bsh.EvalError;
  -import bsh.Interpreter;
  -   
   import org.apache.jmeter.samplers.SampleResult;
   import org.apache.jmeter.testelement.AbstractTestElement;
  +import org.apache.jmeter.util.BeanShellInterpreter;
   import org.apache.jmeter.util.JMeterUtils;
   import org.apache.jorphan.logging.LoggingManager;
  +import org.apache.jorphan.util.JMeterException;
   import org.apache.jorphan.util.JOrphanUtils;
   import org.apache.log.Logger;
   
  @@ -48,27 +47,22 @@
   	// can be specified in jmeter.properties
   	public static final String INIT_FILE = "beanshell.assertion.init"; //$NON-NLS-1$
   
  -    private Interpreter bshInterpreter;
  +    transient private BeanShellInterpreter bshInterpreter = null;
   	
   	public BeanShellAssertion()
   	{
  -		String init="";
  -		try{
  -			bshInterpreter = new Interpreter();
  -			init = JMeterUtils.getPropDefault(INIT_FILE,null);
  -			if (init != null)
  -			{
  -				try
  -				{
  -					 bshInterpreter.source(null);
  -				} catch (IOException e){
  -					log.warn("Error processing init file "+init+" "+e);
  -				} catch (Exception e){
  -					log.warn("Error processing init file "+init+" "+e);
  -				}
  +		try {
  +			bshInterpreter = new BeanShellInterpreter();
  +			String init = JMeterUtils.getProperty(INIT_FILE);
  +			try {
  +				bshInterpreter.init(init,log);
  +			} catch (IOException e) {
  +				log.warn("Could not initialise interpreter",e);
  +			} catch (JMeterException e) {
  +				log.warn("Could not initialise interpreter",e);
   			}
  -		} catch (NoClassDefFoundError e){
  -			bshInterpreter=null;
  +		} catch (ClassNotFoundException e) {
  +			log.error("Could not establish BeanShellInterpreter: "+e);
   		}
   	}
   
  @@ -94,14 +88,17 @@
   	{
   		AssertionResult result = new AssertionResult();
   		
  +		if (bshInterpreter == null){
  +			result.setFailure(true);
  +			result.setError(true);
  +			result.setFailureMessage("BeanShell Interpreter not found");
  +			return result;
  +		}
   		try
           {
           	String request=getScript();
           	String fileName=getFilename();
           	
  -        	// Has to be done after construction, otherwise fails serialisation check
  -        	bshInterpreter.set("log",log);  //$NON-NLS-1$
  -			
           	bshInterpreter.set("FileName",getFilename());//$NON-NLS-1$
   			bshInterpreter.set("Parameters",getParameters());// as a single line $NON-NLS-1$
   			bshInterpreter.set("bsh.args",//$NON-NLS-1$
  @@ -155,12 +152,6 @@
   			result.setFailureMessage("BeanShell Jar missing? "+ex.toString());
   			response.setStopThread(true); // No point continuing
           }
  -		catch (IOException ex)
  -		{
  -			result.setError(true);
  -			result.setFailureMessage(ex.toString());
  -			log.warn(ex.toString());
  -		}
   		catch (Exception ex) // Mainly for bsh.EvalError
   		{
   			result.setError(true);
  
  
  
  No                   revision
  No                   revision
  1.3.2.5   +25 -83    jakarta-jmeter/src/functions/org/apache/jmeter/functions/BeanShell.java
  
  Index: BeanShell.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/BeanShell.java,v
  retrieving revision 1.3.2.4
  retrieving revision 1.3.2.5
  diff -u -r1.3.2.4 -r1.3.2.5
  --- BeanShell.java	11 Dec 2004 01:25:50 -0000	1.3.2.4
  +++ BeanShell.java	7 Mar 2005 00:26:57 -0000	1.3.2.5
  @@ -18,9 +18,8 @@
   
   package org.apache.jmeter.functions;
   
  +import java.io.IOException;
   import java.io.Serializable;
  -import java.lang.reflect.InvocationTargetException;
  -import java.lang.reflect.Method;
   import java.util.Collection;
   import java.util.LinkedList;
   import java.util.List;
  @@ -31,15 +30,14 @@
   import org.apache.jmeter.threads.JMeterContext;
   import org.apache.jmeter.threads.JMeterContextService;
   import org.apache.jmeter.threads.JMeterVariables;
  +import org.apache.jmeter.util.BeanShellInterpreter;
   import org.apache.jmeter.util.JMeterUtils;
   import org.apache.jorphan.logging.LoggingManager;
  +import org.apache.jorphan.util.JMeterException;
   import org.apache.log.Logger;
   
   /**
    * A function which understands BeanShell
  - *
  - * ALPHA CODE - liable to change without notice!
  - * =============================================
    * 
    * @version    $Revision$ Updated on: $Date$
    */
  @@ -60,6 +58,7 @@
       }
   
       transient private Object[] values;
  +	transient private BeanShellInterpreter bshInterpreter=null;
   
       public BeanShell()
       {
  @@ -79,7 +78,7 @@
           throws InvalidVariableException
       {
   
  -    	if (bshSet == null) // did we find BeanShell?
  +    	if (bshInterpreter == null) // did we find BeanShell?
       	{
       		throw new InvalidVariableException("BeanShell not found");
       	}
  @@ -103,21 +102,21 @@
   			// Pass in some variables
           	if (currentSampler != null)
           	{
  -				bshInvoke(bshSet,"Sampler",currentSampler);  //$NON-NLS-1$
  +				bshInterpreter.set("Sampler",currentSampler);  //$NON-NLS-1$
           	}
   			
   			if (previousResult != null)
   			{
  -				bshInvoke(bshSet,"SampleResult",previousResult);  //$NON-NLS-1$
  +				bshInterpreter.set("SampleResult",previousResult);  //$NON-NLS-1$
   			}
   			
   			// Allow access to context and variables directly
  -			bshInvoke(bshSet,"ctx",jmctx);  //$NON-NLS-1$
  -			bshInvoke(bshSet,"vars",vars); //$NON-NLS-1$
  -			bshInvoke(bshSet,"threadName",Thread.currentThread().getName());  //$NON-NLS-1$
  +			bshInterpreter.set("ctx",jmctx);  //$NON-NLS-1$
  +			bshInterpreter.set("vars",vars); //$NON-NLS-1$
  +			bshInterpreter.set("threadName",Thread.currentThread().getName());  //$NON-NLS-1$
   			
               // Execute the script
  -            Object bshOut = bshInvoke(bshEval,script,null);
  +            Object bshOut = bshInterpreter.eval(script);
   			if (bshOut != null) {
   				resultStr = bshOut.toString();
   			}
  @@ -144,73 +143,6 @@
       	log.info(s);
       }
   
  -	transient private Object instance;
  -	transient private Method bshSet;
  -	transient private Method bshEval;
  -	transient private Method bshSource;
  -
  -//TODO move to common class (in jorphan?) so can be shared with other BSH modules
  -
  -
  -	// Helper method for invoking bsh methods
  -	private Object bshInvoke(Method m, String s, Object o)
  -	{
  -		Object r=null;
  -		try {
  -			if (o == null)
  -			{
  -				r = m.invoke(instance, new Object[] {s});
  -			}
  -			else
  -			{
  -			    r = m.invoke(instance, new Object[] {s, o});
  -			}
  -		} catch (IllegalArgumentException e) {
  -			log.error("Error invoking bsh method "+m.getName()+"\n",e);
  -		} catch (IllegalAccessException e) {
  -			log.error("Error invoking bsh method "+m.getName()+"\n",e);
  -		} catch (InvocationTargetException e) {
  -			log.error("Error invoking bsh method "+m.getName()+"\n",e);
  -		}		
  -		return r;
  -	}
  -
  -	private void setupBeanShell()
  -    {    
  -		ClassLoader loader = Thread.currentThread().getContextClassLoader();
  -
  -	try
  -	{
  -		Class Interpreter = loader.loadClass("bsh.Interpreter");
  -		instance = Interpreter.newInstance();
  -		Class string = String.class;
  -		Class object = Object.class;
  -			
  -		bshEval = Interpreter.getMethod(
  -				"eval", //$NON-NLS-1$
  -				new Class[] {string});
  -		bshSet = Interpreter.getMethod(
  -				"set", //$NON-NLS-1$
  -				new Class[] {string,object});
  -		
  -		bshSource = Interpreter.getMethod(
  -				"source", //$NON-NLS-1$
  -				new Class[] {string});
  -
  -	}
  -	catch(ClassNotFoundException e ){
  -		log.error("Beanshell Interpreter not found");
  -	}
  -	catch (Exception e)
  -	{
  -		log.error("Problem starting BeanShell server ",e);
  -	}
  -
  -	// These don't vary between executes, so can be done once
  -	bshInvoke(bshSet,"log",log); //$NON-NLS-1$
  -    String initFile = JMeterUtils.getPropDefault(INIT_FILE,null);
  -	if (initFile!=null) bshInvoke(bshSource,initFile,null);
  -	}
   	
       /* (non-Javadoc)
        * @see org.apache.jmeter.functions.Function#setParameters(Collection)
  @@ -227,8 +159,18 @@
               		"Expecting 1 or 2 parameters, but found "+values.length);//$NON-NLS-1$
           }
           
  -        setupBeanShell();
  -
  +		try {
  +			bshInterpreter = new BeanShellInterpreter();
  +			try {
  +				bshInterpreter.init(JMeterUtils.getProperty(INIT_FILE),	log);
  +			} catch (IOException e) {
  +				log.warn("Can't init interpreter");
  +			} catch (JMeterException e) {
  +				log.warn("Can't init interpreter");
  +			}
  +		} catch (ClassNotFoundException e) {
  +			throw new InvalidVariableException("BeanShell not found");
  +		}
       }
   
       /* (non-Javadoc)
  
  
  
  No                   revision
  No                   revision
  1.6.2.5   +23 -31    jakarta-jmeter/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BeanShellSampler.java
  
  Index: BeanShellSampler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BeanShellSampler.java,v
  retrieving revision 1.6.2.4
  retrieving revision 1.6.2.5
  diff -u -r1.6.2.4 -r1.6.2.5
  --- BeanShellSampler.java	8 Oct 2004 23:20:10 -0000	1.6.2.4
  +++ BeanShellSampler.java	7 Mar 2005 00:26:57 -0000	1.6.2.5
  @@ -18,17 +18,15 @@
   
   package org.apache.jmeter.protocol.java.sampler;
   
  -import java.io.FileNotFoundException;
   import java.io.IOException;
   
  -//import bsh.EvalError;
  -import bsh.Interpreter;
  -   
   import org.apache.jmeter.samplers.AbstractSampler;
   import org.apache.jmeter.samplers.Entry;
   import org.apache.jmeter.samplers.SampleResult;
  +import org.apache.jmeter.util.BeanShellInterpreter;
   import org.apache.jmeter.util.JMeterUtils;
   import org.apache.jorphan.logging.LoggingManager;
  +import org.apache.jorphan.util.JMeterException;
   import org.apache.jorphan.util.JOrphanUtils;
   import org.apache.log.Logger;
   
  @@ -46,27 +44,22 @@
   	public static final String PARAMETERS = "BeanShellSampler.parameters"; //$NON-NLS-1$
   	public static final String INIT_FILE = "beanshell.sampler.init"; //$NON-NLS-1$
   
  -    private Interpreter bshInterpreter;
  +    transient private BeanShellInterpreter bshInterpreter;
   	
   	public BeanShellSampler()
   	{
  -		String init="";
  -		try{
  -			bshInterpreter = new Interpreter();
  -			init = JMeterUtils.getPropDefault(INIT_FILE,null);
  -			if (init != null)
  -			{
  -				try
  -				{
  -					 bshInterpreter.source(init);
  -				} catch (IOException e){
  -					log.warn("Error processing init file "+init+" "+e);
  -				} catch (Exception e){
  -					log.warn("Error processing init file "+init+" "+e);
  -				}
  +		try {
  +			bshInterpreter = new BeanShellInterpreter();
  +			String init = JMeterUtils.getProperty(INIT_FILE);
  +			try {
  +				bshInterpreter.init(init,log);
  +			} catch (IOException e) {
  +				log.warn("Could not initialise interpreter",e);
  +			} catch (JMeterException e) {
  +				log.warn("Could not initialise interpreter",e);
   			}
  -		} catch (NoClassDefFoundError e){
  -			bshInterpreter=null;
  +		} catch (ClassNotFoundException e) {
  +			log.error("Could not establish BeanShellInterpreter: "+e);
   		}
   	}
   
  @@ -103,6 +96,13 @@
           boolean isSuccessful = false;
           res.setSampleLabel(getLabel());
           res.sampleStart();
  +		if (bshInterpreter == null){
  +			res.sampleEnd();
  +			res.setResponseCode("503");//$NON-NLS-1$
  +			res.setResponseMessage("BeanShell Interpreter not found");
  +			res.setSuccessful(false);
  +			return res;
  +		}
           try
           {
           	String request=getScript();
  @@ -112,8 +112,6 @@
           	} else {
   				res.setSamplerData(fileName);
           	}
  -        	// Has to be done after construction, otherwise fails serialisation check
  -        	bshInterpreter.set("log",log);  //$NON-NLS-1$
   
   			bshInterpreter.set("Label",getLabel());  //$NON-NLS-1$
   			bshInterpreter.set("FileName",getFilename()); //$NON-NLS-1$
  @@ -164,12 +162,6 @@
   			res.setResponseMessage(ex.toString());
   			res.setStopThread(true); // No point continuing
           }
  -		catch (IOException ex)
  -		{
  -			log.warn(ex.toString());
  -			res.setResponseCode("500");//$NON-NLS-1$
  -			res.setResponseMessage(ex.toString());
  -		}
   		catch (Exception ex) // Mainly for bsh.EvalError
   		{
   			log.warn(ex.toString());
  
  
  

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