Clover coverage report - Code Coverage for hivemind-jmx release 1.1-beta-2
Coverage timestamp: Tue Jun 28 2005 10:29:45 EDT
file stats: LOC: 238   Methods: 15
NCLOC: 154   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LogManagementMBean.java 75% 75.6% 80% 76.5%
coverage coverage
 1    // Copyright 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 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.MBeanRegistration;
 27    import javax.management.MBeanServer;
 28    import javax.management.ObjectName;
 29   
 30    import mx4j.AbstractDynamicMBean;
 31   
 32    import org.apache.hivemind.ApplicationRuntimeException;
 33    import org.apache.hivemind.management.ObjectNameBuilder;
 34    import org.apache.hivemind.util.StringUtils;
 35    import org.apache.log4j.LogManager;
 36    import org.apache.log4j.Logger;
 37    import org.apache.log4j.helpers.OptionConverter;
 38    import org.apache.log4j.jmx.LoggerDynamicMBean;
 39    import org.apache.log4j.spi.LoggerRepository;
 40    import org.apache.oro.text.regex.MalformedPatternException;
 41    import org.apache.oro.text.regex.Pattern;
 42    import org.apache.oro.text.regex.Perl5Compiler;
 43    import org.apache.oro.text.regex.Perl5Matcher;
 44   
 45    /**
 46    * MBean that manages MBeans for Log4j Loggers. New MBeans can be added by specifying the Logger
 47    * name or a logger pattern. Each MBean allows managing level and appenders of a single logger. Uses
 48    * the LoggerDynamicMBean from the log4j library. Similar to
 49    * {@link org.apache.log4j.jmx.HierarchyDynamicMBean} but implements the hivemind ObjectName scheme
 50    * by using ObjectNameBuilder service.
 51    *
 52    * @author Achim Huegen
 53    * @since 1.1
 54    */
 55    public class LogManagementMBean extends AbstractDynamicMBean implements MBeanRegistration,
 56    LogManagement
 57    {
 58    private static final String OBJECT_NAME_TYPE = "logger";
 59   
 60    private static final char WILDCARD = '*';
 61   
 62    private static Logger logger = Logger.getLogger(LogManagementMBean.class);
 63   
 64    private ObjectNameBuilder _objectNameBuilder;
 65   
 66    private LoggerRepository _loggerRepository;
 67   
 68    private MBeanServer _mbeanserver;
 69   
 70    private List _loggerContributions;
 71   
 72  2 public LogManagementMBean(ObjectNameBuilder objectNameBuilder, List loggerContributions)
 73    {
 74  2 _objectNameBuilder = objectNameBuilder;
 75  2 _loggerRepository = LogManager.getLoggerRepository();
 76  2 _loggerContributions = loggerContributions;
 77    }
 78   
 79  1 protected MBeanAttributeInfo[] createMBeanAttributeInfo()
 80    {
 81  1 return new MBeanAttributeInfo[]
 82    { new MBeanAttributeInfo("Threshold", String.class.getName(),
 83    "The \"threshold\" state of the logger hierarchy.", true, true, false) };
 84    }
 85   
 86  1 protected MBeanOperationInfo[] createMBeanOperationInfo()
 87    {
 88  1 MBeanParameterInfo parameterInfo[] = new MBeanParameterInfo[1];
 89  1 parameterInfo[0] = new MBeanParameterInfo("loggerPattern", "java.lang.String",
 90    "Name of the Logger. Use * as wildcard");
 91  1 return new MBeanOperationInfo[]
 92    { new MBeanOperationInfo("addLoggerMBean", "Adds a MBean for a single Logger or "
 93    + "a group of Loggers", parameterInfo, "void", 1) };
 94    }
 95   
 96  2 public ObjectName preRegister(MBeanServer mbeanserver, ObjectName objectname)
 97    {
 98  2 _mbeanserver = mbeanserver;
 99  2 return objectname;
 100    }
 101   
 102  2 public void postRegister(Boolean registrationDone)
 103    {
 104  2 addConfiguredLoggerMBeans();
 105    }
 106   
 107  1 public void preDeregister() throws Exception
 108    {
 109    }
 110   
 111  1 public void postDeregister()
 112    {
 113    }
 114   
 115  0 public String getThreshold()
 116    {
 117  0 return _loggerRepository.getThreshold().toString();
 118    }
 119   
 120  0 public void setThreshold(String threshold)
 121    {
 122  0 OptionConverter.toLevel(threshold, _loggerRepository.getThreshold());
 123   
 124  0 _loggerRepository.setThreshold(threshold);
 125    }
 126   
 127    /**
 128    * @see org.apache.hivemind.management.log4j.LogManagement#addLoggerMBean(java.lang.String)
 129    */
 130  0 public void addLoggerMBean(String loggerPattern)
 131    {
 132  0 boolean hasWildcard = loggerPattern.indexOf(WILDCARD) >= 0;
 133  0 if (hasWildcard)
 134    {
 135  0 addLoggerMBeansForPattern(loggerPattern);
 136    }
 137    else
 138    {
 139  0 Logger log = LogManager.getLogger(loggerPattern);
 140  0 addLoggerMBean(log);
 141    }
 142    }
 143   
 144    /**
 145    * Adds a MBean for a logger.
 146    *
 147    * @param log
 148    * the logger
 149    * @return ObjectName of created MBean
 150    */
 151  9 protected ObjectName addLoggerMBean(Logger log)
 152    {
 153  9 String name = log.getName();
 154  9 ObjectName objectname = null;
 155  9 try
 156    {
 157  9 LoggerDynamicMBean loggerMBean = new LoggerDynamicMBean(log);
 158  9 objectname = getObjectNameBuilder().createObjectName(name, OBJECT_NAME_TYPE);
 159  9 _mbeanserver.registerMBean(loggerMBean, objectname);
 160    }
 161    catch (InstanceAlreadyExistsException exception)
 162    {
 163    // just warn
 164  0 log.warn("MBean for Logger " + log.getName() + " already exists");
 165    }
 166    catch (JMException exception)
 167    {
 168  0 throw new ApplicationRuntimeException(exception);
 169    }
 170  9 return objectname;
 171    }
 172   
 173    /**
 174    * Adds MBeans for all Loggers that are defined in the service configuration
 175    */
 176  2 protected void addConfiguredLoggerMBeans()
 177    {
 178  2 for (Iterator iterContributions = _loggerContributions.iterator(); iterContributions
 179    .hasNext();)
 180    {
 181  4 LoggerContribution contribution = (LoggerContribution) iterContributions.next();
 182  4 String loggerPattern = contribution.getLoggerPattern();
 183   
 184  4 addLoggerMBeansForPattern(loggerPattern);
 185    }
 186    }
 187   
 188    /**
 189    * Adds MBeans for all existing Loggers, that match the loggerPattern
 190    *
 191    * @param loggerPattern
 192    */
 193  4 protected void addLoggerMBeansForPattern(String loggerPattern)
 194    {
 195    // Add MBeans for all loggers that match the pattern
 196  4 Enumeration loggers = LogManager.getCurrentLoggers();
 197  4 while (loggers.hasMoreElements())
 198    {
 199  241 Logger log = (Logger) loggers.nextElement();
 200  241 if (isMatch(log.getName(), loggerPattern))
 201  9 addLoggerMBean(log);
 202    }
 203    }
 204   
 205    /**
 206    * @return Returns the _objectNameBuilder.
 207    */
 208  9 public ObjectNameBuilder getObjectNameBuilder()
 209    {
 210  9 return _objectNameBuilder;
 211    }
 212   
 213    /**
 214    * Returns true if loggerName matches a loggerPattern The pattern kann contain '*' as wildcard
 215    * character. This gets translated to '.*' and is used for a regex match using jakarta oro
 216    */
 217  241 protected boolean isMatch(String loggerName, String loggerPattern)
 218    {
 219    // Adapt loggerPattern for oro
 220  241 String realLoggerPattern = StringUtils
 221    .replace(loggerPattern, "" + WILDCARD, "." + WILDCARD);
 222   
 223  241 Perl5Compiler compiler = new Perl5Compiler();
 224  241 Perl5Matcher matcher = new Perl5Matcher();
 225  241 Pattern compiled;
 226  241 try
 227    {
 228  241 compiled = compiler.compile(realLoggerPattern);
 229    }
 230    catch (MalformedPatternException e)
 231    {
 232  0 throw new ApplicationRuntimeException("Malformed Logger Pattern:" + realLoggerPattern);
 233    }
 234  241 return matcher.matches(loggerName, compiled);
 235   
 236    }
 237   
 238    }