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.util.accesslog;
020:
021: import java.io.File;
022: import java.io.FileOutputStream;
023: import java.io.FileWriter;
024: import java.io.IOException;
025: import java.io.OutputStream;
026: import java.io.Serializable;
027:
028: import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
029: import org.apache.jmeter.protocol.http.sampler.HTTPSamplerFactory;
030: import org.apache.jorphan.util.JOrphanUtils;
031:
032: /**
033: * Description:<br>
034: * <br>
035: * StandardGenerator will be the default generator used to pre-process logs. It
036: * uses JMeter classes to generate the .jmx file. The first version of the
037: * utility only generated the HTTP requests as XML, but it required users to
038: * copy and paste it into a blank jmx file. Doing that way isn't flexible and
039: * would require changes to keep the format in sync.
040: * <p>
041: * This version is a completely new class with a totally different
042: * implementation, since generating the XML is no longer handled by the
043: * generator. The generator is only responsible for handling the parsed results
044: * and passing it to the appropriate JMeter class.
045: * <p>
046: * Notes:<br>
047: * the class needs to first create a thread group and add it to the HashTree.
048: * Then the samplers should be added to the thread group. Listeners shouldn't be
049: * added and should be left up to the user. One option is to provide parameters,
050: * so the user can pass the desired listener to the tool.
051: * <p>
052: *
053: * author Peter Lin<br>
054: * Created on: Jul 1, 2003<br>
055: */
056:
057: public class StandardGenerator implements Generator, Serializable {
058:
059: protected HTTPSamplerBase SAMPLE = null;
060:
061: transient protected FileWriter WRITER = null;
062:
063: transient protected OutputStream OUTPUT = null;
064:
065: protected String FILENAME = null;
066:
067: protected File FILE = null;
068:
069: // NOT USED transient protected ThreadGroup THREADGROUP = null;
070: // Anyway, was this supposed to be the class from java.lang, or
071: // jmeter.threads?
072:
073: /**
074: * The constructor is used by GUI and samplers to generate request objects.
075: */
076: public StandardGenerator() {
077: super ();
078: init();
079: }
080:
081: /**
082: *
083: * @param file
084: */
085: public StandardGenerator(String file) {
086: FILENAME = file;
087: init();
088: }
089:
090: /**
091: * initialize the generator. It should create the following objects.
092: * <p>
093: * <ol>
094: * <li> ListedHashTree</li>
095: * <li> ThreadGroup</li>
096: * <li> File object</li>
097: * <li> Writer</li>
098: * </ol>
099: */
100: protected void init() {
101: generateRequest();
102: }
103:
104: /**
105: * Create the FileWriter to save the JMX file.
106: */
107: protected void initStream() {
108: try {
109: this .OUTPUT = new FileOutputStream(FILE);
110: } catch (IOException exception) {
111: // do nothing
112: }
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#close()
119: */
120: public void close() {
121: JOrphanUtils.closeQuietly(OUTPUT);
122: JOrphanUtils.closeQuietly(WRITER);
123: }
124:
125: /*
126: * (non-Javadoc)
127: *
128: * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setHost(java.lang.String)
129: */
130: public void setHost(String host) {
131: SAMPLE.setDomain(host);
132: }
133:
134: /*
135: * (non-Javadoc)
136: *
137: * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setLabel(java.lang.String)
138: */
139: public void setLabel(String label) {
140:
141: }
142:
143: /*
144: * (non-Javadoc)
145: *
146: * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setMethod(java.lang.String)
147: */
148: public void setMethod(String post_get) {
149: SAMPLE.setMethod(post_get);
150: }
151:
152: /*
153: * (non-Javadoc)
154: *
155: * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setParams(org.apache.jmeter.protocol.http.util.accesslog.NVPair[])
156: */
157: public void setParams(NVPair[] params) {
158: for (int idx = 0; idx < params.length; idx++) {
159: SAMPLE.addArgument(params[idx].getName(), params[idx]
160: .getValue());
161: }
162: }
163:
164: /*
165: * (non-Javadoc)
166: *
167: * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setPath(java.lang.String)
168: */
169: public void setPath(String path) {
170: SAMPLE.setPath(path);
171: }
172:
173: /*
174: * (non-Javadoc)
175: *
176: * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setPort(int)
177: */
178: public void setPort(int port) {
179: SAMPLE.setPort(port);
180: }
181:
182: /*
183: * (non-Javadoc)
184: *
185: * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setQueryString(java.lang.String)
186: */
187: public void setQueryString(String querystring) {
188: SAMPLE.parseArguments(querystring);
189: }
190:
191: /*
192: * (non-Javadoc)
193: *
194: * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setSourceLogs(java.lang.String)
195: */
196: public void setSourceLogs(String sourcefile) {
197: }
198:
199: /*
200: * (non-Javadoc)
201: *
202: * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#setTarget(java.lang.Object)
203: */
204: public void setTarget(Object target) {
205: }
206:
207: /*
208: * (non-Javadoc)
209: *
210: * @see org.apache.jmeter.protocol.http.util.accesslog.Generator#generateRequest()
211: */
212: public Object generateRequest() {
213: try {
214: SAMPLE = HTTPSamplerFactory.newInstance();
215: } catch (NullPointerException e) {
216: e.printStackTrace();
217: }
218: return SAMPLE;
219: }
220:
221: /**
222: * save must be called to write the jmx file, otherwise it will not be
223: * saved.
224: */
225: public void save() {
226: try {
227: // no implementation at this time, since
228: // we bypass the idea of having a console
229: // tool to generate test plans. Instead
230: // I decided to have a sampler that uses
231: // the generator and parser directly
232: } catch (Exception exception) {
233: }
234: }
235:
236: /**
237: * Reset the HTTPSampler to make sure it is a new instance.
238: */
239: public void reset() {
240: SAMPLE = null;
241: generateRequest();
242: }
243:
244: // TODO write some tests
245: }
|