KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > log > LoggerBroadcaster


1 /*
2  * Copyright (C) MX4J.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.log;
10
11 import javax.management.NotificationListener JavaDoc;
12 import javax.management.NotificationFilter JavaDoc;
13 import javax.management.ListenerNotFoundException JavaDoc;
14 import javax.management.MBeanNotificationInfo JavaDoc;
15 import javax.management.Notification JavaDoc;
16 import javax.management.NotificationBroadcasterSupport JavaDoc;
17 import javax.management.MBeanRegistration JavaDoc;
18 import javax.management.ObjectName JavaDoc;
19 import javax.management.MBeanServer JavaDoc;
20
21 /**
22  * This broadcaster MBean allows to redirect MX4J internal logging to registered JMX listeners. <p>
23  * Simply register this MBean in the MBeanServer, register one or more listener (eventually with filters
24  * on the notification type), then call (directly or through MBeanServer) one of the <code>start</code>
25  * operations. From this moment, MX4J internal logging is redirected to this MBean, and from here to all registered
26  * listeners, basing on their filters.
27  *
28  * @author <a HREF="mailto:biorn_steedom@users.sourceforge.net">Simone Bordet</a>
29  * @version $Revision: 1.5 $
30  */

31 public class LoggerBroadcaster extends NotificationBroadcasterSupport JavaDoc implements MBeanRegistration JavaDoc, LoggerBroadcasterMBean
32 {
33     private long m_sequence;
34     private boolean m_registered;
35     private int m_recursionLevel;
36
37     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc
38     {
39         return name;
40     }
41     public void postRegister(Boolean JavaDoc registrationDone)
42     {
43         if (!registrationDone.booleanValue()) {return;}
44         m_registered = true;
45     }
46     public void preDeregister() throws Exception JavaDoc {}
47     public void postDeregister() {m_registered = false;}
48
49     public void removeNotificationListener(NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback) throws ListenerNotFoundException JavaDoc
50     {
51         // PENDING: for JMX 1.2 this has to be changed
52
super.removeNotificationListener(listener);
53     }
54
55     public void sendNotification(Notification JavaDoc n)
56     {
57         // Since I use a broadcaster to log, it happens that log requests of the broadcaster itself are redirected
58
// to the broadcaster, which broadcast to itself generating other log requests, an endless loop.
59
// Here I stop reentrant calls: maxRecursionLevel == 1 means that initial log requests are broadcasted, but log
60
// requests happening during this broadcast aren't; maxRecursionLevel == 2 means that initial log requests are
61
// broadcasted, and also log requests happening during this broadcast, while log requests happening during
62
// broadcast of log requests due to broacasting of the initial log request aren't.
63
int maxRecursionLevel = 1;
64         synchronized (this)
65         {
66             if (m_recursionLevel < maxRecursionLevel)
67             {
68                 ++m_recursionLevel;
69                 super.sendNotification(n);
70                 --m_recursionLevel;
71             }
72         }
73     }
74
75     public void start()
76     {
77         Logger logger = createLoggerPrototype();
78         Log.redirectTo(logger);
79     }
80
81     public void start(String JavaDoc category)
82     {
83         Logger logger = createLoggerPrototype();
84         Log.redirectTo(logger, category);
85     }
86
87     public void stop()
88     {
89         Log.redirectTo(null);
90     }
91
92     public void stop(String JavaDoc category)
93     {
94         Log.redirectTo(null, category);
95     }
96
97     private boolean isRegistered() {return m_registered;}
98
99     public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
100     {
101         String JavaDoc[] types = new String JavaDoc[] {"mx4j.logger.trace",
102                                        "mx4j.logger.debug",
103                                        "mx4j.logger.info",
104                                        "mx4j.logger.warn",
105                                        "mx4j.logger.error",
106                                        "mx4j.logger.fatal"};
107         MBeanNotificationInfo JavaDoc notifs = new MBeanNotificationInfo JavaDoc(types, "javax.management.Notification", "MX4J Logger MBean notifications");
108         return new MBeanNotificationInfo JavaDoc[] {notifs};
109     }
110
111     protected Logger createLoggerPrototype()
112     {
113         return new LoggerNotifier(this);
114     }
115
116     public static class LoggerNotifier extends Logger
117     {
118         private static LoggerBroadcaster m_loggerBroadcaster;
119
120         private LoggerNotifier(LoggerBroadcaster mbean) {m_loggerBroadcaster = mbean;}
121
122         public LoggerNotifier() {}
123
124         protected void log(int priority, Object JavaDoc message, Throwable JavaDoc t)
125         {
126             // Notify listeners
127
notify(priority, message, t);
128         }
129
130         private void notify(int priority, Object JavaDoc message, Throwable JavaDoc t)
131         {
132             if (m_loggerBroadcaster.isRegistered())
133             {
134                 long sequence = 0;
135                 synchronized (this) {sequence = ++m_loggerBroadcaster.m_sequence;}
136
137                 String JavaDoc type = null;
138                 switch (priority)
139                 {
140                     case TRACE: type = "mx4j.logger.trace"; break;
141                     case DEBUG: type = "mx4j.logger.debug"; break;
142                     case INFO: type = "mx4j.logger.info"; break;
143                     case WARN: type = "mx4j.logger.warn"; break;
144                     case ERROR: type = "mx4j.logger.error"; break;
145                     case FATAL: type = "mx4j.logger.fatal"; break;
146                     default: type = "mx4j.logger." + priority; break;
147                 }
148
149                 String JavaDoc msg = message == null ? "" : message.toString();
150
151                 // TODO: the source must be the object name of the MBean if the listener was registered through MBeanServer
152
Notification JavaDoc n = new Notification JavaDoc(type, this, sequence, msg);
153                 if (t != null)
154                 {
155                     n.setUserData(t);
156                 }
157
158                 m_loggerBroadcaster.sendNotification(n);
159             }
160         }
161     }
162 }
163
Popular Tags