KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > javaLog > JMXHandler


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.util.monolog.wrapper.javaLog;
19 import org.objectweb.util.monolog.wrapper.common.OutputStreamSwitcher;
20 //import org.objectweb.util.monolog.api.BasicLevel;
21

22 //import java.io.OutputStream;
23
import java.io.Writer JavaDoc;
24 //import java.io.OutputStreamWriter;
25
//import java.io.UnsupportedEncodingException;
26
import java.util.logging.LogRecord JavaDoc;
27 import java.util.logging.ErrorManager JavaDoc;
28 import javax.management.ListenerNotFoundException JavaDoc;
29 import javax.management.MBeanNotificationInfo JavaDoc;
30 import javax.management.Notification JavaDoc;
31 import javax.management.NotificationBroadcasterSupport JavaDoc;
32 import javax.management.NotificationEmitter JavaDoc;
33 import javax.management.NotificationFilter JavaDoc;
34 import javax.management.NotificationListener JavaDoc;
35
36 /**
37  * The aim of this class is to permit to specify the output for the console
38  * handler of the java.util.logging system.
39  *
40  * This console handler is also able to choose the right ouput (System.err
41  * or System.out) depending on the message level.
42  *
43  * @author S.Chassande-Barrioz
44  */

45 public class JMXHandler extends java.util.logging.Handler JavaDoc implements NotificationEmitter JavaDoc {
46
47     protected OutputStreamSwitcher oss;
48     private Writer JavaDoc writer;
49
50     public JMXHandler() {
51         super();
52     }
53     
54     CustomNotificationBroadcasterSupport emitter = new CustomNotificationBroadcasterSupport();
55     
56     public void addNotificationListener(NotificationListener JavaDoc listener,
57             NotificationFilter JavaDoc filter,
58             Object JavaDoc handback)
59             throws IllegalArgumentException JavaDoc {
60         emitter.addNotificationListener(listener, filter, handback);
61     }
62     
63     public void removeNotificationListener( NotificationListener JavaDoc listener )
64     throws ListenerNotFoundException JavaDoc {
65         emitter.removeNotificationListener( listener );
66     }
67     
68     public void removeNotificationListener( NotificationListener JavaDoc listener,
69             NotificationFilter JavaDoc filter, Object JavaDoc handback)
70             throws ListenerNotFoundException JavaDoc {
71         emitter.removeNotificationListener( listener, filter, handback );
72     }
73     
74     public MBeanNotificationInfo JavaDoc[] getNotificationInfo(){
75         return emitter.getNotificationInfo();
76     }
77
78     private long notificationSequence = 0 ;
79     
80     public void publish(LogRecord JavaDoc record) {
81         if (!isLoggable(record)) {
82             return;
83         }
84         String JavaDoc msg = record.getMessage();
85         Notification JavaDoc notification = new Notification JavaDoc ("Monolog.JMXHandler.Log",
86                 "JMXHandler:Type=javaLog",++notificationSequence,
87                 System.currentTimeMillis(), msg );
88         notification.setUserData(record);
89         emitter.sendNotification(notification);
90     }
91
92     class CustomNotificationBroadcasterSupport extends NotificationBroadcasterSupport JavaDoc {
93         public void sendNotification (Notification JavaDoc notification) {
94             super.sendNotification (notification);
95         }
96         public void addNotificationListener(NotificationListener JavaDoc listener,
97                 NotificationFilter JavaDoc filter,
98                 Object JavaDoc handback)
99                 throws IllegalArgumentException JavaDoc {
100                 super.addNotificationListener(listener, filter, handback);
101                 
102         }
103     }
104
105     public void flush() {
106         if (writer != null) {
107             try {
108                 writer.flush();
109             } catch (Exception JavaDoc ex) {
110                 // We don't want to throw an exception here, but we
111
// report the exception to any registered ErrorManager.
112
reportError(null, ex, ErrorManager.FLUSH_FAILURE);
113             }
114         }
115     }
116
117     public void close() throws SecurityException JavaDoc {
118         flush();
119     }
120 }
121
Popular Tags