View Javadoc
1   package net.logAnalyzer.handlers;
2   
3   import java.io.FileNotFoundException;
4   import java.io.IOException;
5   import java.util.Vector;
6   
7   import net.logAnalyzer.utils.FilesSetReader;
8   
9   /***
10   * This log handler parses messages on demand. It must be used to handle large
11   * log files with small memory needs.
12   * 
13   * @author Karim REFEYTON
14   * @version 0.1
15   */
16  public class OnDemandLogHandler extends AbstractLogHandler {
17      /***
18       * Index of the current parsed messageLabel.
19       */
20      private int currentIndex = -1;
21  
22      /***
23       * Vector of messages pointers. A pointer represents the start of a
24       * messageLabel in the log file.
25       */
26      private Vector pointers = null;
27  
28      /***
29       * Pointer at the end of the last messageLabel.
30       */
31      private long endPointer;
32  
33      /***
34       * Current parsed messageLabel.
35       */
36      private LAMessage currentMessage = null;
37  
38      /***
39       * @param input
40       * @param pattern
41       * @throws FileNotFoundException
42       * @throws NoConverterException
43       */
44      public OnDemandLogHandler(FilesSetReader input, String pattern)
45              throws FileNotFoundException, NoConverterException {
46          super(input, pattern);
47      }
48  
49      /***
50       * Returns the number of messages read.
51       * 
52       * @see net.logAnalyzer.handlers.LALogHandler#getSize()
53       */
54      public int getSize() {
55          return pointers.size();
56      }
57  
58      /***
59       * Modification to the inherited behavior.
60       * 
61       * @see AbstractLogHandler#loadMessage(int)
62       */
63      protected synchronized LAMessage loadMessage(int index) {
64          if (index >= 0 && index < pointers.size()
65                  && (currentIndex != index || currentMessage == null)) {
66              // loads messageLabel from log file
67              try {
68                  // The messageLabel is between the start pointer and the next
69                  // start
70                  // pointer or end of file
71                  long pointer = ((Long) pointers.get(index)).longValue();
72                  long nextPointer = -1;
73                  if (index < pointers.size() - 1) {
74                      nextPointer = ((Long) pointers.get(index + 1)).longValue();
75                  }
76                  getReader().seek(pointer);
77                  // Reads and parses the messageLabel
78                  String line = readNextLine();
79                  currentMessage = parseMessage(line, getConverters());
80                  currentIndex = index;
81                  // Is it a multi-lines messageLabel ?
82                  while ((nextPointer == -1 || getFilePointer() < nextPointer)
83                          && getFilePointer() < endPointer
84                          && ((line = readNextLine()) != null)) {
85                      currentMessage.extendsMessage(line);
86                  }
87              } catch (IOException ioe) {
88                  // NOP
89                  return currentMessage;
90              } catch (ParsingException pe) {
91                  // NOP
92                  return currentMessage;
93              }
94          }
95          return currentMessage;
96      }
97  
98      /***
99       * Modification to the inherited behavior.
100      * 
101      * @see AbstractLogHandler#saveMessage(LAMessage, long)
102      */
103     protected void saveMessage(LAMessage message, long pointer) {
104         // Saves messageLabel position
105         pointers.add(new Long(pointer));
106         endPointer = getFilePointer();
107         this.currentMessage = message;
108         currentIndex = getSize() - 1;
109     }
110 
111     /***
112      * Modification to the inherited behavior.
113      * 
114      * @see AbstractLogHandler#initParsing()
115      */
116     protected void initParsing() throws IOException {
117         super.initParsing();
118         currentMessage = null;
119         pointers = new Vector();
120     }
121 
122     /***
123      * Returns to the start of the files set.
124      * 
125      * @see AbstractLogHandler#releaseParsing()
126      */
127     protected void releaseParsing() throws IOException {
128         super.releaseParsing();
129         // Opens the reader
130         getReader().seek(0);
131     }
132 }