KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > jmx > LoggerDynamicMBean


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

16
17 package org.apache.log4j.jmx;
18
19 import java.lang.reflect.Constructor JavaDoc;
20 import org.apache.log4j.Logger;
21 import org.apache.log4j.Level;
22 import org.apache.log4j.Appender;
23 import org.apache.log4j.helpers.OptionConverter;
24
25 import java.util.Vector JavaDoc;
26 import java.util.Enumeration JavaDoc;
27
28 import javax.management.MBeanAttributeInfo JavaDoc;
29 import javax.management.MBeanConstructorInfo JavaDoc;
30 import javax.management.MBeanNotificationInfo JavaDoc;
31 import javax.management.MBeanOperationInfo JavaDoc;
32 import javax.management.MBeanParameterInfo JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.MBeanInfo JavaDoc;
35 import javax.management.Attribute JavaDoc;
36
37 import javax.management.MBeanException JavaDoc;
38 import javax.management.AttributeNotFoundException JavaDoc;
39 import javax.management.RuntimeOperationsException JavaDoc;
40 import javax.management.ReflectionException JavaDoc;
41 import javax.management.InvalidAttributeValueException JavaDoc;
42 import javax.management.NotificationListener JavaDoc;
43 import javax.management.Notification JavaDoc;
44
45 public class LoggerDynamicMBean extends AbstractDynamicMBean
46                                   implements NotificationListener JavaDoc {
47
48   private MBeanConstructorInfo JavaDoc[] dConstructors = new MBeanConstructorInfo JavaDoc[1];
49   private MBeanOperationInfo JavaDoc[] dOperations = new MBeanOperationInfo JavaDoc[1];
50
51   private Vector JavaDoc dAttributes = new Vector JavaDoc();
52   private String JavaDoc dClassName = this.getClass().getName();
53
54   private String JavaDoc dDescription =
55      "This MBean acts as a management facade for a org.apache.log4j.Logger instance.";
56
57   // This Logger instance is for logging.
58
private static Logger cat = Logger.getLogger(LoggerDynamicMBean.class);
59
60   // We wrap this Logger instance.
61
private Logger logger;
62
63   public LoggerDynamicMBean(Logger logger) {
64     this.logger = logger;
65     buildDynamicMBeanInfo();
66   }
67
68   public
69   void handleNotification(Notification JavaDoc notification, Object JavaDoc handback) {
70     cat.debug("Received notification: "+notification.getType());
71     registerAppenderMBean((Appender) notification.getUserData() );
72
73
74   }
75
76   private
77   void buildDynamicMBeanInfo() {
78     Constructor JavaDoc[] constructors = this.getClass().getConstructors();
79     dConstructors[0] = new MBeanConstructorInfo JavaDoc(
80              "HierarchyDynamicMBean(): Constructs a HierarchyDynamicMBean instance",
81          constructors[0]);
82
83     dAttributes.add(new MBeanAttributeInfo JavaDoc("name",
84                        "java.lang.String",
85                        "The name of this Logger.",
86                        true,
87                        false,
88                        false));
89
90     dAttributes.add(new MBeanAttributeInfo JavaDoc("priority",
91                        "java.lang.String",
92                        "The priority of this logger.",
93                        true,
94                        true,
95                        false));
96
97
98
99
100
101     MBeanParameterInfo JavaDoc[] params = new MBeanParameterInfo JavaDoc[2];
102     params[0] = new MBeanParameterInfo JavaDoc("class name", "java.lang.String",
103                        "add an appender to this logger");
104     params[1] = new MBeanParameterInfo JavaDoc("appender name", "java.lang.String",
105                        "name of the appender");
106
107     dOperations[0] = new MBeanOperationInfo JavaDoc("addAppender",
108                         "addAppender(): add an appender",
109                         params,
110                         "void",
111                         MBeanOperationInfo.ACTION);
112   }
113
114   protected
115   Logger getLogger() {
116     return logger;
117   }
118
119
120   public
121   MBeanInfo JavaDoc getMBeanInfo() {
122     //cat.debug("getMBeanInfo called.");
123

124     MBeanAttributeInfo JavaDoc[] attribs = new MBeanAttributeInfo JavaDoc[dAttributes.size()];
125     dAttributes.toArray(attribs);
126
127     MBeanInfo JavaDoc mb = new MBeanInfo JavaDoc(dClassName,
128              dDescription,
129              attribs,
130              dConstructors,
131              dOperations,
132              new MBeanNotificationInfo JavaDoc[0]);
133     //cat.debug("getMBeanInfo exit.");
134
return mb;
135   }
136
137   public
138   Object JavaDoc invoke(String JavaDoc operationName, Object JavaDoc params[], String JavaDoc signature[])
139     throws MBeanException JavaDoc,
140     ReflectionException JavaDoc {
141
142     if(operationName.equals("addAppender")) {
143       addAppender((String JavaDoc) params[0], (String JavaDoc) params[1]);
144       return "Hello world.";
145     }
146
147     return null;
148   }
149
150
151   public
152   Object JavaDoc getAttribute(String JavaDoc attributeName) throws AttributeNotFoundException JavaDoc,
153                                                    MBeanException JavaDoc,
154                                                    ReflectionException JavaDoc {
155
156        // Check attributeName is not null to avoid NullPointerException later on
157
if (attributeName == null) {
158       throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
159             "Attribute name cannot be null"),
160        "Cannot invoke a getter of " + dClassName + " with null attribute name");
161     }
162
163     // Check for a recognized attributeName and call the corresponding getter
164
if (attributeName.equals("name")) {
165       return logger.getName();
166     } else if(attributeName.equals("priority")) {
167       Level l = logger.getLevel();
168       if(l == null) {
169     return null;
170       } else {
171     return l.toString();
172       }
173     } else if(attributeName.startsWith("appender=")) {
174       try {
175     return new ObjectName JavaDoc("log4j:"+attributeName );
176       } catch(Exception JavaDoc e) {
177     cat.error("Could not create ObjectName" + attributeName);
178       }
179     }
180
181
182     // If attributeName has not been recognized throw an AttributeNotFoundException
183
throw(new AttributeNotFoundException JavaDoc("Cannot find " + attributeName +
184                      " attribute in " + dClassName));
185
186   }
187
188
189   void addAppender(String JavaDoc appenderClass, String JavaDoc appenderName) {
190     cat.debug("addAppender called with "+appenderClass+", "+appenderName);
191     Appender appender = (Appender)
192        OptionConverter.instantiateByClassName(appenderClass,
193                           org.apache.log4j.Appender.class,
194                           null);
195     appender.setName(appenderName);
196     logger.addAppender(appender);
197
198     //appenderMBeanRegistration();
199

200   }
201
202
203   public
204   void setAttribute(Attribute JavaDoc attribute) throws AttributeNotFoundException JavaDoc,
205                                                 InvalidAttributeValueException JavaDoc,
206                                                 MBeanException JavaDoc,
207                                                 ReflectionException JavaDoc {
208
209     // Check attribute is not null to avoid NullPointerException later on
210
if (attribute == null) {
211       throw new RuntimeOperationsException JavaDoc(
212                   new IllegalArgumentException JavaDoc("Attribute cannot be null"),
213           "Cannot invoke a setter of " + dClassName +
214           " with null attribute");
215     }
216     String JavaDoc name = attribute.getName();
217     Object JavaDoc value = attribute.getValue();
218
219     if (name == null) {
220       throw new RuntimeOperationsException JavaDoc(
221                     new IllegalArgumentException JavaDoc("Attribute name cannot be null"),
222             "Cannot invoke the setter of "+dClassName+
223             " with null attribute name");
224     }
225
226
227     if(name.equals("priority")) {
228       if (value instanceof String JavaDoc) {
229     String JavaDoc s = (String JavaDoc) value;
230     Level p = logger.getLevel();
231     if(s.equalsIgnoreCase("NULL")) {
232       p = null;
233     } else {
234       p = OptionConverter.toLevel(s, p);
235     }
236     logger.setLevel(p);
237       }
238     } else {
239       throw(new AttributeNotFoundException JavaDoc("Attribute " + name +
240                        " not found in " +
241                        this.getClass().getName()));
242     }
243   }
244
245   void appenderMBeanRegistration() {
246     Enumeration JavaDoc enumeration = logger.getAllAppenders();
247     while(enumeration.hasMoreElements()) {
248       Appender appender = (Appender) enumeration.nextElement();
249       registerAppenderMBean(appender);
250     }
251   }
252
253   void registerAppenderMBean(Appender appender) {
254     String JavaDoc name = appender.getName();
255     cat.debug("Adding AppenderMBean for appender named "+name);
256     ObjectName JavaDoc objectName = null;
257     try {
258       AppenderDynamicMBean appenderMBean = new AppenderDynamicMBean(appender);
259       objectName = new ObjectName JavaDoc("log4j", "appender", name);
260       server.registerMBean(appenderMBean, objectName);
261
262       dAttributes.add(new MBeanAttributeInfo JavaDoc("appender="+name,
263                          "javax.management.ObjectName",
264                          "The "+name+" appender.",
265                          true,
266                          true,
267                          false));
268
269     } catch(Exception JavaDoc e) {
270       cat.error("Could not add appenderMBean for ["+name+"].", e);
271     }
272   }
273
274   public
275   void postRegister(java.lang.Boolean JavaDoc registrationDone) {
276     appenderMBeanRegistration();
277   }
278 }
279
Popular Tags