View Javadoc

1   /*
2    * $Id: AbstractJmxSupport.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.module.management.support;
11  
12  import org.mule.api.MuleContext;
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  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          List servers = MBeanServerFactory.findMBeanServer(null);
37          if (servers.isEmpty())
38          {
39              throw new IllegalStateException("MBeanServer is not available.");
40          }
41          MBeanServer server = (MBeanServer) servers.get(0);
42  
43          Collection registeredDomains = getDomains(server);
44          int counter = 1;
45          // Just append .<n> suffix to the domain for a start
46          if (registeredDomains.contains(domain))
47          {
48              domain += "." + counter;
49          }
50          // recheck in case there were any suffixed domains already
51          while (registeredDomains.contains(domain))
52          {
53              // append .<n> until we succeed
54              domain = domain.substring(0, domain.lastIndexOf(".") + 1) + ++counter;
55          }
56  
57          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(MuleContext context)
69      {
70          return getDomainName(context, true);
71      }
72  
73      public String getDomainName(MuleContext context, boolean resolveClash)
74      {
75          String domain = DEFAULT_JMX_DOMAIN_PREFIX;
76          String instanceId = StringUtils.defaultString(context.getConfiguration().getId());
77          if (instanceId.length() > 0)
78          {
79              domain += "." + instanceId;
80          }
81  
82          JmxRegistrationContext ctx = JmxRegistrationContext.getCurrent(context);
83  
84          String resolvedDomain = ctx.getResolvedDomain();
85          if (resolveClash)
86          {
87              if (StringUtils.isBlank(resolvedDomain))
88              {
89                  domain = resolveDomainClash(domain);
90                  ctx.setResolvedDomain(domain);
91              }
92          }
93  
94          return domain;
95      }
96  }