KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > remote > lib > MonologFactoryMBeanImpl


1 /**
2  * Copyright (C) 2001-2005 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.remote.lib;
19
20 import org.objectweb.util.monolog.Monolog;
21 import org.objectweb.util.monolog.api.Handler;
22 import org.objectweb.util.monolog.api.Level;
23 import org.objectweb.util.monolog.api.Logger;
24 import org.objectweb.util.monolog.api.MonologFactory;
25 import org.objectweb.util.monolog.api.TopicalLogger;
26 import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess;
27 import org.objectweb.util.monolog.wrapper.remote.api.LoggerInfo;
28 import org.objectweb.util.monolog.wrapper.remote.api.MonologFactoryMBean;
29
30 import java.util.Arrays JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.Comparator JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 /**
38  * This class is a JMX MBean implementing the MonologFactoryMBean interface. It
39  * permits to configure loggers, handlers and levels on a monolog factory
40  *
41  * @author S.Chassande-Barrioz
42  */

43 public class MonologFactoryMBeanImpl implements MonologFactoryMBean {
44
45     private MonologFactory mf;
46     
47     /**
48      * Build a JMX MBean managing the default MonologFactory registered
49      * as current int the Monolog class.
50      */

51     public MonologFactoryMBeanImpl() {
52         this(Monolog.getMonologFactory());
53     }
54     
55     /**
56      * Build a JMX MBean for administrate the a MonologFactory.
57      * @param mf is the monolog factory instance encapsulated into this MBean.
58      */

59     public MonologFactoryMBeanImpl(MonologFactory mf) {
60         this.mf = mf;
61     }
62     
63     public boolean defineLevel(String JavaDoc name, int value) {
64         return mf.defineLevel(name, value) != null;
65     }
66
67     public boolean defineLevel(String JavaDoc name, String JavaDoc value)
68              {
69         return mf.defineLevel(name, value) != null;
70     }
71
72     public void removeLevel(String JavaDoc name) {
73         mf.removeLevel(name);
74     }
75
76     public Level getLevel(String JavaDoc name) {
77         return mf.getLevel(name);
78     }
79
80     public Level getLevel(int value) {
81         return mf.getLevel(value);
82     }
83
84     public Level[] getLevels() {
85         return mf.getLevels();
86     }
87
88     public int compareTo(String JavaDoc levelname1, String JavaDoc levelname2)
89              {
90         Level l1 = mf.getLevel(levelname1);
91         if (l1 == null) {
92             return Integer.MAX_VALUE;
93         }
94         Level l2 = mf.getLevel(levelname2);
95         if (l2 == null) {
96             return Integer.MIN_VALUE;
97         }
98         return l1.compareTo(l2);
99     }
100
101     public boolean createHandler(String JavaDoc hn, String JavaDoc handlertype)
102              {
103         return mf.createHandler(hn, handlertype) != null;
104     }
105
106     public boolean removeHandler(String JavaDoc handlername) {
107         return mf.removeHandler(handlername) == null;
108     }
109
110     public String JavaDoc[] getHandlerNames() {
111         Handler[] hs = mf.getHandlers();
112         String JavaDoc[] hns = new String JavaDoc[hs.length];
113         for(int i=0; i<hs.length; i++) {
114             hns[i] = hs[i].getName();
115         }
116         return hns;
117     }
118
119     public Map JavaDoc getHandlerAttributes(String JavaDoc handlername) {
120         Handler h = mf.getHandler(handlername);
121         String JavaDoc[] ans = h.getAttributeNames();
122         Map JavaDoc m = new HashMap JavaDoc(ans.length);
123         for(int i=0; i<ans.length; i++) {
124             m.put(ans[i], h.getAttribute(ans[i]));
125         }
126         m.put("name", handlername);
127         return m;
128     }
129
130     public Map JavaDoc getAllHandlerAttributes()
131              {
132         Handler[] hs = mf.getHandlers();
133         Map JavaDoc m = new HashMap JavaDoc(hs.length);
134         for(int i=0; i<hs.length; i++) {
135             m.put(hs[i], getHandlerAttributes(hs[i].getName()));
136         }
137         return m;
138     }
139
140     public void setHandlerAttribute(String JavaDoc handlername, String JavaDoc attributeName,
141             String JavaDoc value) {
142         Handler h = mf.getHandler(handlername);
143         if (h == null) {
144             return;
145         }
146         h.setAttribute(attributeName, value);
147     }
148
149     public LoggerInfo getLogger(String JavaDoc loggername) {
150         return new LoggerInfo((TopicalLogger) mf.getLogger(loggername));
151     }
152
153     public LoggerInfo getLogger(String JavaDoc loggername, String JavaDoc resourceBundleName)
154              {
155         return new LoggerInfo((TopicalLogger)
156                 mf.getLogger(loggername, resourceBundleName));
157     }
158
159     public String JavaDoc getResourceBundleName() {
160         return mf.getResourceBundleName();
161     }
162
163     public void setResourceBundleName(String JavaDoc resourceBundleName)
164              {
165         mf.setResourceBundleName(resourceBundleName);
166     }
167
168     public LoggerInfo[] getLoggers() {
169         Logger[] ls = mf.getLoggers();
170         LoggerInfo[] lis = new LoggerInfo[ls.length];
171         for(int i=0; i<ls.length; i++) {
172             lis[i] = new LoggerInfo((TopicalLogger) ls[i]);
173         }
174         Arrays.sort(lis, new Comparator JavaDoc() {
175             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
176                 return ((LoggerInfo) o1).topics[0].compareTo(
177                         ((LoggerInfo) o2).topics[0]);
178             }
179         });
180         return lis;
181     }
182
183     public void addHandlerToLogger(String JavaDoc handlername, String JavaDoc loggerName)
184              {
185         Handler h = mf.getHandler(handlername);
186         if (h == null) {
187             return;
188         }
189         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
190         try {
191             l.addHandler(h);
192         } catch (Exception JavaDoc e) {
193             return;
194         }
195     }
196
197     public void removeHandlerFromLogger(String JavaDoc handlerName, String JavaDoc loggerName)
198              {
199         Handler h = mf.getHandler(handlerName);
200         if (h == null) {
201             return;
202         }
203         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
204         try {
205             l.removeHandler(h);
206         } catch (Exception JavaDoc e) {
207             return;
208         }
209     }
210
211      public void removeAllHandlersFromLogger(String JavaDoc loggerName) {
212         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
213         try {
214             l.removeAllHandlers();
215         } catch (Exception JavaDoc e) {
216             return;
217         }
218     }
219
220     public void setAdditivity(boolean a, String JavaDoc loggerName) {
221         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
222         l.setAdditivity(a);
223     }
224
225     /**
226      * It assigns the level to a logger
227      * @param level is the level to assign the logger
228      * @param loggerName is the name of logger which the level must be set.
229      */

