Coverage Report - org.dozer.jmx.JMXPlatformImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
JMXPlatformImpl
80%
25/31
100%
2/2
2
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 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  
 package org.dozer.jmx;
 17  
 
 18  
 import org.slf4j.Logger;
 19  
 import org.slf4j.LoggerFactory;
 20  
 
 21  
 import javax.management.InstanceAlreadyExistsException;
 22  
 import javax.management.InstanceNotFoundException;
 23  
 import javax.management.MBeanRegistrationException;
 24  
 import javax.management.MBeanServer;
 25  
 import javax.management.MalformedObjectNameException;
 26  
 import javax.management.NotCompliantMBeanException;
 27  
 import javax.management.ObjectName;
 28  
 import java.lang.management.ManagementFactory;
 29  
 
 30  
 /**
 31  
  *
 32  
  * Default JMX Platform implementation working with java.lang.management.ManagementFactory.
 33  
  *
 34  
  * @author dmitry.buzdin
 35  
  */
 36  13
 public class JMXPlatformImpl implements JMXPlatform {
 37  
 
 38  13
   private final Logger log = LoggerFactory.getLogger(JMXPlatformImpl.class);
 39  
 
 40  
   public boolean isAvailable() {
 41  
     try {
 42  10
       Class.forName("java.lang.management.ManagementFactory");
 43  10
       Class.forName("javax.management.ObjectName");
 44  10
       Class.forName("javax.management.MBeanServer");
 45  10
       return true;
 46  0
     } catch (Throwable t) {
 47  0
       return false;
 48  
     }
 49  
   }
 50  
 
 51  
   public void registerMBean(String name, Object bean) throws MalformedObjectNameException, MBeanRegistrationException, NotCompliantMBeanException {
 52  13
     ObjectName mbeanObjectName = new ObjectName(name);
 53  13
     unregister(name, mbeanObjectName);
 54  13
     register(name, bean, mbeanObjectName);
 55  13
   }
 56  
 
 57  
   public void unregisterMBean(String name) throws MBeanRegistrationException, MalformedObjectNameException {
 58  10
     ObjectName mbeanObjectName = new ObjectName(name);
 59  10
     unregister(name, mbeanObjectName);
 60  10
   }
 61  
   
 62  
   private void register(String name, Object bean, ObjectName mbeanObjectName) throws MBeanRegistrationException, NotCompliantMBeanException {
 63  13
     MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
 64  
     try {
 65  13
       mbs.registerMBean(bean, mbeanObjectName);
 66  13
       log.info("Dozer JMX MBean [" + name + "] auto registered with the Platform MBean Server");
 67  0
     } catch (InstanceAlreadyExistsException e) {
 68  0
       log.info("JMX MBean instance exists, unable to overwrite [{}].", name);
 69  13
     }
 70  13
   }
 71  
 
 72  
   private void unregister(String name, ObjectName mbeanObjectName) throws MBeanRegistrationException {
 73  23
     MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
 74  23
     boolean isMBeanRegistered = mbs.isRegistered(mbeanObjectName);
 75  23
     if (isMBeanRegistered) {
 76  11
       log.info("Unregistering existing Dozer JMX MBean [{}].", name);
 77  
       try {
 78  11
         mbs.unregisterMBean(mbeanObjectName);
 79  0
       } catch (InstanceNotFoundException e) {
 80  0
         log.info("JMX MBean not found to unregister [{}].", name);
 81  11
       }
 82  
     }
 83  23
   }
 84  
 
 85  
 }