View Javadoc
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  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          List servers = MBeanServerFactory.findMBeanServer(null);
34          if (servers.isEmpty())
35          {
36              throw new IllegalStateException("MBeanServer is not available.");
37          }
38          MBeanServer server = (MBeanServer) servers.get(0);
39  
40          Collection registeredDomains = getDomains(server);
41          int counter = 1;
42          // Just append .<n> suffix to the domain for a start
43          if (registeredDomains.contains(domain))
44          {
45              domain += "." + counter;
46          }
47          // recheck in case there were any suffixed domains already
48          while (registeredDomains.contains(domain))
49          {
50              // append .<n> until we succeed
51              domain = domain.substring(0, domain.lastIndexOf(".") + 1) + ++counter;
52          }
53  
54          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          return getDomainName(context, true);
68      }
69  
70      public String getDomainName(MuleContext context, boolean resolveClash)
71      {
72          String domain = DEFAULT_JMX_DOMAIN_PREFIX;
73          String instanceId = StringUtils.defaultString(context.getConfiguration().getId());
74          if (instanceId.length() > 0)
75          {
76              domain += "." + instanceId;
77          }
78  
79          JmxRegistrationContext ctx = JmxRegistrationContext.getCurrent(context);
80  
81          String resolvedDomain = ctx.getResolvedDomain();
82          if (resolveClash)
83          {
84              if (StringUtils.isBlank(resolvedDomain))
85              {
86                  domain = resolveDomainClash(domain);
87                  ctx.setResolvedDomain(domain);
88              }
89          }
90  
91          return domain;
92      }
93  }