KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > filter > TCLFilter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.logging.filter;
23
24
25
26 import java.lang.reflect.Method JavaDoc;
27
28 import java.net.URL JavaDoc;
29
30
31
32 import org.apache.log4j.spi.Filter;
33
34 import org.apache.log4j.spi.LoggingEvent;
35
36
37
38 import org.jboss.util.collection.WeakSet;
39
40
41
42 /** An appender filter that accepts log events based on whether the thread
43
44  context class loader has a classpath URL that has the DeployURL
45
46  attribute as a substring. A sample usage would be:
47
48
49
50    <appender name="JMX-CONSOLE" class="org.jboss.logging.appender.FileAppender">
51
52       <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
53
54       <param name="File" value="${jboss.server.home.dir}/log/jmx-console.log"/>
55
56       <layout class="org.apache.log4j.PatternLayout">
57
58          <!-- The default pattern: Date Priority [Category] Message\n -->
59
60          <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
61
62       </layout>
63
64       <filter class="org.jboss.logging.filter.TCLFilter">
65
66          <param name="AcceptOnMatch" value="true"/>
67
68          <param name="DeployURL" value="jmx-console.war"/>
69
70       </filter>
71
72    </appender>
73
74
75
76  @author Scott.Stark@jboss.org
77
78  @version $Revison:$
79
80  */

81
82 public class TCLFilter extends Filter
83
84 {
85
86    /** The set of TCLs seen to match DeployURL */
87
88    private WeakSet matchSet = new WeakSet();
89
90    /** The set of TCLs seen to not match DeployURL */
91
92    private WeakSet missSet = new WeakSet();
93
94    /** The deployment URL string fragment to match against */
95
96    private String JavaDoc deployURL;
97
98    /** Whether a match should return ACCEPT or DENY */
99
100    private boolean acceptOnMatch = true;
101
102
103
104    public boolean isAcceptOnMatch()
105
106    {
107
108       return acceptOnMatch;
109
110    }
111
112    public void setAcceptOnMatch(boolean acceptOnMatch)
113
114    {
115
116       this.acceptOnMatch = acceptOnMatch;
117
118    }
119
120    public String JavaDoc getDeployURL()
121
122    {
123
124       return deployURL;
125
126    }
127
128    public void setDeployURL(String JavaDoc deployURL)
129
130    {
131
132       this.deployURL = deployURL;
133
134    }
135
136
137
138    public int decide(LoggingEvent event)
139
140    {
141
142       int ok = Filter.DENY;
143
144       if( acceptOnMatch == true )
145
146       {
147
148          ok = Filter.DENY;
149
150          if( isMatchingTCL() )
151
152             ok = Filter.ACCEPT;
153
154       }
155
156       else
157
158       {
159
160          ok = Filter.ACCEPT;
161
162          if( isMatchingTCL() )
163
164             ok = Filter.DENY;
165
166       }
167
168       return ok;
169
170    }
171
172
173
174    /** Start with the current thread context class loader
175
176     * @return true if the caller tcl has a url matching our deployURL
177
178     */

179
180    private boolean isMatchingTCL()
181
182    {
183
184       ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
185
186       if( matchSet.contains(tcl) )
187
188          return true;
189
190       if( missSet.contains(tcl) )
191
192          return false;
193
194
195
196       // Search the class loader URLs for a match
197

198       ClassLoader JavaDoc cl = tcl;
199
200       boolean match = false;
201
202       while( cl != null )
203
204       {
205
206          URL JavaDoc[] urls = getClassLoaderURLs(cl);
207
208          for(int n = 0; n < urls.length; n ++)
209
210          {
211
212             URL JavaDoc u = urls[n];
213
214             String JavaDoc file = u.getFile();
215
216             if( file.indexOf(deployURL) > 0 )
217
218             {
219
220                match = true;
221
222                break;
223
224             }
225
226          }
227
228          cl = cl.getParent();
229
230       }
231
232       if( match == true )
233
234          matchSet.add(tcl);
235
236       else
237
238          missSet.add(tcl);
239
240
241
242       return match;
243
244    }
245
246
247
248    /** Use reflection to access a URL[] getURLs method so that non-URLClassLoader
249
250     class loaders that support this method can provide info.
251
252     */

253
254    private static URL JavaDoc[] getClassLoaderURLs(ClassLoader JavaDoc cl)
255
256    {
257
258       URL JavaDoc[] urls = {};
259
260       try
261
262       {
263
264          Class JavaDoc returnType = urls.getClass();
265
266          Class JavaDoc[] parameterTypes = {};
267
268          Method JavaDoc getURLs = cl.getClass().getMethod("getURLs", parameterTypes);
269
270          if( returnType.isAssignableFrom(getURLs.getReturnType()) )
271
272          {
273
274             Object JavaDoc[] args = {};
275
276             urls = (URL JavaDoc[]) getURLs.invoke(cl, args);
277
278          }
279
280          if( urls == null || urls.length == 0 )
281
282          {
283
284             getURLs = cl.getClass().getMethod("getClasspath", parameterTypes);
285
286             if( returnType.isAssignableFrom(getURLs.getReturnType()) )
287
288             {
289
290                Object JavaDoc[] args = {};
291
292                urls = (URL JavaDoc[]) getURLs.invoke(cl, args);
293
294             }
295
296          }
297
298       }
299
300       catch(Exception JavaDoc ignore)
301
302       {
303
304       }
305
306       return urls;
307
308    }
309
310
311
312 }
313
314
315
316
Popular Tags