KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > management > log4j > LoggerMBean


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.management.log4j;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.management.Attribute JavaDoc;
22 import javax.management.AttributeNotFoundException JavaDoc;
23 import javax.management.InvalidAttributeValueException JavaDoc;
24 import javax.management.MBeanAttributeInfo JavaDoc;
25 import javax.management.MBeanConstructorInfo JavaDoc;
26 import javax.management.MBeanException JavaDoc;
27 import javax.management.MBeanInfo JavaDoc;
28 import javax.management.MBeanNotificationInfo JavaDoc;
29 import javax.management.MBeanOperationInfo JavaDoc;
30 import javax.management.MBeanParameterInfo JavaDoc;
31 import javax.management.Notification JavaDoc;
32 import javax.management.NotificationListener JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.ReflectionException JavaDoc;
35 import javax.management.RuntimeOperationsException JavaDoc;
36
37 import org.apache.hivemind.management.mbeans.AbstractDynamicMBean;
38 import org.apache.log4j.Appender;
39 import org.apache.log4j.Level;
40 import org.apache.log4j.Logger;
41 import org.apache.log4j.helpers.OptionConverter;
42 import org.apache.log4j.jmx.AppenderDynamicMBean;
43
44 /**
45  * MBean for the management of a Log4j logger. Allows to change the level and add appenders. This is
46  * a copy of the {@link org.apache.log4j.jmx.LoggerDynamicMBean} from the log4 library. The copy was
47  * made to fix an issue with jboss 3.2.7, that don't accept spaces in attribute names. If somebody
48  * feels that such a copy from one apache project to another is not ok, please tell me.
49  *
50  * @author Achim Huegen
51  */

