KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > util > accesslog > StandardGenerator


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/util/accesslog/StandardGenerator.java,v 1.7.2.1 2004/10/24 00:25:59 sebb Exp $
2
/*
3  * Copyright 2003-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.util.accesslog;
20
21 import java.io.File JavaDoc;
22 import java.io.FileWriter JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
28
29 /**
30  * Description:<br>
31  * <br>
32  * StandardGenerator will be the default generator used
33  * to pre-process logs. It uses JMeter classes to
34  * generate the .jmx file. The first version of the
35  * utility only generated the HTTP requests as XML, but
36  * it required users to copy and paste it into a blank
37  * jmx file. Doing that way isn't flexible and would
38  * require changes to keep the format in sync.<p>
39  * This version is a completely new class with a totally
40  * different implementation, since generating the XML
41  * is no longer handled by the generator. The generator
42  * is only responsible for handling the parsed results
43  * and passing it to the appropriate JMeter class.<p>
44  * Notes:<br>
45  * the class needs to first create a thread group and
46  * add it to the HashTree. Then the samplers should
47  * be added to the thread group. Listeners shouldn't
48  * be added and should be left up to the user. One
49  * option is to provide parameters, so the user can
50  * pass the desired listener to the tool.
51  * <p>
52  * @author Peter Lin<br>
53  * @version $Revision: 1.7.2.1 $ last updated $Date: 2004/10/24 00:25:59 $
54  * Created on: Jul 1, 2003<br>
55  */

56
57 public class StandardGenerator implements Generator, Serializable JavaDoc {
58
59     protected HTTPSampler SAMPLE = null;
60     transient protected FileWriter JavaDoc WRITER = null;
61     transient protected OutputStream JavaDoc OUTPUT = null;
62     protected String JavaDoc FILENAME = null;
63     protected File JavaDoc FILE = null;
64     //NOT USED transient protected ThreadGroup THREADGROUP = null;
65
//Anyway, was this supposed to be the class from java.lang, or jmeter.threads?
66

67     /**
68      * The constructor is used by GUI and samplers
69      * to generate request objects.
70      */

71     public StandardGenerator() {
72         super();
73         init();
74     }
75
76     /**
77      *
78      * @param file
79      */

80     public StandardGenerator(String JavaDoc file){
81         FILENAME = file;
82         init();
83     }
84
85     /**
86      * initialize the generator. It should create
87      * the following objects.<p>
88      * <ol>
89      * <li> ListedHashTree</li>
90      * <li> ThreadGroup</li>
91      * <li> File object</li>
92      * <li> Writer</li>
93      * </ol>
94      */

95     protected void init(){
96         generateRequest();
97     }
98
99     /**
100      * Create the FileWriter to save the JMX file.
101      */

102     protected void initStream(){
103         try {
104             this.OUTPUT = new FileOutputStream JavaDoc(FILE);
105         } catch (IOException JavaDoc exception){
106             // do nothing
107
}
108     }
109     
110     /* (non-Javadoc)
111      * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#close()
112      */

113     public void close() {
114         try {
115             if (OUTPUT != null){
116                 OUTPUT.close();
117             }
118             if (WRITER != null){
119                 WRITER.close();
120             }
121         } catch (IOException JavaDoc exception){
122         }
123     }
124
125     /* (non-Javadoc)
126      * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setHost(java.lang.String)
127      */

128     public void setHost(String JavaDoc host) {
129         SAMPLE.setDomain(host);
130     }
131
132     /* (non-Javadoc)
133      * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setLabel(java.lang.String)
134      */

135     public void setLabel(String JavaDoc label) {
136
137     }
138
139     /* (non-Javadoc)
140      * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setMethod(java.lang.String)
141      */

142     public void setMethod(String JavaDoc post_get) {
143         SAMPLE.setMethod(post_get);
144     }
145
146     /* (non-Javadoc)
147      * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setParams(org.apache.jmeter.protocol.http.util.accesslog.NVPair[])
148      */

149     public void setParams(NVPair[] params) {
150         for (int idx=0; idx < params.length; idx++){
151             SAMPLE.addArgument(params[idx].getName(),params[idx].getValue());
152         }
153     }
154
155     /* (non-Javadoc)
156      * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setPath(java.lang.String)
157      */

158     public void setPath(String JavaDoc path) {
159         SAMPLE.setPath(path);
160     }
161
162     /* (non-Javadoc)
163      * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setPort(int)
164      */

165     public void setPort(int port) {
166         SAMPLE.setPort(port);
167     }
168
169     /* (non-Javadoc)
170      * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setQueryString(java.lang.String)
171      */

172     public void setQueryString(String JavaDoc querystring) {
173         SAMPLE.parseArguments(querystring);
174     }
175
176     /* (non-Javadoc)
177      * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setSourceLogs(java.lang.String)
178      */

179     public void setSourceLogs(String JavaDoc sourcefile) {
180     }
181
182     /* (non-Javadoc)
183      * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setTarget(java.lang.Object)
184      */

185     public void setTarget(Object JavaDoc target) {
186     }
187
188     /* (non-Javadoc)
189      * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#generateRequest()
190      */

191     public Object JavaDoc generateRequest() {
192         try {
193             SAMPLE = new HTTPSampler();
194         } catch (NullPointerException JavaDoc e){
195             e.printStackTrace();
196         }
197         return SAMPLE;
198     }
199     
200     /**
201      * save must be called to write the jmx file,
202      * otherwise it will not be saved.
203      */

204     public void save(){
205         try {
206             // no implementation at this time, since
207
// we bypass the idea of having a console
208
// tool to generate test plans. Instead
209
// I decided to have a sampler that uses
210
// the generator and parser directly
211
} catch (Exception JavaDoc exception){
212         }
213     }
214
215     /**
216      * Reset the HTTPSampler to make sure it is a new instance.
217      */

218     public void reset(){
219         SAMPLE = null;
220         generateRequest();
221     }
222     
223     //TODO write some tests
224
}
225
Popular Tags