View Javadoc
1   package net.logAnalyzer.analysis;
2   
3   import java.lang.reflect.Constructor;
4   
5   import net.logAnalyzer.patternParser.PatternParsingException;
6   
7   /***
8    * This class contains the definition of an analyzer.
9    * <p>
10   * An analyzer is used to analyze each log messageLabel loaded from a log file during
11   * the {@link net.logAnalyzer.handlers.LALogHandler} parsing process (see
12   * {@link net.logAnalyzer.handlers.LALogHandler#parse()}).
13   * </p>
14   * <p>
15   * There is two kinds of analyzers :
16   * <ul>
17   * <li>Simple analyzers returning discreet results (see
18   * {@link net.logAnalyzer.analysis.LAAnalyzer})</li>
19   * <li>Simple analyzers returning time stamped result chronological results
20   * (see {@link net.logAnalyzer.analysis.LAPeriodicAnalyzer})</li>
21   * </ul>
22   * </p>
23   * <p>
24   * To create a new analyzer from its definition, call
25   * {@link #createAnalyzerInstance()}.
26   * </p>
27   * 
28   * @author Karim REFEYTON
29   * @version 0.1
30   */
31  public final class AnalyzerDefinition {
32  
33      /***
34       * LAAnalyzer class.
35       */
36      private Class analyzerClass;
37  
38      /***
39       * User friendly label.
40       */
41      private String label;
42  
43      /***
44       * Name for IHM.
45       */
46      private String name;
47  
48      /***
49       * Time period.
50       */
51      private LATimePeriod period;
52  
53      /***
54       * Construct a new analyzer definition.
55       * 
56       * @param name
57       *            Name of the analyzer.
58       * @param period
59       *            Time period for time analyzer.
60       * @param classname
61       *            Class of the new analyzer instances.
62       * @param label
63       *            User friendly label.
64       * @throws ClassNotFoundException
65       *             If the specified analyzer class does not exists.
66       */
67      public AnalyzerDefinition(String name, String period, String classname,
68              String label) throws ClassNotFoundException {
69          this.name = name;
70          if (period != null) {
71              this.period = LATimePeriod.getPeriod(period);
72          }
73          this.analyzerClass = ClassLoader.getSystemClassLoader().loadClass(
74                  classname);
75          this.label = label;
76      }
77  
78      /***
79       * Return the analyzer class.
80       * 
81       * @return Analyzer class.
82       */
83      public Class getAnalyzerClass() {
84          return this.analyzerClass;
85      }
86  
87      /***
88       * Return the analyzer name.
89       * 
90       * @return Analyzer name.
91       */
92      public String getName() {
93          return this.name;
94      }
95  
96      /***
97       * Return the analyzer period.
98       * 
99       * @return Period.
100      */
101     public LATimePeriod getPeriod() {
102         return this.period;
103     }
104 
105     /***
106      * Return <tt>true</tt> if the analyzer has a period.
107      * 
108      * @return <tt>true</tt> if has a period; <tt>false</tt> otherwise.
109      */
110     public boolean hasPeriod() {
111         return this.period != null;
112     }
113 
114     /***
115      * Return the user friendly label of the analyzer.
116      * 
117      * @return User friendly label.
118      */
119     public String getLabel() {
120         return this.label;
121     }
122 
123     /***
124      * Create a new instance of the analyzer.
125      * 
126      * @return New instance.
127      * @throws PatternParsingException
128      *             If an error occures during pattern parsing.
129      */
130     public LAAnalyzer createAnalyzerInstance() throws PatternParsingException {
131         try {
132             Class[] classes = null;
133             Object[] parameters = null;
134             if (period == null) {
135                 classes = new Class[] { AnalyzerDefinition.class };
136                 parameters = new Object[] { this };
137             } else {
138                 classes = new Class[] { AnalyzerDefinition.class,
139                         LATimePeriod.class };
140                 parameters = new Object[] { this, period };
141             }
142             Constructor constructor = analyzerClass.getConstructor(classes);
143             return ((LAAnalyzer) constructor.newInstance(parameters));
144         } catch (Exception e) {
145             throw new PatternParsingException(e);
146         }
147     }
148 
149 }