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, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations
015: * under the License.
016: *
017: */
018:
019: package org.apache.jmeter.util;
020:
021: import java.io.Serializable;
022:
023: import org.apache.jmeter.engine.event.LoopIterationEvent;
024: import org.apache.jmeter.testelement.AbstractTestElement;
025: import org.apache.jmeter.testelement.TestListener;
026: import org.apache.jmeter.testelement.ThreadListener;
027: import org.apache.jmeter.util.BeanShellInterpreter;
028: import org.apache.jmeter.util.JMeterUtils;
029: import org.apache.jorphan.logging.LoggingManager;
030: import org.apache.jorphan.util.JMeterException;
031: import org.apache.jorphan.util.JOrphanUtils;
032: import org.apache.log.Logger;
033:
034: public abstract class BeanShellTestElement extends AbstractTestElement
035: implements Serializable, Cloneable, ThreadListener,
036: TestListener {
037: private static final Logger log = LoggingManager
038: .getLoggerForClass();
039:
040: private static final long serialVersionUID = 4;
041:
042: //++ For TestBean implementations only
043: private String parameters; // passed to file or script
044:
045: private String filename; // file to source (overrides script)
046:
047: private String script; // script (if file not provided)
048: //-- For TestBean implementations only
049:
050: transient private BeanShellInterpreter bshInterpreter = null;
051:
052: transient private boolean hasInitFile = false;
053:
054: public BeanShellTestElement() {
055: super ();
056: init();
057: }
058:
059: protected abstract String getInitFileProperty();
060:
061: protected BeanShellInterpreter getBeanShellInterpreter() {
062: return bshInterpreter;
063: }
064:
065: private void init() {
066: parameters = ""; // ensure variables are not null
067: filename = "";
068: script = "";
069: try {
070: String initFileName = JMeterUtils
071: .getProperty(getInitFileProperty());
072: hasInitFile = initFileName != null;
073: bshInterpreter = new BeanShellInterpreter(initFileName, log);
074: } catch (ClassNotFoundException e) {
075: log.error("Cannot find BeanShell: " + e.toString());
076: }
077: }
078:
079: private Object readResolve() {
080: init();
081: return this ;
082: }
083:
084: public Object clone() {
085: BeanShellTestElement o = (BeanShellTestElement) super .clone();
086: o.init();
087: return o;
088: }
089:
090: protected Object processFileOrScript(BeanShellInterpreter bsh)
091: throws JMeterException {
092: String fileName = getFilename();
093:
094: bsh.set("FileName", getFilename());//$NON-NLS-1$
095: // Set params as a single line
096: bsh.set("Parameters", getParameters()); // $NON-NLS-1$
097: // and set as an array
098: bsh.set("bsh.args",//$NON-NLS-1$
099: JOrphanUtils.split(getParameters(), " "));//$NON-NLS-1$
100:
101: if (fileName.length() == 0) {
102: return bsh.eval(getScript());
103: } else {
104: return bsh.source(fileName);
105: }
106: }
107:
108: /**
109: * Return the script (TestBean version).
110: * Must be overridden for subclasses that don't implement TestBean
111: * otherwise the clone() method won't work.
112: *
113: * @return the script to execute
114: */
115: public String getScript() {
116: return script;
117: }
118:
119: /**
120: * Set the script (TestBean version).
121: * Must be overridden for subclasses that don't implement TestBean
122: * otherwise the clone() method won't work.
123: *
124: * @param s the script to execute (may be blank)
125: */
126: public void setScript(String s) {
127: script = s;
128: }
129:
130: public void threadStarted() {
131: if (bshInterpreter == null || !hasInitFile)
132: return;
133: try {
134: bshInterpreter.evalNoLog("threadStarted()"); // $NON-NLS-1$
135: } catch (JMeterException ignored) {
136: log.debug(getClass().getName() + " : "
137: + ignored.getLocalizedMessage()); // $NON-NLS-1$
138: }
139: }
140:
141: public void threadFinished() {
142: if (bshInterpreter == null || !hasInitFile)
143: return;
144: try {
145: bshInterpreter.evalNoLog("threadFinished()"); // $NON-NLS-1$
146: } catch (JMeterException ignored) {
147: log.debug(getClass().getName() + " : "
148: + ignored.getLocalizedMessage()); // $NON-NLS-1$
149: }
150: }
151:
152: public void testEnded() {
153: if (bshInterpreter == null || !hasInitFile)
154: return;
155: try {
156: bshInterpreter.evalNoLog("testEnded()"); // $NON-NLS-1$
157: } catch (JMeterException ignored) {
158: log.debug(getClass().getName() + " : "
159: + ignored.getLocalizedMessage()); // $NON-NLS-1$
160: }
161: }
162:
163: public void testEnded(String host) {
164: if (bshInterpreter == null || !hasInitFile)
165: return;
166: try {
167: bshInterpreter.eval((new StringBuffer("testEnded(")) // $NON-NLS-1$
168: .append(host).append(")") // $NON-NLS-1$
169: .toString()); // $NON-NLS-1$
170: } catch (JMeterException ignored) {
171: log.debug(getClass().getName() + " : "
172: + ignored.getLocalizedMessage()); // $NON-NLS-1$
173: }
174: }
175:
176: public void testIterationStart(LoopIterationEvent event) {
177: // Not implemented
178: }
179:
180: public void testStarted() {
181: if (bshInterpreter == null || !hasInitFile)
182: return;
183: try {
184: bshInterpreter.evalNoLog("testStarted()"); // $NON-NLS-1$
185: } catch (JMeterException ignored) {
186: log.debug(getClass().getName() + " : "
187: + ignored.getLocalizedMessage()); // $NON-NLS-1$
188: }
189: }
190:
191: public void testStarted(String host) {
192: if (bshInterpreter == null || !hasInitFile)
193: return;
194: try {
195: bshInterpreter.eval((new StringBuffer("testStarted(")) // $NON-NLS-1$
196: .append(host).append(")") // $NON-NLS-1$
197: .toString()); // $NON-NLS-1$
198: } catch (JMeterException ignored) {
199: log.debug(getClass().getName() + " : "
200: + ignored.getLocalizedMessage()); // $NON-NLS-1$
201: }
202: }
203:
204: public String getParameters() {
205: return parameters;
206: }
207:
208: public void setParameters(String s) {
209: parameters = s;
210: }
211:
212: public String getFilename() {
213: return filename;
214: }
215:
216: public void setFilename(String s) {
217: filename = s;
218: }
219: }
|