1 /* MBeanServerFactory.java -- Manages server instances.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.management;
40 import gnu.classpath.SystemProperties;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.Iterator;
47 import javax.management.loading.ClassLoaderRepository;
51 * Creates and maintains a set of {@link MBeanServer} instances.
52 * Server instances, as of JMX 1.2, are created using a subclass
53 * of {@link MBeanServerBuilder}. The exact class used is controlled
54 * by the property <code>javax.management.builder.initial</code>,
55 * and allows the instances created by {@link MBeanServerBuilder}
56 * to be wrapped, thus providing additional functionality.
59 * The property is used as follows:
62 * <li>If the property has no value, then an instance of
63 * {@link MBeanServerBuilder} is used.</li>
64 * <li>If a value is given, then:
66 * <li>The class is loaded using
67 * <code>Thread.currentThread().getContextClassLoader()</code>, or,
68 * if this is <code>null</code>, by <code>Class.forName()</code>.</li>
69 * <li><code>Class.newInstance()</code> is used to create an instance
70 * of the class. The class must be public and have a public empty
71 * constructor. If an exception is thrown, it is propogated as
72 * a {@link JMRuntimeException} and no new server instances may be
73 * created until the property is set to a valid value.</li>
75 * <li>The value is checked on each successive request for a server.
76 * If it differs from the class of the existing instance of
77 * {@link MBeanServerBuilder}, then the value is used to create
78 * a new instance.</li>
81 public class MBeanServerFactory
85 * The last builder instance.
87 private static MBeanServerBuilder builder;
90 * The map of registered servers (identifiers to servers).
92 private static Map servers;
95 * Private constructor to prevent instance creation.
97 private MBeanServerFactory() {}
100 * Returns a server implementation using the default domain name
101 * of <code>"DefaultDomain"</code>. The default domain name is
102 * used when the domain name specified by the user is <code>null</code.
103 * A reference to the created server is retained, so that it can
104 * be retrieved at a later date using {@link #findMBeanServer}.
105 * Calling this method is equivalent to calling
106 * {@link createMBeanServer(String)} with a <code>null</code> value.
108 * @return a new {@link MBeanServer} instance.
109 * @throws SecurityException if a security manager exists and the
110 * caller's permissions don't imply {@link
111 * MBeanServerPermission(String)}("createMBeanServer")
112 * @throws JMRuntimeException if the property
113 * <code>javax.management.builder.initial</code>
114 * exists but names a class which either can not be
115 * instantiated or provides an implementation that returns
116 * <code>null</code> from either
117 * {@link MBeanServerBuilder#newMBeanServerDelegate()}
118 * or {@link MBeanServerBuilder#newMBeanServer()}
119 * @throws ClassCastException if the property
120 * <code>javax.management.builder.initial</code>
121 * exists but names a class which is not a subclass
122 * of {@link MBeanServerBuilder}.
123 * @see #createMBeanServer(String)
125 public static MBeanServer createMBeanServer()
127 return createMBeanServer(null);
131 * Returns a server implementation using the default domain name
132 * given, or <code>"DefaultDomain"</code> if this is <code>null</code>.
133 * The default domain name is used when the domain name specified by
134 * the user is <code>null</code. A reference to the created server is
135 * retained, so that it can be retrieved at a later date using
136 * {@link #findMBeanServer}.
138 * @param domain the default domain name of the server.
139 * @return a new {@link MBeanServer} instance.
140 * @throws SecurityException if a security manager exists and the
141 * caller's permissions don't imply {@link
142 * MBeanServerPermission(String)}("createMBeanServer")
143 * @throws JMRuntimeException if the property
144 * <code>javax.management.builder.initial</code>
145 * exists but names a class which either can not be
146 * instantiated or provides an implementation that returns
147 * <code>null</code> from either
148 * {@link MBeanServerBuilder#newMBeanServerDelegate()}
149 * or {@link MBeanServerBuilder#newMBeanServer()}
150 * @throws ClassCastException if the property
151 * <code>javax.management.builder.initial</code>
152 * exists but names a class which is not a subclass
153 * of {@link MBeanServerBuilder}.
155 public static MBeanServer createMBeanServer(String domain)
157 SecurityManager sm = System.getSecurityManager();
159 sm.checkPermission(new MBeanServerPermission("createMBeanServer"));
160 MBeanServer server = createServer(domain);
162 servers = new HashMap();
166 ObjectName("JMImplementation:type=MBeanServerDelegate");
167 servers.put(server.getAttribute(dn, "MBeanServerId"), server);
169 catch (MalformedObjectNameException e)
172 (new InternalError("Malformed delegate bean name.").initCause(e));
174 catch (MBeanException e)
177 (new InternalError("Exception in getMBeanServerId().").initCause(e));
179 catch (AttributeNotFoundException e)
182 (new InternalError("Could not find MBeanServerId attribute.").initCause(e));
184 catch (InstanceNotFoundException e)
187 (new InternalError("Could not find the delegate bean.").initCause(e));
189 catch (ReflectionException e)
192 (new InternalError("Could not call getMBeanServerId().").initCause(e));
198 * Returns the specified server, or, if <code>id</code> is <code>null</code>,
199 * a list of all registered servers. A registered server is one that
200 * was created using {@link #createMBeanServer()} or
201 * {@link #createMBeanServer(String)} and has not yet been released
202 * using {@link releaseMBeanServer(MBeanServer)}.
204 * @param id the id of the server to retrieve, or <code>null</code>
205 * to return all servers.
206 * @return a list of {@link MBeanServer}s.
207 * @throws SecurityException if a security manager exists and the
208 * caller's permissions don't imply {@link
209 * MBeanServerPermission(String)}("findMBeanServer")
211 public static ArrayList findMBeanServer(String id)
213 SecurityManager sm = System.getSecurityManager();
215 sm.checkPermission(new MBeanServerPermission("findMBeanServer"));
217 return new ArrayList(servers.values());
218 ArrayList list = new ArrayList();
219 MBeanServer server = (MBeanServer) servers.get(id);
221 list.add(servers.get(id));
226 * Returns the class loader repository used by the specified server.
227 * This is equivalent to calling {@link MBeanServer#getClassLoaderRepository()}
228 * on the given server.
230 * @param server the server whose class loader repository should be
232 * @throws NullPointerException if <code>server</code> is <code>null</code>.
233 * @throws SecurityException if a security manager exists and the
234 * caller's permissions don't imply {@link
235 * MBeanPermission(String,String,ObjectName,String)
236 * <code>MBeanPermission(null, null, null,
237 * "getClassLoaderRepository")</code>
239 public static ClassLoaderRepository getClassLoaderRepository(MBeanServer server)
241 return server.getClassLoaderRepository();
245 * Returns a server implementation using the default domain name
246 * of <code>"DefaultDomain"</code>. The default domain name is
247 * used when the domain name specified by the user is <code>null</code.
248 * No reference to the created server is retained, so the server is
249 * garbage collected when it is no longer used, but it can not be
250 * retrieved at a later date using {@link #findMBeanServer}.
251 * Calling this method is equivalent to calling
252 * {@link newMBeanServer(String)} with a <code>null</code> value.
254 * @return a new {@link MBeanServer} instance.
255 * @throws SecurityException if a security manager exists and the
256 * caller's permissions don't imply {@link
257 * MBeanServerPermission(String)}("newMBeanServer")
258 * @throws JMRuntimeException if the property
259 * <code>javax.management.builder.initial</code>
260 * exists but names a class which either can not be
261 * instantiated or provides an implementation that returns
262 * <code>null</code> from either
263 * {@link MBeanServerBuilder#newMBeanServerDelegate()}
264 * or {@link MBeanServerBuilder#newMBeanServer()}
265 * @throws ClassCastException if the property
266 * <code>javax.management.builder.initial</code>
267 * exists but names a class which is not a subclass
268 * of {@link MBeanServerBuilder}.
269 * @see #newMBeanServer(String)
271 public static MBeanServer newMBeanServer()
273 return newMBeanServer(null);
277 * Returns a server implementation using the default domain name
278 * given, or <code>"DefaultDomain"</code> if this is <code>null</code>.
279 * The default domain name is used when the domain name specified by
280 * the user is <code>null</code. No reference to the created server is
281 * retained, so the server is garbage collected when it is no longer
282 * used, but it can not be retrieved at a later date using
283 * {@link #findMBeanServer}.
285 * @param domain the default domain name of the server.
286 * @return a new {@link MBeanServer} instance.
287 * @throws SecurityException if a security manager exists and the
288 * caller's permissions don't imply {@link
289 * MBeanServerPermission(String)}("newMBeanServer")
290 * @throws JMRuntimeException if the property
291 * <code>javax.management.builder.initial</code>
292 * exists but names a class which either can not be
293 * instantiated or provides an implementation that returns
294 * <code>null</code> from either
295 * {@link MBeanServerBuilder#newMBeanServerDelegate()}
296 * or {@link MBeanServerBuilder#newMBeanServer()}
297 * @throws ClassCastException if the property
298 * <code>javax.management.builder.initial</code>
299 * exists but names a class which is not a subclass
300 * of {@link MBeanServerBuilder}.
302 public static MBeanServer newMBeanServer(String domain)
304 SecurityManager sm = System.getSecurityManager();
306 sm.checkPermission(new MBeanServerPermission("newMBeanServer"));
307 return createServer(domain);
311 * Common method to create a server for the {@link #createMBeanServer(String)}
312 * and {@link #newMBeanServer(String)} methods above.
314 * @param domain the default domain name of the server.
315 * @throws JMRuntimeException if the property
316 * <code>javax.management.builder.initial</code>
317 * exists but names a class which either can not be
318 * instantiated or provides an implementation that returns
319 * <code>null</code> from either
320 * {@link MBeanServerBuilder#newMBeanServerDelegate()}
321 * or {@link MBeanServerBuilder#newMBeanServer()}
322 * @throws ClassCastException if the property
323 * <code>javax.management.builder.initial</code>
324 * exists but names a class which is not a subclass
325 * of {@link MBeanServerBuilder}.
327 private static MBeanServer createServer(String domain)
330 domain = "DefaultDomain";
331 String builderClass =
332 SystemProperties.getProperty("javax.management.builder.initial");
333 if (builderClass == null)
335 if (builder == null ||
336 builder.getClass() != MBeanServerBuilder.class)
337 builder = new MBeanServerBuilder();
339 else if (!(builderClass.equals(builder.getClass().getName())))
341 ClassLoader cl = Thread.currentThread().getContextClassLoader();
343 cl = MBeanServerFactory.class.getClassLoader();
346 Class bClass = Class.forName(builderClass, true, cl);
347 builder = (MBeanServerBuilder) bClass.newInstance();
349 catch (ClassNotFoundException e)
351 throw (JMRuntimeException) (new JMRuntimeException("The builder class, "
353 ", could not be found."))
356 catch (InstantiationException e)
358 throw (JMRuntimeException) (new JMRuntimeException("The builder class, "
360 ", could not be instantiated."))
363 catch (IllegalAccessException e)
365 throw (JMRuntimeException) (new JMRuntimeException("The builder class, "
367 ", could not be accessed."))
371 MBeanServerDelegate delegate = builder.newMBeanServerDelegate();
372 if (delegate == null)
373 throw new JMRuntimeException("A delegate could not be created.");
374 MBeanServer server = builder.newMBeanServer("DefaultDomain", null, delegate);
376 throw new JMRuntimeException("A server could not be created.");
381 * Removes the reference to the specified server, thus allowing it to
382 * be garbage collected.
384 * @param server the server to remove.
385 * @throws IllegalArgumentException if a reference to the server is not
386 * held (i.e. it wasn't created by
387 * {@link #createMBeanServer(String)}
388 * or this method has already been called
390 * @throws SecurityException if a security manager exists and the
391 * caller's permissions don't imply {@link
392 * MBeanServerPermission(String)}("releaseMBeanServer")
394 public static void releaseMBeanServer(MBeanServer server)
396 SecurityManager sm = System.getSecurityManager();
398 sm.checkPermission(new MBeanServerPermission("releaseMBeanServer"));
399 Iterator i = servers.values().iterator();
402 MBeanServer s = (MBeanServer) i.next();
409 throw new IllegalArgumentException("The server given is not referenced.");