protected void initManager(ScriptEngineManager sem) {
    final String label = getName();
    final String fileName = getFilename();
    final String scriptParameters = getParameters();
    // Use actual class name for log
    final Logger logger = LoggingManager.getLoggerForShortName(getClass().getName());

    sem.put("log", logger);
    sem.put("Label", label);
    sem.put("FileName", fileName);
    sem.put("Parameters", scriptParameters);
    String[] args = JOrphanUtils.split(scriptParameters, " "); // $NON-NLS-1$
    sem.put("args", args);
    // Add variables for access to context and variables
    JMeterContext jmctx = JMeterContextService.getContext();
    sem.put("ctx", jmctx);
    JMeterVariables vars = jmctx.getVariables();
    sem.put("vars", vars);
    Properties props = JMeterUtils.getJMeterProperties();
    sem.put("props", props);
    // For use in debugging:
    sem.put("OUT", System.out);

    // Most subclasses will need these:
    Sampler sampler = jmctx.getCurrentSampler();
    sem.put("sampler", sampler);
    SampleResult prev = jmctx.getPreviousResult();
    sem.put("prev", prev);
  }
  protected void initManager(BSFManager mgr) throws BSFException {
    final String label = getName();
    final String fileName = getFilename();
    final String scriptParameters = getParameters();
    // Use actual class name for log
    final Logger logger = LoggingManager.getLoggerForShortName(getClass().getName());
    mgr.declareBean("log", logger, Logger.class); // $NON-NLS-1$
    mgr.declareBean("Label", label, String.class); // $NON-NLS-1$
    mgr.declareBean("FileName", fileName, String.class); // $NON-NLS-1$
    mgr.declareBean("Parameters", scriptParameters, String.class); // $NON-NLS-1$
    String[] args = JOrphanUtils.split(scriptParameters, " "); // $NON-NLS-1$
    mgr.declareBean("args", args, args.getClass()); // $NON-NLS-1$
    // Add variables for access to context and variables
    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();
    Properties props = JMeterUtils.getJMeterProperties();

    mgr.declareBean("ctx", jmctx, jmctx.getClass()); // $NON-NLS-1$
    mgr.declareBean("vars", vars, vars.getClass()); // $NON-NLS-1$
    mgr.declareBean("props", props, props.getClass()); // $NON-NLS-1$
    // For use in debugging:
    mgr.declareBean("OUT", System.out, PrintStream.class); // $NON-NLS-1$

    // Most subclasses will need these:
    Sampler sampler = jmctx.getCurrentSampler();
    mgr.declareBean("sampler", sampler, Sampler.class);
    SampleResult prev = jmctx.getPreviousResult();
    mgr.declareBean("prev", prev, SampleResult.class);
  }
示例#3
0
  /**
   * Parses the response data using regular expressions and saving the results into variables for
   * use later in the test.
   *
   * @see org.apache.jmeter.processor.PostProcessor#process()
   */
  @Override
  public void process() {
    initTemplate();
    JMeterContext context = getThreadContext();
    SampleResult previousResult = context.getPreviousResult();
    if (previousResult == null) {
      return;
    }
    log.debug("RegexExtractor processing result");

    // Fetch some variables
    JMeterVariables vars = context.getVariables();
    String refName = getRefName();
    int matchNumber = getMatchNumber();

    final String defaultValue = getDefaultValue();
    if (defaultValue.length() > 0) { // Only replace default if it is provided
      vars.put(refName, defaultValue);
    }
    Perl5Matcher matcher = JMeterUtils.getMatcher();
    String regex = getRegex();
    Pattern pattern = null;
    try {
      pattern = JMeterUtils.getPatternCache().getPattern(regex, Perl5Compiler.READ_ONLY_MASK);
      List<MatchResult> matches = processMatches(pattern, regex, previousResult, matchNumber, vars);
      int prevCount = 0;
      String prevString = vars.get(refName + REF_MATCH_NR);
      if (prevString != null) {
        vars.remove(refName + REF_MATCH_NR); // ensure old value is not left defined
        try {
          prevCount = Integer.parseInt(prevString);
        } catch (NumberFormatException e1) {
          log.warn("Could not parse " + prevString + " " + e1);
        }
      }
      int matchCount = 0; // Number of refName_n variable sets to keep
      try {
        MatchResult match;
        if (matchNumber >= 0) { // Original match behaviour
          match = getCorrectMatch(matches, matchNumber);
          if (match != null) {
            vars.put(refName, generateResult(match));
            saveGroups(vars, refName, match);
          } else {
            // refname has already been set to the default (if present)
            removeGroups(vars, refName);
          }
        } else // < 0 means we save all the matches
        {
          removeGroups(vars, refName); // remove any single matches
          matchCount = matches.size();
          vars.put(refName + REF_MATCH_NR, Integer.toString(matchCount)); // Save the count
          for (int i = 1; i <= matchCount; i++) {
            match = getCorrectMatch(matches, i);
            if (match != null) {
              final String refName_n =
                  new StringBuilder(refName).append(UNDERSCORE).append(i).toString();
              vars.put(refName_n, generateResult(match));
              saveGroups(vars, refName_n, match);
            }
          }
        }
        // Remove any left-over variables
        for (int i = matchCount + 1; i <= prevCount; i++) {
          final String refName_n =
              new StringBuilder(refName).append(UNDERSCORE).append(i).toString();
          vars.remove(refName_n);
          removeGroups(vars, refName_n);
        }
      } catch (RuntimeException e) {
        log.warn("Error while generating result");
      }
    } catch (MalformedCachePatternException e) {
      log.error("Error in pattern: " + regex);
    } finally {
      JMeterUtils.clearMatcherMemory(matcher, pattern);
    }
  }
 public String execute() throws InvalidVariableException {
   JMeterContext context = JMeterContextService.getContext();
   SampleResult previousResult = context.getPreviousResult();
   Sampler currentSampler = context.getCurrentSampler();
   return execute(previousResult, currentSampler);
 }