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.BeanShellInterpreter;
   32   import org.apache.jmeter.util.JMeterUtils;
   33   import org.apache.jorphan.logging.LoggingManager;
   34   import org.apache.log.Logger;
   35   
   36   /**
   37    * A function which understands BeanShell
   38    *
   39    */
   40   public class BeanShell extends AbstractFunction {
   41   
   42       private static final Logger log = LoggingManager.getLoggerForClass();
   43   
   44       private static final List desc = new LinkedList();
   45   
   46       private static final String KEY = "__BeanShell"; //$NON-NLS-1$
   47   
   48       public static final String INIT_FILE = "beanshell.function.init"; //$NON-NLS-1$
   49   
   50       static {
   51           desc.add(JMeterUtils.getResString("bsh_function_expression"));// $NON-NLS1$
   52           desc.add(JMeterUtils.getResString("function_name_paropt"));// $NON-NLS1$
   53       }
   54   
   55       private Object[] values;
   56   
   57       private BeanShellInterpreter bshInterpreter = null;
   58   
   59       public BeanShell() {
   60       }
   61   
   62       /*
   63        * (non-Javadoc)
   64        *
   65        * @see org.apache.jmeter.functions.Function#execute(SampleResult, Sampler)
   66        */
   67       public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
   68               throws InvalidVariableException {
   69   
   70           if (bshInterpreter == null) // did we find BeanShell?
   71           {
   72               throw new InvalidVariableException("BeanShell not found");
   73           }
   74   
   75           JMeterContext jmctx = JMeterContextService.getContext();
   76           JMeterVariables vars = jmctx.getVariables();
   77   
   78           String script = ((CompoundVariable) values[0]).execute();
   79           String varName = ""; //$NON-NLS-1$
   80           if (values.length > 1) {
   81               varName = ((CompoundVariable) values[1]).execute().trim();
   82           }
   83   
   84           String resultStr = ""; //$NON-NLS-1$
   85   
   86           log.debug("Script=" + script);
   87   
   88           try {
   89   
   90               // Pass in some variables
   91               if (currentSampler != null) {
   92                   bshInterpreter.set("Sampler", currentSampler); //$NON-NLS-1$
   93               }
   94   
   95               if (previousResult != null) {
   96                   bshInterpreter.set("SampleResult", previousResult); //$NON-NLS-1$
   97               }
   98   
   99               // Allow access to context and variables directly
  100               bshInterpreter.set("ctx", jmctx); //$NON-NLS-1$
  101               bshInterpreter.set("vars", vars); //$NON-NLS-1$
  102               bshInterpreter.set("props", JMeterUtils.getJMeterProperties()); //$NON-NLS-1$
  103               bshInterpreter.set("threadName", Thread.currentThread().getName()); //$NON-NLS-1$
  104   
  105               // Execute the script
  106               Object bshOut = bshInterpreter.eval(script);
  107               if (bshOut != null) {
  108                   resultStr = bshOut.toString();
  109               }
  110               if (vars != null && varName.length() > 0) {// vars will be null on TestPlan
  111                   vars.put(varName, resultStr);
  112               }
  113           } catch (Exception ex) // Mainly for bsh.EvalError
  114           {
  115               log.warn("Error running BSH script", ex);
  116           }
  117   
  118           log.debug("Output=" + resultStr);
  119           return resultStr;
  120   
  121       }
  122   
  123       /*
  124        * Helper method for use by scripts
  125        *
  126        */
  127       public void log_info(String s) {
  128           log.info(s);
  129       }
  130   
  131       /*
  132        * (non-Javadoc)
  133        *
  134        * @see org.apache.jmeter.functions.Function#setParameters(Collection)
  135        */
  136       public synchronized void setParameters(Collection parameters) throws InvalidVariableException {
  137   
  138           checkParameterCount(parameters, 1, 2);
  139   
  140           values = parameters.toArray();
  141   
  142           try {
  143               bshInterpreter = new BeanShellInterpreter(JMeterUtils.getProperty(INIT_FILE), log);
  144           } catch (ClassNotFoundException e) {
  145               throw new InvalidVariableException("BeanShell not found");
  146           }
  147       }
  148   
  149       /*
  150        * (non-Javadoc)
  151        *
  152        * @see org.apache.jmeter.functions.Function#getReferenceKey()
  153        */
  154       public String getReferenceKey() {
  155           return KEY;
  156       }
  157   
  158       /*
  159        * (non-Javadoc)
  160        *
  161        * @see org.apache.jmeter.functions.Function#getArgumentDesc()
  162        */
  163       public List getArgumentDesc() {
  164           return desc;
  165       }
  166   
  167   }

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