1 18 19 package org.apache.jmeter.util; 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Method ; 23 24 import junit.framework.TestCase; 25 26 import org.apache.jorphan.logging.LoggingManager; 27 import org.apache.log.Logger; 28 29 38 public class BeanShellServer implements Runnable { 39 40 transient private static Logger log = LoggingManager.getLoggerForClass(); 41 42 private final int serverport; 43 private final String serverfile; 44 45 48 public BeanShellServer(int port,String file) { 49 super(); 50 serverfile=file; serverport=port; 52 } 53 54 private BeanShellServer(){ super(); 56 serverport=0; 57 serverfile=""; 58 } 59 private static String getprop(String s){ 61 return JMeterUtils.getPropDefault(s,s); 62 } 63 64 private static void setprop(String s, String v){ 65 JMeterUtils.getJMeterProperties().setProperty(s, v); 66 } 67 68 public void run(){ 69 70 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 71 72 try 73 { 74 Class Interpreter = loader.loadClass("bsh.Interpreter"); 75 Object instance = Interpreter.newInstance(); 76 Class string = String .class; 77 Class object = Object .class; 78 79 Method eval = 80 Interpreter.getMethod( 81 "eval", 82 new Class [] {string}); 83 Method setObj = Interpreter.getMethod( 84 "set", 85 new Class [] {string,object}); 86 Method setInt = Interpreter.getMethod( 87 "set", 88 new Class [] {string,int.class}); 89 Method source = 90 Interpreter.getMethod( 91 "source", 92 new Class [] {string}); 93 94 setObj.invoke(instance, new Object [] { "t",this }); 95 setInt.invoke(instance, new Object [] { "portnum",new Integer (serverport) }); 96 97 if (serverfile.length() > 0 ){ 98 try { 99 source.invoke(instance, new Object []{serverfile}); 100 } catch (InvocationTargetException e1) { 101 log.warn("Could not source "+serverfile); 102 } 105 } 106 eval.invoke(instance, new Object []{"setAccessibility(true);"}); 107 eval.invoke(instance, new Object []{"server(portnum);"}); 108 109 } 110 catch(ClassNotFoundException e ){ 111 log.error("Beanshell Interpreter not found"); 112 } 113 catch (Exception e) 114 { 115 log.error("Problem starting BeanShell server ",e); 116 } 117 } 118 public static class Test extends TestCase 119 { 120 122 public Test() 123 { 124 super(); 125 } 126 127 public void testServer() throws Exception 128 { 129 BeanShellServer bshs = new BeanShellServer(9876,""); 130 assertNotNull(bshs); 131 } 133 134 public void testProps() throws Exception 135 { 136 if (JMeterUtils.getJMeterProperties() != null){ assertNotNull("Property user.dir should not be null",getprop("user.dir")); 138 setprop("beanshelltest","xyz"); 139 assertEquals("xyz",getprop("beanshelltest")); 140 } 141 } 142 } 143 } 144 | Popular Tags |