KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > common > LogManagement


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: LogManagement.java,v 1.4 2005/07/27 13:10:23 goebelg Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.common;
27
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.TreeSet JavaDoc;
32
33 import javax.ejb.EJBException JavaDoc;
34 import javax.management.Notification JavaDoc;
35 import javax.management.NotificationBroadcaster JavaDoc;
36 import javax.management.NotificationEmitter JavaDoc;
37 import javax.management.NotificationFilter JavaDoc;
38 import javax.management.NotificationListener JavaDoc;
39
40 import org.objectweb.jonas.jms.JmsServiceImplMBean;
41 import org.objectweb.jonas.management.ReconfigDispatcher;
42 import org.objectweb.jonas.management.ReconfiguredProp;
43
44 import org.objectweb.util.monolog.Monolog;
45 import org.objectweb.util.monolog.api.Handler;
46 import org.objectweb.util.monolog.api.Logger;
47 import org.objectweb.util.monolog.api.Level;
48 import org.objectweb.util.monolog.api.LoggerFactory;
49 import org.objectweb.util.monolog.api.MonologFactory;
50
51 public class LogManagement extends ReconfigDispatcher implements LogManagementMBean {
52
53     // Value used as sequence number by reconfiguration notifications
54
long sequenceNumber = 0;
55
56     // Name as used to label configuration properties
57
public static final String JavaDoc SERVICE_NAME = "log";
58
59     /**
60      * The unique instance of LogManagement
61      */

62     private static LogManagement unique = null;
63
64     public static LogManagement getInstance() {
65         if (unique == null) {
66             unique = new LogManagement();
67         }
68         return unique;
69     }
70
71     /**
72      * get Topics. Assumes that all Loggers are TopicalLoggers.
73      */

74     public String JavaDoc[] getTopics() {
75         Logger[] logs = Log.getLoggerFactory().getLoggers();
76         // put names in alphabetical order
77
TreeSet JavaDoc tset = new TreeSet JavaDoc();
78         for (int i = 0; i < logs.length; i++) {
79             tset.add(logs[i].getName());
80         }
81         return (String JavaDoc[]) tset.toArray(new String JavaDoc[0]);
82     }
83
84     /**
85      * get Topic Level
86      */

87     public String JavaDoc getTopicLevel(String JavaDoc topic) {
88         String JavaDoc ret = null;
89         Logger topicLogger = Log.getLoggerFactory().getLogger(topic);
90         Level lev = topicLogger.getCurrentLevel();
91         return lev.getName();
92     }
93
94     /**
95      * set Topic Level
96      */

97     public void setTopicLevel(String JavaDoc topic, String JavaDoc level) {
98         Logger topicLogger = Log.getLoggerFactory().getLogger(topic);
99         Level lev = Log.getLevelFactory().getLevel(level);
100         // must check null (bug monolog)
101
if (lev != null) {
102             topicLogger.setLevel(lev);
103         } else {
104             throw new EJBException JavaDoc("Unknown level " + level);
105         }
106         // the modified property name is 'logger.topic.level'
107
String JavaDoc propName = "logger." + topic + ".level";
108         // Send a notification containing the new value of this property to the
109
// listner MBean
110
sendReconfigNotification(++sequenceNumber, SERVICE_NAME, new ReconfiguredProp(propName, level));
111     }
112
113     /**
114      * returns list of properties for logging system
115      */

116     public Properties JavaDoc getProperties() {
117         Properties JavaDoc props = Log.getProperties();
118         if (props == null) {
119             Log.getLoggerFactory();
120             props = Log.getProperties();
121         }
122         return props;
123     }
124
125     public void saveConfig() {
126         // Send save reconfig notification
127
sendSaveNotification(++sequenceNumber, SERVICE_NAME);
128     }
129
130
131     /**
132      * Returns the names of the Monolog handlers
133      * @return The handler names defines in Monolog
134      */

135     public String JavaDoc[] getHandlerNames() {
136         LoggerFactory lf = Log.getLoggerFactory();
137
138         if (lf instanceof MonologFactory) {
139             MonologFactory mf = (MonologFactory) lf;
140             Handler[] hs = mf.getHandlers();
141             String JavaDoc[] hns = new String JavaDoc[hs.length];
142             for (int i = 0; i < hs.length; i++) {
143                 hns[i] = hs[i].getName();
144             }
145             return hns;
146         }
147         return null;
148     }
149
150     /**
151      * Getter for the map of the attributes of a handler
152      * @return The map of the attributes defines for the handler
153      */

154     public Map JavaDoc getHandlerAttributes(String JavaDoc handlername) {
155         LoggerFactory lf = Log.getLoggerFactory();
156
157         if (lf instanceof MonologFactory) {
158             MonologFactory mf = (MonologFactory) lf;
159
160             Handler h = mf.getHandler(handlername);
161             String JavaDoc[] ans = h.getAttributeNames();
162             Map JavaDoc m = new HashMap JavaDoc(ans.length);
163             for (int i = 0; i < ans.length; i++) {
164                 m.put(ans[i], h.getAttribute(ans[i]));
165             }
166             return m;
167         }
168         return null;
169     }
170
171     /**
172      * Super charge of the methode to take advantage of JMX notification
173      * offered by Monolog 2.0
174      * @param arg0 The notification Listener
175      * @param arg1 The notification Filter
176      * @param arg2 Handback object
177      */

178     public void addNotificationListener(NotificationListener JavaDoc arg0, NotificationFilter JavaDoc arg1, Object JavaDoc arg2)
179             throws IllegalArgumentException JavaDoc {
180         Handler handler = Monolog.getMonologFactory().getHandler("jmxHandler");
181         if (handler != null && (handler instanceof NotificationEmitter JavaDoc)) {
182             ((NotificationEmitter JavaDoc) handler).addNotificationListener(arg0, arg1, arg2);
183         }
184         super.addNotificationListener(arg0, arg1, arg2);
185     }
186
187 }
188
Popular Tags