Coverage report

  %line %branch
net.sf.infrared.agent.setup.InfraREDLifeCycleListener
47% 
67% 

 1  
 /* 
 2  
  * Copyright 2005 Tavant Technologies and Contributors
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License")
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  * 
 16  
  *
 17  
  *
 18  
  * Original Author:  kamal.govindraj (Tavant Technologies)
 19  
  * Contributor(s):   -;
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.agent.setup;
 23  
 
 24  
 import javax.management.InstanceAlreadyExistsException;
 25  
 import javax.management.InstanceNotFoundException;
 26  
 import javax.management.MBeanRegistrationException;
 27  
 import javax.management.MBeanServer;
 28  
 import javax.management.MalformedObjectNameException;
 29  
 import javax.management.NotCompliantMBeanException;
 30  
 import javax.management.ObjectName;
 31  
 
 32  
 import net.sf.infrared.agent.MonitorConfig;
 33  
 import net.sf.infrared.agent.MonitorConfigImpl;
 34  
 import net.sf.infrared.agent.MonitorFacade;
 35  
 import net.sf.infrared.agent.MonitorFacadeImpl;
 36  
 import net.sf.infrared.agent.MonitorFactory;
 37  
 import net.sf.infrared.agent.MultipleEntryGuard;
 38  
 import net.sf.infrared.agent.configmgmt.InfraredProperties;
 39  
 import net.sf.infrared.base.configmgmt.AbstractMBeanServerFactory;
 40  
 import net.sf.infrared.base.util.LoggingFactory;
 41  
 
 42  
 import org.apache.log4j.Logger;
 43  
 
 44  
 /**
 45  
  * Implements the lifecycle methods for InfraRED.
 46  
  */
 47  
 public class InfraREDLifeCycleListener {
 48  
 
 49  4
     private static final Logger log = LoggingFactory.getLogger(InfraREDLifeCycleListener.class);
 50  
 
 51  
     private static final String KEY_MBEAN_SERVER_PROVIDER = "mbean-server-provider";
 52  
     
 53  3
     private MBeanServer mbeanServer = null;
 54  
 
 55  
     private ObjectName propertiesObjectName;
 56  
 
 57  3
     private MonitorFacade facade = null;
 58  
 
 59  
     /**
 60  
      * The default constructor
 61  
      */
 62  3
     public InfraREDLifeCycleListener() {
 63  3
     }
 64  
 
 65  
     /**
 66  
      * Initialized infraRED agent for the specified application name. It is
 67  
      * important that the threads context class loader is setup correctly when
 68  
      * this call is made. All subsequently calls to infraRED orginating from
 69  
      * this application should have the class loader or a child class loader as
 70  
      * the threads context class loader. This is usually true if the methods are
 71  
      * being called from a ServletContextListener or an ApplicationLifeCycleListener
 72  
      * 
 73  
      * @param applicationName
 74  
      */
 75  
     public void initialized(String applicationName, String instanceId, String configProvider) {
 76  1
         MonitorConfig config = new MonitorConfigImpl(configProvider);
 77  
 
 78  1
         facade = new MonitorFacadeImpl(applicationName, instanceId, config, true);
 79  1
         facade = new MultipleEntryGuard(facade);
 80  
 
 81  1
         MonitorFactory.registerFacadeImpl(facade);
 82  1
         if (log.isDebugEnabled()) {
 83  0
             log.debug("Initializing application : " + applicationName + " InstanceId : "
 84  
                     + instanceId);
 85  
         }
 86  
 
 87  
         // Get the MBeanServer instance and register the MBeans.
 88  1
         setMBeanServer(config, applicationName, instanceId);
 89  1
         registerMBeans(config);
 90  1
     }
 91  
 
 92  
     /**
 93  
      * Reset the infrared agent setting for the specified applciation
 94  
      * 
 95  
      * @param applicationName
 96  
      */
 97  
     public void destroyed(String applicationName) {
 98  
         // Unregister the MBeans before unregistering the FacadeImpl
 99  1
         unregisterMBeans();
 100  
 
 101  
         // MonitorFactory.unregisterFacadeImpl(facade);
 102  
         // unregisters the Facade belonging to this threads context classloader
 103  1
         MonitorFacade facade = MonitorFactory.unregisterFacadeImpl();
 104  1
         if (facade != null) {
 105  1
             facade.destroy();
 106  
         }
 107  1
         if (log.isDebugEnabled()) {
 108  0
             log.debug("Destroying application " + applicationName);
 109  
         }
 110  1
     }
 111  
 
 112  
     /**
 113  
      * Method to register the MBeans with the MBeanServer if the MBeanServer is
 114  
      * not null
 115  
      */
 116  
     public void registerMBeans(MonitorConfig config) {
 117  1
         if (mbeanServer != null) {
 118  0
             InfraredProperties infraredProperties = new InfraredProperties(config);
 119  
             try {
 120  0
                 String objectName = "InfraredProperties:name=Infrared Properties,"
 121  
                         + "type=InfraredPropertiesMBean";
 122  0
                 propertiesObjectName = new ObjectName(objectName);
 123  0
                 mbeanServer.registerMBean(infraredProperties, propertiesObjectName);
 124  0
                 log.debug("The MBean has been successfully registered !");
 125  0
             } catch (MalformedObjectNameException e) {
 126  0
                 log.error("The MBean Object name is Malformed", e);
 127  0
             } catch (InstanceAlreadyExistsException e) {
 128  0
                 log.error("The MBean instance already exists", e);
 129  0
             } catch (MBeanRegistrationException e) {
 130  0
                 log.error("The MBean Registration failed", e);
 131  0
             } catch (NotCompliantMBeanException e) {
 132  0
                 log.error("The MBean is not Compliant &  Registration failed", e);
 133  0
             }
 134  
         }
 135  1
     }
 136  
 
 137  
     public void unregisterMBeans() {
 138  1
         if (mbeanServer == null) {
 139  1
             return;
 140  
         }
 141  
 
 142  
         try {
 143  0
             mbeanServer.unregisterMBean(propertiesObjectName);
 144  0
         } catch (MBeanRegistrationException e) {
 145  0
             log.error("Unable to unregister the MBean", e);
 146  0
         } catch (InstanceNotFoundException e) {
 147  0
             log.error("The MBean instance is not registered in the MBeanServer", e);
 148  0
         }
 149  0
     }
 150  
 
 151  
     public MonitorFacade getFacade() {
 152  0
         return facade;
 153  
     }
 154  
 
 155  
     private void setMBeanServer(MonitorConfig config, String appName, String instanceId) {
 156  1
         String mbeanServerClass = config.getProperty(KEY_MBEAN_SERVER_PROVIDER, (String) null);
 157  1
         if (mbeanServerClass == null) {
 158  1
             log.warn("No MBean Server Class set for application " + appName 
 159  
                     + ", instance " + instanceId);
 160  1
             return;
 161  
         }
 162  
         try {
 163  0
             AbstractMBeanServerFactory mbeanServerProvider = 
 164  
                 (AbstractMBeanServerFactory) Class.forName(mbeanServerClass).newInstance();
 165  0
             mbeanServer = mbeanServerProvider.getMBeanServer();
 166  0
         } catch (Exception e) {
 167  0
             log.error("Unable to load MBeanServer provider", e);
 168  0
         }
 169  0
     }
 170  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.