KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > selfmanagement > LogMgmtEventsNotificationListener


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  * LogMgmtEventsNotificationListener.java
26  *
27  * Created on August 19, 2005, 1:18 PM
28  */

29
30 package com.sun.enterprise.management.selfmanagement;
31
32 import java.util.Map JavaDoc;
33 import javax.management.Notification JavaDoc;
34 import javax.management.NotificationListener JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import com.sun.enterprise.config.serverbeans.ServerTags;
38 import com.sun.logging.LogDomains;
39 import com.sun.enterprise.util.i18n.StringManager;
40 /**
41  *
42  * @author Harpreet Singh
43  */

44 public class LogMgmtEventsNotificationListener implements NotificationListener JavaDoc {
45   
46     /** Logger for self management service */
47     private static Logger JavaDoc _logger = null;
48   
49     /** Local Strings manager for the class */
50     private static StringManager localStrings = null;
51                                                                                                                             
52     static {
53         _logger = LogDomains.getLogger(LogDomains.SELF_MANAGEMENT_LOGGER);
54         localStrings = StringManager.getManager(LogMgmtEventsNotificationListener.class);
55     }
56
57     
58     /**
59      * Is logging turned on for this particular even. Key corresponds to
60      * the <i>record-event</i> from the event element in domain xml
61      */

62     public static final String JavaDoc RECORD_EVENT_KEY = ServerTags.RECORD_EVENT;
63     
64     /*
65      * The logging level for logging events. An event is logged if the
66      * record-event is turned. Key corresponds to level in event element
67      * in domain xml.
68      */

69     public static final String JavaDoc RECORD_LOG_LEVEL_KEY = ServerTags.LEVEL;
70         
71     public static final String JavaDoc EVENT_TYPE_KEY = ServerTags.TYPE;
72     
73     public static final String JavaDoc EVENT_DESCRIPTION_KEY = ServerTags.DESCRIPTION;
74
75     /** Creates a new instance of LogMgmtEventsNotificationListener */
76     private LogMgmtEventsNotificationListener() {
77         // do Nothing
78
}
79
80     public static LogMgmtEventsNotificationListener getInstance (){
81         return new LogMgmtEventsNotificationListener ();
82     }
83     public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback) {
84         
85         Map JavaDoc<String JavaDoc, String JavaDoc> property =
86                 (Map JavaDoc<String JavaDoc, String JavaDoc>) handback;
87         
88         if (property == null)
89             return;
90         
91         String JavaDoc recordevent = null;;
92         recordevent = (String JavaDoc)property.get(RECORD_EVENT_KEY);
93     if (recordevent == null)
94        return;
95
96         boolean recordEvent = false;
97         if (recordevent != null){
98             recordEvent = Boolean.valueOf(recordevent);
99             
100             if(recordEvent){
101                 try{
102             StringBuffer JavaDoc message =
103             new StringBuffer JavaDoc(localStrings.
104                 getString("logMgmtEventsNotificationListener.prefix"));
105
106                     String JavaDoc eventType = (String JavaDoc)property.get(EVENT_TYPE_KEY);
107
108             if (eventType != null){
109             message.append (eventType);
110             message.append (":");
111             }
112                     String JavaDoc logLevel = (String JavaDoc) property.get(RECORD_LOG_LEVEL_KEY);
113                 if(logLevel == null){
114             logLevel = Level.FINE.toString();
115             }
116             message.append (logLevel);
117             message.append (":");
118                     Level JavaDoc level = Level.parse(logLevel);
119
120                     String JavaDoc description = (String JavaDoc) property.get(EVENT_DESCRIPTION_KEY);
121
122             if (description != null){
123             message.append (description);
124             message.append (":");
125            }
126                     _logger.log(level, message.toString());
127                 } catch (IllegalArgumentException JavaDoc iae) {
128                     _logger.log(Level.FINE, "Incorrect Log Level set for event. Cannot log event ", iae);
129             iae.printStackTrace();
130                 } catch (NullPointerException JavaDoc npe){
131                     _logger.log(Level.FINE, "Incorrect Log Level set for event. Cannot log event ", npe);
132             npe.printStackTrace();
133         }
134             }
135         }
136     }
137 }
138     
139
Popular Tags