KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > util > MBeanServerLocator


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.util;
23
24 import java.util.Iterator JavaDoc;
25
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28
29 /**
30  * A helper class to locate an MBeanServer.
31  *
32  * MBeanServer lookup strategy enhanced to allow the explicit
33  * setting of a particular instance to be returned. This is needed to
34  * allow re-using the jdk5 ManagementFactory.getPlatformMBeanServer()
35  * as our main MBeanServer. The DefaultDomain name of this server cannot
36  * be set, and it seems to be "null" by default (probably a bug).
37  *
38  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
39  * @author Scott.Stark@jboss.org
40  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
41  * @version $Revision: 37459 $
42  */

43 public class MBeanServerLocator
44 {
45    /** The MBeanServer to return, if set */
46    private static MBeanServer JavaDoc instance = null;
47    
48    /**
49     * Private CTOR to disable instantiation
50     */

51    private MBeanServerLocator()
52    {
53       // empty
54
}
55
56    /**
57     * Optionally set the MBeanServer to be returned
58     * by calls to locateJBoss(). Setting this back
59     * to null reverts to the normal lookup strategy.
60     *
61     * @param server the main jboss MBeanServer or null
62     */

63    public static void setJBoss(MBeanServer JavaDoc server)
64    {
65       synchronized (MBeanServerLocator.class)
66       {
67          instance = server;
68       }
69    }
70    
71    /**
72     * Returns the first MBeanServer registered under the agentID
73     *
74     * @param agentID the id of the MBeanServer to look for
75     * @return the first MBeanServer with an agentID
76     */

77    public static MBeanServer JavaDoc locate(final String JavaDoc agentID)
78    {
79       MBeanServer JavaDoc server = (MBeanServer JavaDoc)
80          MBeanServerFactory.findMBeanServer(agentID).iterator().next();
81
82       return server;
83    }
84
85    /**
86     * Returns the first available MBeanServer
87     *
88     * @return the first available MBeanServer
89     */

90    public static MBeanServer JavaDoc locate()
91    {
92       return locate(null);
93    }
94
95    /**
96     * Returns the main jboss MBeanServer.
97     *
98     * If there is one set using setJBoss(), it will be returned.
99     * Otherwise the strategy is to return the first MBeanServer
100     * registered under the "jboss" id (or else, default domain name)
101     *
102     * @return the main jboss MBeanServer
103     * @throws IllegalStateException when no MBeanServer can be found
104     */

105    public static MBeanServer JavaDoc locateJBoss()
106    {
107       synchronized (MBeanServerLocator.class)
108       {
109          if (instance != null)
110          {
111             return instance;
112          }
113       }
114       for (Iterator JavaDoc i = MBeanServerFactory.findMBeanServer(null).iterator(); i.hasNext(); )
115       {
116          MBeanServer JavaDoc server = (MBeanServer JavaDoc) i.next();
117          if (server.getDefaultDomain().equals("jboss"))
118          {
119             return server;
120          }
121       }
122       throw new IllegalStateException JavaDoc("No 'jboss' MBeanServer found!");
123    }
124 }
125
Popular Tags