FileDocCategorySizeDatePackage
HierarchyDynamicMBean.javaAPI DocApache log4j 1.2.159855Sat Aug 25 00:09:40 BST 2007org.apache.log4j.jmx

HierarchyDynamicMBean

public class HierarchyDynamicMBean extends AbstractDynamicMBean implements NotificationBroadcaster, HierarchyEventListener

Fields Summary
static final String
ADD_APPENDER
static final String
THRESHOLD
private MBeanConstructorInfo[]
dConstructors
private MBeanOperationInfo[]
dOperations
private Vector
vAttributes
private String
dClassName
private String
dDescription
private NotificationBroadcasterSupport
nbs
private LoggerRepository
hierarchy
private static Logger
log
Constructors Summary
public HierarchyDynamicMBean()


    
    hierarchy = LogManager.getLoggerRepository();
    buildDynamicMBeanInfo();
  
Methods Summary
public voidaddAppenderEvent(org.apache.log4j.Category logger, org.apache.log4j.Appender appender)

    log.debug("addAppenderEvent called: logger="+logger.getName()+
	      ", appender="+appender.getName());
    Notification n = new Notification(ADD_APPENDER+logger.getName(), this, 0);
    n.setUserData(appender);
    log.debug("sending notification.");
    nbs.sendNotification(n);
  
public javax.management.ObjectNameaddLoggerMBean(java.lang.String name)

    Logger cat = LogManager.exists(name);

    if(cat != null) {
      return addLoggerMBean(cat);
    } else {
      return null;
    }
  
javax.management.ObjectNameaddLoggerMBean(org.apache.log4j.Logger logger)

    String name = logger.getName();
    ObjectName objectName = null;
    try {
      LoggerDynamicMBean loggerMBean = new LoggerDynamicMBean(logger);
      objectName = new ObjectName("log4j", "logger", name);
      
      if (!server.isRegistered(objectName)) {
        server.registerMBean(loggerMBean, objectName);
        NotificationFilterSupport nfs = new NotificationFilterSupport();
        nfs.enableType(ADD_APPENDER + logger.getName());
        log.debug("---Adding logger [" + name + "] as listener.");
        nbs.addNotificationListener(loggerMBean, nfs, null);
        vAttributes.add(new MBeanAttributeInfo("logger=" + name, "javax.management.ObjectName",
                "The " + name + " logger.", true, true, // this makes the object
                // clickable
                false));
        
      }

    } catch(Exception e) {
      log.error("Could not add loggerMBean for ["+name+"].", e);
    }
    return objectName;
  
public voidaddNotificationListener(javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback)

    nbs.addNotificationListener(listener, filter, handback);
  
private voidbuildDynamicMBeanInfo()

    Constructor[] constructors = this.getClass().getConstructors();
    dConstructors[0] = new MBeanConstructorInfo(
         "HierarchyDynamicMBean(): Constructs a HierarchyDynamicMBean instance",
	 constructors[0]);

    vAttributes.add(new MBeanAttributeInfo(THRESHOLD,
					   "java.lang.String",
					   "The \"threshold\" state of the hiearchy.",
					   true,
					   true,
					   false));

    MBeanParameterInfo[] params = new MBeanParameterInfo[1];
    params[0] = new MBeanParameterInfo("name", "java.lang.String",
				       "Create a logger MBean" );
    dOperations[0] = new MBeanOperationInfo("addLoggerMBean",
				    "addLoggerMBean(): add a loggerMBean",
				    params ,
				    "javax.management.ObjectName",
				    MBeanOperationInfo.ACTION);
  
public java.lang.ObjectgetAttribute(java.lang.String attributeName)


    // Check attributeName is not null to avoid NullPointerException later on
    if (attributeName == null) {
      throw new RuntimeOperationsException(new IllegalArgumentException(
			"Attribute name cannot be null"),
       "Cannot invoke a getter of " + dClassName + " with null attribute name");
    }

    log.debug("Called getAttribute with ["+attributeName+"].");

    // Check for a recognized attributeName and call the corresponding getter
    if (attributeName.equals(THRESHOLD)) {
      return hierarchy.getThreshold();
    } else if(attributeName.startsWith("logger")) {
      int k = attributeName.indexOf("%3D");
      String val = attributeName;
      if(k > 0) {
	val = attributeName.substring(0, k)+'="+ attributeName.substring(k+3);
      }
      try {
	return new ObjectName("log4j:"+val);
      } catch(Exception e) {
	log.error("Could not create ObjectName" + val);
      }
    }



    // If attributeName has not been recognized throw an AttributeNotFoundException
    throw(new AttributeNotFoundException("Cannot find " + attributeName +
					 " attribute in " + dClassName));

  
protected org.apache.log4j.LoggergetLogger()

    return log;
  
public javax.management.MBeanInfogetMBeanInfo()

    //cat.debug("getMBeanInfo called.");

    MBeanAttributeInfo[] attribs = new MBeanAttributeInfo[vAttributes.size()];
    vAttributes.toArray(attribs);

    return new MBeanInfo(dClassName,
			 dDescription,
			 attribs,
			 dConstructors,
			 dOperations,
			 new MBeanNotificationInfo[0]);
  
public javax.management.MBeanNotificationInfo[]getNotificationInfo()

    return nbs.getNotificationInfo();
  
public java.lang.Objectinvoke(java.lang.String operationName, java.lang.Object[] params, java.lang.String[] signature)


    if (operationName == null) {
      throw new RuntimeOperationsException(
        new IllegalArgumentException("Operation name cannot be null"),
	"Cannot invoke a null operation in " + dClassName);
    }
    // Check for a recognized operation name and call the corresponding operation

    if(operationName.equals("addLoggerMBean")) {
      return addLoggerMBean((String)params[0]);
    } else {
      throw new ReflectionException(
	    new NoSuchMethodException(operationName),
	    "Cannot find the operation " + operationName + " in " + dClassName);
    }

  
public voidpostRegister(java.lang.Boolean registrationDone)

    log.debug("postRegister is called.");
    hierarchy.addHierarchyEventListener(this);
    Logger root = hierarchy.getRootLogger();
    addLoggerMBean(root);
  
public voidremoveAppenderEvent(org.apache.log4j.Category cat, org.apache.log4j.Appender appender)

    log.debug("removeAppenderCalled: logger="+cat.getName()+
	      ", appender="+appender.getName());
  
public voidremoveNotificationListener(javax.management.NotificationListener listener)

    nbs.removeNotificationListener(listener);
  
public voidsetAttribute(javax.management.Attribute attribute)


    // Check attribute is not null to avoid NullPointerException later on
    if (attribute == null) {
      throw new RuntimeOperationsException(
                  new IllegalArgumentException("Attribute cannot be null"),
	  "Cannot invoke a setter of "+dClassName+" with null attribute");
    }
    String name = attribute.getName();
    Object value = attribute.getValue();

    if (name == null) {
      throw new RuntimeOperationsException(
               new IllegalArgumentException("Attribute name cannot be null"),
	       "Cannot invoke the setter of "+dClassName+
	       " with null attribute name");
    }

    if(name.equals(THRESHOLD)) {
      Level l = OptionConverter.toLevel((String) value,
					   hierarchy.getThreshold());
      hierarchy.setThreshold(l);
    }