Coverage Report - org.mule.module.management.agent.Log4jAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
Log4jAgent
0%
0/27
0%
0/4
0
 
 1  
 /*
 2  
  * $Id: Log4jAgent.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.module.management.agent;
 12  
 
 13  
 import org.mule.AbstractAgent;
 14  
 import org.mule.api.MuleException;
 15  
 import org.mule.api.lifecycle.InitialisationException;
 16  
 import org.mule.config.i18n.CoreMessages;
 17  
 import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory;
 18  
 import org.mule.module.management.support.JmxSupport;
 19  
 import org.mule.module.management.support.JmxSupportFactory;
 20  
 
 21  
 import java.util.Iterator;
 22  
 import java.util.Set;
 23  
 
 24  
 import javax.management.InstanceNotFoundException;
 25  
 import javax.management.MBeanRegistrationException;
 26  
 import javax.management.MBeanServer;
 27  
 import javax.management.MBeanServerFactory;
 28  
 import javax.management.MalformedObjectNameException;
 29  
 import javax.management.ObjectInstance;
 30  
 import javax.management.ObjectName;
 31  
 
 32  
 import org.apache.log4j.jmx.HierarchyDynamicMBean;
 33  
 
 34  
 /**
 35  
  * <code>Log4jAgent</code> exposes the configuration of the Log4J instance running
 36  
  * in Mule for Jmx management
 37  
  */
 38  
 public class Log4jAgent extends AbstractAgent
 39  
 {
 40  
     private MBeanServer mBeanServer;
 41  
     public static final String JMX_OBJECT_NAME = "log4j:type=Hierarchy";
 42  
 
 43  0
     private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
 44  0
     private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
 45  
 
 46  
     public Log4jAgent()
 47  
     {
 48  0
         super("jmx-log4j");
 49  0
     }
 50  
 
 51  
     @Override
 52  
     public String getDescription() {
 53  0
         return "JMX Log4J Agent";
 54  
     }
 55  
 
 56  
     public void initialise() throws InitialisationException
 57  
     {
 58  
         try
 59  
         {
 60  0
             mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
 61  0
             final ObjectName objectName = jmxSupport.getObjectName(JMX_OBJECT_NAME);
 62  
             // unregister existing Log4j MBean first if required
 63  0
             unregisterMBeansIfNecessary();
 64  0
             mBeanServer.registerMBean(new HierarchyDynamicMBean(), objectName);
 65  
         }
 66  0
         catch (Exception e)
 67  
         {
 68  0
             throw new InitialisationException(CoreMessages.failedToStart("Log4j Agent"), e, this);
 69  0
         }
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Unregister log4j MBeans if there are any left over the old deployment
 74  
      */
 75  
     protected void unregisterMBeansIfNecessary()
 76  
         throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException
 77  
     {
 78  0
         if (mBeanServer.isRegistered(jmxSupport.getObjectName(JMX_OBJECT_NAME)))
 79  
         {
 80  
             // unregister all log4jMBeans and loggers
 81  0
             Set log4jMBeans = mBeanServer.queryMBeans(jmxSupport.getObjectName("log4j*:*"), null);
 82  0
             for (Iterator it = log4jMBeans.iterator(); it.hasNext();)
 83  
             {
 84  0
                 ObjectInstance objectInstance = (ObjectInstance)it.next();
 85  0
                 ObjectName theName = objectInstance.getObjectName();
 86  0
                 mBeanServer.unregisterMBean(theName);
 87  0
             }
 88  
         }
 89  0
     }
 90  
 
 91  
     public void start() throws MuleException
 92  
     {
 93  
         // nothing to do
 94  0
     }
 95  
 
 96  
     public void stop() throws MuleException
 97  
     {
 98  
         // nothing to do
 99  0
     }
 100  
 
 101  
     public void dispose()
 102  
     {
 103  
         try
 104  
         {
 105  0
             unregisterMBeansIfNecessary();
 106  
         }
 107  0
         catch (Exception ex)
 108  
         {
 109  
             // ignore
 110  0
         }
 111  0
     }
 112  
 
 113  
 }