KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/util/Attic/BeanShellInterpreter.java,v 1.1.2.1 2005/03/07 00:25:56 sebb Exp $
2
/*
3  * Copyright 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.io.File JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
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 /**
32  * BeanShell setup function - encapsulates all the access to the BeanShell Interpreter
33  * in a single class.
34  *
35  * The class uses dynamic class loading to access BeanShell, which means that
36  * all the source files can be built without needing access to the bsh jar.
37  *
38  * If the beanshell jar is not present at run-time, an error will be logged
39  *
40  * @version $Revision: 1.1.2.1 $ Updated on: $Date: 2005/03/07 00:25:56 $
41  */

42
43 public class BeanShellInterpreter
44 {
45     private static Logger log = LoggingManager.getLoggerForClass();
46
47     private static final Method JavaDoc bshGet;
48     private static final Method JavaDoc bshSet;
49     private static final Method JavaDoc bshEval;
50     private static final Method JavaDoc bshSource;
51     private static final Class JavaDoc bshClass;
52
53     static{
54         Method JavaDoc get=null, eval=null, set=null, source=null;// Temporary, so can set the final ones
55
Class JavaDoc clazz = null;
56         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
57         try
58         {
59             clazz = loader.loadClass("bsh.Interpreter");
60             Class JavaDoc string = String JavaDoc.class;
61             Class JavaDoc object = Object JavaDoc.class;
62                 
63             get = clazz.getMethod("get", //$NON-NLS-1$
64
new Class JavaDoc[] {string});
65             eval = clazz.getMethod("eval", //$NON-NLS-1$
66
new Class JavaDoc[] {string});
67             set = clazz.getMethod("set", //$NON-NLS-1$
68
new Class JavaDoc[] {string,object});
69             source = clazz.getMethod("source", //$NON-NLS-1$
70
new Class JavaDoc[] {string});
71         }
72         catch(ClassNotFoundException JavaDoc e ){
73             log.error("Beanshell Interpreter not found");
74         } catch (SecurityException JavaDoc e) {
75             log.error("Beanshell Interpreter not found",e);
76         } catch (NoSuchMethodException JavaDoc 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 JavaDoc bshInstance = null; // The interpreter instance for this class
89

90     public BeanShellInterpreter() throws ClassNotFoundException JavaDoc
91     {
92         if (bshClass == null) {
93             throw new ClassNotFoundException JavaDoc("bsh.Interpreter");
94         }
95         try {
96             bshInstance = bshClass.newInstance();
97         } catch (InstantiationException JavaDoc e) {
98             log.error("Can't instantiate BeanShell",e);
99             throw new ClassNotFoundException JavaDoc("Can't instantiate BeanShell",e);
100         } catch (IllegalAccessException JavaDoc e) {
101             log.error("Can't instantiate BeanShell",e);
102             throw new ClassNotFoundException JavaDoc("Can't instantiate BeanShell",e);
103         }
104     }
105     
106     public void init(String JavaDoc initFile, Object JavaDoc logger)
107         throws IOException JavaDoc, JMeterException
108     {
109         if (logger != null){// Do this before starting the script
110
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             // Check file so we can distinguish file error from script error
119
File JavaDoc in = new File JavaDoc(initFile);
120             if (!in.exists()) {
121                 throw new FileNotFoundException JavaDoc("initFile");
122             }
123             if (!in.canRead()) {
124                 throw new IOException JavaDoc("initFile");
125             } else {
126                 source(initFile);
127             }
128         }
129     }
130     
131     private Object JavaDoc bshInvoke(Method JavaDoc m, String JavaDoc s, Object JavaDoc o) throws JMeterException
132     {
133         Object JavaDoc r=null;
134         try {
135             if (o == null)
136             {
137                 r = m.invoke(bshInstance, new Object JavaDoc[] {s});
138             }
139             else
140             {
141                 r = m.invoke(bshInstance, new Object JavaDoc[] {s, o});
142             }
143         } catch (IllegalArgumentException JavaDoc e) { // Programming error
144
log.error("Error invoking bsh method "+m.getName()+"\n",e);
145             throw new JMeterError("Error invoking bsh method "+m.getName(),e);
146         } catch (IllegalAccessException JavaDoc e) { // Also programming error
147
log.error("Error invoking bsh method "+m.getName()+"\n",e);
148             throw new JMeterError("Error invoking bsh method "+m.getName(),e);
149         } catch (InvocationTargetException JavaDoc e) { // Can occur at run-time
150
//could be caused by the bsh Exceptions:
151
// EvalError, ParseException or TargetError
152
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 JavaDoc eval(String JavaDoc s) throws JMeterException{
159         return bshInvoke(bshEval,s, null);
160     }
161     public Object JavaDoc set(String JavaDoc s, Object JavaDoc o) throws JMeterException{
162         return bshInvoke(bshSet,s, o);
163     }
164     public Object JavaDoc set(String JavaDoc s, boolean b) throws JMeterException{
165         return bshInvoke(bshSet,s, b ? Boolean.TRUE : Boolean.FALSE);//JDK1.4 Boolean.vaueof
166
}
167     public Object JavaDoc source(String JavaDoc s) throws JMeterException{
168         return bshInvoke(bshSource,s, null);
169     }
170
171     public Object JavaDoc get(String JavaDoc s) throws JMeterException{
172         return bshInvoke(bshGet,s,null);
173     }
174     // These don't vary between executes, so can be done once TODO is this true?
175
// bshInvoke(bshSet,"log",log); //$NON-NLS-1$
176
// ;
177
}
178
Popular Tags