Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart4.png 41% of files have more coverage
90   264   25   9
22   148   0,28   10
10     2,5  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  CombinedLogFormatLogger       Line # 50 90 0% 25 84 31,1% 0.3114754
 
  (22)
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
31    */
32    package net.sourceforge.pebble.logging;
33   
34    import net.sourceforge.pebble.domain.Blog;
35    import net.sourceforge.pebble.Constants;
36   
37    import javax.servlet.http.HttpServletRequest;
38    import java.io.*;
39    import java.text.SimpleDateFormat;
40    import java.util.ArrayList;
41    import java.util.Calendar;
42    import java.util.Iterator;
43    import java.util.List;
44   
45    /**
46    * Supports the <a href="http://httpd.apache.org/docs/logs.html#combined">Combined Log Format</a>.
47    *
48    * @author Simon Brown
49    */
 
50    public class CombinedLogFormatLogger extends AbstractLogger {
51   
52    private static final String REFERER_HEADER = "Referer";
53    private static final String USER_AGENT_HEADER = "User-Agent";
54    private static final int FLUSH_SIZE = 0;
55   
56    /** the format of the log filenames */
57    private SimpleDateFormat filenameFormat = new SimpleDateFormat("'blog-'yyyyMMdd'.log'");
58   
59    private List entries = new ArrayList();
60   
 
61  1336 toggle public CombinedLogFormatLogger(Blog blog) {
62  1336 super(blog);
63  1336 filenameFormat.setTimeZone(blog.getTimeZone());
64    }
65   
66    /**
67    * Logs a HTTP request.
68    *
69    * @param request a HttpServletRequest
70    */
 
71  0 toggle public synchronized void log(HttpServletRequest request, int status) {
72  0 String externalUri = (String)request.getAttribute(Constants.EXTERNAL_URI);
73  0 LogEntry entry = new LogEntry();
74  0 entry.setHost(request.getRemoteAddr());
75  0 entry.setDate(blog.getCalendar().getTime());
76  0 entry.setStatusCode(status);
77  0 StringBuffer buf = new StringBuffer();
78  0 buf.append(request.getMethod());
79  0 buf.append(" ");
80  0 buf.append(externalUri);
81  0 entry.setRequest(buf.toString());
82  0 entry.setReferer(request.getHeader(REFERER_HEADER));
83  0 entry.setAgent(request.getHeader(USER_AGENT_HEADER));
84  0 entries.add(entry);
85   
86  0 if (entries.size() >= FLUSH_SIZE) {
87  0 flush();
88    }
89    }
90   
 
91  1322 toggle private void flush() {
92  1322 try {
93  1322 write(entries);
94  1322 entries.clear();
95    } catch (IOException ioe) {
96  0 ioe.printStackTrace();
97    }
98    }
99   
100    /**
101    * Called to start this logger.
102    */
 
103  1320 toggle public void start() {
104    }
105   
106    /**
107    * Called to stop this logger.
108    */
 
109  1322 toggle public synchronized void stop() {
110  1322 flush();
111    }
112   
113    /**
114    * Gets a copy of the log file for a given year, month and day.
115    *
116    * @param year the year to get entries for
117    * @param month the month to get entries for
118    * @param day the day to get entries for
119    * @return a String containing the contents of the requested log file
120    */
 
121  0 toggle public String getLogFile(int year, int month, int day) {
122  0 StringBuffer buf = new StringBuffer();
123  0 try {
124    // read the file a line at a time, creating a String as we go
125  0 File file = new File(blog.getLogsDirectory(), getFilename(year, month, day));
126  0 if (file.exists()) {
127  0 BufferedReader reader = new BufferedReader(new FileReader(file));
128  0 String line = reader.readLine();
129  0 while (line != null) {
130  0 buf.append(line);
131  0 buf.append(System.getProperty("line.separator"));
132  0 line = reader.readLine();
133    }
134  0 reader.close();
135    }
136    } catch (Exception e) {
137  0 e.printStackTrace();
138    } finally {
139  0 return buf.toString();
140    }
141    }
142   
143    /**
144    * Gets the log for a given year, month and day.
145    *
146    * @param year the year to get entries for
147    * @param month the month to get entries for
148    * @param day the day to get entries for
149    * @return a Log object
150    */
 
151  0 toggle public Log getLog(int year, int month, int day) {
152  0 List logEntries = new ArrayList();
153  0 CombinedFormatLogEntryFormat format = new CombinedFormatLogEntryFormat(blog);
154   
155  0 try {
156    // read the file a line at a time, parsing into LogEntry objects
157  0 File file = new File(blog.getLogsDirectory(), getFilename(year, month, day));
158  0 if (file.exists()) {
159  0 BufferedReader reader = new BufferedReader(new FileReader(file));
160  0 String line = reader.readLine();
161  0 while (line != null) {
162  0 logEntries.add(format.parse(line));
163  0 line = reader.readLine();
164    }
165  0 reader.close();
166    }
167    } catch (Exception e) {
168  0 e.printStackTrace();
169    } finally {
170  0 return new Log(blog, logEntries);
171    }
172    }
173   
174    /**
175    * Gets the log summary information for the given year, month and day.
176    *
177    * @param year the year to get entries for
178    * @param month the month to get entries for
179    * @param day the day to get entries for
180    * @return a LogSummary object
181    */
 
182  730 toggle public LogSummary getLogSummary(int year, int month, int day) {
183  730 Calendar cal = blog.getCalendar();
184  730 cal.set(Calendar.YEAR, year);
185  730 cal.set(Calendar.MONTH, month-1);
186  730 cal.set(Calendar.DAY_OF_MONTH, day);
187  730 int totalRequests = 0;
188   
189  730 try {
190    // read the file a line at a time
191  730 File file = new File(blog.getLogsDirectory(), getFilename(year, month, day));
192  730 if (file.exists()) {
193  0 BufferedReader reader = new BufferedReader(new FileReader(file));
194  0 String line = reader.readLine();
195  0 while (line != null) {
196  0 totalRequests++;
197  0 line = reader.readLine();
198    }
199  0 reader.close();
200    }
201    } catch (Exception e) {
202  0 e.printStackTrace();
203    }
204   
205  730 return new LogSummaryItem(blog, cal.getTime(), totalRequests);
206    }
207   
208    /**
209    * Determines the name of the log file.
210    *
211    * @param year the year to get entries for
212    * @param month the month to get entries for
213    * @param day the day to get entries for
214    * @return the name of the log file for the given year, month and day
215    */
 
216  730 toggle private String getFilename(int year, int month, int day) {
217  730 Calendar cal = blog.getCalendar();
218  730 cal.set(Calendar.YEAR, year);
219  730 cal.set(Calendar.MONTH, month-1);
220  730 cal.set(Calendar.DAY_OF_MONTH, day);
221   
222  730 return filenameFormat.format(cal.getTime());
223    }
224   
225    /**
226    * Writes the given list of entries to the log file, creating it if
227    * necessary.
228    *
229    * @param entries the list of entries to write
230    */
 
231  1322 toggle private void write(List entries) throws IOException {
232  1322 CombinedFormatLogEntryFormat format = new CombinedFormatLogEntryFormat(blog);
233  1322 File file;
234  1322 BufferedWriter writer = null;
235  1322 String currentFilename = "";
236  1322 String filename;
237  1322 Iterator it = entries.iterator();
238  1322 while (it.hasNext()) {
239  0 LogEntry entry = (LogEntry)it.next();
240  0 filename = filenameFormat.format(entry.getDate());
241  0 if (!filename.equals(currentFilename)) {
242    // close the old file (if there is one)
243  0 if (writer != null) {
244  0 writer.flush();
245  0 writer.close();
246    }
247   
248    // and open a new file
249  0 currentFilename = filename;
250  0 file = new File(blog.getLogsDirectory(), currentFilename);
251  0 writer = new BufferedWriter(new FileWriter(file, true));
252    }
253   
254  0 writer.write(format.format(entry));
255  0 writer.newLine();
256    }
257   
258  1322 if (writer != null) {
259  0 writer.flush();
260  0 writer.close();
261    }
262    }
263   
264    }