1 18 19 package org.apache.jmeter.util; 20 21 import java.io.File ; 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import org.apache.jorphan.logging.LoggingManager; 27 import org.apache.jorphan.util.JMeterError; 28 import org.apache.jorphan.util.JMeterException; 29 import org.apache.log.Logger; 30 31 42 43 public class BeanShellInterpreter 44 { 45 private static Logger log = LoggingManager.getLoggerForClass(); 46 47 private static final Method bshGet; 48 private static final Method bshSet; 49 private static final Method bshEval; 50 private static final Method bshSource; 51 private static final Class bshClass; 52 53 static{ 54 Method get=null, eval=null, set=null, source=null; Class clazz = null; 56 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 57 try 58 { 59 clazz = loader.loadClass("bsh.Interpreter"); 60 Class string = String .class; 61 Class object = Object .class; 62 63 get = clazz.getMethod("get", new Class [] {string}); 65 eval = clazz.getMethod("eval", new Class [] {string}); 67 set = clazz.getMethod("set", new Class [] {string,object}); 69 source = clazz.getMethod("source", new Class [] {string}); 71 } 72 catch(ClassNotFoundException e ){ 73 log.error("Beanshell Interpreter not found"); 74 } catch (SecurityException e) { 75 log.error("Beanshell Interpreter not found",e); 76 } catch (NoSuchMethodException e) { 77 log.error("Beanshell Interpreter not found",e); 78 } 79 finally{ 80 bshEval=eval; 81 bshGet=get; 82 bshSet=set; 83 bshSource=source; 84 bshClass=clazz; 85 } 86 } 87 88 transient private Object bshInstance = null; 90 public BeanShellInterpreter() throws ClassNotFoundException 91 { 92 if (bshClass == null) { 93 throw new ClassNotFoundException ("bsh.Interpreter"); 94 } 95 try { 96 bshInstance = bshClass.newInstance(); 97 } catch (InstantiationException e) { 98 log.error("Can't instantiate BeanShell",e); 99 throw new ClassNotFoundException ("Can't instantiate BeanShell",e); 100 } catch (IllegalAccessException e) { 101 log.error("Can't instantiate BeanShell",e); 102 throw new ClassNotFoundException ("Can't instantiate BeanShell",e); 103 } 104 } 105 106 public void init(String initFile, Object logger) 107 throws IOException , JMeterException 108 { 109 if (logger != null){ try { 111 set("log",logger); 112 } catch (JMeterException e) { 113 log.error("Can't set logger variable",e); 114 throw e; 115 } 116 } 117 if (initFile!=null && initFile.length()> 0) { 118 File in = new File (initFile); 120 if (!in.exists()) { 121 throw new FileNotFoundException ("initFile"); 122 } 123 if (!in.canRead()) { 124 throw new IOException ("initFile"); 125 } else { 126 source(initFile); 127 } 128 } 129 } 130 131 private Object bshInvoke(Method m, String s, Object o) throws JMeterException 132 { 133 Object r=null; 134 try { 135 if (o == null) 136 { 137 r = m.invoke(bshInstance, new Object [] {s}); 138 } 139 else 140 { 141 r = m.invoke(bshInstance, new Object [] {s, o}); 142 } 143 } catch (IllegalArgumentException e) { log.error("Error invoking bsh method "+m.getName()+"\n",e); 145 throw new JMeterError("Error invoking bsh method "+m.getName(),e); 146 } catch (IllegalAccessException e) { log.error("Error invoking bsh method "+m.getName()+"\n",e); 148 throw new JMeterError("Error invoking bsh method "+m.getName(),e); 149 } catch (InvocationTargetException e) { log.error("Error invoking bsh method "+m.getName()+"\n",e); 153 throw new JMeterException("Error invoking bsh method "+m.getName(),e); 154 } 155 return r; 156 } 157 158 public Object eval(String s) throws JMeterException{ 159 return bshInvoke(bshEval,s, null); 160 } 161 public Object set(String s, Object o) throws JMeterException{ 162 return bshInvoke(bshSet,s, o); 163 } 164 public Object set(String s, boolean b) throws JMeterException{ 165 return bshInvoke(bshSet,s, b ? Boolean.TRUE : Boolean.FALSE); } 167 public Object source(String s) throws JMeterException{ 168 return bshInvoke(bshSource,s, null); 169 } 170 171 public Object get(String s) throws JMeterException{ 172 return bshInvoke(bshGet,s,null); 173 } 174 } 178 | Popular Tags |