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 org.apache.jmeter.samplers.Entry;
022: import org.apache.jmeter.samplers.SampleResult;
023: import org.apache.jmeter.samplers.Sampler;
024: import org.apache.jmeter.threads.JMeterContext;
025: import org.apache.jmeter.threads.JMeterContextService;
026: import org.apache.jmeter.threads.JMeterVariables;
027: import org.apache.jmeter.util.BeanShellInterpreter;
028: import org.apache.jmeter.util.BeanShellTestElement;
029: import org.apache.jorphan.logging.LoggingManager;
030: import org.apache.jorphan.util.JOrphanUtils;
031: import org.apache.log.Logger;
032:
033: /**
034: * A sampler which understands BeanShell
035: *
036: */
037: public class BeanShellSampler extends BeanShellTestElement implements
038: Sampler {
039: private static final Logger log = LoggingManager
040: .getLoggerForClass();
041:
042: private static final long serialVersionUID = 3;
043:
044: public static final String FILENAME = "BeanShellSampler.filename"; //$NON-NLS-1$
045:
046: public static final String SCRIPT = "BeanShellSampler.query"; //$NON-NLS-1$
047:
048: public static final String PARAMETERS = "BeanShellSampler.parameters"; //$NON-NLS-1$
049:
050: public static final String INIT_FILE = "beanshell.sampler.init"; //$NON-NLS-1$
051:
052: protected String getInitFileProperty() {
053: return INIT_FILE;
054: }
055:
056: /**
057: * Returns a formatted string label describing this sampler
058: *
059: * @return a formatted string label describing this sampler
060: */
061:
062: public String getLabel() {
063: return getName();
064: }
065:
066: public String getScript() {
067: return this .getPropertyAsString(SCRIPT);
068: }
069:
070: public String getFilename() {
071: return getPropertyAsString(FILENAME);
072: }
073:
074: public String getParameters() {
075: return getPropertyAsString(PARAMETERS);
076: }
077:
078: public SampleResult sample(Entry e)// Entry tends to be ignored ...
079: {
080: // log.info(getLabel()+" "+getFilename());
081: SampleResult res = new SampleResult();
082: boolean isSuccessful = false;
083: res.setSampleLabel(getLabel());
084: res.sampleStart();
085: final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
086: if (bshInterpreter == null) {
087: res.sampleEnd();
088: res.setResponseCode("503");//$NON-NLS-1$
089: res.setResponseMessage("BeanShell Interpreter not found");
090: res.setSuccessful(false);
091: return res;
092: }
093: try {
094: String request = getScript();
095: String fileName = getFilename();
096: if (fileName.length() == 0) {
097: res.setSamplerData(request);
098: } else {
099: res.setSamplerData(fileName);
100: }
101:
102: bshInterpreter.set("Label", getLabel()); //$NON-NLS-1$
103: bshInterpreter.set("FileName", getFilename()); //$NON-NLS-1$
104: bshInterpreter.set("SampleResult", res); //$NON-NLS-1$
105:
106: // Save parameters as single line and as string array
107: bshInterpreter.set("Parameters", getParameters());//$NON-NLS-1$
108: bshInterpreter.set("bsh.args", //$NON-NLS-1$
109: JOrphanUtils.split(getParameters(), " "));//$NON-NLS-1$
110:
111: // Set default values
112: bshInterpreter.set("ResponseCode", "200"); //$NON-NLS-1$
113: bshInterpreter.set("ResponseMessage", "OK");//$NON-NLS-1$
114: bshInterpreter.set("IsSuccess", true);//$NON-NLS-1$
115:
116: // Add variables for access to context and variables
117: JMeterContext jmctx = JMeterContextService.getContext();
118: JMeterVariables vars = jmctx.getVariables();
119: bshInterpreter.set("ctx", jmctx);//$NON-NLS-1$
120: bshInterpreter.set("vars", vars);//$NON-NLS-1$
121:
122: res.setDataType(SampleResult.TEXT); // assume text output - script can override if necessary
123:
124: Object bshOut;
125:
126: if (fileName.length() == 0) {
127: bshOut = bshInterpreter.eval(request);
128: } else {
129: bshOut = bshInterpreter.source(fileName);
130: }
131:
132: if (bshOut != null) {// Set response data
133: String out = bshOut.toString();
134: res.setResponseData(out.getBytes());
135: }
136: // script can also use setResponseData() so long as it returns null
137:
138: res.setResponseCode(bshInterpreter
139: .get("ResponseCode").toString());//$NON-NLS-1$
140: res.setResponseMessage(bshInterpreter
141: .get("ResponseMessage").toString());//$NON-NLS-1$
142: isSuccessful = Boolean.valueOf(
143: bshInterpreter.get("IsSuccess") //$NON-NLS-1$
144: .toString()).booleanValue();
145: }
146: /*
147: * To avoid class loading problems when bsh,jar is missing, we don't try
148: * to catch this error separately catch (bsh.EvalError ex) {
149: * log.debug("",ex); res.setResponseCode("500");//$NON-NLS-1$
150: * res.setResponseMessage(ex.toString()); }
151: */
152: // but we do trap this error to make tests work better
153: catch (NoClassDefFoundError ex) {
154: log.error("BeanShell Jar missing? " + ex.toString());
155: res.setResponseCode("501");//$NON-NLS-1$
156: res.setResponseMessage(ex.toString());
157: res.setStopThread(true); // No point continuing
158: } catch (Exception ex) // Mainly for bsh.EvalError
159: {
160: log.warn(ex.toString());
161: res.setResponseCode("500");//$NON-NLS-1$
162: res.setResponseMessage(ex.toString());
163: }
164:
165: res.sampleEnd();
166:
167: // Set if we were successful or not
168: res.setSuccessful(isSuccessful);
169:
170: return res;
171: }
172: }
|