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