2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.log4j.jmx;
20 import org.apache.log4j.Appender;
21 import org.apache.log4j.Level;
22 import org.apache.log4j.Logger;
23 import org.apache.log4j.helpers.OptionConverter;
25 import javax.management.Attribute;
26 import javax.management.AttributeNotFoundException;
27 import javax.management.InvalidAttributeValueException;
28 import javax.management.JMException;
29 import javax.management.MBeanAttributeInfo;
30 import javax.management.MBeanConstructorInfo;
31 import javax.management.MBeanException;
32 import javax.management.MBeanInfo;
33 import javax.management.MBeanNotificationInfo;
34 import javax.management.MBeanOperationInfo;
35 import javax.management.MBeanParameterInfo;
36 import javax.management.MalformedObjectNameException;
37 import javax.management.Notification;
38 import javax.management.NotificationListener;
39 import javax.management.ObjectName;
40 import javax.management.ReflectionException;
41 import javax.management.RuntimeOperationsException;
42 import java.lang.reflect.Constructor;
43 import java.util.Enumeration;
44 import java.util.Vector;
46 public class LoggerDynamicMBean extends AbstractDynamicMBean
47 implements NotificationListener {
49 private MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[1];
50 private MBeanOperationInfo[] dOperations = new MBeanOperationInfo[1];
52 private Vector dAttributes = new Vector();
53 private String dClassName = this.getClass().getName();
55 private String dDescription =
56 "This MBean acts as a management facade for a org.apache.log4j.Logger instance.";
58 // This Logger instance is for logging.
59 private static Logger cat = Logger.getLogger(LoggerDynamicMBean.class);
61 // We wrap this Logger instance.
62 private Logger logger;
64 public LoggerDynamicMBean(Logger logger) {
66 buildDynamicMBeanInfo();
70 void handleNotification(Notification notification, Object handback) {
71 cat.debug("Received notification: "+notification.getType());
72 registerAppenderMBean((Appender) notification.getUserData() );
78 void buildDynamicMBeanInfo() {
79 Constructor[] constructors = this.getClass().getConstructors();
80 dConstructors[0] = new MBeanConstructorInfo(
81 "HierarchyDynamicMBean(): Constructs a HierarchyDynamicMBean instance",
84 dAttributes.add(new MBeanAttributeInfo("name",
86 "The name of this Logger.",
91 dAttributes.add(new MBeanAttributeInfo("priority",
93 "The priority of this logger.",
102 MBeanParameterInfo[] params = new MBeanParameterInfo[2];
103 params[0] = new MBeanParameterInfo("class name", "java.lang.String",
104 "add an appender to this logger");
105 params[1] = new MBeanParameterInfo("appender name", "java.lang.String",
106 "name of the appender");
108 dOperations[0] = new MBeanOperationInfo("addAppender",
109 "addAppender(): add an appender",
112 MBeanOperationInfo.ACTION);
122 MBeanInfo getMBeanInfo() {
123 //cat.debug("getMBeanInfo called.");
125 MBeanAttributeInfo[] attribs = new MBeanAttributeInfo[dAttributes.size()];
126 dAttributes.toArray(attribs);
128 MBeanInfo mb = new MBeanInfo(dClassName,
133 new MBeanNotificationInfo[0]);
134 //cat.debug("getMBeanInfo exit.");
139 Object invoke(String operationName, Object params[], String signature[])
140 throws MBeanException,
141 ReflectionException {
143 if(operationName.equals("addAppender")) {
144 addAppender((String) params[0], (String) params[1]);
145 return "Hello world.";
153 Object getAttribute(String attributeName) throws AttributeNotFoundException,
155 ReflectionException {
157 // Check attributeName is not null to avoid NullPointerException later on
158 if (attributeName == null) {
159 throw new RuntimeOperationsException(new IllegalArgumentException(
160 "Attribute name cannot be null"),
161 "Cannot invoke a getter of " + dClassName + " with null attribute name");
164 // Check for a recognized attributeName and call the corresponding getter
165 if (attributeName.equals("name")) {
166 return logger.getName();
167 } else if(attributeName.equals("priority")) {
168 Level l = logger.getLevel();
174 } else if(attributeName.startsWith("appender=")) {
176 return new ObjectName("log4j:"+attributeName );
177 } catch(MalformedObjectNameException e) {
178 cat.error("Could not create ObjectName" + attributeName);
179 } catch(RuntimeException e) {
180 cat.error("Could not create ObjectName" + attributeName);
185 // If attributeName has not been recognized throw an AttributeNotFoundException
186 throw(new AttributeNotFoundException("Cannot find " + attributeName +
187 " attribute in " + dClassName));
192 void addAppender(String appenderClass, String appenderName) {
193 cat.debug("addAppender called with "+appenderClass+", "+appenderName);
194 Appender appender = (Appender)
195 OptionConverter.instantiateByClassName(appenderClass,
196 org.apache.log4j.Appender.class,
198 appender.setName(appenderName);
199 logger.addAppender(appender);
201 //appenderMBeanRegistration();
207 void setAttribute(Attribute attribute) throws AttributeNotFoundException,
208 InvalidAttributeValueException,
210 ReflectionException {
212 // Check attribute is not null to avoid NullPointerException later on
213 if (attribute == null) {
214 throw new RuntimeOperationsException(
215 new IllegalArgumentException("Attribute cannot be null"),
216 "Cannot invoke a setter of " + dClassName +
217 " with null attribute");
219 String name = attribute.getName();
220 Object value = attribute.getValue();
223 throw new RuntimeOperationsException(
224 new IllegalArgumentException("Attribute name cannot be null"),
225 "Cannot invoke the setter of "+dClassName+
226 " with null attribute name");
230 if(name.equals("priority")) {
231 if (value instanceof String) {
232 String s = (String) value;
233 Level p = logger.getLevel();
234 if(s.equalsIgnoreCase("NULL")) {
237 p = OptionConverter.toLevel(s, p);
242 throw(new AttributeNotFoundException("Attribute " + name +
244 this.getClass().getName()));
248 void appenderMBeanRegistration() {
249 Enumeration enumeration = logger.getAllAppenders();
250 while(enumeration.hasMoreElements()) {
251 Appender appender = (Appender) enumeration.nextElement();
252 registerAppenderMBean(appender);
256 void registerAppenderMBean(Appender appender) {
257 String name = getAppenderName(appender);
258 cat.debug("Adding AppenderMBean for appender named "+name);
259 ObjectName objectName = null;
261 AppenderDynamicMBean appenderMBean = new AppenderDynamicMBean(appender);
262 objectName = new ObjectName("log4j", "appender", name);
263 if (!server.isRegistered(objectName)) {
264 registerMBean(appenderMBean, objectName);
265 dAttributes.add(new MBeanAttributeInfo("appender=" + name, "javax.management.ObjectName",
266 "The " + name + " appender.", true, true, false));
269 } catch(JMException e) {
270 cat.error("Could not add appenderMBean for ["+name+"].", e);
271 } catch(java.beans.IntrospectionException e) {
272 cat.error("Could not add appenderMBean for ["+name+"].", e);
273 } catch(RuntimeException e) {
274 cat.error("Could not add appenderMBean for ["+name+"].", e);
279 void postRegister(java.lang.Boolean registrationDone) {
280 appenderMBeanRegistration();