Coverage Report - org.mule.management.support.AbstractJmxSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractJmxSupport
0%
0/22
0%
0/5
3
 
 1  
 /*
 2  
  * $Id: AbstractJmxSupport.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
 package org.mule.management.support;
 11  
 
 12  
 import org.mule.MuleManager;
 13  
 import org.mule.util.StringUtils;
 14  
 
 15  
 import java.util.Collection;
 16  
 import java.util.List;
 17  
 
 18  
 import javax.management.MBeanServer;
 19  
 import javax.management.MBeanServerFactory;
 20  
 
 21  0
 public abstract class AbstractJmxSupport implements JmxSupport
 22  
 {
 23  
 
 24  
     /**
 25  
      * Resolve JMX domain clash by adding an incremented number suffix to the name. E.g. if
 26  
      * 'Mule.TradeProcessor' is already registered with the accessible MBeanServer, will return
 27  
      * 'Mule.TradeProcessor.1'. If the latter one is already registered, will return
 28  
      * 'Mule.TradeProcessor.2' and so on.
 29  
      * <p/>
 30  
      * If no clash detected, returns the domain name unmodified.
 31  
      * @param domain domain name causing a conflict
 32  
      * @return resolved non-conflicting domain name
 33  
      */
 34  
     protected String resolveDomainClash(String domain)
 35  
     {
 36  0
         List servers = MBeanServerFactory.findMBeanServer(null);
 37  0
         if (servers.isEmpty())
 38  
         {
 39  0
             throw new IllegalStateException("MBeanServer is not available.");
 40  
         }
 41  0
         MBeanServer server = (MBeanServer) servers.get(0);
 42  
 
 43  0
         Collection registeredDomains = getDomains(server);
 44  0
         int counter = 1;
 45  
         // Just append .<n> suffix to the domain for a start
 46  0
         if (registeredDomains.contains(domain))
 47  
         {
 48  0
             domain += "." + counter;
 49  
         }
 50  
         // recheck in case there were any suffixed domains already
 51  0
         while (registeredDomains.contains(domain))
 52  
         {
 53  
             // append .<n> until we succeed
 54  0
             domain = domain.substring(0, domain.lastIndexOf(".") + 1) + ++counter;
 55  
         }
 56  
 
 57  0
         return domain;
 58  
     }
 59  
 
 60  
     /**
 61  
      * List all domains of this MBean server.
 62  
      * @param server server to contact
 63  
      * @return a collection of unique JMX domains
 64  
      */
 65  
     protected abstract Collection getDomains(MBeanServer server);
 66  
 
 67  
     /** {@inheritDoc} */
 68  
     public String getDomainName()
 69  
     {
 70  
         // TODO add some config options to the JmxAgent
 71  0
         String domain = DEFAULT_JMX_DOMAIN_PREFIX;
 72  0
         String instanceId = StringUtils.defaultString(MuleManager.getInstance().getId());
 73  0
         if (instanceId.length() > 0)
 74  
         {
 75  0
             domain += "." + instanceId;
 76  
         }
 77  
 
 78  0
         JmxRegistrationContext ctx = JmxRegistrationContext.getCurrent();
 79  
 
 80  0
         String resolvedDomain = ctx.getResolvedDomain();
 81  0
         if (StringUtils.isBlank(resolvedDomain))
 82  
         {
 83  0
             domain = resolveDomainClash(domain);
 84  0
             ctx.setResolvedDomain(domain);
 85  
         }
 86  
 
 87  0
         return domain;
 88  
     }
 89  
 }