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

LayoutDynamicMBean

public class LayoutDynamicMBean extends AbstractDynamicMBean

Fields Summary
private MBeanConstructorInfo[]
dConstructors
private Vector
dAttributes
private String
dClassName
private Hashtable
dynamicProps
private MBeanOperationInfo[]
dOperations
private String
dDescription
private static Logger
cat
private Layout
layout
Constructors Summary
public LayoutDynamicMBean(Layout layout)


        
    this.layout = layout;
    buildDynamicMBeanInfo();
  
Methods Summary
private voidbuildDynamicMBeanInfo()

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


    BeanInfo bi = Introspector.getBeanInfo(layout.getClass());
    PropertyDescriptor[] pd = bi.getPropertyDescriptors();

    int size = pd.length;

    for(int i = 0; i < size; i++) {
      String name = pd[i].getName();
      Method readMethod =  pd[i].getReadMethod();
      Method writeMethod =  pd[i].getWriteMethod();
      if(readMethod != null) {
	Class returnClass = readMethod.getReturnType();
	if(isSupportedType(returnClass)) {
	  String returnClassName;
	  if(returnClass.isAssignableFrom(Level.class)) {
	    returnClassName = "java.lang.String";
	  } else {
	    returnClassName = returnClass.getName();
	  }

	  dAttributes.add(new MBeanAttributeInfo(name,
						 returnClassName,
						 "Dynamic",
						 true,
						 writeMethod != null,
						 false));
	  dynamicProps.put(name, new MethodUnion(readMethod, writeMethod));
	}
      }
    }

    MBeanParameterInfo[] params = new MBeanParameterInfo[0];

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


    MethodUnion mu = (MethodUnion) dynamicProps.get(attributeName);

    cat.debug("----name="+attributeName+", mu="+mu);

    if(mu != null && mu.readMethod != null) {
      try {
	return mu.readMethod.invoke(layout, null);
      } catch(Exception e) {
	return null;
      }
    }



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

  
protected org.apache.log4j.LoggergetLogger()

    return cat;
  
public javax.management.MBeanInfogetMBeanInfo()

    cat.debug("getMBeanInfo called.");

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

    return new MBeanInfo(dClassName,
			 dDescription,
			 attribs,
			 dConstructors,
			 dOperations,
			 new MBeanNotificationInfo[0]);
  
public java.lang.Objectinvoke(java.lang.String operationName, java.lang.Object[] params, java.lang.String[] signature)


    if(operationName.equals("activateOptions") &&
                     layout instanceof OptionHandler) {
      OptionHandler oh = (OptionHandler) layout;
      oh.activateOptions();
      return "Options activated.";
    }
    return null;
  
private booleanisSupportedType(java.lang.Class clazz)

    if(clazz.isPrimitive()) {
      return true;
    }

    if(clazz == String.class) {
      return true;
    }
    if(clazz.isAssignableFrom(Level.class)) {
      return true;
    }

    return false;
  
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");
    }



    MethodUnion mu = (MethodUnion) dynamicProps.get(name);

    if(mu != null && mu.writeMethod != null) {
      Object[] o = new Object[1];

      Class[] params = mu.writeMethod.getParameterTypes();
      if(params[0] == org.apache.log4j.Priority.class) {
	value = OptionConverter.toLevel((String) value,
					(Level) getAttribute(name));
      }
      o[0] = value;

      try {
	mu.writeMethod.invoke(layout,  o);

      } catch(Exception e) {
	cat.error("FIXME", e);
      }
    } else {
      throw(new AttributeNotFoundException("Attribute " + name +
					   " not found in " +
					   this.getClass().getName()));
    }