KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > util > BeanShellServer


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/util/BeanShellServer.java,v 1.7 2004/03/30 18:08:09 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.util;
20
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.apache.jorphan.logging.LoggingManager;
27 import org.apache.log.Logger;
28
29 /**
30  * Implements a BeanShell server to allow access to JMeter variables and methods.
31  *
32  * To enable, define the JMeter property:
33  * beanshell.server.port (see JMeter.java)
34  * beanshell.server.file (optional, startup file)
35  *
36  * @version $Revision: 1.7 $ Last updated: $Date: 2004/03/30 18:08:09 $
37  */

38 public class BeanShellServer implements Runnable JavaDoc {
39
40     transient private static Logger log = LoggingManager.getLoggerForClass();
41
42     private final int serverport;
43     private final String JavaDoc serverfile;
44     
45     /**
46      *
47      */

48     public BeanShellServer(int port,String JavaDoc file) {
49         super();
50         serverfile=file;// can be the empty string
51
serverport=port;
52     }
53
54     private BeanShellServer(){// do not use!
55
super();
56         serverport=0;
57         serverfile="";
58     }
59     // For use by the server script
60
private static String JavaDoc getprop(String JavaDoc s){
61         return JMeterUtils.getPropDefault(s,s);
62     }
63
64     private static void setprop(String JavaDoc s, String JavaDoc v){
65         JMeterUtils.getJMeterProperties().setProperty(s, v);
66     }
67
68     public void run(){
69
70         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
71
72         try
73         {
74             Class JavaDoc Interpreter = loader.loadClass("bsh.Interpreter");
75             Object JavaDoc instance = Interpreter.newInstance();
76             Class JavaDoc string = String JavaDoc.class;
77             Class JavaDoc object = Object JavaDoc.class;
78             
79             Method JavaDoc eval =
80                 Interpreter.getMethod(
81                     "eval",
82                     new Class JavaDoc[] {string});
83             Method JavaDoc setObj = Interpreter.getMethod(
84                     "set",
85                     new Class JavaDoc[] {string,object});
86             Method JavaDoc setInt = Interpreter.getMethod(
87                     "set",
88                     new Class JavaDoc[] {string,int.class});
89             Method JavaDoc source =
90                 Interpreter.getMethod(
91                     "source",
92                     new Class JavaDoc[] {string});
93             
94             setObj.invoke(instance, new Object JavaDoc[] { "t",this });
95             setInt.invoke(instance, new Object JavaDoc[] { "portnum",new Integer JavaDoc(serverport) });
96
97             if (serverfile.length() > 0 ){
98                 try {
99                     source.invoke(instance, new Object JavaDoc[]{serverfile});
100                 } catch (InvocationTargetException JavaDoc e1) {
101                     log.warn("Could not source "+serverfile);
102                     //JDK1.4: Throwable t= e1.getCause();
103
//JDK1.4: if (t != null) log.warn(t.toString());
104
}
105             }
106             eval.invoke(instance, new Object JavaDoc[]{"setAccessibility(true);"});
107             eval.invoke(instance, new Object JavaDoc[]{"server(portnum);"});
108
109         }
110         catch(ClassNotFoundException JavaDoc e ){
111             log.error("Beanshell Interpreter not found");
112         }
113         catch (Exception JavaDoc e)
114         {
115             log.error("Problem starting BeanShell server ",e);
116         }
117     }
118     public static class Test extends TestCase
119     {
120         //private static Logger log = LoggingManager.getLoggerForClass();
121

122         public Test()
123         {
124             super();
125         }
126
127         public void testServer() throws Exception JavaDoc
128         {
129             BeanShellServer bshs = new BeanShellServer(9876,"");
130             assertNotNull(bshs);
131             // Not sure we can test anything else here
132
}
133         
134         public void testProps() throws Exception JavaDoc
135         {
136             if (JMeterUtils.getJMeterProperties() != null){//Can't test standalone
137
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