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   package org.apache.jmeter.functions;
   18   
   19   import java.util.Collection;
   20   import java.util.LinkedList;
   21   import java.util.List;
   22   import java.util.Map;
   23   
   24   import org.apache.commons.jexl.JexlContext;
   25   import org.apache.commons.jexl.JexlHelper;
   26   import org.apache.commons.jexl.Script;
   27   import org.apache.commons.jexl.ScriptFactory;
   28   import org.apache.jmeter.engine.util.CompoundVariable;
   29   import org.apache.jmeter.samplers.SampleResult;
   30   import org.apache.jmeter.samplers.Sampler;
   31   import org.apache.jmeter.threads.JMeterContext;
   32   import org.apache.jmeter.threads.JMeterContextService;
   33   import org.apache.jmeter.threads.JMeterVariables;
   34   import org.apache.jmeter.util.JMeterUtils;
   35   import org.apache.jorphan.logging.LoggingManager;
   36   import org.apache.log.Logger;
   37   
   38   /**
   39    * A function which understands Commons JEXL
   40    */
   41   // For unit tests, see TestJexlFunction
   42   public class JexlFunction extends AbstractFunction {
   43   
   44       private static final Logger log = LoggingManager.getLoggerForClass();
   45   
   46       private static final String KEY = "__jexl"; //$NON-NLS-1$
   47   
   48       private static final List desc = new LinkedList();
   49   
   50       static
   51       {
   52           desc.add(JMeterUtils.getResString("jexl_expression")); //$NON-NLS-1$
   53           desc.add(JMeterUtils.getResString("function_name_paropt"));// $NON-NLS1$
   54       }
   55   
   56       private Object[] values;
   57   
   58       public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
   59               throws InvalidVariableException
   60       {
   61           String str = ""; //$NON-NLS-1$
   62   
   63           CompoundVariable var = (CompoundVariable) values[0];
   64           String exp = var.execute();
   65   
   66           String varName = ""; //$NON-NLS-1$
   67           if (values.length > 1) {
   68               varName = ((CompoundVariable) values[1]).execute().trim();
   69           }
   70   
   71           JMeterContext jmctx = JMeterContextService.getContext();
   72           JMeterVariables vars = jmctx.getVariables();
   73   
   74           try
   75           {
   76               Script script = ScriptFactory.createScript(exp);
   77               JexlContext jc = JexlHelper.createContext();
   78               final Map jexlVars = jc.getVars();
   79               jexlVars.put("log", log); //$NON-NLS-1$
   80               jexlVars.put("ctx", jmctx); //$NON-NLS-1$
   81               jexlVars.put("vars", vars); //$NON-NLS-1$
   82               jexlVars.put("props", JMeterUtils.getJMeterProperties()); //$NON-NLS-1$
   83               // Previously mis-spelt as theadName
   84               jexlVars.put("threadName", Thread.currentThread().getName()); //$NON-NLS-1$
   85               jexlVars.put("sampler", currentSampler); //$NON-NLS-1$ (may be null)
   86               jexlVars.put("sampleResult", previousResult); //$NON-NLS-1$ (may be null)
   87               jexlVars.put("OUT", System.out);//$NON-NLS-1$
   88   
   89               // Now evaluate the script, getting the result
   90               Object o = script.execute(jc);
   91               if (o != null)
   92               {
   93                   str = o.toString();
   94               }
   95               if (vars != null && varName.length() > 0) {// vars will be null on TestPlan
   96                   vars.put(varName, str);
   97               }
   98           } catch (Exception e)
   99           {
  100               log.error("An error occurred while evaluating the expression \""
  101                       + exp + "\"\n",e);
  102           }
  103           return str;
  104       }
  105   
  106       public List getArgumentDesc()
  107       {
  108           return desc;
  109       }
  110   
  111       public String getReferenceKey()
  112       {
  113           return KEY;
  114       }
  115   
  116       public synchronized void setParameters(Collection parameters)
  117               throws InvalidVariableException
  118       {
  119           checkParameterCount(parameters, 1, 2);
  120           values = parameters.toArray();
  121       }
  122   
  123   }

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