Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.java.sampler » [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.protocol.java.sampler;
   20   
   21   import org.apache.jmeter.samplers.Entry;
   22   import org.apache.jmeter.samplers.SampleResult;
   23   import org.apache.jmeter.samplers.Sampler;
   24   import org.apache.jmeter.threads.JMeterContext;
   25   import org.apache.jmeter.threads.JMeterContextService;
   26   import org.apache.jmeter.threads.JMeterVariables;
   27   import org.apache.jmeter.util.BeanShellInterpreter;
   28   import org.apache.jmeter.util.BeanShellTestElement;
   29   import org.apache.jorphan.logging.LoggingManager;
   30   import org.apache.jorphan.util.JOrphanUtils;
   31   import org.apache.log.Logger;
   32   
   33   /**
   34    * A sampler which understands BeanShell
   35    *
   36    */
   37   public class BeanShellSampler extends BeanShellTestElement implements Sampler
   38   {
   39       private static final Logger log = LoggingManager.getLoggerForClass();
   40   
   41       private static final long serialVersionUID = 3;
   42   
   43       public static final String FILENAME = "BeanShellSampler.filename"; //$NON-NLS-1$
   44   
   45       public static final String SCRIPT = "BeanShellSampler.query"; //$NON-NLS-1$
   46   
   47       public static final String PARAMETERS = "BeanShellSampler.parameters"; //$NON-NLS-1$
   48   
   49       public static final String INIT_FILE = "beanshell.sampler.init"; //$NON-NLS-1$
   50   
   51       public static final String RESET_INTERPRETER = "BeanShellSampler.resetInterpreter"; //$NON-NLS-1$
   52   
   53       protected String getInitFileProperty() {
   54           return INIT_FILE;
   55       }
   56   
   57       /**
   58        * Returns a formatted string label describing this sampler
   59        *
   60        * @return a formatted string label describing this sampler
   61        */
   62   
   63       public String getLabel() {
   64           return getName();
   65       }
   66   
   67       public String getScript() {
   68           return this.getPropertyAsString(SCRIPT);
   69       }
   70   
   71       public String getFilename() {
   72           return getPropertyAsString(FILENAME);
   73       }
   74   
   75       public String getParameters() {
   76           return getPropertyAsString(PARAMETERS);
   77       }
   78   
   79       public boolean isResetInterpreter() {
   80           return getPropertyAsBoolean(RESET_INTERPRETER);
   81       }
   82   
   83       public SampleResult sample(Entry e)// Entry tends to be ignored ...
   84       {
   85           // log.info(getLabel()+" "+getFilename());
   86           SampleResult res = new SampleResult();
   87           boolean isSuccessful = false;
   88           res.setSampleLabel(getLabel());
   89           res.sampleStart();
   90           final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
   91           if (bshInterpreter == null) {
   92               res.sampleEnd();
   93               res.setResponseCode("503");//$NON-NLS-1$
   94               res.setResponseMessage("BeanShell Interpreter not found");
   95               res.setSuccessful(false);
   96               return res;
   97           }
   98           try {
   99               String request = getScript();
  100               String fileName = getFilename();
  101               if (fileName.length() == 0) {
  102                   res.setSamplerData(request);
  103               } else {
  104                   res.setSamplerData(fileName);
  105               }
  106   
  107               bshInterpreter.set("Label", getLabel()); //$NON-NLS-1$
  108               bshInterpreter.set("FileName", getFilename()); //$NON-NLS-1$
  109               bshInterpreter.set("SampleResult", res); //$NON-NLS-1$
  110   
  111               // Save parameters as single line and as string array
  112               bshInterpreter.set("Parameters", getParameters());//$NON-NLS-1$
  113               bshInterpreter.set("bsh.args", //$NON-NLS-1$
  114                       JOrphanUtils.split(getParameters(), " "));//$NON-NLS-1$
  115   
  116               // Set default values
  117               bshInterpreter.set("ResponseCode", "200"); //$NON-NLS-1$
  118               bshInterpreter.set("ResponseMessage", "OK");//$NON-NLS-1$
  119               bshInterpreter.set("IsSuccess", true);//$NON-NLS-1$
  120   
  121               // Add variables for access to context and variables
  122               JMeterContext jmctx = JMeterContextService.getContext();
  123               JMeterVariables vars = jmctx.getVariables();
  124               bshInterpreter.set("ctx", jmctx);//$NON-NLS-1$
  125               bshInterpreter.set("vars", vars);//$NON-NLS-1$
  126   
  127               res.setDataType(SampleResult.TEXT); // assume text output - script can override if necessary
  128   
  129               Object bshOut;
  130   
  131               if (fileName.length() == 0) {
  132                   bshOut = bshInterpreter.eval(request);
  133               } else {
  134                   bshOut = bshInterpreter.source(fileName);
  135               }
  136   
  137               if (bshOut != null) {// Set response data
  138                   String out = bshOut.toString();
  139                   res.setResponseData(out.getBytes());
  140               }
  141               // script can also use setResponseData() so long as it returns null
  142   
  143               res.setResponseCode(bshInterpreter.get("ResponseCode").toString());//$NON-NLS-1$
  144               res.setResponseMessage(bshInterpreter.get("ResponseMessage").toString());//$NON-NLS-1$
  145               isSuccessful = Boolean.valueOf(bshInterpreter.get("IsSuccess") //$NON-NLS-1$
  146                       .toString()).booleanValue();
  147           }
  148           /*
  149            * To avoid class loading problems when bsh,jar is missing, we don't try
  150            * to catch this error separately catch (bsh.EvalError ex) {
  151            * log.debug("",ex); res.setResponseCode("500");//$NON-NLS-1$
  152            * res.setResponseMessage(ex.toString()); }
  153            */
  154           // but we do trap this error to make tests work better
  155           catch (NoClassDefFoundError ex) {
  156               log.error("BeanShell Jar missing? " + ex.toString());
  157               res.setResponseCode("501");//$NON-NLS-1$
  158               res.setResponseMessage(ex.toString());
  159               res.setStopThread(true); // No point continuing
  160           } catch (Exception ex) // Mainly for bsh.EvalError
  161           {
  162               log.warn(ex.toString());
  163               res.setResponseCode("500");//$NON-NLS-1$
  164               res.setResponseMessage(ex.toString());
  165           }
  166   
  167           res.sampleEnd();
  168   
  169           // Set if we were successful or not
  170           res.setSuccessful(isSuccessful);
  171   
  172           return res;
  173       }
  174   }

Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.java.sampler » [javadoc | source]