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