1
2
3
4
5
6
7
8
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
26
27
28
29
30
31
32
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
46 if (registeredDomains.contains(domain))
47 {
48 domain += "." + counter;
49 }
50
51 while (registeredDomains.contains(domain))
52 {
53
54 domain = domain.substring(0, domain.lastIndexOf(".") + 1) + ++counter;
55 }
56
57 return domain;
58 }
59
60
61
62
63
64
65 protected abstract Collection getDomains(MBeanServer server);
66
67
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 }