230     public void setLoggerLevel(int level, String JavaDoc loggerName) {
231         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
232         l.setIntLevel(level);
233     }
234
235     /**
236      * It assigns the level to a logger
237      * @param level is the level to assign the logger
238      * @param loggerName is the name of logger which the level must be set.
239      */

240     public void setLoggerLevel(String JavaDoc levelName, String JavaDoc loggerName) {
241         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
242         Level level = mf.getLevel(levelName);
243         if (level != null) {
244             l.setLevel(level);
245         }
246     }
247
248
249     public void addTopicToLogger(String JavaDoc topic, String JavaDoc loggerName) {
250         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
251         try {
252             l.addTopic(topic);
253         } catch (Exception JavaDoc e) {
254             return;
255         }
256     }
257
258     public void removeTopicFromLogger(String JavaDoc topic, String JavaDoc loggerName) {
259         TopicalLogger l = (TopicalLogger) mf.getLogger(loggerName);
260         try {
261             l.removeTopic(topic);
262         } catch (Exception JavaDoc e) {
263             return;
264         }
265     }
266
267     public Properties JavaDoc getMonologProperties() {
268         Properties JavaDoc p = new Properties JavaDoc();
269         try {
270             PropertiesConfAccess.store(p, mf);
271         } catch (Exception JavaDoc e) {
272             return null;
273         }
274         return p;
275     }
276     
277     public void setMonologProperties(Properties JavaDoc p) {
278         try {
279             PropertiesConfAccess.load(p, mf);
280         } catch (Exception JavaDoc e) {
281         }
282     }
283     
284 }
285
Popular Tags