KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > functions > BeanShell


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/BeanShell.java,v 1.3.2.5 2005/03/07 00:26:57 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.functions;
20
21 import java.io.IOException JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
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 /**
40  * A function which understands BeanShell
41  *
42  * @version $Revision: 1.3.2.5 $ Updated on: $Date: 2005/03/07 00:26:57 $
43  */

44
45 public class BeanShell extends AbstractFunction implements Serializable JavaDoc
46 {
47
48     private static Logger log = LoggingManager.getLoggerForClass();
49
50     private static final List JavaDoc desc = new LinkedList JavaDoc();
51     private static final String JavaDoc KEY = "__BeanShell"; //$NON-NLS-1$
52
public static final String JavaDoc INIT_FILE = "beanshell.function.init"; //$NON-NLS-1$
53

54     
55     static {
56         desc.add(JMeterUtils.getResString("bsh_function_expression"));//$NON-NLS1$
57
desc.add(JMeterUtils.getResString("function_name_param"));//$NON-NLS1$
58
}
59
60     transient private Object JavaDoc[] values;
61     transient private BeanShellInterpreter bshInterpreter=null;
62
63     public BeanShell()
64     {
65     }
66
67     public Object JavaDoc clone()
68     {
69         return new BeanShell();
70     }
71
72     /* (non-Javadoc)
73      * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
74      */

75     public synchronized String JavaDoc execute(
76         SampleResult previousResult,
77         Sampler currentSampler)
78         throws InvalidVariableException
79     {
80
81         if (bshInterpreter == null) // did we find BeanShell?
82
{
83             throw new InvalidVariableException("BeanShell not found");
84         }
85         
86         JMeterContext jmctx = JMeterContextService.getContext();
87         JMeterVariables vars = jmctx.getVariables();
88
89         String JavaDoc script = ((CompoundVariable) values[0]).execute();
90         String JavaDoc varName = "";
91         if (values.length > 1){
92             varName = ((CompoundVariable) values[1]).execute();
93         }
94         
95         String JavaDoc resultStr = "";
96         
97         log.debug("Script="+script);
98
99         try
100         {
101
102             // Pass in some variables
103
if (currentSampler != null)
104             {
105                 bshInterpreter.set("Sampler",currentSampler); //$NON-NLS-1$
106
}
107             
108             if (previousResult != null)
109             {
110                 bshInterpreter.set("SampleResult",previousResult); //$NON-NLS-1$
111
}
112             
113             // Allow access to context and variables directly
114
bshInterpreter.set("ctx",jmctx); //$NON-NLS-1$
115
bshInterpreter.set("vars",vars); //$NON-NLS-1$
116
bshInterpreter.set("threadName",Thread.currentThread().getName()); //$NON-NLS-1$
117

118             // Execute the script
119
Object JavaDoc 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 JavaDoc ex) // Mainly for bsh.EvalError
129
{
130             log.warn("Error running BSH script",ex);
131         }
132
133         log.debug("Output="+resultStr);
134         return resultStr;
135
136     }
137
138     /*
139      * Helper method for use by scripts
140      *
141      */

142     public void log_info(String JavaDoc s){
143         log.info(s);
144     }
145
146     
147     /* (non-Javadoc)
148      * @see org.apache.jmeter.functions.Function#setParameters(Collection)
149      */

150     public void setParameters(Collection JavaDoc 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);//$NON-NLS-1$
160
}
161         
162         try {
163             bshInterpreter = new BeanShellInterpreter();
164             try {
165                 bshInterpreter.init(JMeterUtils.getProperty(INIT_FILE), log);
166             } catch (IOException JavaDoc e) {
167                 log.warn("Can't init interpreter");
168             } catch (JMeterException e) {
169                 log.warn("Can't init interpreter");
170             }
171         } catch (ClassNotFoundException JavaDoc e) {
172             throw new InvalidVariableException("BeanShell not found");
173         }
174     }
175
176     /* (non-Javadoc)
177      * @see org.apache.jmeter.functions.Function#getReferenceKey()
178      */

179     public String JavaDoc getReferenceKey()
180     {
181         return KEY;
182     }
183
184     /* (non-Javadoc)
185      * @see org.apache.jmeter.functions.Function#getArgumentDesc()
186      */

187     public List JavaDoc getArgumentDesc()
188     {
189         return desc;
190     }
191
192 }
193
Popular Tags