1 18 19 package org.apache.jmeter.functions; 20 21 import java.io.IOException ; 22 import java.io.Serializable ; 23 import java.util.Collection ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 27 import org.apache.jmeter.engine.util.CompoundVariable; 28 import org.apache.jmeter.samplers.SampleResult; 29 import org.apache.jmeter.samplers.Sampler; 30 import org.apache.jmeter.threads.JMeterContext; 31 import org.apache.jmeter.threads.JMeterContextService; 32 import org.apache.jmeter.threads.JMeterVariables; 33 import org.apache.jmeter.util.BeanShellInterpreter; 34 import org.apache.jmeter.util.JMeterUtils; 35 import org.apache.jorphan.logging.LoggingManager; 36 import org.apache.jorphan.util.JMeterException; 37 import org.apache.log.Logger; 38 39 44 45 public class BeanShell extends AbstractFunction implements Serializable 46 { 47 48 private static Logger log = LoggingManager.getLoggerForClass(); 49 50 private static final List desc = new LinkedList (); 51 private static final String KEY = "__BeanShell"; public static final String INIT_FILE = "beanshell.function.init"; 54 55 static { 56 desc.add(JMeterUtils.getResString("bsh_function_expression")); desc.add(JMeterUtils.getResString("function_name_param")); } 59 60 transient private Object [] values; 61 transient private BeanShellInterpreter bshInterpreter=null; 62 63 public BeanShell() 64 { 65 } 66 67 public Object clone() 68 { 69 return new BeanShell(); 70 } 71 72 75 public synchronized String execute( 76 SampleResult previousResult, 77 Sampler currentSampler) 78 throws InvalidVariableException 79 { 80 81 if (bshInterpreter == null) { 83 throw new InvalidVariableException("BeanShell not found"); 84 } 85 86 JMeterContext jmctx = JMeterContextService.getContext(); 87 JMeterVariables vars = jmctx.getVariables(); 88 89 String script = ((CompoundVariable) values[0]).execute(); 90 String varName = ""; 91 if (values.length > 1){ 92 varName = ((CompoundVariable) values[1]).execute(); 93 } 94 95 String resultStr = ""; 96 97 log.debug("Script="+script); 98 99 try 100 { 101 102 if (currentSampler != null) 104 { 105 bshInterpreter.set("Sampler",currentSampler); } 107 108 if (previousResult != null) 109 { 110 bshInterpreter.set("SampleResult",previousResult); } 112 113 bshInterpreter.set("ctx",jmctx); bshInterpreter.set("vars",vars); bshInterpreter.set("threadName",Thread.currentThread().getName()); 118 Object bshOut = bshInterpreter.eval(script); 120 if (bshOut != null) { 121 resultStr = bshOut.toString(); 122 } 123 if (varName.length() > 0) 124 { 125 vars.put(varName, resultStr); 126 } 127 } 128 catch (Exception ex) { 130 log.warn("Error running BSH script",ex); 131 } 132 133 log.debug("Output="+resultStr); 134 return resultStr; 135 136 } 137 138 142 public void log_info(String s){ 143 log.info(s); 144 } 145 146 147 150 public void setParameters(Collection parameters) 151 throws InvalidVariableException 152 { 153 154 values = parameters.toArray(); 155 156 if (values.length < 1 || values.length > 2) 157 { 158 throw new InvalidVariableException( 159 "Expecting 1 or 2 parameters, but found "+values.length); } 161 162 try { 163 bshInterpreter = new BeanShellInterpreter(); 164 try { 165 bshInterpreter.init(JMeterUtils.getProperty(INIT_FILE), log); 166 } catch (IOException e) { 167 log.warn("Can't init interpreter"); 168 } catch (JMeterException e) { 169 log.warn("Can't init interpreter"); 170 } 171 } catch (ClassNotFoundException e) { 172 throw new InvalidVariableException("BeanShell not found"); 173 } 174 } 175 176 179 public String getReferenceKey() 180 { 181 return KEY; 182 } 183 184 187 public List getArgumentDesc() 188 { 189 return desc; 190 } 191 192 } 193 | Popular Tags |