KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > management > support > AbstractJmxSupport


1 /*
2  * $Id: AbstractJmxSupport.java 4219 2006-12-09 10:15:14Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10 package org.mule.management.support;
11
12 import org.mule.MuleManager;
13 import org.mule.util.StringUtils;
14
15 import javax.management.MBeanServer JavaDoc;
16 import javax.management.MBeanServerFactory JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.List JavaDoc;
19
20 public abstract class AbstractJmxSupport implements JmxSupport
21 {
22
23     /**
24      * Resolve JMX domain clash by adding an incremented number suffix to the name. E.g. if
25      * 'Mule.TradeProcessor' is already registered with the accessible MBeanServer, will return
26      * 'Mule.TradeProcessor.1'. If the latter one is already registered, will return
27      * 'Mule.TradeProcessor.2' and so on.
28      * <p/>
29      * If no clash detected, returns the domain name unmodified.
30      * @param domain domain name causing a conflict
31      * @return resolved non-conflicting domain name
32      */

33     protected String JavaDoc resolveDomainClash(String JavaDoc domain)
34     {
35         List JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
36         if (servers.isEmpty())
37         {
38             throw new IllegalStateException JavaDoc("MBeanServer is not available.");
39         }
40         MBeanServer JavaDoc server = (MBeanServer JavaDoc) servers.get(0);
41         List JavaDoc registeredDomains = Arrays.asList(server.getDomains());
42         int counter = 1;
43         // Just append .<n> suffix to the domain for a start
44
if (registeredDomains.contains(domain))
45         {
46             domain += "." + counter;
47         }
48         // recheck in case there were any suffixed domains already
49
while (registeredDomains.contains(domain))
50         {
51             // append .<n> until we succeed
52
domain = domain.substring(0, domain.lastIndexOf(".") + 1) + ++counter;
53         }
54
55         return domain;
56     }
57
58     /** {@inheritDoc} */
59     public String JavaDoc getDomainName()
60     {
61         // TODO add some config options to the JmxAgent
62
String JavaDoc domain = DEFAULT_JMX_DOMAIN_PREFIX;
63         String JavaDoc instanceId = StringUtils.defaultIfEmpty(MuleManager.getInstance().getId(), StringUtils.EMPTY);
64         if (instanceId.length() > 0)
65         {
66             domain += "." + instanceId;
67         }
68
69         JmxRegistrationContext ctx = JmxRegistrationContext.getCurrent();
70
71         String JavaDoc resolvedDomain = ctx.getResolvedDomain();
72         if (StringUtils.isBlank(resolvedDomain))
73         {
74             domain = resolveDomainClash(domain);
75             ctx.setResolvedDomain(domain);
76         }
77
78         return domain;
79     }
80 }
81
Popular Tags