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