blob: e251196ab8ef6dc6f62bfafdf20997125d829b49 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.management.remote;
27
28
29import com.sun.jmx.remote.util.ClassLogger;
30import com.sun.jmx.remote.util.EnvHelp;
31
32import java.io.IOException;
33import java.net.MalformedURLException;
34import java.util.Collections;
35import java.util.HashMap;
36import java.util.Iterator;
37import java.util.Map;
38import java.util.ServiceLoader;
39
40import javax.management.MBeanServer;
41import javax.management.ObjectName;
42
43/**
44 * <p>Factory to create JMX API connector servers. There
45 * are no instances of this class.</p>
46 *
47 * <p>Each connector server is created by an instance of {@link
48 * JMXConnectorServerProvider}. This instance is found as follows. Suppose
49 * the given {@link JMXServiceURL} looks like
50 * <code>"service:jmx:<em>protocol</em>:<em>remainder</em>"</code>.
51 * Then the factory will attempt to find the appropriate {@link
52 * JMXConnectorServerProvider} for <code><em>protocol</em></code>. Each
53 * occurrence of the character <code>+</code> or <code>-</code> in
54 * <code><em>protocol</em></code> is replaced by <code>.</code> or
55 * <code>_</code>, respectively.</p>
56 *
57 * <p>A <em>provider package list</em> is searched for as follows:</p>
58 *
59 * <ol>
60 *
61 * <li>If the <code>environment</code> parameter to {@link
62 * #newJMXConnectorServer(JMXServiceURL,Map,MBeanServer)
63 * newJMXConnectorServer} contains the key
64 * <code>jmx.remote.protocol.provider.pkgs</code> then the associated
65 * value is the provider package list.
66 *
67 * <li>Otherwise, if the system property
68 * <code>jmx.remote.protocol.provider.pkgs</code> exists, then its value
69 * is the provider package list.
70 *
71 * <li>Otherwise, there is no provider package list.
72 *
73 * </ol>
74 *
75 * <p>The provider package list is a string that is interpreted as a
76 * list of non-empty Java package names separated by vertical bars
77 * (<code>|</code>). If the string is empty, then so is the provider
78 * package list. If the provider package list is not a String, or if
79 * it contains an element that is an empty string, a {@link
80 * JMXProviderException} is thrown.</p>
81 *
82 * <p>If the provider package list exists and is not empty, then for
83 * each element <code><em>pkg</em></code> of the list, the factory
84 * will attempt to load the class
85 *
86 * <blockquote>
87 * <code><em>pkg</em>.<em>protocol</em>.ServerProvider</code>
88 * </blockquote>
89
90 * <p>If the <code>environment</code> parameter to {@link
91 * #newJMXConnectorServer(JMXServiceURL, Map, MBeanServer)
92 * newJMXConnectorServer} contains the key
93 * <code>jmx.remote.protocol.provider.class.loader</code> then the
94 * associated value is the class loader to use to load the provider.
95 * If the associated value is not an instance of {@link
96 * java.lang.ClassLoader}, an {@link
97 * java.lang.IllegalArgumentException} is thrown.</p>
98 *
99 * <p>If the <code>jmx.remote.protocol.provider.class.loader</code>
100 * key is not present in the <code>environment</code> parameter, the
101 * calling thread's context class loader is used.</p>
102 *
103 * <p>If the attempt to load this class produces a {@link
104 * ClassNotFoundException}, the search for a handler continues with
105 * the next element of the list.</p>
106 *
107 * <p>Otherwise, a problem with the provider found is signalled by a
108 * {@link JMXProviderException} whose {@link
109 * JMXProviderException#getCause() <em>cause</em>} indicates the
110 * underlying exception, as follows:</p>
111 *
112 * <ul>
113 *
114 * <li>if the attempt to load the class produces an exception other
115 * than <code>ClassNotFoundException</code>, that is the
116 * <em>cause</em>;
117 *
118 * <li>if {@link Class#newInstance()} for the class produces an
119 * exception, that is the <em>cause</em>.
120 *
121 * </ul>
122 *
123 * <p>If no provider is found by the above steps, including the
124 * default case where there is no provider package list, then the
125 * implementation will use its own provider for
126 * <code><em>protocol</em></code>, or it will throw a
127 * <code>MalformedURLException</code> if there is none. An
128 * implementation may choose to find providers by other means. For
129 * example, it may support the <a
130 * href="{@docRoot}/../technotes/guides/jar/jar.html#Service Provider">
131 * JAR conventions for service providers</a>, where the service
132 * interface is <code>JMXConnectorServerProvider</code>.</p>
133 *
134 * <p>Every implementation must support the RMI connector protocols,
135 * specified with the string <code>rmi</code> or
136 * <code>iiop</code>.</p>
137 *
138 * <p>Once a provider is found, the result of the
139 * <code>newJMXConnectorServer</code> method is the result of calling
140 * {@link
141 * JMXConnectorServerProvider#newJMXConnectorServer(JMXServiceURL,
142 * Map, MBeanServer) newJMXConnectorServer} on the provider.</p>
143 *
144 * <p>The <code>Map</code> parameter passed to the
145 * <code>JMXConnectorServerProvider</code> is a new read-only
146 * <code>Map</code> that contains all the entries that were in the
147 * <code>environment</code> parameter to {@link
148 * #newJMXConnectorServer(JMXServiceURL,Map,MBeanServer)
149 * JMXConnectorServerFactory.newJMXConnectorServer}, if there was one.
150 * Additionally, if the
151 * <code>jmx.remote.protocol.provider.class.loader</code> key is not
152 * present in the <code>environment</code> parameter, it is added to
153 * the new read-only <code>Map</code>. The associated value is the
154 * calling thread's context class loader.</p>
155 *
156 * @since 1.5
157 */
158public class JMXConnectorServerFactory {
159
160 /**
161 * <p>Name of the attribute that specifies the default class
162 * loader. This class loader is used to deserialize objects in
163 * requests received from the client, possibly after consulting an
164 * MBean-specific class loader. The value associated with this
165 * attribute is an instance of {@link ClassLoader}.</p>
166 */
167 public static final String DEFAULT_CLASS_LOADER =
168 JMXConnectorFactory.DEFAULT_CLASS_LOADER;
169
170 /**
171 * <p>Name of the attribute that specifies the default class
172 * loader MBean name. This class loader is used to deserialize objects in
173 * requests received from the client, possibly after consulting an
174 * MBean-specific class loader. The value associated with this
175 * attribute is an instance of {@link ObjectName}.</p>
176 */
177 public static final String DEFAULT_CLASS_LOADER_NAME =
178 "jmx.remote.default.class.loader.name";
179
180 /**
181 * <p>Name of the attribute that specifies the provider packages
182 * that are consulted when looking for the handler for a protocol.
183 * The value associated with this attribute is a string with
184 * package names separated by vertical bars (<code>|</code>).</p>
185 */
186 public static final String PROTOCOL_PROVIDER_PACKAGES =
187 "jmx.remote.protocol.provider.pkgs";
188
189 /**
190 * <p>Name of the attribute that specifies the class
191 * loader for loading protocol providers.
192 * The value associated with this attribute is an instance
193 * of {@link ClassLoader}.</p>
194 */
195 public static final String PROTOCOL_PROVIDER_CLASS_LOADER =
196 "jmx.remote.protocol.provider.class.loader";
197
198 private static final String PROTOCOL_PROVIDER_DEFAULT_PACKAGE =
199 "com.sun.jmx.remote.protocol";
200
201 private static final ClassLogger logger =
202 new ClassLogger("javax.management.remote.misc","JMXConnectorServerFactory");
203
204 /** There are no instances of this class. */
205 private JMXConnectorServerFactory() {
206 }
207
208 private static JMXConnectorServer
209 getConnectorServerAsService(ClassLoader loader,
210 JMXServiceURL url,
211 Map<String, ?> map,
212 MBeanServer mbs)
213 throws IOException {
214 Iterator<JMXConnectorServerProvider> providers =
215 JMXConnectorFactory.
216 getProviderIterator(JMXConnectorServerProvider.class, loader);
217
218 JMXConnectorServer connection = null;
219 IOException exception = null;
220 while (providers.hasNext()) {
221 try {
222 connection = providers.next().newJMXConnectorServer(url, map, mbs);
223 return connection;
224 } catch (JMXProviderException e) {
225 throw e;
226 } catch (Exception e) {
227 if (logger.traceOn())
228 logger.trace("getConnectorAsService",
229 "URL[" + url +
230 "] Service provider exception: " + e);
231 if (!(e instanceof MalformedURLException)) {
232 if (exception == null) {
233 if (exception instanceof IOException) {
234 exception = (IOException) e;
235 } else {
236 exception = EnvHelp.initCause(
237 new IOException(e.getMessage()), e);
238 }
239 }
240 }
241 continue;
242 }
243 }
244 if (exception == null)
245 return null;
246 else
247 throw exception;
248 }
249
250 /**
251 * <p>Creates a connector server at the given address. The
252 * resultant server is not started until its {@link
253 * JMXConnectorServer#start() start} method is called.</p>
254 *
255 * @param serviceURL the address of the new connector server. The
256 * actual address of the new connector server, as returned by its
257 * {@link JMXConnectorServer#getAddress() getAddress} method, will
258 * not necessarily be exactly the same. For example, it might
259 * include a port number if the original address did not.
260 *
261 * @param environment a set of attributes to control the new
262 * connector server's behavior. This parameter can be null.
263 * Keys in this map must be Strings. The appropriate type of each
264 * associated value depends on the attribute. The contents of
265 * <code>environment</code> are not changed by this call.
266 *
267 * @param mbeanServer the MBean server that this connector server
268 * is attached to. Null if this connector server will be attached
269 * to an MBean server by being registered in it.
270 *
271 * @return a <code>JMXConnectorServer</code> representing the new
272 * connector server. Each successful call to this method produces
273 * a different object.
274 *
275 * @exception NullPointerException if <code>serviceURL</code> is null.
276 *
277 * @exception IOException if the connector server cannot be made
278 * because of a communication problem.
279 *
280 * @exception MalformedURLException if there is no provider for the
281 * protocol in <code>serviceURL</code>.
282 *
283 * @exception JMXProviderException if there is a provider for the
284 * protocol in <code>serviceURL</code> but it cannot be used for
285 * some reason.
286 */
287 public static JMXConnectorServer
288 newJMXConnectorServer(JMXServiceURL serviceURL,
289 Map<String,?> environment,
290 MBeanServer mbeanServer)
291 throws IOException {
292 Map<String, Object> envcopy;
293 if (environment == null)
294 envcopy = new HashMap<String, Object>();
295 else {
296 EnvHelp.checkAttributes(environment);
297 envcopy = new HashMap<String, Object>(environment);
298 }
299
300 final Class<JMXConnectorServerProvider> targetInterface =
301 JMXConnectorServerProvider.class;
302 final ClassLoader loader =
303 JMXConnectorFactory.resolveClassLoader(envcopy);
304 final String protocol = serviceURL.getProtocol();
305 final String providerClassName = "ServerProvider";
306
307 JMXConnectorServerProvider provider =
308 JMXConnectorFactory.getProvider(serviceURL,
309 envcopy,
310 providerClassName,
311 targetInterface,
312 loader);
313
314 IOException exception = null;
315 if (provider == null) {
316 // Loader is null when context class loader is set to null
317 // and no loader has been provided in map.
318 // com.sun.jmx.remote.util.Service class extracted from j2se
319 // provider search algorithm doesn't handle well null classloader.
320 if (loader != null) {
321 try {
322 JMXConnectorServer connection =
323 getConnectorServerAsService(loader,
324 serviceURL,
325 envcopy,
326 mbeanServer);
327 if (connection != null)
328 return connection;
329 } catch (JMXProviderException e) {
330 throw e;
331 } catch (IOException e) {
332 exception = e;
333 }
334 }
335 provider =
336 JMXConnectorFactory.getProvider(
337 protocol,
338 PROTOCOL_PROVIDER_DEFAULT_PACKAGE,
339 JMXConnectorFactory.class.getClassLoader(),
340 providerClassName,
341 targetInterface);
342 }
343
344 if (provider == null) {
345 MalformedURLException e =
346 new MalformedURLException("Unsupported protocol: " + protocol);
347 if (exception == null) {
348 throw e;
349 } else {
350 throw EnvHelp.initCause(e, exception);
351 }
352 }
353
354 envcopy = Collections.unmodifiableMap(envcopy);
355
356 return provider.newJMXConnectorServer(serviceURL,
357 envcopy,
358 mbeanServer);
359 }
360}