KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > filters > JoSQLLogRecordFilter


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.filters;
16
17 import java.util.logging.Filter JavaDoc;
18 import java.util.logging.LogRecord JavaDoc;
19
20 import org.josql.Query;
21 import org.josql.QueryParseException;
22
23 /**
24  * A {@link Filter} that uses a JoSQL statement to provide the filtering.
25  * The value returned by the {@link #isLoggable(LogRecord)} method is determined by executing the
26  * WHERE clause of a JoSQL statement on each {@link LogRecord} passed in.
27  * <p>
28  * Since this uses a sub-set of the JoSQL functionality certain restrictions apply:
29  * <ul>
30  * <li>The SELECT clause is ignored.</li>
31  * <li>The object defined in the FROM clause <b>must</b> be java.util.logging.LogRecord</li>
32  * <li>EXECUTE ON functions are not supported here since there is no way to get the entire set
33  * of objects to work on.</li>
34  * <li>ORDER BY, GROUP BY, HAVING and LIMIT clauses are ignored.</li>
35  * </ul>
36  * Examples:
37  * <p>
38  * <pre>
39  * SELECT *
40  * FROM {@link LogRecord java.tuil.logging.LogRecord}
41  * WHERE name $LIKE '%.html'
42  * AND millis > {@link org.josql.functions.ConversionFunctions#toDateMillis(String,String) toDateMillis}('12-04-2004')
43  * AND sequenceNumber BETWEEN (10000 AND 20000)
44  * AND message $LIKE '%internal%'
45  * AND loggerName = 'internal_logger'
46  * AND level.name IN ('SEVERE', 'WARNING')
47  * </pre>
48  * <p>
49  * If you are using a custom log record then you can always just extend this class and override
50  * the {@link #getExpectedClass()} method to return your specific sub-class.
51  * <p>
52  * Last Modified By: $Author: barrygently $<br />
53  * Last Modified On: $Date: 2005/01/07 17:10:40 $<br />
54  * Current Revision: $Revision: 1.1 $<br />
55  */

56 public class JoSQLLogRecordFilter extends AbstractJoSQLFilter implements Filter JavaDoc
57 {
58
59     protected Class JavaDoc expected = LogRecord JavaDoc.class;
60
61     /**
62      * Init this filter with the query.
63      *
64      * @param q The query.
65      * @throws QueryParseException If there is an issue with the parsing of the query,
66      * or if the FROM class is not {@link LogRecord}.
67      */

68     public JoSQLLogRecordFilter (String JavaDoc q)
69                              throws QueryParseException
70     {
71
72     super (q);
73
74     }
75
76     /**
77      * Init this filter with the query already built and parsed.
78      *
79      * @param q The query.
80      * @throws IllegalStateException If the Query object has not been parsed.
81      * @throws QueryParseException If the FROM class is not {@link LogRecord}.
82      */

83     public JoSQLLogRecordFilter (Query q)
84                              throws IllegalStateException JavaDoc,
85                                     QueryParseException
86     {
87
88     super (q);
89
90     }
91
92     public boolean accept (Object JavaDoc o)
93     {
94
95     if (!(o instanceof LogRecord JavaDoc))
96     {
97
98         throw new IllegalArgumentException JavaDoc ("Expected object to be of type: " +
99                         LogRecord JavaDoc.class.getName () +
100                         ", got: " +
101                         o.getClass ().getName ());
102
103     }
104
105     return this.accept ((LogRecord JavaDoc) o);
106
107     }
108
109     /**
110      * Apply the WHERE clause of the statement to the {@link LogRecord} passed in.
111      * If an exception is thrown by the execution of the WHERE clause the Query
112      * is marked as "dirty" and the where clause is no longer executed on the passed in
113      * files (since it is likely that the WHERE clause will fail for all LogRecord objects).
114      * You can get access to exception by using: {@link #getException()}.
115      *
116      * @param l The log record to evaluate the WHERE on.
117      * @return <code>true</code> if the WHERE clause evaluates to <code>true</code>.
118      */

119     public boolean isLoggable (LogRecord JavaDoc l)
120     {
121     
122     if (this.badQuery)
123     {
124
125         return false;
126
127     }
128
129     try
130     {
131
132         return this.q.getWhereClause ().isTrue (l,
133                             this.q);
134
135     } catch (Exception JavaDoc e) {
136
137         this.badQuery = true;
138
139         this.exp = e;
140
141     }
142
143     return false;
144
145     }
146
147     /**
148      * Always returns {@link LogRecord}.
149      *
150      * @return The log record class.
151      */

152     public Class JavaDoc getExpectedClass ()
153     {
154
155     return LogRecord JavaDoc.class;
156
157     }
158
159 }
160
Popular Tags