if (shutdown)
return 0;
MBeanServer server = JmxUtils.locateMBeanServer();
HierarchyDynamicMBean hdm;
synchronized (this.getClass())
{
boolean usedNew = hierarchyDynamicMBean.compareAndSet(null, new HierarchyDynamicMBean());
hdm = hierarchyDynamicMBean.get();
if (usedNew)
{
try {
ObjectName mbo = new ObjectName(LOG4J_HIERARCHY_DEFAULT);
server.registerMBean(hdm, mbo);
registeredHierarchy = true;
// Add the root logger to the Hierarchy MBean
hdm.addLoggerMBean(Logger.getRootLogger().getName());
}
catch (Exception e) {
log.error("Error initializing Log4jMBeanExporter", e);
return 0;
}
}
}
// Get each logger from the Log4J Repository and add it to
// the Hierarchy MBean created above.
LoggerRepository r = LogManager.getLoggerRepository();
Enumeration<?> loggers = r.getCurrentLoggers();
int count = 0;
while ( loggers.hasMoreElements() ) {
String name = ((Logger) loggers.nextElement()).getName();
// this name definition copied from HierarchyDynamicMBean
ObjectName objectName;
try {
objectName = new ObjectName("log4j", "logger", name);
}
catch (Exception e) {
log.error("Error creating JMX name for " + name, e);
continue;
}
if (!server.isRegistered(objectName))
{
if (log.isDebugEnabled()) {
log.debug("[contextInitialized]: Registering " + name);
}
registeredNames.add(hdm.addLoggerMBean(name));
count++;
}
}
log.debug("registered " + count + " new Log4j MBeans");