Project Report: fawkez

Packagesummary org.jcoderz.commons.logging

org.jcoderz.commons.logging.LogReader

LineHitsNoteSource
1  /*
2   * $Id: LogReader.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.logging;
34  
35  import java.io.BufferedReader;
36  import java.io.File;
37  import java.io.FileReader;
38  import java.io.IOException;
39  import java.util.ArrayList;
40  import java.util.Iterator;
41  import java.util.List;
42  
43  /**
44   * This implements a reader reading from log files. It can be used for reading
45   * the next entry from the log file, which matches the filter criteria or
46   * skipping a number of log file entries.
47   *
48   */
49 (1)public class LogReader
50  {
510    private final List mBufferedLines = new ArrayList();
52     private final BufferedReader mReader;
53     private final File mFile;
540    private final List mFilters = new ArrayList();
55  
56     /**
57      * Creates a new LogReader for reading from the supplied file.
58      *
59      * @param fileName The name of the file to read.
60      *
61      * @throws InstantiationException in case there is an error opening the file
62      * for reading.
63      */
64     LogReader (final String fileName)
65           throws InstantiationException
660    {
67        try
68        {
690(2)         mFile = new File(fileName);
700          mReader = new BufferedReader(new FileReader(mFile));
71        }
720       catch (Exception ex)
73        {
740          final InstantiationException iex = new InstantiationException(
75                 "Cannot install LogReader for file '" + fileName + "'.");
760          iex.initCause(ex);
770          throw iex;
780       }
79  
800    }
81  
82     /**
83      * Installs a new filter for filtering log file entries.
84      *
85      * @param filter The filter to add to the already stored filters.
86      */
87     void addFilter (final Filter filter)
88     {
890       mFilters.add(filter);
900    }
91  
92     /**
93      * Gets the next LogFileEntry from the log file, which passes all installed
94      * filters. If end of file is reached before an entry has been found matching
95      * all criteria, this returns null.
96      * Each LogFileEntry instance being returned by this should be released if it
97      * is not needed anymore.
98      *
99      * @return the next LogFileEnbtry passing all filters or null if no such
100      * available.
101      * @throws LoggingException if an error occurs.
102      */
103 (3)   LogFileEntry readLogFileEntry ()
104           throws LoggingException
105     {
1060       LogFileEntry rc = null;
1070       final LogFileEntry currentEntry = LogFileEntry.getLogFileEntry();
1080       int numBufferedLines = mBufferedLines.size();
1090       int bufferedLine = 0;
110        boolean readBuffered;
1110       boolean consumedLine = false;
112  
1130       while (((bufferedLine < numBufferedLines) || available()) && (rc == null))
114        {
115           final StringBuffer currentLine;
1160          if (bufferedLine < numBufferedLines)
117           {
1180             currentLine = (StringBuffer) mBufferedLines.get(bufferedLine++);
1190             readBuffered = true;
120           }
121           else
122           {
1230             currentLine = readLine();
1240             readBuffered = false;
125           }
1260          if (currentLine != null)
127           {
128              try
129              {
1300                if (currentEntry.addLogLine(currentLine))
131                 {
1320                   if (! readBuffered)
133                    {
1340                      mBufferedLines.add(currentLine);
135                    }
1360                   consumedLine = true;
137                 }
138                 else
139                 {
1400                   mBufferedLines.clear();
1410                   mBufferedLines.add(currentLine);
1420                   numBufferedLines = 1;
1430                   bufferedLine = 0;
1440                   consumedLine = false;
1450                   if (passesFilters(currentEntry))
146                    {
1470                      rc = currentEntry;
148                    }
149                    else
150                    {
1510                      currentEntry.reset();
152                    }
153                 }
154              }
1550             catch (Exception ex)
156              {
157                 /* in case of exception the entry currently in process is
158                    returned and the current log line is discarded */
1590(4)               System.err.println("Got an exception when processing line: "
160                       + currentLine);
1610(5)               System.err.println(ex);
1620(6)               ex.printStackTrace();
1630                mBufferedLines.clear();
1640                numBufferedLines = 0;
1650                bufferedLine = 0;
1660                if (consumedLine && passesFilters(currentEntry))
167                 {
1680                   rc = currentEntry;
169                 }
1700             }
171           }
1720       }
1730       if ((rc == null) && consumedLine && passesFilters(currentEntry))
174        {
1750          rc = currentEntry;
1760          mBufferedLines.clear();
177        }
1780       if (rc != currentEntry)
179        {
1800          currentEntry.release();
181        }
1820       return rc;
183     }
184  
185     /**
186      * Checks whether more data is to read from the log file.
187      *
188      * @return true if the log file contains data not already read by this;
189      * false, else.
190      *
191 (7)    * TODO: Add some more criteria, e.g. modification time to detect log file
192      * switches etc.
193      */
194     boolean available ()
195     {
1960       boolean rc = false;
197  
198        try
199        {
2000          rc = mReader.ready();
201        }
2020       catch (IOException iex)
203        {
2040(8)         System.err.println(
205                 "Error while checking whether more data is available");
2060(9)         iex.printStackTrace();
2070       }
2080       return rc;
209     }
210  
211     /**
212      * Checks whether the LogFileEntry passes all filters. An implicit filter is
213      * that the type of the entry has been successfully parsed.
214      *
215      * @param entry The LogFileEntry to check.
216      *
217      * @return true if <code>entry</code>passes all filters.
218      */
219     private boolean passesFilters (final LogFileEntry entry)
220     {
2210       boolean rc = entry.getType() != null;
2220       for (final Iterator filterIterator = mFilters.iterator();
2230             filterIterator.hasNext() && rc; )
224        {
2250          final Filter filter = (Filter) filterIterator.next();
2260          rc = filter.isPassable(entry);
2270       }
2280       return rc;
229     }
230  
231     /**
232      * Reads the current line from the log file.
233      *
234      * @return line wrapped in a StringBuffer
235      */
236     private StringBuffer readLine ()
237     {
2380       StringBuffer rc = null;
239        try
240        {
2410          final String line = mReader.readLine();
2420          if (line != null)
243           {
2440             rc = new StringBuffer(line);
245           }
246        }
2470       catch (Exception ex)
248        {
2490(10)         System.err.println("Caught an exception reading the current log line");
2500(11)         ex.printStackTrace();
2510       }
2520       return rc;
253     }
254  
255     void close ()
256     {
2570       if (mReader != null)
258        {
259           try
260           {
2610             mReader.close();
262           }
2630          catch (IOException iex)
264           {
2650(12)            System.err.println("Error closing this:" + iex);
2660(13)            iex.printStackTrace();
2670          }
268        }
2690    }
270  }

Findings in this File

c (1) 49 : 0 Type Javadoc comment is missing an @author tag.
w (2) 69 : 0 class org.jcoderz.commons.logging.LogReader defines fields that are used only as locals
c (3) 103 : 4 Cyclomatic Complexity is 16 (max allowed is 12).
d (4) 159 : 16 System.out.print is used
d (5) 161 : 16 System.out.print is used
d (6) 162 : 16 Avoid printStackTrace(); use a logger call instead.
i (7) 191 : 0 Comment matches to-do format '(TODO|FIXME|CHECKME)'.
d (8) 204 : 10 System.out.print is used
d (9) 206 : 10 Avoid printStackTrace(); use a logger call instead.
d (10) 249 : 10 System.out.print is used
d (11) 250 : 10 Avoid printStackTrace(); use a logger call instead.
d (12) 265 : 13 System.out.print is used
d (13) 266 : 13 Avoid printStackTrace(); use a logger call instead.