View Javadoc
1   package net.logAnalyzer.analysis;
2   
3   import java.text.DecimalFormat;
4   import java.text.NumberFormat;
5   import java.text.SimpleDateFormat;
6   
7   import net.logAnalyzer.converters.UnknownOrLiteralConverterException;
8   import net.logAnalyzer.handlers.LALogHandler;
9   import net.logAnalyzer.handlers.LAMessage;
10  
11  /***
12   * This class defines a template for log analyzers. A log analyzer analysis a
13   * messageLabel and generate an {@link LAAnalysis}.
14   * 
15   * @author Karim REFEYTON
16   * @version 0.1
17   */
18  public abstract class LAAnalyzer {
19  
20      /***
21       * Date format.
22       */
23      private SimpleDateFormat dateFormat = new SimpleDateFormat(
24              "dd/MM/yyyy HH:mm:ss");
25  
26      /***
27       * Percent format.
28       */
29      private NumberFormat percentFormat = new DecimalFormat("##0.00%");
30  
31      /***
32       * <tt>true</tt> if the handler messages can't be analyzed.
33       * 
34       * @see #canAnalyze(LALogHandler)
35       */
36      private boolean cantAnalyze = false;
37  
38      /***
39       * LAAnalyzer definition.
40       */
41      private AnalyzerDefinition definition = null;
42  
43      /***
44       * Result of the analysis.
45       */
46      private LAAnalysis analysis = null;
47  
48      /***
49       * Constructs a new analyzer from an analyzer definition.
50       * 
51       * @param definition
52       *            Definition of the analyzer.
53       */
54      public LAAnalyzer(AnalyzerDefinition definition) {
55          this.definition = definition;
56      }
57  
58      /***
59       * Constructs a new analyzer from an analyzer definition and a date format.
60       * 
61       * @param definition
62       *            Definition of the analyzer.
63       * @param dateFormat
64       *            Date format.
65       */
66      public LAAnalyzer(AnalyzerDefinition definition, SimpleDateFormat dateFormat) {
67          this.definition = definition;
68          this.dateFormat = dateFormat;
69      }
70  
71      /***
72       * Returns the result of the analysis.
73       * 
74       * @return Result of the analysis.
75       */
76      public final LAAnalysis getAnalysis() {
77          return this.analysis;
78      }
79  
80      /***
81       * Sets the result of the analysis.
82       * 
83       * @param analysis
84       *            Result of the analysis.
85       */
86      protected final void setAnalysis(LAAnalysis analysis) {
87          this.analysis = analysis;
88      }
89  
90      /***
91       * Returns the date format.
92       * 
93       * @return Date format.
94       */
95      public final SimpleDateFormat getDateFormat() {
96          return this.dateFormat;
97      }
98  
99      /***
100      * Returns the percent format.
101      * 
102      * @return Percent format.
103      */
104     public final NumberFormat getPercentFormat() {
105         return this.percentFormat;
106     }
107 
108     /***
109      * Returns the label of the analyzer.
110      * 
111      * @return LAAnalyzer label.
112      */
113     public final String getLabel() {
114         return definition.getLabel();
115     }
116 
117     /***
118      * Returns the name of the analyzer.
119      * 
120      * @return LAAnalyzer name.
121      */
122     public final String getName() {
123         return definition.getName();
124     }
125 
126     /***
127      * Returns <tt>true</tt> if the handler messages can be analyzed. For
128      * example, it controls if all needed converters are presents.
129      * 
130      * @param handler
131      *            Log handler using the analyzer.
132      * @return <tt>true</tt> if the handler messages can be analyzed.
133      * @throws UnknownOrLiteralConverterException
134      *             If a column is missing.
135      */
136     public abstract boolean canAnalyze(LALogHandler handler)
137             throws UnknownOrLiteralConverterException;
138 
139     /***
140      * Make all initializations to prepare analysis. Call
141      * {@link #canAnalyze(LALogHandler)} and {@link #init(LALogHandler)};
142      * 
143      * @param handler
144      *            Log handler using the analyzer.
145      */
146     public final void initAnalyze(LALogHandler handler) {
147         try {
148             cantAnalyze = !canAnalyze(handler);
149             init(handler);
150         } catch (UnknownOrLiteralConverterException uolce) {
151             cantAnalyze = true;
152         }
153     }
154 
155     /***
156      * Make all initializations to prepare analysis. Specific analyzer
157      * implementation.
158      * 
159      * @param handler
160      *            Log handler using the analyzer.
161      */
162     public abstract void init(LALogHandler handler);
163 
164     /***
165      * Analyzes the current message. Call {@link #analyzeMessage(LAMessage)},
166      * the analyzer specific implementation. If an exception is thrown, call
167      * {@link #skipMessage(LAMessage, Exception)}.
168      * 
169      * @param message
170      *            messageLabel to analysis.
171      * @param handler
172      *            Log handler using the analyzer.
173      */
174     public final void analyze(LAMessage message, LALogHandler handler) {
175         if (!cantAnalyze) {
176             try {
177                 analyzeMessage(message);
178             } catch (UnknownOrLiteralConverterException uolce) {
179                 skipMessage(message, uolce);
180             }
181         }
182     }
183 
184     /***
185      * Analyze the current messageLabel. Specific analyzer implementation.
186      * 
187      * @param message
188      *            messageLabel to analyze.
189      * @throws UnknownOrLiteralConverterException
190      *             If a converter or a literal can't be handled by the analyzer.
191      */
192     protected abstract void analyzeMessage(LAMessage message)
193             throws UnknownOrLiteralConverterException;
194 
195     /***
196      * Skips the specified message. Called by
197      * {@link #analyze(LAMessage, LALogHandler)} when the previous call to
198      * {@link #analyzeMessage(LAMessage)} has thrown an
199      * {@link UnknownOrLiteralConverterException}. This implementation do
200      * nothing.
201      * 
202      * @param message
203      *            messageLabel to skip.
204      * @param exception
205      *            Exception thrown during analysis.
206      */
207     protected void skipMessage(LAMessage message, Exception exception) {
208         // NOP
209     }
210 
211     /***
212      * Make all releases after the analysis. Call {@link #release(LALogHandler)}.
213      * 
214      * @param handler
215      *            Log handler using the analyzer.
216      */
217     public final void releaseAnalyze(LALogHandler handler) {
218         if (!cantAnalyze) {
219             release(handler);
220         }
221     }
222 
223     /***
224      * Make all releases after the analysis. Specific analyzer implementation.
225      * 
226      * @param handler
227      *            Log handler using the analyzer.
228      */
229     public abstract void release(LALogHandler handler);
230 }