001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.util;
023:
024: import java.util.Iterator;
025:
026: import javax.management.MBeanServer;
027: import javax.management.MBeanServerFactory;
028:
029: /**
030: * A helper class to locate an MBeanServer.
031: *
032: * MBeanServer lookup strategy enhanced to allow the explicit
033: * setting of a particular instance to be returned. This is needed to
034: * allow re-using the jdk5 ManagementFactory.getPlatformMBeanServer()
035: * as our main MBeanServer. The DefaultDomain name of this server cannot
036: * be set, and it seems to be "null" by default (probably a bug).
037: *
038: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
039: * @author Scott.Stark@jboss.org
040: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
041: * @version $Revision: 57200 $
042: */
043: public class MBeanServerLocator {
044: /** The MBeanServer to return, if set */
045: private static MBeanServer instance = null;
046:
047: /**
048: * Private CTOR to disable instantiation
049: */
050: private MBeanServerLocator() {
051: // empty
052: }
053:
054: /**
055: * Optionally set the MBeanServer to be returned
056: * by calls to locateJBoss(). Setting this back
057: * to null reverts to the normal lookup strategy.
058: *
059: * @param server the main jboss MBeanServer or null
060: */
061: public static void setJBoss(MBeanServer server) {
062: synchronized (MBeanServerLocator.class) {
063: instance = server;
064: }
065: }
066:
067: /**
068: * Returns the first MBeanServer registered under the agentID
069: *
070: * @param agentID the id of the MBeanServer to look for
071: * @return the first MBeanServer with an agentID
072: */
073: public static MBeanServer locate(final String agentID) {
074: MBeanServer server = (MBeanServer) MBeanServerFactory
075: .findMBeanServer(agentID).iterator().next();
076:
077: return server;
078: }
079:
080: /**
081: * Returns the first available MBeanServer
082: *
083: * @return the first available MBeanServer
084: */
085: public static MBeanServer locate() {
086: return locate(null);
087: }
088:
089: /**
090: * Returns the main jboss MBeanServer.
091: *
092: * If there is one set using setJBoss(), it will be returned.
093: * Otherwise the strategy is to return the first MBeanServer
094: * registered under the "jboss" id (or else, default domain name)
095: *
096: * @return the main jboss MBeanServer
097: * @throws IllegalStateException when no MBeanServer can be found
098: */
099: public static MBeanServer locateJBoss() {
100: synchronized (MBeanServerLocator.class) {
101: if (instance != null) {
102: return instance;
103: }
104: }
105: for (Iterator i = MBeanServerFactory.findMBeanServer(null)
106: .iterator(); i.hasNext();) {
107: MBeanServer server = (MBeanServer) i.next();
108: if (server.getDefaultDomain().equals("jboss")) {
109: return server;
110: }
111: }
112: throw new IllegalStateException("No 'jboss' MBeanServer found!");
113: }
114: }
|