KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > modifier > UserParameterModifier


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/modifier/UserParameterModifier.java,v 1.13.2.1 2004/07/03 02:07:53 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.http.modifier;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.jmeter.config.Argument;
27 import org.apache.jmeter.config.ConfigTestElement;
28 import org.apache.jmeter.engine.event.LoopIterationEvent;
29 import org.apache.jmeter.processor.PreProcessor;
30 import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
31 import org.apache.jmeter.samplers.Sampler;
32 import org.apache.jmeter.testelement.TestListener;
33 import org.apache.jmeter.testelement.property.PropertyIterator;
34 import org.apache.jorphan.logging.LoggingManager;
35 import org.apache.log.Logger;
36
37 /**
38  * This modifier will replace any http sampler's url parameter values with
39  * parameter values defined in a XML file for each simulated user.
40  * <P>
41  * For example if userid and password are defined in the XML parameter file
42  * for each user (ie thread), then simulated multiple user activity can occur.
43  *
44  * @author Mark Walsh
45  * @version $Revision: 1.13.2.1 $
46  */

47 public class UserParameterModifier
48     extends ConfigTestElement
49     implements PreProcessor, Serializable JavaDoc, TestListener
50 {
51     transient private static Logger log = LoggingManager.getLoggerForClass();
52     private static final String JavaDoc XMLURI = "UserParameterModifier.xmluri";
53
54     private UserSequence allAvailableUsers;
55
56     /**
57      * Default constructor.
58      */

59     public UserParameterModifier()
60     {
61     } //end constructor
62

63     /**
64      * Runs before the start of every test. Reload the Sequencer with the
65      * latest parameter data for each user
66      */

67     public void testStarted()
68     {
69         // try to populate allUsers, if fail, leave as any empty set
70
List JavaDoc allUsers = new LinkedList JavaDoc();
71         try
72         {
73             UserParameterXMLParser readXMLParameters =
74                 new UserParameterXMLParser();
75             allUsers = readXMLParameters.getXMLParameters(getXmlUri());
76         }
77         catch (Exception JavaDoc e)
78         {
79             // do nothing, now object allUsers contains an empty set
80
log.error("Unable to read parameters from xml file " + getXmlUri());
81             log.error(
82                 "No unique values for http requests will be substituted for " +
83                 "each thread",
84                 e);
85         }
86         allAvailableUsers = new UserSequence(allUsers);
87     }
88     public void testEnded()
89     {
90     }
91     public void testStarted(String JavaDoc host)
92     {
93         testStarted();
94     }
95     public void testEnded(String JavaDoc host)
96     {
97     }
98
99     /*
100      * ------------------------------------------------------------------------
101      * Methods implemented from interface org.apache.jmeter.config.Modifier
102      * ------------------------------------------------------------------------
103      */

104      
105     /**
106      * Modifies an entry object to replace the value of any url parameter that
107      * matches a parameter name in the XML file.
108      *
109      */

110     public void process()
111     {
112         Sampler entry = getThreadContext().getCurrentSampler();
113         if (!(entry instanceof HTTPSamplerBase))
114         {
115             return;
116         }
117         HTTPSamplerBase config = (HTTPSamplerBase) entry;
118         Map JavaDoc currentUser = allAvailableUsers.getNextUserMods();
119         PropertyIterator iter = config.getArguments().iterator();
120         while (iter.hasNext())
121         {
122             Argument arg = (Argument) iter.next().getObjectValue();
123             // if parameter name exists in http request
124
// then change its value
125
// (Note: each jmeter thread (ie user) gets to have unique values)
126
if (currentUser.containsKey(arg.getName()))
127             {
128                 arg.setValue((String JavaDoc) currentUser.get(arg.getName()));
129             }
130         }
131     }
132
133     /*
134      * ------------------------------------------------------------------------
135      * Methods (used by UserParameterModifierGui to get/set the name of XML
136      * parameter file)
137      * ------------------------------------------------------------------------
138      */

139      
140     /**
141      * Return the current XML file name to be read to obtain the parameter data
142      * for all users
143      *
144      * @return the name of the XML file containing parameter data for each user
145      */

146     public String JavaDoc getXmlUri()
147     {
148         return this.getPropertyAsString(XMLURI);
149     }
150     
151     /**
152      * From the GUI screen, set file name of XML to read
153      *
154      * @param xmlURI the name of the XML file containing the HTTP name value
155      * pair parameters per user
156      */

157     public void setXmlUri(String JavaDoc xmlURI)
158     {
159         setProperty(XMLURI, xmlURI);
160     }
161     
162     /* (non-Javadoc)
163      * @see TestListener#testIterationStart(LoopIterationEvent)
164      */

165     public void testIterationStart(LoopIterationEvent event)
166     {
167     }
168
169     /* (non-Javadoc)
170      * @see java.lang.Object#clone()
171      */

172     public Object JavaDoc clone()
173     {
174         UserParameterModifier clone = (UserParameterModifier) super.clone();
175         clone.allAvailableUsers = allAvailableUsers;
176         return clone;
177     }
178 }
Popular Tags