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.timers;
20:
21: import org.apache.jmeter.testbeans.TestBean;
22: import org.apache.jmeter.threads.JMeterContext;
23: import org.apache.jmeter.threads.JMeterContextService;
24: import org.apache.jmeter.threads.JMeterVariables;
25: import org.apache.jmeter.util.BeanShellInterpreter;
26: import org.apache.jmeter.util.BeanShellTestElement;
27: import org.apache.jorphan.logging.LoggingManager;
28: import org.apache.jorphan.util.JMeterException;
29: import org.apache.log.Logger;
30:
31: public class BeanShellTimer extends BeanShellTestElement implements
32: Cloneable, Timer, TestBean {
33: private static final Logger log = LoggingManager
34: .getLoggerForClass();
35:
36: private static final long serialVersionUID = 4;
37:
38: // can be specified in jmeter.properties
39: private static final String INIT_FILE = "beanshell.timer.init"; //$NON-NLS-1$
40:
41: protected String getInitFileProperty() {
42: return INIT_FILE;
43: }
44:
45: /*
46: * (non-Javadoc)
47: *
48: * @see org.apache.jmeter.timers.Timer#delay()
49: */
50: public long delay() {
51: String ret = "0";
52: final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
53: if (bshInterpreter == null) {
54: log.error("BeanShell not found");
55: return 0;
56: }
57: JMeterContext jmctx = JMeterContextService.getContext();
58: JMeterVariables vars = jmctx.getVariables();
59: try {
60: // Add variables for access to context and variables
61: bshInterpreter.set("ctx", jmctx);//$NON-NLS-1$
62: bshInterpreter.set("vars", vars);//$NON-NLS-1$
63: Object o = processFileOrScript(bshInterpreter);
64: if (o != null)
65: ret = o.toString();
66: } catch (JMeterException e) {
67: log.warn("Problem in BeanShell script " + e);
68: }
69: try {
70: return Long.decode(ret).longValue();
71: } catch (NumberFormatException e) {
72: log.warn(e.getLocalizedMessage());
73: return 0;
74: }
75: }
76: }
|