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

LoggerDynamicMBean

public class LoggerDynamicMBean extends AbstractDynamicMBean implements NotificationListener

Fields Summary
private MBeanConstructorInfo[]
dConstructors
private MBeanOperationInfo[]
dOperations
private Vector
dAttributes
private String
dClassName
private String
dDescription
private static Logger
cat
private Logger
logger
Constructors Summary
public LoggerDynamicMBean(Logger logger)


     
    this.logger = logger;
    buildDynamicMBeanInfo();
  
Methods Summary
voidaddAppender(java.lang.String appenderClass, java.lang.String appenderName)

    cat.debug("addAppender called with "+appenderClass+", "+appenderName);
    Appender appender = (Appender)
       OptionConverter.instantiateByClassName(appenderClass,
					      org.apache.log4j.Appender.class,
					      null);
    appender.setName(appenderName);
    logger.addAppender(appender);

    //appenderMBeanRegistration();

  
voidappenderMBeanRegistration()

    Enumeration enumeration = logger.getAllAppenders();
    while(enumeration.hasMoreElements()) {
      Appender appender = (Appender) enumeration.nextElement();
      registerAppenderMBean(appender);
    }
  
private voidbuildDynamicMBeanInfo()

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

    dAttributes.add(new MBeanAttributeInfo("name",
					   "java.lang.String",
					   "The name of this Logger.",
					   true,
					   false,
					   false));

    dAttributes.add(new MBeanAttributeInfo("priority",
					   "java.lang.String",
					   "The priority of this logger.",
					   true,
					   true,
					   false));





    MBeanParameterInfo[] params = new MBeanParameterInfo[2];
    params[0] = new MBeanParameterInfo("class name", "java.lang.String",
				       "add an appender to this logger");
    params[1] = new MBeanParameterInfo("appender name", "java.lang.String",
				       "name of the appender");

    dOperations[0] = new MBeanOperationInfo("addAppender",
					    "addAppender(): add an appender",
					    params,
					    "void",
					    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");
    }

    // Check for a recognized attributeName and call the corresponding getter
    if (attributeName.equals("name")) {
      return logger.getName();
    }  else if(attributeName.equals("priority")) {
      Level l = logger.getLevel();
      if(l == null) {
	return null;
      } else {
	return l.toString();
      }
    } else if(attributeName.startsWith("appender=")) {
      try {
	return new ObjectName("log4j:"+attributeName );
      } catch(Exception e) {
	cat.error("Could not create ObjectName" + attributeName);
      }
    }


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

  
protected org.apache.log4j.LoggergetLogger()

    return logger;
  
public javax.management.MBeanInfogetMBeanInfo()

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

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

    MBeanInfo mb = new MBeanInfo(dClassName,
			 dDescription,
			 attribs,
			 dConstructors,
			 dOperations,
			 new MBeanNotificationInfo[0]);
    //cat.debug("getMBeanInfo exit.");
    return mb;
  
public voidhandleNotification(javax.management.Notification notification, java.lang.Object handback)

    cat.debug("Received notification: "+notification.getType());
    registerAppenderMBean((Appender) notification.getUserData() );


  
public java.lang.Objectinvoke(java.lang.String operationName, java.lang.Object[] params, java.lang.String[] signature)


    if(operationName.equals("addAppender")) {
      addAppender((String) params[0], (String) params[1]);
      return "Hello world.";
    }

    return null;
  
public voidpostRegister(java.lang.Boolean registrationDone)

    appenderMBeanRegistration();
  
voidregisterAppenderMBean(org.apache.log4j.Appender appender)

    String name = appender.getName();
    cat.debug("Adding AppenderMBean for appender named "+name);
    ObjectName objectName = null;
    try {
      AppenderDynamicMBean appenderMBean = new AppenderDynamicMBean(appender);
      objectName = new ObjectName("log4j", "appender", name);
      if (!server.isRegistered(objectName)) {
        server.registerMBean(appenderMBean, objectName);
        dAttributes.add(new MBeanAttributeInfo("appender=" + name, "javax.management.ObjectName",
                "The " + name + " appender.", true, true, false));
      }

    } catch(Exception e) {
      cat.error("Could not add appenderMBean for ["+name+"].", e);
    }
  
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("priority")) {
      if (value instanceof String) {
	String s = (String) value;
	Level p = logger.getLevel();
	if(s.equalsIgnoreCase("NULL")) {
	  p = null;
	} else {
	  p = OptionConverter.toLevel(s, p);
	}
	logger.setLevel(p);
      }
    } else {
      throw(new AttributeNotFoundException("Attribute " + name +
					   " not found in " +
					   this.getClass().getName()));
    }