KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > java > sampler > BeanShellSampler


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BeanShellSampler.java,v 1.6.2.5 2005/03/07 00:26:57 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.protocol.java.sampler;
20
21 import java.io.IOException JavaDoc;
22
23 import org.apache.jmeter.samplers.AbstractSampler;
24 import org.apache.jmeter.samplers.Entry;
25 import org.apache.jmeter.samplers.SampleResult;
26 import org.apache.jmeter.util.BeanShellInterpreter;
27 import org.apache.jmeter.util.JMeterUtils;
28 import org.apache.jorphan.logging.LoggingManager;
29 import org.apache.jorphan.util.JMeterException;
30 import org.apache.jorphan.util.JOrphanUtils;
31 import org.apache.log.Logger;
32
33 /**
34  * A sampler which understands BeanShell
35  *
36  * @version $Revision: 1.6.2.5 $ Updated on: $Date: 2005/03/07 00:26:57 $
37  */

38 public class BeanShellSampler extends AbstractSampler
39 {
40     private static final Logger log = LoggingManager.getLoggerForClass();
41
42     public static final String JavaDoc FILENAME = "BeanShellSampler.filename"; //$NON-NLS-1$
43
public static final String JavaDoc SCRIPT = "BeanShellSampler.query"; //$NON-NLS-1$
44
public static final String JavaDoc PARAMETERS = "BeanShellSampler.parameters"; //$NON-NLS-1$
45
public static final String JavaDoc INIT_FILE = "beanshell.sampler.init"; //$NON-NLS-1$
46

47     transient private BeanShellInterpreter bshInterpreter;
48     
49     public BeanShellSampler()
50     {
51         try {
52             bshInterpreter = new BeanShellInterpreter();
53             String JavaDoc init = JMeterUtils.getProperty(INIT_FILE);
54             try {
55                 bshInterpreter.init(init,log);
56             } catch (IOException JavaDoc e) {
57                 log.warn("Could not initialise interpreter",e);
58             } catch (JMeterException e) {
59                 log.warn("Could not initialise interpreter",e);
60             }
61         } catch (ClassNotFoundException JavaDoc e) {
62             log.error("Could not establish BeanShellInterpreter: "+e);
63         }
64     }
65
66     /**
67      * Returns a formatted string label describing this sampler
68      *
69      * @return a formatted string label describing this sampler
70      */

71
72     public String JavaDoc getLabel()
73     {
74         return getName();
75     }
76
77     public String JavaDoc getScript()
78     {
79         return this.getPropertyAsString(SCRIPT);
80     }
81     
82     public String JavaDoc getFilename()
83     {
84         return getPropertyAsString(FILENAME);
85     }
86
87     public String JavaDoc getParameters()
88     {
89         return getPropertyAsString(PARAMETERS);
90     }
91
92     public SampleResult sample(Entry e)// Entry tends to be ignored ...
93
{
94         //log.info(getLabel()+" "+getFilename());
95
SampleResult res = new SampleResult();
96         boolean isSuccessful = false;
97         res.setSampleLabel(getLabel());
98         res.sampleStart();
99         if (bshInterpreter == null){
100             res.sampleEnd();
101             res.setResponseCode("503");//$NON-NLS-1$
102
res.setResponseMessage("BeanShell Interpreter not found");
103             res.setSuccessful(false);
104             return res;
105         }
106         try
107         {
108             String JavaDoc request=getScript();
109             String JavaDoc fileName=getFilename();
110             if (fileName.length() == 0) {
111                 res.setSamplerData(request);
112             } else {
113                 res.setSamplerData(fileName);
114             }
115
116             bshInterpreter.set("Label",getLabel()); //$NON-NLS-1$
117
bshInterpreter.set("FileName",getFilename()); //$NON-NLS-1$
118
bshInterpreter.set("SampleResult",res); //$NON-NLS-1$
119
bshInterpreter.set("Parameters",getParameters());// as a single line//$NON-NLS-1$
120
bshInterpreter.set("bsh.args",JOrphanUtils.split(getParameters()," "));
121
122             // Set default values
123
bshInterpreter.set("ResponseCode","200"); //$NON-NLS-1$
124
bshInterpreter.set("ResponseMessage","OK");//$NON-NLS-1$
125
bshInterpreter.set("IsSuccess",true);//$NON-NLS-1$
126

127             Object JavaDoc bshOut;
128             
129             if (fileName.length() == 0){
130                 bshOut = bshInterpreter.eval(request);
131             } else {
132                 bshOut = bshInterpreter.source(fileName);
133             }
134             
135             String JavaDoc out;
136             if (bshOut == null) {// Script did not return anything...
137
out="";
138             } else {
139                 out = bshOut.toString();
140             }
141             res.setResponseData(out.getBytes());
142             res.setDataType(SampleResult.TEXT);
143             res.setResponseCode(bshInterpreter.get("ResponseCode").toString());//$NON-NLS-1$
144
res.setResponseMessage(bshInterpreter.get("ResponseMessage").toString());//$NON-NLS-1$
145
isSuccessful = Boolean.valueOf(bshInterpreter.get("IsSuccess") //$NON-NLS-1$
146
.toString()).booleanValue();
147         }
148 /*
149  * To avoid class loading problems when bsh,jar is missing,
150  * we don't try to catch this error separately
151  * catch (bsh.EvalError ex)
152         {
153             log.debug("",ex);
154             res.setResponseCode("500");//$NON-NLS-1$
155             res.setResponseMessage(ex.toString());
156         }
157  */

158         // but we do trap this error to make tests work better
159
catch(NoClassDefFoundError JavaDoc ex){
160             log.error("BeanShell Jar missing? "+ex.toString());
161             res.setResponseCode("501");//$NON-NLS-1$
162
res.setResponseMessage(ex.toString());
163             res.setStopThread(true); // No point continuing
164
}
165         catch (Exception JavaDoc ex) // Mainly for bsh.EvalError
166
{
167             log.warn(ex.toString());
168             res.setResponseCode("500");//$NON-NLS-1$
169
res.setResponseMessage(ex.toString());
170         }
171
172         res.sampleEnd();
173
174         // Set if we were successful or not
175
res.setSuccessful(isSuccessful);
176
177         return res;
178     }
179 }
Popular Tags