| Method from org.apache.log4j.jmx.LoggerDynamicMBean Detail: |
void addAppender(String appenderClass,
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();
}
|
void appenderMBeanRegistration() {
Enumeration enumeration = logger.getAllAppenders();
while(enumeration.hasMoreElements()) {
Appender appender = (Appender) enumeration.nextElement();
registerAppenderMBean(appender);
}
}
|
public Object getAttribute(String attributeName) throws ReflectionException, AttributeNotFoundException, MBeanException {
// 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 Logger getLogger() {
return logger;
}
|
public MBeanInfo getMBeanInfo() {
//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 void handleNotification(Notification notification,
Object handback) {
cat.debug("Received notification: "+notification.getType());
registerAppenderMBean((Appender) notification.getUserData() );
}
|
public Object invoke(String operationName,
Object[] params,
String[] signature) throws ReflectionException, MBeanException {
if(operationName.equals("addAppender")) {
addAppender((String) params[0], (String) params[1]);
return "Hello world.";
}
return null;
}
|
public void postRegister(Boolean registrationDone) {
appenderMBeanRegistration();
}
|
void registerAppenderMBean(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 void setAttribute(Attribute attribute) throws InvalidAttributeValueException, ReflectionException, AttributeNotFoundException, MBeanException {
// 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()));
}
}
|