001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.engine;
020:
021: import java.util.Map;
022:
023: import org.apache.jmeter.config.Arguments;
024: import org.apache.jmeter.engine.util.ValueReplacer;
025: import org.apache.jmeter.functions.InvalidVariableException;
026: import org.apache.jmeter.reporters.ResultCollector;
027: import org.apache.jmeter.testelement.TestElement;
028: import org.apache.jmeter.testelement.TestPlan;
029: import org.apache.jmeter.threads.JMeterContextService;
030: import org.apache.jmeter.threads.JMeterVariables;
031: import org.apache.jorphan.collections.HashTree;
032: import org.apache.jorphan.collections.HashTreeTraverser;
033: import org.apache.jorphan.logging.LoggingManager;
034: import org.apache.log.Logger;
035:
036: /**
037: * Class to replace function and variable references in the test tree.
038: *
039: */
040: public class PreCompiler implements HashTreeTraverser {
041: private static final Logger log = LoggingManager
042: .getLoggerForClass();
043:
044: private final ValueReplacer replacer;
045:
046: // Used by both StandardJMeterEngine and ClientJMeterEngine.
047: // In the latter case, only ResultCollectors are updated,
048: // as only these are relevant to the client, and updating
049: // other elements causes all sorts of problems.
050: private final boolean isRemote; // skip certain processing for remote tests
051:
052: public PreCompiler() {
053: replacer = new ValueReplacer();
054: isRemote = false;
055: }
056:
057: public PreCompiler(boolean remote) {
058: replacer = new ValueReplacer();
059: isRemote = remote;
060: }
061:
062: /*
063: * (non-Javadoc)
064: *
065: * @see HashTreeTraverser#addNode(Object, HashTree)
066: */
067: public void addNode(Object node, HashTree subTree) {
068: if (isRemote && node instanceof ResultCollector) {
069: try {
070: replacer.replaceValues((TestElement) node);
071: } catch (InvalidVariableException e) {
072: log.error("invalid variables", e);
073: }
074: }
075: if (isRemote) {
076: return;
077: }
078: if (node instanceof TestElement) {
079: try {
080: replacer.replaceValues((TestElement) node);
081: } catch (InvalidVariableException e) {
082: log.error("invalid variables", e);
083: }
084: }
085: if (node instanceof TestPlan) {
086: ((TestPlan) node).prepareForPreCompile(); //A hack to make user-defined variables in the testplan element more dynamic
087: Map args = ((TestPlan) node).getUserDefinedVariables();
088: replacer.setUserDefinedVariables(args);
089: JMeterVariables vars = new JMeterVariables();
090: vars.putAll(args);
091: JMeterContextService.getContext().setVariables(vars);
092: }
093:
094: if (node instanceof Arguments) {
095: ((Arguments) node).setRunningVersion(true);
096: Map args = ((Arguments) node).getArgumentsAsMap();
097: replacer.addVariables(args);
098: JMeterContextService.getContext().getVariables().putAll(
099: args);
100: }
101: }
102:
103: /*
104: * (non-Javadoc)
105: *
106: * @see HashTreeTraverser#subtractNode()
107: */
108: public void subtractNode() {
109: }
110:
111: /*
112: * (non-Javadoc)
113: *
114: * @see HashTreeTraverser#processPath()
115: */
116: public void processPath() {
117: }
118: }
|