Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.functions » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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.util.Collection;
   22   import java.util.LinkedList;
   23   import java.util.List;
   24   
   25   import org.apache.jmeter.engine.util.CompoundVariable;
   26   import org.apache.jmeter.samplers.SampleResult;
   27   import org.apache.jmeter.samplers.Sampler;
   28   import org.apache.jmeter.threads.JMeterContext;
   29   import org.apache.jmeter.threads.JMeterContextService;
   30   import org.apache.jmeter.threads.JMeterVariables;
   31   import org.apache.jmeter.util.JMeterUtils;
   32   import org.apache.jorphan.logging.LoggingManager;
   33   import org.apache.log.Logger;
   34   import org.mozilla.javascript.Context;
   35   import org.mozilla.javascript.EcmaError;
   36   import org.mozilla.javascript.JavaScriptException;
   37   import org.mozilla.javascript.Scriptable;
   38   import org.mozilla.javascript.WrappedException;
   39   
   40   public class JavaScript extends AbstractFunction {
   41   
   42       private static final List desc = new LinkedList();
   43   
   44       private static final String KEY = "__javaScript"; //$NON-NLS-1$
   45   
   46       private static final Logger log = LoggingManager.getLoggerForClass();
   47   
   48       static {
   49           desc.add(JMeterUtils.getResString("javascript_expression"));//$NON-NLS-1$
   50           desc.add(JMeterUtils.getResString("function_name_paropt")); //$NON-NLS-1$
   51       }
   52   
   53       private Object[] values;
   54   
   55       public JavaScript() {
   56       }
   57   
   58       /*
   59        * (non-Javadoc)
   60        *
   61        * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
   62        */
   63       public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
   64               throws InvalidVariableException {
   65   
   66           JMeterContext jmctx = JMeterContextService.getContext();
   67           JMeterVariables vars = jmctx.getVariables();
   68   
   69           String script = ((CompoundVariable) values[0]).execute();
   70           // Allow variable to be omitted
   71           String varName = values.length < 2 ? null : ((CompoundVariable) values[1]).execute().trim();
   72           String resultStr = "";
   73   
   74           Context cx = Context.enter();
   75           try {
   76   
   77               Scriptable scope = cx.initStandardObjects(null);
   78   
   79               // Set up some objects for the script to play with
   80               scope.put("log", scope, log); //$NON-NLS-1$
   81               scope.put("ctx", scope, jmctx); //$NON-NLS-1$
   82               scope.put("vars", scope, vars); //$NON-NLS-1$
   83               scope.put("props", scope, JMeterUtils.getJMeterProperties()); //$NON-NLS-1$
   84               // Previously mis-spelt as theadName
   85               scope.put("threadName", scope, Thread.currentThread().getName()); //$NON-NLS-1$
   86               scope.put("sampler", scope, currentSampler); //$NON-NLS-1$
   87               scope.put("sampleResult", scope, previousResult); //$NON-NLS-1$
   88   
   89               Object result = cx.evaluateString(scope, script, "<cmd>", 1, null); //$NON-NLS-1$
   90   
   91               resultStr = Context.toString(result);
   92               if (varName != null && vars != null) {// vars can be null if run from TestPlan
   93                   vars.put(varName, resultStr);
   94               }
   95   
   96           } catch (WrappedException e) {
   97               log.error("Error processing Javascript", e);
   98               throw new InvalidVariableException();
   99           } catch (EcmaError e) {
  100               log.error("Error processing Javascript", e);
  101               throw new InvalidVariableException();
  102           } catch (JavaScriptException e) {
  103               log.error("Error processing Javascript", e);
  104               throw new InvalidVariableException();
  105           } finally {
  106               Context.exit();
  107           }
  108   
  109           return resultStr;
  110   
  111       }
  112   
  113       /*
  114        * (non-Javadoc)
  115        *
  116        * @see org.apache.jmeter.functions.Function#setParameters(Collection)
  117        */
  118       public synchronized void setParameters(Collection parameters) throws InvalidVariableException {
  119           checkParameterCount(parameters, 1, 2);
  120           values = parameters.toArray();
  121       }
  122   
  123       /*
  124        * (non-Javadoc)
  125        *
  126        * @see org.apache.jmeter.functions.Function#getReferenceKey()
  127        */
  128       public String getReferenceKey() {
  129           return KEY;
  130       }
  131   
  132       /*
  133        * (non-Javadoc)
  134        *
  135        * @see org.apache.jmeter.functions.Function#getArgumentDesc()
  136        */
  137       public List getArgumentDesc() {
  138           return desc;
  139       }
  140   
  141   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.functions » [javadoc | source]