52 public class LoggerMBean extends AbstractDynamicMBean implements NotificationListener JavaDoc
53 {
54
55     private MBeanConstructorInfo JavaDoc[] _constructors = new MBeanConstructorInfo JavaDoc[0];
56
57     private MBeanOperationInfo JavaDoc[] _operations = new MBeanOperationInfo JavaDoc[1];
58
59     private List JavaDoc _attributes = new ArrayList JavaDoc();
60
61     private String JavaDoc _className = this.getClass().getName();
62
63     private String JavaDoc _description = "This MBean acts as a management facade for a org.apache.log4j.Logger instance.";
64
65     // This Logger instance is for logging.
66
private static Logger _log = Logger.getLogger(LoggerMBean.class);
67
68     // We wrap this Logger instance.
69
private Logger _logger;
70
71     public LoggerMBean(Logger logger)
72     {
73         this._logger = logger;
74         buildDynamicMBeanInfo();
75     }
76
77     public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
78     {
79         _log.debug("Received notification: " + notification.getType());
80         registerAppenderMBean((Appender) notification.getUserData());
81
82     }
83
84     private void buildDynamicMBeanInfo()
85     {
86         _attributes.add(new MBeanAttributeInfo JavaDoc("name", "java.lang.String",
87                 "The name of this Logger.", true, false, false));
88
89         _attributes.add(new MBeanAttributeInfo JavaDoc("priority", "java.lang.String",
90                 "The priority of this logger.", true, true, false));
91
92         MBeanParameterInfo JavaDoc[] params = new MBeanParameterInfo JavaDoc[2];
93         params[0] = new MBeanParameterInfo JavaDoc("class_name", "java.lang.String",
94                 "add an appender to this logger");
95         params[1] = new MBeanParameterInfo JavaDoc("appender_name", "java.lang.String",
96                 "name of the appender");
97
98         _operations[0] = new MBeanOperationInfo JavaDoc("addAppender", "addAppender(): add an appender",
99                 params, "void", MBeanOperationInfo.ACTION);
100     }
101
102     protected Logger getLogger()
103     {
104         return _logger;
105     }
106
107     public MBeanInfo JavaDoc getMBeanInfo()
108     {
109         MBeanAttributeInfo JavaDoc[] attribs = new MBeanAttributeInfo JavaDoc[_attributes.size()];
110         _attributes.toArray(attribs);
111
112         MBeanInfo JavaDoc mb = new MBeanInfo JavaDoc(_className, _description, attribs, _constructors, _operations,
113                 new MBeanNotificationInfo JavaDoc[0]);
114         // cat.debug("getMBeanInfo exit.");
115
return mb;
116     }
117
118     public Object JavaDoc invoke(String JavaDoc operationName, Object JavaDoc params[], String JavaDoc signature[])
119             throws MBeanException JavaDoc, ReflectionException JavaDoc
120     {
121
122         if (operationName.equals("addAppender"))
123         {
124             addAppender((String JavaDoc) params[0], (String JavaDoc) params[1]);
125             return "Hello world.";
126         }
127
128         return null;
129     }
130
131     public Object JavaDoc getAttribute(String JavaDoc attributeName) throws AttributeNotFoundException JavaDoc,
132             MBeanException JavaDoc, ReflectionException JavaDoc
133     {
134
135         // Check attributeName is not null to avoid NullPointerException later on
136
if (attributeName == null)
137         {
138             throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
139                     "Attribute name cannot be null"), "Cannot invoke a getter of " + _className
140                     + " with null attribute name");
141         }
142
143         // Check for a recognized attributeName and call the corresponding getter
144
if (attributeName.equals("name"))
145         {
146             return _logger.getName();
147         }
148         else if (attributeName.equals("priority"))
149         {
150             Level l = _logger.getLevel();
151             if (l == null)
152                 return null;
153
154             return l.toString();
155         }
156         else if (attributeName.startsWith("appender="))
157         {
158             try
159             {
160                 return new ObjectName JavaDoc("log4j:" + attributeName);
161             }
162             catch (Exception JavaDoc e)
163             {
164                 _log.error("Could not create ObjectName" + attributeName);
165             }
166         }
167
168         // If attributeName has not been recognized throw an AttributeNotFoundException
169
throw (new AttributeNotFoundException JavaDoc("Cannot find " + attributeName + " attribute in "
170                 + _className));
171
172     }
173
174     void addAppender(String JavaDoc appenderClass, String JavaDoc appenderName)
175     {
176         _log.debug("addAppender called with " + appenderClass + ", " + appenderName);
177         Appender appender = (Appender) OptionConverter.instantiateByClassName(
178                 appenderClass,
179                 org.apache.log4j.Appender.class,
180                 null);
181         appender.setName(appenderName);
182         _logger.addAppender(appender);
183
184     }
185
186     public void setAttribute(Attribute JavaDoc attribute) throws AttributeNotFoundException JavaDoc,
187             InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
188     {
189
190         // Check attribute is not null to avoid NullPointerException later on
191
if (attribute == null)
192         {
193             throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
194                     "Attribute cannot be null"), "Cannot invoke a setter of " + _className
195                     + " with null attribute");
196         }
197         String JavaDoc name = attribute.getName();
198         Object JavaDoc value = attribute.getValue();
199
200         if (name == null)
201         {
202             throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
203                     "Attribute name cannot be null"), "Cannot invoke the setter of " + _className
204                     + " with null attribute name");
205         }
206
207         if (name.equals("priority"))
208         {
209             if (value instanceof String JavaDoc)
210             {
211                 String JavaDoc s = (String JavaDoc) value;
212                 Level p = _logger.getLevel();
213                 if (s.equalsIgnoreCase("NULL"))
214                 {
215                     p = null;
216                 }
217                 else
218                 {
219                     p = OptionConverter.toLevel(s, p);
220                 }
221                 _logger.setLevel(p);
222             }
223         }
224         else
225         {
226             throw (new AttributeNotFoundException JavaDoc("Attribute " + name + " not found in "
227                     + this.getClass().getName()));
228         }
229     }
230
231     void appenderMBeanRegistration()
232     {
233         Enumeration JavaDoc enumeration = _logger.getAllAppenders();
234         while (enumeration.hasMoreElements())
235         {
236             Appender appender = (Appender) enumeration.nextElement();
237             registerAppenderMBean(appender);
238         }
239     }
240
241     /**
242      * Register a mbean for an appender.
243      *
244      * @param appender
245      */

246     void registerAppenderMBean(Appender appender)
247     {
248         String JavaDoc name = appender.getName();
249         _log.debug("Adding AppenderMBean for appender named " + name);
250         ObjectName JavaDoc objectName = null;
251         try
252         {
253             objectName = new ObjectName JavaDoc("log4j", "appender", name);
254             // register appender as mbean if not already existing
255
if (!getMBeanServer().isRegistered(objectName))
256             {
257                 AppenderDynamicMBean appenderMBean = new AppenderDynamicMBean(appender);
258                 getMBeanServer().registerMBean(appenderMBean, objectName);
259
260                 _attributes.add(new MBeanAttributeInfo JavaDoc("appender=" + name,
261                         "javax.management.ObjectName", "The " + name + " appender.", true, true,
262                         false));
263             }
264
265         }
266         catch (Exception JavaDoc e)
267         {
268             _log.error("Could not add appenderMBean for [" + name + "].", e);
269         }
270     }
271
272     public void postRegister(java.lang.Boolean JavaDoc registrationDone)
273     {
274         appenderMBeanRegistration();
275     }
276 }
277
Popular Tags