1 15 package org.apache.hivemind.management.log4j; 16 17 import java.util.Enumeration ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import javax.management.InstanceAlreadyExistsException ; 22 import javax.management.JMException ; 23 import javax.management.MBeanAttributeInfo ; 24 import javax.management.MBeanOperationInfo ; 25 import javax.management.MBeanParameterInfo ; 26 import javax.management.ObjectName ; 27 28 import org.apache.hivemind.ApplicationRuntimeException; 29 import org.apache.hivemind.management.ObjectNameBuilder; 30 import org.apache.hivemind.management.mbeans.AbstractDynamicMBean; 31 import org.apache.hivemind.util.StringUtils; 32 import org.apache.log4j.LogManager; 33 import org.apache.log4j.Logger; 34 import org.apache.log4j.helpers.OptionConverter; 35 import org.apache.log4j.spi.LoggerRepository; 36 import org.apache.oro.text.regex.MalformedPatternException; 37 import org.apache.oro.text.regex.Pattern; 38 import org.apache.oro.text.regex.Perl5Compiler; 39 import org.apache.oro.text.regex.Perl5Matcher; 40 41 51 public class LogManagementMBean extends AbstractDynamicMBean implements LogManagement 52 { 53 private static final String OBJECT_NAME_TYPE = "logger"; 54 55 private static final char WILDCARD = '*'; 56 57 private static Logger logger = Logger.getLogger(LogManagementMBean.class); 58 59 private ObjectNameBuilder _objectNameBuilder; 60 61 private LoggerRepository _loggerRepository; 62 63 private List _loggerContributions; 64 65 public LogManagementMBean(ObjectNameBuilder objectNameBuilder, List loggerContributions) 66 { 67 _objectNameBuilder = objectNameBuilder; 68 _loggerRepository = LogManager.getLoggerRepository(); 69 _loggerContributions = loggerContributions; 70 } 71 72 protected MBeanAttributeInfo [] createMBeanAttributeInfo() 73 { 74 return new MBeanAttributeInfo [] 75 { new MBeanAttributeInfo ("Threshold", String .class.getName(), 76 "The \"threshold\" state of the logger hierarchy.", true, true, false) }; 77 } 78 79 protected MBeanOperationInfo [] createMBeanOperationInfo() 80 { 81 MBeanParameterInfo parameterInfo[] = new MBeanParameterInfo [1]; 82 parameterInfo[0] = new MBeanParameterInfo ("loggerPattern", "java.lang.String", 83 "Name of the Logger. Use * as wildcard"); 84 return new MBeanOperationInfo [] 85 { new MBeanOperationInfo ("addLoggerMBean", "Adds a MBean for a single Logger or " 86 + "a group of Loggers", parameterInfo, "void", 1) }; 87 } 88 89 public void postRegister(Boolean registrationDone) 90 { 91 addConfiguredLoggerMBeans(); 92 } 93 94 public String getThreshold() 95 { 96 return _loggerRepository.getThreshold().toString(); 97 } 98 99 public void setThreshold(String threshold) 100 { 101 OptionConverter.toLevel(threshold, _loggerRepository.getThreshold()); 102 103 _loggerRepository.setThreshold(threshold); 104 } 105 106 109 public void addLoggerMBean(String loggerPattern) 110 { 111 boolean hasWildcard = loggerPattern.indexOf(WILDCARD) >= 0; 112 if (hasWildcard) 113 { 114 addLoggerMBeansForPattern(loggerPattern); 115 } 116 else 117 { 118 Logger log = LogManager.getLogger(loggerPattern); 119 addLoggerMBean(log); 120 } 121 } 122 123 130 protected ObjectName addLoggerMBean(Logger log) 131 { 132 String name = log.getName(); 133 ObjectName objectname = null; 134 try 135 { 136 LoggerMBean loggerMBean = new LoggerMBean(log); 137 objectname = getObjectNameBuilder().createObjectName(name, OBJECT_NAME_TYPE); 138 getMBeanServer().registerMBean(loggerMBean, objectname); 139 } 140 catch (InstanceAlreadyExistsException exception) 141 { 142 logger.warn("MBean for Logger " + log.getName() + " already exists"); 144 } 145 catch (JMException exception) 146 { 147 throw new ApplicationRuntimeException(exception); 148 } 149 return objectname; 150 } 151 152 155 protected void addConfiguredLoggerMBeans() 156 { 157 for (Iterator iterContributions = _loggerContributions.iterator(); iterContributions 158 .hasNext();) 159 { 160 LoggerContribution contribution = (LoggerContribution) iterContributions.next(); 161 String loggerPattern = contribution.getLoggerPattern(); 162 163 addLoggerMBeansForPattern(loggerPattern); 164 } 165 } 166 167 172 protected void addLoggerMBeansForPattern(String loggerPattern) 173 { 174 Enumeration loggers = LogManager.getCurrentLoggers(); 176 while (loggers.hasMoreElements()) 177 { 178 Logger log = (Logger) loggers.nextElement(); 179 if (isMatch(log.getName(), loggerPattern)) 180 addLoggerMBean(log); 181 } 182 } 183 184 187 public ObjectNameBuilder getObjectNameBuilder() 188 { 189 return _objectNameBuilder; 190 } 191 192 196 protected boolean isMatch(String loggerName, String loggerPattern) 197 { 198 String realLoggerPattern = StringUtils 200 .replace(loggerPattern, "" + WILDCARD, "." + WILDCARD); 201 202 Perl5Compiler compiler = new Perl5Compiler(); 203 Perl5Matcher matcher = new Perl5Matcher(); 204 Pattern compiled; 205 try 206 { 207 compiled = compiler.compile(realLoggerPattern); 208 } 209 catch (MalformedPatternException e) 210 { 211 throw new ApplicationRuntimeException("Malformed Logger Pattern:" + realLoggerPattern); 212 } 213 return matcher.matches(loggerName, compiled); 214 215 } 216 217 } | Popular Tags |