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.protocol.http.modifier;
020:
021: import java.io.Serializable;
022: import java.util.LinkedList;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.jmeter.config.Argument;
027: import org.apache.jmeter.config.ConfigTestElement;
028: import org.apache.jmeter.engine.event.LoopIterationEvent;
029: import org.apache.jmeter.processor.PreProcessor;
030: import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
031: import org.apache.jmeter.samplers.Sampler;
032: import org.apache.jmeter.testelement.TestListener;
033: import org.apache.jmeter.testelement.property.PropertyIterator;
034: import org.apache.jorphan.logging.LoggingManager;
035: import org.apache.log.Logger;
036:
037: /**
038: * This modifier will replace any http sampler's url parameter values with
039: * parameter values defined in a XML file for each simulated user.
040: * <P>
041: * For example if userid and password are defined in the XML parameter file for
042: * each user (ie thread), then simulated multiple user activity can occur.
043: *
044: * @author Mark Walsh
045: * @version $Revision: 493789 $
046: */
047: public class UserParameterModifier extends ConfigTestElement implements
048: PreProcessor, Serializable, TestListener {
049: transient private static Logger log = LoggingManager
050: .getLoggerForClass();
051:
052: private static final String XMLURI = "UserParameterModifier.xmluri";
053:
054: private UserSequence allAvailableUsers;
055:
056: /**
057: * Default constructor.
058: */
059: public UserParameterModifier() {
060: } // end constructor
061:
062: /**
063: * Runs before the start of every test. Reload the Sequencer with the latest
064: * parameter data for each user
065: */
066: public void testStarted() {
067: // try to populate allUsers, if fail, leave as any empty set
068: List allUsers = new LinkedList();
069: try {
070: UserParameterXMLParser readXMLParameters = new UserParameterXMLParser();
071: allUsers = readXMLParameters.getXMLParameters(getXmlUri());
072: } catch (Exception e) {
073: // do nothing, now object allUsers contains an empty set
074: log.error("Unable to read parameters from xml file "
075: + getXmlUri());
076: log.error(
077: "No unique values for http requests will be substituted for "
078: + "each thread", e);
079: }
080: allAvailableUsers = new UserSequence(allUsers);
081: }
082:
083: public void testEnded() {
084: }
085:
086: public void testStarted(String host) {
087: testStarted();
088: }
089:
090: public void testEnded(String host) {
091: }
092:
093: /*
094: * ------------------------------------------------------------------------
095: * Methods implemented from interface org.apache.jmeter.config.Modifier
096: * ------------------------------------------------------------------------
097: */
098:
099: /**
100: * Modifies an entry object to replace the value of any url parameter that
101: * matches a parameter name in the XML file.
102: *
103: */
104: public void process() {
105: Sampler entry = getThreadContext().getCurrentSampler();
106: if (!(entry instanceof HTTPSamplerBase)) {
107: return;
108: }
109: HTTPSamplerBase config = (HTTPSamplerBase) entry;
110: Map currentUser = allAvailableUsers.getNextUserMods();
111: PropertyIterator iter = config.getArguments().iterator();
112: while (iter.hasNext()) {
113: Argument arg = (Argument) iter.next().getObjectValue();
114: // if parameter name exists in http request
115: // then change its value
116: // (Note: each jmeter thread (ie user) gets to have unique values)
117: if (currentUser.containsKey(arg.getName())) {
118: arg.setValue((String) currentUser.get(arg.getName()));
119: }
120: }
121: }
122:
123: /*
124: * ------------------------------------------------------------------------
125: * Methods (used by UserParameterModifierGui to get/set the name of XML
126: * parameter file)
127: * ------------------------------------------------------------------------
128: */
129:
130: /**
131: * Return the current XML file name to be read to obtain the parameter data
132: * for all users
133: *
134: * @return the name of the XML file containing parameter data for each user
135: */
136: public String getXmlUri() {
137: return this .getPropertyAsString(XMLURI);
138: }
139:
140: /**
141: * From the GUI screen, set file name of XML to read
142: *
143: * @param xmlURI
144: * the name of the XML file containing the HTTP name value pair
145: * parameters per user
146: */
147: public void setXmlUri(String xmlURI) {
148: setProperty(XMLURI, xmlURI);
149: }
150:
151: /*
152: * (non-Javadoc)
153: *
154: * @see TestListener#testIterationStart(LoopIterationEvent)
155: */
156: public void testIterationStart(LoopIterationEvent event) {
157: }
158:
159: /*
160: * (non-Javadoc)
161: *
162: * @see java.lang.Object#clone()
163: */
164: public Object clone() {
165: UserParameterModifier clone = (UserParameterModifier) super
166: .clone();
167: clone.allAvailableUsers = allAvailableUsers;
168: return clone;
169: }
170: }
|