2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind-jmx release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:34:17 PST
file stats: LOC: 217   Methods: 12
NCLOC: 138   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LogManagementMBean.java 75% 74.4% 75% 74.6%
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.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    /**
 42    * MBean that manages MBeans for Log4j Loggers. New MBeans can be added by specifying the Logger
 43    * name or a logger pattern. Each MBean allows managing level and appenders of a single logger. Uses
 44    * the LoggerDynamicMBean from the log4j library. Similar to
 45    * {@link org.apache.log4j.jmx.HierarchyDynamicMBean} but implements the hivemind ObjectName scheme
 46    * by using ObjectNameBuilder service.
 47    *
 48    * @author Achim Huegen
 49    * @since 1.1
 50    */
 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  2 public LogManagementMBean(ObjectNameBuilder objectNameBuilder, List loggerContributions)
 66    {
 67  2 _objectNameBuilder = objectNameBuilder;
 68  2 _loggerRepository = LogManager.getLoggerRepository();
 69  2 _loggerContributions = loggerContributions;
 70    }
 71   
 72  1 protected MBeanAttributeInfo[] createMBeanAttributeInfo()
 73    {
 74  1 return new MBeanAttributeInfo[]
 75    { new MBeanAttributeInfo("Threshold", String.class.getName(),
 76    "The \"threshold\" state of the logger hierarchy.", true, true, false) };
 77    }
 78   
 79  1 protected MBeanOperationInfo[] createMBeanOperationInfo()
 80    {
 81  1 MBeanParameterInfo parameterInfo[] = new MBeanParameterInfo[1];
 82  1 parameterInfo[0] = new MBeanParameterInfo("loggerPattern", "java.lang.String",
 83    "Name of the Logger. Use * as wildcard");
 84  1 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  2 public void postRegister(Boolean registrationDone)
 90    {
 91  2 addConfiguredLoggerMBeans();
 92    }
 93   
 94  0 public String getThreshold()
 95    {
 96  0 return _loggerRepository.getThreshold().toString();
 97    }
 98   
 99  0 public void setThreshold(String threshold)
 100    {
 101  0 OptionConverter.toLevel(threshold, _loggerRepository.getThreshold());
 102   
 103  0 _loggerRepository.setThreshold(threshold);
 104    }
 105   
 106    /**
 107    * @see org.apache.hivemind.management.log4j.LogManagement#addLoggerMBean(java.lang.String)
 108    */
 109  0 public void addLoggerMBean(String loggerPattern)
 110    {
 111  0 boolean hasWildcard = loggerPattern.indexOf(WILDCARD) >= 0;
 112  0 if (hasWildcard)
 113    {
 114  0 addLoggerMBeansForPattern(loggerPattern);
 115    }
 116    else
 117    {
 118  0 Logger log = LogManager.getLogger(loggerPattern);
 119  0 addLoggerMBean(log);
 120    }
 121    }
 122   
 123    /**
 124    * Adds a MBean for a logger.
 125    *
 126    * @param log
 127    * the logger
 128    * @return ObjectName of created MBean
 129    */
 130  9 protected ObjectName addLoggerMBean(Logger log)
 131    {
 132  9 String name = log.getName();
 133  9 ObjectName objectname = null;
 134  9 try
 135    {
 136  9 LoggerMBean loggerMBean = new LoggerMBean(log);
 137  9 objectname = getObjectNameBuilder().createObjectName(name, OBJECT_NAME_TYPE);
 138  9 getMBeanServer().registerMBean(loggerMBean, objectname);
 139    }
 140    catch (InstanceAlreadyExistsException exception)
 141    {
 142    // just warn
 143  0 logger.warn("MBean for Logger " + log.getName() + " already exists");
 144    }
 145    catch (JMException exception)
 146    {
 147  0 throw new ApplicationRuntimeException(exception);
 148    }
 149  9 return objectname;
 150    }
 151   
 152    /**
 153    * Adds MBeans for all Loggers that are defined in the service configuration
 154    */
 155  2 protected void addConfiguredLoggerMBeans()
 156    {
 157  2 for (Iterator iterContributions = _loggerContributions.iterator(); iterContributions
 158    .hasNext();)
 159    {
 160  4 LoggerContribution contribution = (LoggerContribution) iterContributions.next();
 161  4 String loggerPattern = contribution.getLoggerPattern();
 162   
 163  4 addLoggerMBeansForPattern(loggerPattern);
 164    }
 165    }
 166   
 167    /**
 168    * Adds MBeans for all existing Loggers, that match the loggerPattern
 169    *
 170    * @param loggerPattern
 171    */
 172  4 protected void addLoggerMBeansForPattern(String loggerPattern)
 173    {
 174    // Add MBeans for all loggers that match the pattern
 175  4 Enumeration loggers = LogManager.getCurrentLoggers();
 176  4 while (loggers.hasMoreElements())
 177    {
 178  237 Logger log = (Logger) loggers.nextElement();
 179  237 if (isMatch(log.getName(), loggerPattern))
 180  9 addLoggerMBean(log);
 181    }
 182    }
 183   
 184    /**
 185    * @return Returns the _objectNameBuilder.
 186    */
 187  9 public ObjectNameBuilder getObjectNameBuilder()
 188    {
 189  9 return _objectNameBuilder;
 190    }
 191   
 192    /**
 193    * Returns true if loggerName matches a loggerPattern The pattern kann contain '*' as wildcard
 194    * character. This gets translated to '.*' and is used for a regex match using jakarta oro
 195    */
 196  237 protected boolean isMatch(String loggerName, String loggerPattern)
 197    {
 198    // Adapt loggerPattern for oro
 199  237 String realLoggerPattern = StringUtils
 200    .replace(loggerPattern, "" + WILDCARD, "." + WILDCARD);
 201   
 202  237 Perl5Compiler compiler = new Perl5Compiler();
 203  237 Perl5Matcher matcher = new Perl5Matcher();
 204  237 Pattern compiled;
 205  237 try
 206    {
 207  237 compiled = compiler.compile(realLoggerPattern);
 208    }
 209    catch (MalformedPatternException e)
 210    {
 211  0 throw new ApplicationRuntimeException("Malformed Logger Pattern:" + realLoggerPattern);
 212    }
 213  237 return matcher.matches(loggerName, compiled);
 214   
 215    }
 216   
 217    }