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.util.JMeterUtils;
   29   import org.apache.jorphan.logging.LoggingManager;
   30   import org.apache.log.Logger;
   31   
   32   /**
   33    * <p>
   34    * Function to log a message.
   35    * </p>
   36    *
   37    * <p>
   38    * Parameters:
   39    * <ul>
   40    * <li>string value</li>
   41    * <li>log level (optional; defaults to INFO; or DEBUG if unrecognised; or can use OUT or ERR)</li>
   42    * <li>throwable message (optional)</li>
   43    * </ul>
   44    * </p>
   45    * Returns: - Empty String (so can be used where return value would be a nuisance)
   46    *
   47    */
   48   public class LogFunction2 extends AbstractFunction {
   49       private static final Logger log = LoggingManager.getLoggerForClass();
   50   
   51       private static final List desc = new LinkedList();
   52   
   53       private static final String KEY = "__logn"; //$NON-NLS-1$
   54   
   55       // Number of parameters expected - used to reject invalid calls
   56       private static final int MIN_PARAMETER_COUNT = 1;
   57   
   58       private static final int MAX_PARAMETER_COUNT = 3;
   59       static {
   60           desc.add(JMeterUtils.getResString("log_function_string"));    //$NON-NLS-1$
   61           desc.add(JMeterUtils.getResString("log_function_level"));     //$NON-NLS-1$
   62           desc.add(JMeterUtils.getResString("log_function_throwable")); //$NON-NLS-1$
   63       }
   64   
   65       private static final String DEFAULT_PRIORITY = "INFO"; //$NON-NLS-1$
   66   
   67       private Object[] values;
   68   
   69       public LogFunction2() {
   70       }
   71   
   72       public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
   73               throws InvalidVariableException {
   74           String stringToLog = ((CompoundVariable) values[0]).execute();
   75   
   76           String priorityString;
   77           if (values.length > 1) { // We have a default
   78               priorityString = ((CompoundVariable) values[1]).execute();
   79               if (priorityString.length() == 0) {
   80                   priorityString = DEFAULT_PRIORITY;
   81               }
   82           } else {
   83               priorityString = DEFAULT_PRIORITY;
   84           }
   85   
   86           Throwable t = null;
   87           if (values.length > 2) { // Throwable wanted
   88               t = new Throwable(((CompoundVariable) values[2]).execute());
   89           }
   90   
   91           LogFunction.logDetails(log, stringToLog, priorityString, t, "");
   92   
   93           return "";
   94   
   95       }
   96   
   97       public synchronized void setParameters(Collection parameters) throws InvalidVariableException {
   98           checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
   99           values = parameters.toArray();
  100       }
  101   
  102       public String getReferenceKey() {
  103           return KEY;
  104       }
  105   
  106       public List getArgumentDesc() {
  107           return desc;
  108       }
  109   
  110   }

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