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.functions;
020:
021: import java.io.Serializable;
022: import java.util.Collection;
023: import java.util.LinkedList;
024: import java.util.List;
025:
026: import org.apache.jmeter.engine.util.CompoundVariable;
027: import org.apache.jmeter.samplers.SampleResult;
028: import org.apache.jmeter.samplers.Sampler;
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.jmeter.util.JMeterUtils;
033: import org.apache.jorphan.logging.LoggingManager;
034: import org.apache.log.Logger;
035: import org.mozilla.javascript.Context;
036: import org.mozilla.javascript.EcmaError;
037: import org.mozilla.javascript.JavaScriptException;
038: import org.mozilla.javascript.Scriptable;
039: import org.mozilla.javascript.WrappedException;
040:
041: public class JavaScript extends AbstractFunction implements
042: Serializable {
043:
044: private static final List desc = new LinkedList();
045:
046: private static final String KEY = "__javaScript"; //$NON-NLS-1$
047:
048: private static Logger log = LoggingManager.getLoggerForClass();
049:
050: static {
051: desc.add(JMeterUtils.getResString("javascript_expression"));//$NON-NLS-1$
052: desc.add(JMeterUtils.getResString("function_name_param")); //$NON-NLS-1$
053: }
054:
055: private Object[] values;
056:
057: public JavaScript() {
058: }
059:
060: public Object clone() throws CloneNotSupportedException {
061: return super .clone();
062: }
063:
064: /*
065: * (non-Javadoc)
066: *
067: * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
068: */
069: public synchronized String execute(SampleResult previousResult,
070: Sampler currentSampler) throws InvalidVariableException {
071:
072: JMeterContext jmctx = JMeterContextService.getContext();
073: JMeterVariables vars = jmctx.getVariables();
074:
075: String script = ((CompoundVariable) values[0]).execute();
076: // Allow variable to be omitted
077: String varName = values.length < 2 ? null
078: : ((CompoundVariable) values[1]).execute();
079: String resultStr = "";
080:
081: Context cx = Context.enter();
082: try {
083:
084: Scriptable scope = cx.initStandardObjects(null);
085:
086: // Set up some objects for the script to play with
087: scope.put("ctx", scope, jmctx); //$NON-NLS-1$
088: scope.put("vars", scope, vars); //$NON-NLS-1$
089: scope
090: .put(
091: "theadName", scope, Thread.currentThread().getName()); //$NON-NLS-1$
092: scope.put("sampler", scope, currentSampler); //$NON-NLS-1$
093: scope.put("sampleResult", scope, previousResult); //$NON-NLS-1$
094:
095: Object result = cx.evaluateString(scope, script,
096: "<cmd>", 1, null); //$NON-NLS-1$
097:
098: resultStr = Context.toString(result);
099: if (varName != null && vars != null) {// vars can be null if run from TestPlan
100: vars.put(varName, resultStr);
101: }
102:
103: } catch (WrappedException e) {
104: log.error("Error processing Javascript", e);
105: throw new InvalidVariableException();
106: } catch (EcmaError e) {
107: log.error("Error processing Javascript", e);
108: throw new InvalidVariableException();
109: } catch (JavaScriptException e) {
110: log.error("Error processing Javascript", e);
111: throw new InvalidVariableException();
112: } finally {
113: Context.exit();
114: }
115:
116: return resultStr;
117:
118: }
119:
120: /*
121: * (non-Javadoc)
122: *
123: * @see org.apache.jmeter.functions.Function#setParameters(Collection)
124: */
125: public synchronized void setParameters(Collection parameters)
126: throws InvalidVariableException {
127:
128: values = parameters.toArray();
129:
130: if (values.length < 1 || values.length > 2) {
131: throw new InvalidVariableException(
132: "Expecting 1 or 2 parameters, but found " + values.length);//$NON-NLS-1$
133: }
134:
135: }
136:
137: /*
138: * (non-Javadoc)
139: *
140: * @see org.apache.jmeter.functions.Function#getReferenceKey()
141: */
142: public String getReferenceKey() {
143: return KEY;
144: }
145:
146: /*
147: * (non-Javadoc)
148: *
149: * @see org.apache.jmeter.functions.Function#getArgumentDesc()
150: */
151: public List getArgumentDesc() {
152: return desc;
153: }
154:
155: }
|