KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > functions > JavaScript


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/JavaScript.java,v 1.6.2.2 2004/12/11 01:25:50 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.functions;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.jmeter.engine.util.CompoundVariable;
27 import org.apache.jmeter.samplers.SampleResult;
28 import org.apache.jmeter.samplers.Sampler;
29 import org.apache.jmeter.threads.JMeterVariables;
30 import org.apache.jmeter.util.JMeterUtils;
31 import org.apache.jorphan.logging.LoggingManager;
32 import org.apache.log.Logger;
33 import org.mozilla.javascript.Context;
34 import org.mozilla.javascript.EcmaError;
35 import org.mozilla.javascript.JavaScriptException;
36 import org.mozilla.javascript.Scriptable;
37 import org.mozilla.javascript.WrappedException;
38
39 public class JavaScript extends AbstractFunction implements Serializable JavaDoc
40 {
41
42     private static final List JavaDoc desc = new LinkedList JavaDoc();
43     private static final String JavaDoc KEY = "__javaScript";
44     private static Logger log = LoggingManager.getLoggerForClass();
45
46     static {
47         desc.add("JavaScript expression to evaluate");
48         desc.add(JMeterUtils.getResString("function_name_param"));
49     }
50
51     private Object JavaDoc[] values;
52
53     public JavaScript()
54     {
55     }
56
57     public Object JavaDoc clone()
58     {
59         JavaScript newJavaScript = new JavaScript();
60         return newJavaScript;
61     }
62
63     /* (non-Javadoc)
64      * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
65      */

66     public synchronized String JavaDoc execute(
67         SampleResult previousResult,
68         Sampler currentSampler)
69         throws InvalidVariableException
70     {
71
72         JMeterVariables vars = getVariables();
73
74         String JavaDoc script = ((CompoundVariable) values[0]).execute();
75         String JavaDoc varName =
76             ((CompoundVariable) values[1]).execute();
77         String JavaDoc resultStr = "";
78
79         Context cx = Context.enter();
80         try
81         {
82
83             Scriptable scope = cx.initStandardObjects(null);
84             Object JavaDoc result = cx.evaluateString(scope, script, "<cmd>", 1, null);
85
86             resultStr = Context.toString(result);
87             vars.put(varName, resultStr);
88
89         }
90         catch (WrappedException e)
91         {
92             log.error("Error processing Javascript",e);
93             throw new InvalidVariableException();
94         }
95         catch (EcmaError e)
96         {
97             log.error("Error processing Javascript",e);
98             throw new InvalidVariableException();
99         }
100         catch (JavaScriptException e)
101         {
102             log.error("Error processing Javascript",e);
103             throw new InvalidVariableException();
104         }
105         finally
106         {
107             Context.exit();
108         }
109
110         return resultStr;
111
112     }
113
114     /* (non-Javadoc)
115      * @see org.apache.jmeter.functions.Function#setParameters(Collection)
116      */

117     public void setParameters(Collection JavaDoc parameters)
118         throws InvalidVariableException
119     {
120
121         values = parameters.toArray();
122
123         if (values.length != 2)
124         {
125             throw new InvalidVariableException(
126                     "Expecting 2 parameters, but found " + values.length);//$NON-NLS-1$
127
}
128
129     }
130
131     /* (non-Javadoc)
132      * @see org.apache.jmeter.functions.Function#getReferenceKey()
133      */

134     public String JavaDoc getReferenceKey()
135     {
136         return KEY;
137     }
138
139     /* (non-Javadoc)
140      * @see org.apache.jmeter.functions.Function#getArgumentDesc()
141      */

142     public List JavaDoc getArgumentDesc()
143     {
144         return desc;
145     }
146
147 }
148
Popular Tags