001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package javax.management;
010:
011: import java.security.AccessController;
012: import java.security.PrivilegedAction;
013: import java.security.PrivilegedActionException;
014: import java.security.PrivilegedExceptionAction;
015: import java.util.ArrayList;
016: import javax.management.loading.ClassLoaderRepository;
017:
018: import mx4j.log.Log;
019: import mx4j.log.Logger;
020:
021: /**
022: * @version $Revision: 1.25 $
023: */
024: public class MBeanServerFactory {
025: private static ArrayList servers = new ArrayList();
026:
027: private MBeanServerFactory() {
028: }
029:
030: private static Logger getLogger() {
031: return Log.getLogger(MBeanServerFactory.class.getName());
032: }
033:
034: public static MBeanServer createMBeanServer() {
035: return createMBeanServer(null);
036: }
037:
038: public static MBeanServer createMBeanServer(String defaultDomain) {
039: MBeanServer server = createMBeanServerImpl(defaultDomain,
040: "createMBeanServer");
041: synchronized (servers) {
042: servers.add(server);
043: }
044: Logger logger = getLogger();
045: if (logger.isEnabledFor(Logger.TRACE))
046: logger.trace("MBeanServer " + server
047: + " registered successfully");
048: return server;
049: }
050:
051: public static MBeanServer newMBeanServer() {
052: return newMBeanServer(null);
053: }
054:
055: public static MBeanServer newMBeanServer(String defaultDomain) {
056: return createMBeanServerImpl(defaultDomain, "newMBeanServer");
057: }
058:
059: public static void releaseMBeanServer(MBeanServer server) {
060: Logger logger = getLogger();
061: try {
062: if (logger.isEnabledFor(Logger.TRACE))
063: logger.trace("Releasing MBeanServer " + server);
064:
065: if (server != null) {
066: SecurityManager sm = System.getSecurityManager();
067: if (sm != null)
068: sm.checkPermission(new MBeanServerPermission(
069: "releaseMBeanServer"));
070:
071: boolean removed = false;
072: synchronized (servers) {
073: removed = servers.remove(server);
074: }
075:
076: if (removed) {
077: if (logger.isEnabledFor(Logger.TRACE))
078: logger.trace("MBeanServer " + server
079: + " released successfully");
080: } else {
081: if (logger.isEnabledFor(Logger.INFO))
082: logger.info("MBeanServer " + server
083: + " not released, cannot find it");
084: }
085: } else {
086: if (logger.isEnabledFor(Logger.DEBUG))
087: logger.debug("Cannot release a null MBeanServer");
088: }
089: } catch (SecurityException x) {
090: if (logger.isEnabledFor(Logger.TRACE))
091: logger.trace(
092: "Security Exception caught while releasing MBeanServer "
093: + server, x);
094: throw x;
095: }
096: }
097:
098: public static ArrayList findMBeanServer(String id) {
099: Logger logger = getLogger();
100: ArrayList list = null;
101: try {
102: if (logger.isEnabledFor(Logger.TRACE))
103: logger.trace("Finding MBeanServer with ID: " + id);
104:
105: SecurityManager sm = System.getSecurityManager();
106: if (sm != null)
107: sm.checkPermission(new MBeanServerPermission(
108: "findMBeanServer"));
109:
110: if (id == null) {
111: list = (ArrayList) servers.clone();
112: } else {
113: list = new ArrayList();
114: synchronized (servers) {
115: for (int i = 0; i < servers.size(); ++i) {
116: MBeanServer server = (MBeanServer) servers
117: .get(i);
118: String serverId = getMBeanServerId(server);
119: if (id.equals(serverId)) {
120: list.add(server);
121: if (logger.isEnabledFor(Logger.TRACE))
122: logger
123: .trace("Found matching MBeanServer: "
124: + server);
125: }
126: }
127: }
128: }
129:
130: if (logger.isEnabledFor(Logger.TRACE))
131: logger.trace("MBeanServer(s) found: " + list);
132:
133: return list;
134: } catch (SecurityException x) {
135: if (logger.isEnabledFor(Logger.TRACE))
136: logger.trace(
137: "Security Exception caught while finding MBeanServer with ID: "
138: + id, x);
139: throw x;
140: }
141: }
142:
143: private static String getMBeanServerId(final MBeanServer server) {
144: try {
145: return (String) AccessController
146: .doPrivileged(new PrivilegedExceptionAction() {
147: public Object run() throws Exception {
148: return server
149: .getAttribute(
150: ObjectName
151: .getInstance("JMImplementation:type=MBeanServerDelegate"),
152: "MBeanServerId");
153: }
154: });
155: } catch (SecurityException x) {
156: Logger logger = getLogger();
157: if (logger.isEnabledFor(Logger.TRACE))
158: logger.trace("No permission to get MBeanServerID", x);
159: } catch (PrivilegedActionException x) {
160: Logger logger = getLogger();
161: if (logger.isEnabledFor(Logger.TRACE))
162: logger.trace("Could not get MBeanServerID", x
163: .getException());
164: } catch (Throwable x) {
165: Logger logger = getLogger();
166: if (logger.isEnabledFor(Logger.TRACE))
167: logger.trace("Could not get MBeanServerID", x);
168: }
169: return null;
170: }
171:
172: public static ClassLoaderRepository getClassLoaderRepository(
173: MBeanServer server) {
174: // Yes, throw NPE is server is null (spec compliant)
175: return server.getClassLoaderRepository();
176: }
177:
178: private static MBeanServer createMBeanServerImpl(String domain,
179: String permission) {
180: Logger logger = getLogger();
181: boolean trace = logger.isEnabledFor(Logger.TRACE);
182: try {
183: SecurityManager sm = System.getSecurityManager();
184: if (sm != null)
185: sm
186: .checkPermission(new MBeanServerPermission(
187: permission));
188:
189: // get MBeanServerBuilder
190:
191: if (trace)
192: logger.trace("Obtaining MBeanServerBuilder");
193: final MBeanServerBuilder builder = createMBeanServerBuilder();
194: if (trace)
195: logger.trace("Using MBeanServerBuilder "
196: + builder.getClass());
197:
198: // create delegate
199:
200: if (trace)
201: logger.trace("Creating MBeanServerDelegate...");
202: final MBeanServerDelegate delegate = builder
203: .newMBeanServerDelegate();
204: if (trace)
205: logger
206: .trace("MBeanServerDelegate "
207: + delegate.getClass()
208: + " created successfully");
209:
210: // create MBean server
211:
212: if (trace)
213: logger.trace("Creating MBeanServer...");
214: MBeanServer mbs = builder.newMBeanServer(domain, null,
215: delegate);
216: if (trace)
217: logger.trace("MBeanServer " + mbs
218: + " created successfully");
219: if (logger.isEnabledFor(Logger.INFO)) {
220: String mbeanServerId = getMBeanServerId(mbs);
221: if (mbeanServerId != null)
222: logger.info("Created MBeanServer with ID: "
223: + mbeanServerId);
224: else
225: logger.info("Created MBeanServer");
226: }
227: return mbs;
228: } catch (SecurityException x) {
229: if (trace)
230: logger
231: .trace(
232: "Security Exception caught while creating an MBeanServer",
233: x);
234: throw x;
235: }
236: }
237:
238: private static MBeanServerBuilder createMBeanServerBuilder() {
239: final Class builderClass = loadMBeanServerBuilderClass();
240: try {
241: return (MBeanServerBuilder) builderClass.newInstance();
242: } catch (ClassCastException e) {
243: throw new JMRuntimeException(
244: "Specified MBeanServerBuilder must be a subclass of MBeanServerBuilder: "
245: + builderClass);
246: } catch (IllegalAccessException e) {
247: throw new JMRuntimeException(
248: "Can't instantiate MBeanServerBuilder "
249: + builderClass + ": " + e);
250: } catch (InstantiationException e) {
251: throw new JMRuntimeException(
252: "Can't instantiate MBeanServerBuilder "
253: + builderClass + ": " + e);
254: }
255: }
256:
257: private static Class loadMBeanServerBuilderClass() {
258: String builderClassName = (String) AccessController
259: .doPrivileged(new PrivilegedAction() {
260: public Object run() {
261: return System
262: .getProperty("javax.management.builder.initial");
263: }
264: });
265:
266: if (builderClassName == null || builderClassName.length() == 0) {
267: return MBeanServerBuilder.class;
268: }
269:
270: try {
271: return Thread.currentThread().getContextClassLoader()
272: .loadClass(builderClassName);
273: } catch (ClassNotFoundException e) {
274: throw new JMRuntimeException(
275: "MBeanServerBuilder class not found: "
276: + builderClassName);
277: }
278: }
279: }
|