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;
21 import org.apache.log4j.Appender;
22 import org.apache.log4j.Category;
23 import org.apache.log4j.Level;
24 import org.apache.log4j.LogManager;
25 import org.apache.log4j.Logger;
26 import org.apache.log4j.helpers.OptionConverter;
27 import org.apache.log4j.spi.HierarchyEventListener;
28 import org.apache.log4j.spi.LoggerRepository;
30 import javax.management.Attribute;
31 import javax.management.AttributeNotFoundException;
32 import javax.management.InvalidAttributeValueException;
33 import javax.management.JMException;
34 import javax.management.ListenerNotFoundException;
35 import javax.management.MBeanAttributeInfo;
36 import javax.management.MBeanConstructorInfo;
37 import javax.management.MBeanException;
38 import javax.management.MBeanInfo;
39 import javax.management.MBeanNotificationInfo;
40 import javax.management.MBeanOperationInfo;
41 import javax.management.MBeanParameterInfo;
42 import javax.management.Notification;
43 import javax.management.NotificationBroadcaster;
44 import javax.management.NotificationBroadcasterSupport;
45 import javax.management.NotificationFilter;
46 import javax.management.NotificationFilterSupport;
47 import javax.management.NotificationListener;
48 import javax.management.ObjectName;
49 import javax.management.ReflectionException;
50 import javax.management.RuntimeOperationsException;
51 import java.lang.reflect.Constructor;
52 import java.util.Vector;
54 public class HierarchyDynamicMBean extends AbstractDynamicMBean
55 implements HierarchyEventListener,
56 NotificationBroadcaster {
58 static final String ADD_APPENDER = "addAppender.";
59 static final String THRESHOLD = "threshold";
61 private MBeanConstructorInfo[] dConstructors = new MBeanConstructorInfo[1];
62 private MBeanOperationInfo[] dOperations = new MBeanOperationInfo[1];
64 private Vector vAttributes = new Vector();
65 private String dClassName = this.getClass().getName();
66 private String dDescription =
67 "This MBean acts as a management facade for org.apache.log4j.Hierarchy.";
69 private NotificationBroadcasterSupport nbs = new NotificationBroadcasterSupport();
72 private LoggerRepository hierarchy;
74 private static Logger log = Logger.getLogger(HierarchyDynamicMBean.class);
76 public HierarchyDynamicMBean() {
77 hierarchy = LogManager.getLoggerRepository();
78 buildDynamicMBeanInfo();
82 void buildDynamicMBeanInfo() {
83 Constructor[] constructors = this.getClass().getConstructors();
84 dConstructors[0] = new MBeanConstructorInfo(
85 "HierarchyDynamicMBean(): Constructs a HierarchyDynamicMBean instance",
88 vAttributes.add(new MBeanAttributeInfo(THRESHOLD,
90 "The \"threshold\" state of the hiearchy.",
95 MBeanParameterInfo[] params = new MBeanParameterInfo[1];
96 params[0] = new MBeanParameterInfo("name", "java.lang.String",
97 "Create a logger MBean" );
98 dOperations[0] = new MBeanOperationInfo("addLoggerMBean",
99 "addLoggerMBean(): add a loggerMBean",
101 "javax.management.ObjectName",
102 MBeanOperationInfo.ACTION);
107 ObjectName addLoggerMBean(String name) {
108 Logger cat = LogManager.exists(name);
111 return addLoggerMBean(cat);
117 ObjectName addLoggerMBean(Logger logger) {
118 String name = logger.getName();
119 ObjectName objectName = null;
121 LoggerDynamicMBean loggerMBean = new LoggerDynamicMBean(logger);
122 objectName = new ObjectName("log4j", "logger", name);
124 if (!server.isRegistered(objectName)) {
125 registerMBean(loggerMBean, objectName);
126 NotificationFilterSupport nfs = new NotificationFilterSupport();
127 nfs.enableType(ADD_APPENDER + logger.getName());
128 log.debug("---Adding logger [" + name + "] as listener.");
129 nbs.addNotificationListener(loggerMBean, nfs, null);
130 vAttributes.add(new MBeanAttributeInfo("logger=" + name, "javax.management.ObjectName",
131 "The " + name + " logger.", true, true, // this makes the object
137 } catch(JMException e) {
138 log.error("Could not add loggerMBean for ["+name+"].", e);
139 } catch(RuntimeException e) {
140 log.error("Could not add loggerMBean for ["+name+"].", e);
146 void addNotificationListener(NotificationListener listener,
147 NotificationFilter filter,
148 java.lang.Object handback) {
149 nbs.addNotificationListener(listener, filter, handback);
158 MBeanInfo getMBeanInfo() {
159 //cat.debug("getMBeanInfo called.");
161 MBeanAttributeInfo[] attribs = new MBeanAttributeInfo[vAttributes.size()];
162 vAttributes.toArray(attribs);
164 return new MBeanInfo(dClassName,
169 new MBeanNotificationInfo[0]);
173 MBeanNotificationInfo[] getNotificationInfo(){
174 return nbs.getNotificationInfo();
178 Object invoke(String operationName,
180 String signature[]) throws MBeanException,
181 ReflectionException {
183 if (operationName == null) {
184 throw new RuntimeOperationsException(
185 new IllegalArgumentException("Operation name cannot be null"),
186 "Cannot invoke a null operation in " + dClassName);
188 // Check for a recognized operation name and call the corresponding operation
190 if(operationName.equals("addLoggerMBean")) {
191 return addLoggerMBean((String)params[0]);
193 throw new ReflectionException(
194 new NoSuchMethodException(operationName),
195 "Cannot find the operation " + operationName + " in " + dClassName);
202 Object getAttribute(String attributeName) throws AttributeNotFoundException,
204 ReflectionException {
206 // Check attributeName is not null to avoid NullPointerException later on
207 if (attributeName == null) {
208 throw new RuntimeOperationsException(new IllegalArgumentException(
209 "Attribute name cannot be null"),
210 "Cannot invoke a getter of " + dClassName + " with null attribute name");
213 log.debug("Called getAttribute with ["+attributeName+"].");
215 // Check for a recognized attributeName and call the corresponding getter
216 if (attributeName.equals(THRESHOLD)) {
217 return hierarchy.getThreshold();
218 } else if(attributeName.startsWith("logger")) {
219 int k = attributeName.indexOf("%3D");
220 String val = attributeName;
222 val = attributeName.substring(0, k)+'='+ attributeName.substring(k+3);
225 return new ObjectName("log4j:"+val);
226 } catch(JMException e) {
227 log.error("Could not create ObjectName" + val);
228 } catch(RuntimeException e) {
229 log.error("Could not create ObjectName" + val);
235 // If attributeName has not been recognized throw an AttributeNotFoundException
236 throw(new AttributeNotFoundException("Cannot find " + attributeName +
237 " attribute in " + dClassName));
243 void addAppenderEvent(Category logger, Appender appender) {
244 log.debug("addAppenderEvent called: logger="+logger.getName()+
245 ", appender="+appender.getName());
246 Notification n = new Notification(ADD_APPENDER+logger.getName(), this, 0);
247 n.setUserData(appender);
248 log.debug("sending notification.");
249 nbs.sendNotification(n);
253 void removeAppenderEvent(Category cat, Appender appender) {
254 log.debug("removeAppenderCalled: logger="+cat.getName()+
255 ", appender="+appender.getName());
259 void postRegister(java.lang.Boolean registrationDone) {
260 log.debug("postRegister is called.");
261 hierarchy.addHierarchyEventListener(this);
262 Logger root = hierarchy.getRootLogger();
263 addLoggerMBean(root);
267 void removeNotificationListener(NotificationListener listener)
268 throws ListenerNotFoundException {
269 nbs.removeNotificationListener(listener);
273 void setAttribute(Attribute attribute) throws AttributeNotFoundException,
274 InvalidAttributeValueException,
276 ReflectionException {
278 // Check attribute is not null to avoid NullPointerException later on
279 if (attribute == null) {
280 throw new RuntimeOperationsException(
281 new IllegalArgumentException("Attribute cannot be null"),
282 "Cannot invoke a setter of "+dClassName+" with null attribute");
284 String name = attribute.getName();
285 Object value = attribute.getValue();
288 throw new RuntimeOperationsException(
289 new IllegalArgumentException("Attribute name cannot be null"),
290 "Cannot invoke the setter of "+dClassName+
291 " with null attribute name");
294 if(name.equals(THRESHOLD)) {
295 Level l = OptionConverter.toLevel((String) value,
296 hierarchy.getThreshold());
297 hierarchy.setThreshold(l);