1 18 19 package org.apache.jmeter.reporters; 20 21 import java.io.File ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.Serializable ; 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 43 public class ResultSaver 44 extends AbstractTestElement 45 implements Serializable , 46 SampleListener, 47 Clearable 48 { 49 private static final Logger log = LoggingManager.getLoggerForClass(); 50 51 private static long sequenceNumber = 0; 53 54 public static final String FILENAME = "FileSaver.filename"; public static final String ERRORS_ONLY = "FileSaver.errorsonly"; 57 private static synchronized long nextNumber(){ 58 return ++sequenceNumber; 59 } 60 67 public ResultSaver(){ 68 super(); 69 } 72 73 78 public ResultSaver(String name){ 79 this(); 80 setName(name); 81 } 82 83 88 public void clear() 89 { 90 super.clear(); 92 sequenceNumber=0; } 94 95 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 112 private void saveSample(SampleResult s) { 113 if (s.isSuccessful() && getErrorsOnly()) return; 115 116 nextNumber(); 117 String fileName=makeFileName(s.getContentType()); 118 log.debug("Saving "+s.getSampleLabel()+" in "+fileName); 119 File out = new File (fileName); 121 FileOutputStream pw=null; 122 try { 123 pw = new FileOutputStream (out); 124 pw.write(s.getResponseData()); 125 } catch (FileNotFoundException e1) { 126 log.error("Error creating sample file for "+s.getSampleLabel(),e1); 127 } catch (IOException 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 e) {} 135 } 136 } 137 142 private String makeFileName(String contentType) { 143 String 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 163 public void sampleStarted(SampleEvent e) 164 { 165 } 167 168 171 public void sampleStopped(SampleEvent e) 172 { 173 } 175 private String getFilename() 176 { 177 return getPropertyAsString(FILENAME); 178 } 179 private boolean getErrorsOnly() 180 { 181 return getPropertyAsBoolean(ERRORS_ONLY); 182 } 183 } 184 | Popular Tags |