KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > reporters > ResultSaver


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/reporters/ResultSaver.java,v 1.4.2.4 2004/09/30 17:59:13 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.reporters;
20
21 import java.io.File JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.Serializable JavaDoc;
26
27 import org.apache.jmeter.samplers.Clearable;
28 import org.apache.jmeter.samplers.SampleEvent;
29 import org.apache.jmeter.samplers.SampleListener;
30 import org.apache.jmeter.samplers.SampleResult;
31 import org.apache.jmeter.testelement.AbstractTestElement;
32 import org.apache.jorphan.logging.LoggingManager;
33 import org.apache.log.Logger;
34
35 /**
36  * Save Result responseData to a set of files
37  * TODO - perhaps save other items such as headers?
38  *
39  * This is mainly intended for validation tests
40  *
41  * @version $Revision: 1.4.2.4 $ Last updated: $Date: 2004/09/30 17:59:13 $
42  */

43 public class ResultSaver
44     extends AbstractTestElement
45     implements Serializable JavaDoc,
46     SampleListener,
47     Clearable
48 {
49     private static final Logger log = LoggingManager.getLoggerForClass();
50     
51     // File name sequence number
52
private static long sequenceNumber = 0;
53     
54     public static final String JavaDoc FILENAME = "FileSaver.filename"; // $NON-NLS-1$
55
public static final String JavaDoc ERRORS_ONLY = "FileSaver.errorsonly"; // $NON-NLS-1$
56

57     private static synchronized long nextNumber(){
58         return ++sequenceNumber;
59     }
60     /*
61      * Constructor is initially called once for each occurrence in the test plan
62      * For GUI, several more instances are created
63      * Then clear is called at start of test
64      * Called several times during test startup
65      * The name will not necessarily have been set at this point.
66      */

67     public ResultSaver(){
68         super();
69         //log.debug(Thread.currentThread().getName());
70
//System.out.println(">> "+me+" "+this.getName()+" "+Thread.currentThread().getName());
71
}
72
73     /*
74      * Constructor for use during startup
75      * (intended for non-GUI use)
76      * @param name of summariser
77      */

78     public ResultSaver(String JavaDoc name){
79         this();
80         setName(name);
81     }
82     
83     /*
84      * This is called once for each occurrence in the test plan, before the start of the test.
85      * The super.clear() method clears the name (and all other properties),
86      * so it is called last.
87      */

88     public void clear()
89     {
90         //System.out.println("-- "+me+this.getName()+" "+Thread.currentThread().getName());
91
super.clear();
92         sequenceNumber=0; //TODO is this the right thing to do?
93
}
94     
95     /**
96      * Saves the sample result (and any sub results) in files
97      *
98      * @see org.apache.jmeter.samplers.SampleListener#sampleOccurred(org.apache.jmeter.samplers.SampleEvent)
99      */

100     public void sampleOccurred(SampleEvent e) {
101         SampleResult s = e.getResult();
102         saveSample(s);
103         SampleResult []sr = s.getSubResults();
104         for (int i = 0; i < sr.length; i++){
105             saveSample(sr[i]);
106         }
107     }
108
109     /**
110      * @param s SampleResult to save
111      */

112     private void saveSample(SampleResult s) {
113         // Should we save successful samples?
114
if (s.isSuccessful() && getErrorsOnly()) return;
115         
116         nextNumber();
117         String JavaDoc fileName=makeFileName(s.getContentType());
118         log.debug("Saving "+s.getSampleLabel()+" in "+fileName);
119         //System.out.println(fileName);
120
File JavaDoc out = new File JavaDoc(fileName);
121         FileOutputStream JavaDoc pw=null;
122         try {
123             pw = new FileOutputStream JavaDoc(out);
124             pw.write(s.getResponseData());
125         } catch (FileNotFoundException JavaDoc e1) {
126             log.error("Error creating sample file for "+s.getSampleLabel(),e1);
127         } catch (IOException JavaDoc e1) {
128             log.error("Error saving sample "+s.getSampleLabel(),e1);
129         }
130         finally
131         {
132             try {
133                 if (pw != null) pw.close();
134             } catch (IOException JavaDoc e) {}
135         }
136     }
137     /**
138      * @return fileName composed of fixed prefix, a number,
139      * and a suffix derived from the contentType
140      * e.g. Content-Type: text/html;charset=ISO-8859-1
141      */

142     private String JavaDoc makeFileName(String JavaDoc contentType) {
143         String JavaDoc suffix="unknown";
144         if (contentType!=null){
145             int i = contentType.indexOf("/");
146             if (i != -1){
147                 int j = contentType.indexOf(";");
148                 if (j != -1)
149                 {
150                     suffix=contentType.substring(i+1,j);
151                 }
152                 else
153                 {
154                     suffix=contentType.substring(i+1);
155                 }
156             }
157         }
158         return getFilename()+sequenceNumber+"."+suffix;
159     }
160     /* (non-Javadoc)
161      * @see org.apache.jmeter.samplers.SampleListener#sampleStarted(org.apache.jmeter.samplers.SampleEvent)
162      */

163     public void sampleStarted(SampleEvent e)
164     {
165         // not used
166
}
167
168     /* (non-Javadoc)
169      * @see org.apache.jmeter.samplers.SampleListener#sampleStopped(org.apache.jmeter.samplers.SampleEvent)
170      */

171     public void sampleStopped(SampleEvent e)
172     {
173         // not used
174
}
175     private String JavaDoc getFilename()
176     {
177         return getPropertyAsString(FILENAME);
178     }
179     private boolean getErrorsOnly()
180     {
181         return getPropertyAsBoolean(ERRORS_ONLY);
182     }
183 }
184
Popular Tags