AppenderDynamicMBeanpublic class AppenderDynamicMBean 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 Appender | appender |
Constructors Summary |
---|
public AppenderDynamicMBean(Appender appender)
this.appender = appender;
buildDynamicMBeanInfo();
|
Methods Summary |
---|
private void | buildDynamicMBeanInfo()
Constructor[] constructors = this.getClass().getConstructors();
dConstructors[0] = new MBeanConstructorInfo(
"AppenderDynamicMBean(): Constructs a AppenderDynamicMBean instance",
constructors[0]);
BeanInfo bi = Introspector.getBeanInfo(appender.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(Priority.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 appender",
params,
"void",
MBeanOperationInfo.ACTION);
params = new MBeanParameterInfo[1];
params[0] = new MBeanParameterInfo("layout class", "java.lang.String",
"layout class");
dOperations[1] = new MBeanOperationInfo("setLayout",
"setLayout(): add a layout",
params,
"void",
MBeanOperationInfo.ACTION);
| public java.lang.Object | getAttribute(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");
}
cat.debug("getAttribute called with ["+attributeName+"].");
if(attributeName.startsWith("appender="+appender.getName()+",layout")) {
try {
return new ObjectName("log4j:"+attributeName );
} catch(Exception e) {
cat.error("attributeName", e);
}
}
MethodUnion mu = (MethodUnion) dynamicProps.get(attributeName);
//cat.debug("----name="+attributeName+", b="+b);
if(mu != null && mu.readMethod != null) {
try {
return mu.readMethod.invoke(appender, 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.Logger | getLogger()
return cat;
| public javax.management.MBeanInfo | getMBeanInfo()
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.Object | invoke(java.lang.String operationName, java.lang.Object[] params, java.lang.String[] signature)
if(operationName.equals("activateOptions") &&
appender instanceof OptionHandler) {
OptionHandler oh = (OptionHandler) appender;
oh.activateOptions();
return "Options activated.";
} else if (operationName.equals("setLayout")) {
Layout layout = (Layout) OptionConverter.instantiateByClassName((String)
params[0],
Layout.class,
null);
appender.setLayout(layout);
registerLayoutMBean(layout);
}
return null;
| private boolean | isSupportedType(java.lang.Class clazz)
if(clazz.isPrimitive()) {
return true;
}
if(clazz == String.class) {
return true;
}
if(clazz.isAssignableFrom(Priority.class)) {
return true;
}
return false;
| public javax.management.ObjectName | preRegister(javax.management.MBeanServer server, javax.management.ObjectName name)
cat.debug("preRegister called. Server="+server+ ", name="+name);
this.server = server;
registerLayoutMBean(appender.getLayout());
return name;
| void | registerLayoutMBean(org.apache.log4j.Layout layout)
if(layout == null)
return;
String name = appender.getName()+",layout="+layout.getClass().getName();
cat.debug("Adding LayoutMBean:"+name);
ObjectName objectName = null;
try {
LayoutDynamicMBean appenderMBean = new LayoutDynamicMBean(layout);
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 + " layout.", true, true, false));
}
} catch(Exception e) {
cat.error("Could not add DynamicLayoutMBean for ["+name+"].", e);
}
| public void | setAttribute(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(appender, o);
} catch(Exception e) {
cat.error("FIXME", e);
}
} else if(name.endsWith(".layout")) {
} else {
throw(new AttributeNotFoundException("Attribute " + name +
" not found in " +
this.getClass().getName()));
}
|
|