KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > selfmanagement > event > LogEventFilter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * LogNotificationFilter.java
26  *
27  */

28
29 package com.sun.enterprise.admin.selfmanagement.event;
30
31 import javax.management.NotificationFilter JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.LogRecord JavaDoc;
38 import com.sun.appserv.management.ext.logging.*;
39 import com.sun.appserv.management.base.Util;
40 import com.sun.enterprise.util.i18n.StringManager;
41
42 /**
43  *
44  * This is a NotificationFilter that will be instrumented to LogEvent to
45  * filterout unwanted logEvents. Basically, it will filterout logs based on
46  * LoggerName and Level settings.
47  *
48  * @author Sun Micro Systems, Inc
49  */

50 public class LogEventFilter implements NotificationFilter JavaDoc {
51     private static StringManager sm = StringManager.getManager(LogEventFilter.class);
52     private boolean anyLogger = false;
53     
54     private ArrayList JavaDoc<String JavaDoc> loggerNames;
55     
56     private Level JavaDoc level;
57     
58     /** Creates a new instance of LogNotificationFilter */
59     public LogEventFilter() {
60     }
61     
62     public List JavaDoc getLoggerNames() {
63         return loggerNames;
64     }
65     
66     public void setLoggerNames(String JavaDoc loggers) {
67         if( loggers == null ){
68             return;
69         }
70         loggerNames = new ArrayList JavaDoc<String JavaDoc>( );
71         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(loggers, ",");
72         while( tokenizer.hasMoreTokens()) {
73             String JavaDoc loggerName = tokenizer.nextToken();
74             if ("*".equals(loggerName)) {
75                 anyLogger = true;
76                 loggerNames.add( loggerName );
77                 return;
78             }
79             loggerNames.add( loggerName );
80         }
81     }
82     
83     public String JavaDoc getLevel() {
84         return level.toString();
85     }
86     
87     public void setLevel(String JavaDoc level) {
88         this.level = Level.parse(level);
89     }
90     
91     // IMPORTANT: Do not put any logging statements in this method
92
// This would cause infinite loop as the log statements would
93
// generate another notification
94
// Not even System.out or System.err
95

96     public boolean isNotificationEnabled(
97             javax.management.Notification JavaDoc notification ) {
98         boolean loggerNameMatched = false;
99         boolean logLevelMatched = false;
100         
101         if (anyLogger)
102             loggerNameMatched = true;
103         else {
104             String JavaDoc loggerNameFromNotification = (String JavaDoc)Util.getAMXNotificationValue(notification,
105                     LogRecordEmitter.LOG_RECORD_LOGGER_NAME_KEY);
106             Iterator JavaDoc iterator = loggerNames.iterator( );
107             while( iterator.hasNext( ) ) {
108                 String JavaDoc loggerNameFromList = (String JavaDoc) iterator.next( );
109                 if( loggerNameFromNotification.startsWith( loggerNameFromList )) {
110                     loggerNameMatched = true;
111                     break;
112                 }
113             }
114         }
115         Level JavaDoc logLevelFromNotification = (Level JavaDoc)Util.getAMXNotificationValue(notification,
116                 LogRecordEmitter.LOG_RECORD_LEVEL_KEY);
117         if( logLevelFromNotification.intValue() >= this.level.intValue() ) {
118             logLevelMatched = true;
119         }
120         return loggerNameMatched && logLevelMatched;
121     }
122     
123     
124 }
125
Popular Tags