01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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, WITHOUT
13: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14: * License for the specific language governing permissions and limitations
15: * under the License.
16: *
17: */
18:
19: package org.apache.jmeter.modifiers;
20:
21: import org.apache.jmeter.processor.PreProcessor;
22: import org.apache.jmeter.samplers.SampleResult;
23: import org.apache.jmeter.samplers.Sampler;
24: import org.apache.jmeter.testbeans.TestBean;
25: import org.apache.jmeter.threads.JMeterContext;
26: import org.apache.jmeter.threads.JMeterContextService;
27: import org.apache.jmeter.threads.JMeterVariables;
28: import org.apache.jmeter.util.BeanShellInterpreter;
29: import org.apache.jmeter.util.BeanShellTestElement;
30: import org.apache.jorphan.logging.LoggingManager;
31: import org.apache.jorphan.util.JMeterException;
32: import org.apache.log.Logger;
33:
34: public class BeanShellPreProcessor extends BeanShellTestElement
35: implements Cloneable, PreProcessor, TestBean {
36: private static final Logger log = LoggingManager
37: .getLoggerForClass();
38:
39: private static final long serialVersionUID = 4;
40:
41: // can be specified in jmeter.properties
42: private static final String INIT_FILE = "beanshell.preprocessor.init"; //$NON-NLS-1$
43:
44: protected String getInitFileProperty() {
45: return INIT_FILE;
46: }
47:
48: public void process() {
49: final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
50: if (bshInterpreter == null) {
51: log.error("BeanShell not found");
52: return;
53: }
54: JMeterContext jmctx = JMeterContextService.getContext();
55: JMeterVariables vars = jmctx.getVariables();
56: Sampler sam = jmctx.getCurrentSampler();
57: SampleResult prev = jmctx.getPreviousResult();
58: try {
59: // Add variables for access to context and variables
60: bshInterpreter.set("ctx", jmctx);//$NON-NLS-1$
61: bshInterpreter.set("vars", vars);//$NON-NLS-1$
62: bshInterpreter.set("sampler", sam);//$NON-NLS-1$
63: bshInterpreter.set("prev", prev);//$NON-NLS-1$
64:
65: processFileOrScript(bshInterpreter);
66: } catch (JMeterException e) {
67: log.warn("Problem in BeanShell script " + e);
68: }
69: }
70: }
|