001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.java.sampler;
020:
021: import java.io.FileInputStream;
022:
023: import org.apache.bsf.BSFEngine;
024: import org.apache.bsf.BSFManager;
025: import org.apache.commons.io.IOUtils;
026: import org.apache.jmeter.samplers.AbstractSampler;
027: import org.apache.jmeter.samplers.Entry;
028: import org.apache.jmeter.samplers.SampleResult;
029: import org.apache.jmeter.threads.JMeterContext;
030: import org.apache.jmeter.threads.JMeterContextService;
031: import org.apache.jmeter.threads.JMeterVariables;
032: import org.apache.jorphan.logging.LoggingManager;
033: import org.apache.jorphan.util.JOrphanUtils;
034: import org.apache.log.Logger;
035:
036: /**
037: * A sampler which understands BSF
038: *
039: */
040: public class BSFSampler extends AbstractSampler {
041:
042: private static final Logger log = LoggingManager
043: .getLoggerForClass();
044:
045: //+ JMX file attributes - do not change
046: private static final String FILENAME = "BSFSampler.filename"; //$NON-NLS-1$
047:
048: private static final String SCRIPT = "BSFSampler.query"; //$NON-NLS-1$
049:
050: private static final String LANGUAGE = "BSFSampler.language"; //$NON-NLS-1$
051:
052: private static final String PARAMETERS = "BSFSampler.parameters"; //$NON-NLS-1$
053: //- JMX file attributes
054:
055: private transient BSFManager mgr;
056:
057: public BSFSampler() {
058: mgr = new BSFManager();
059: }
060:
061: public String getFilename() {
062: return getPropertyAsString(FILENAME);
063: }
064:
065: public void setFilename(String newFilename) {
066: this .setProperty(FILENAME, newFilename);
067: }
068:
069: public String getScript() {
070: return this .getPropertyAsString(SCRIPT);
071: }
072:
073: public void setScript(String newScript) {
074: this .setProperty(SCRIPT, newScript);
075: }
076:
077: public String getParameters() {
078: return this .getPropertyAsString(PARAMETERS);
079: }
080:
081: public void setParameters(String newScript) {
082: this .setProperty(PARAMETERS, newScript);
083: }
084:
085: public String getScriptLanguage() {
086: return this .getPropertyAsString(LANGUAGE);
087: }
088:
089: public void setScriptLanguage(String lang) {
090: this .setProperty(LANGUAGE, lang);
091: }
092:
093: /**
094: * Returns a formatted string label describing this sampler
095: *
096: * @return a formatted string label describing this sampler
097: */
098:
099: public String getLabel() {
100: return getName();
101: }
102:
103: public SampleResult sample(Entry e)// Entry tends to be ignored ...
104: {
105: final String label = getLabel();
106: log.info(label + " " + getFilename());
107: SampleResult res = new SampleResult();
108: res.setSampleLabel(label);
109: FileInputStream is = null;
110:
111: res.sampleStart();
112: try {
113: final String request = getScript();
114: final String fileName = getFilename();
115:
116: mgr.declareBean("log", log, log.getClass()); // $NON-NLS-1$
117: mgr.declareBean("Label", label, String.class); // $NON-NLS-1$
118: mgr.declareBean("FileName", fileName, String.class); // $NON-NLS-1$
119: mgr
120: .declareBean("Parameters", getParameters(),
121: String.class); // $NON-NLS-1$
122: String[] args = JOrphanUtils.split(getParameters(), " ");//$NON-NLS-1$
123: mgr.declareBean("args", args, args.getClass());//$NON-NLS-1$
124: mgr.declareBean("SampleResult", res, res.getClass()); // $NON-NLS-1$
125:
126: // TODO: find out how to retrieve these from the script
127: // At present the script has to use SampleResult methods to set them.
128: res.setResponseCode("200"); // $NON-NLS-1$
129: res.setResponseMessage("OK"); // $NON-NLS-1$
130: res.setSuccessful(true);
131:
132: // These are not useful yet, as have not found how to get updated values back
133: //mgr.declareBean("ResponseCode", "200", String.class); // $NON-NLS-1$
134: //mgr.declareBean("ResponseMessage", "OK", String.class); // $NON-NLS-1$
135: //mgr.declareBean("IsSuccess", Boolean.TRUE, Boolean.class); // $NON-NLS-1$
136:
137: res.setDataType(SampleResult.TEXT); // Default (can be overridden by the script)
138:
139: // Add variables for access to context and variables
140: JMeterContext jmctx = JMeterContextService.getContext();
141: JMeterVariables vars = jmctx.getVariables();
142: mgr.declareBean("ctx", jmctx, jmctx.getClass()); // $NON-NLS-1$
143: mgr.declareBean("vars", vars, vars.getClass()); // $NON-NLS-1$
144:
145: BSFEngine bsfEngine = mgr
146: .loadScriptingEngine(getScriptLanguage());
147:
148: Object bsfOut = null;
149: if (fileName.length() > 0) {
150: res.setSamplerData("File: " + fileName);
151: is = new FileInputStream(fileName);
152: bsfOut = bsfEngine.eval(fileName, 0, 0, IOUtils
153: .toString(is));
154: } else {
155: res.setSamplerData("[script]");
156: bsfOut = bsfEngine.eval("script", 0, 0, request);
157: }
158:
159: if (bsfOut != null) {
160: res.setResponseData(bsfOut.toString().getBytes());
161: }
162: } catch (NoClassDefFoundError ex) {
163: log.warn("", ex);
164: res.setSuccessful(false);
165: res.setResponseCode("500"); // $NON-NLS-1$
166: res.setResponseMessage(ex.toString());
167: } catch (Exception ex) {
168: log.warn("", ex);
169: res.setSuccessful(false);
170: res.setResponseCode("500"); // $NON-NLS-1$
171: res.setResponseMessage(ex.toString());
172: } finally {
173: res.sampleEnd();
174: IOUtils.closeQuietly(is);
175: }
176:
177: return res;
178: }
179: }
|