KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/java/org/apache/jmeter/protocol/java/sampler/BSFSampler.java,v 1.5.2.1 2005/03/05 01:19:12 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 org.apache.bsf.BSFEngine;
22 import org.apache.bsf.BSFManager;
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.jorphan.logging.LoggingManager;
27 import org.apache.log.Logger;
28
29 /**
30  * A sampler which understands BSF
31  *
32  * @version $Revision: 1.5.2.1 $ Updated on: $Date: 2005/03/05 01:19:12 $
33  */

34 public class BSFSampler extends AbstractSampler
35 {
36
37     private static final Logger log = LoggingManager.getLoggerForClass();
38
39     public static final String JavaDoc FILENAME = "BSFSampler.filename"; //$NON-NLS-1$
40
public static final String JavaDoc SCRIPT = "BSFSampler.query"; //$NON-NLS-1$
41
public static final String JavaDoc LANGUAGE = "BSFSampler.language"; //$NON-NLS-1$
42
public static final String JavaDoc PARAMETERS = "BSFSampler.parameters"; //$NON-NLS-1$
43

44     private transient BSFManager mgr;
45     private BSFEngine bsfEngine;
46     
47     public BSFSampler()
48     {
49         try {
50             // register beanshell with the BSF framework
51
BSFManager.registerScriptingEngine("beanshell",
52              "bsh.util.BeanShellBSFEngine", new String JavaDoc [] { "bsh"} );
53         } catch (NoClassDefFoundError JavaDoc e){
54         }
55          
56        //TODO: register other scripting languages ...
57

58     }
59     
60     public String JavaDoc getFilename()
61     {
62         return getPropertyAsString(FILENAME);
63     }
64     public void setFilename(String JavaDoc newFilename)
65     {
66         this.setProperty(FILENAME, newFilename);
67     }
68     public String JavaDoc getScript()
69     {
70         return this.getPropertyAsString(SCRIPT);
71     }
72     public void setScript(String JavaDoc newScript)
73     {
74         this.setProperty(SCRIPT, newScript);
75     }
76     public String JavaDoc getParameters()
77     {
78         return this.getPropertyAsString(PARAMETERS);
79     }
80     public void setParameters(String JavaDoc newScript)
81     {
82         this.setProperty(PARAMETERS, newScript);
83     }
84     public String JavaDoc getScriptLanguage()
85     {
86         return this.getPropertyAsString(LANGUAGE);
87     }
88     public void setScriptLanguage(String JavaDoc lang)
89     {
90         this.setProperty(LANGUAGE,lang);
91     }
92
93     /**
94      * Returns a formatted string label describing this sampler
95      *
96      * @return a formatted string label describing this sampler
97      */

98
99     public String JavaDoc getLabel()
100     {
101         return getName();
102     }
103
104
105     public SampleResult sample(Entry e)// Entry tends to be ignored ...
106
{
107         log.info(getLabel()+" "+getFilename());
108         SampleResult res = new SampleResult();
109         boolean isSuccessful = false;
110         res.setSampleLabel(getLabel());
111         res.sampleStart();
112         try
113         {
114             String JavaDoc request=getScript();
115             res.setSamplerData(request);
116
117             mgr.registerBean("Label",getLabel());
118             mgr.registerBean("Name",getFilename());
119
120             bsfEngine = mgr.loadScriptingEngine(getScriptLanguage());
121
122             Object JavaDoc bsfOut = bsfEngine.eval("Sampler",0,0,request);
123
124             res.setResponseData(bsfOut.toString().getBytes());
125             res.setDataType(SampleResult.TEXT);
126             res.setResponseCode("200");//TODO set from script
127
res.setResponseMessage("OK");//TODO set from script
128
isSuccessful = true;//TODO set from script
129
}
130         catch (NoClassDefFoundError JavaDoc ex)
131         {
132             log.warn("",ex);
133             res.setResponseCode("500");
134             res.setResponseMessage(ex.toString());
135         }
136         catch (Exception JavaDoc ex)
137         {
138             log.warn("",ex);
139             res.setResponseCode("500");
140             res.setResponseMessage(ex.toString());
141         }
142
143         res.sampleEnd();
144
145         // Set if we were successful or not
146
res.setSuccessful(isSuccessful);
147
148         return res;
149     }
150 }
151
Popular Tags