2
|
1 |
/*
|
|
2 |
* Copyright 2003-2006 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 |
|
|
26 |
package com.sun.jmx.remote.util;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.ObjectOutputStream;
|
|
30 |
import java.io.OutputStream;
|
|
31 |
import java.util.Collection;
|
|
32 |
import java.util.HashMap;
|
|
33 |
import java.util.Hashtable;
|
|
34 |
import java.util.Iterator;
|
|
35 |
import java.util.Map;
|
|
36 |
import java.util.SortedMap;
|
|
37 |
import java.util.SortedSet;
|
|
38 |
import java.util.StringTokenizer;
|
|
39 |
import java.util.TreeMap;
|
|
40 |
import java.util.TreeSet;
|
|
41 |
|
|
42 |
import java.security.AccessController;
|
|
43 |
|
|
44 |
import javax.management.ObjectName;
|
|
45 |
import javax.management.MBeanServer;
|
|
46 |
import javax.management.InstanceNotFoundException;
|
|
47 |
import javax.management.remote.JMXConnectorFactory;
|
|
48 |
import javax.management.remote.JMXConnectorServerFactory;
|
|
49 |
import com.sun.jmx.mbeanserver.GetPropertyAction;
|
1004
|
50 |
import com.sun.jmx.remote.security.NotificationAccessController;
|
|
51 |
import javax.management.remote.JMXConnector;
|
|
52 |
import javax.management.remote.JMXConnectorServer;
|
2
|
53 |
|
|
54 |
public class EnvHelp {
|
|
55 |
|
|
56 |
/**
|
|
57 |
* <p>Name of the attribute that specifies a default class loader
|
|
58 |
* object.
|
|
59 |
* The value associated with this attribute is a ClassLoader object</p>
|
|
60 |
*/
|
|
61 |
private static final String DEFAULT_CLASS_LOADER =
|
|
62 |
JMXConnectorFactory.DEFAULT_CLASS_LOADER;
|
|
63 |
|
|
64 |
/**
|
|
65 |
* <p>Name of the attribute that specifies a default class loader
|
|
66 |
* ObjectName.
|
|
67 |
* The value associated with this attribute is an ObjectName object</p>
|
|
68 |
*/
|
|
69 |
private static final String DEFAULT_CLASS_LOADER_NAME =
|
|
70 |
JMXConnectorServerFactory.DEFAULT_CLASS_LOADER_NAME;
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Get the Connector Server default class loader.
|
|
74 |
* <p>
|
|
75 |
* Returns:
|
|
76 |
* <p>
|
|
77 |
* <ul>
|
|
78 |
* <li>
|
|
79 |
* The ClassLoader object found in <var>env</var> for
|
|
80 |
* <code>jmx.remote.default.class.loader</code>, if any.
|
|
81 |
* </li>
|
|
82 |
* <li>
|
|
83 |
* The ClassLoader pointed to by the ObjectName found in
|
|
84 |
* <var>env</var> for <code>jmx.remote.default.class.loader.name</code>,
|
|
85 |
* and registered in <var>mbs</var> if any.
|
|
86 |
* </li>
|
|
87 |
* <li>
|
|
88 |
* The current thread's context classloader otherwise.
|
|
89 |
* </li>
|
|
90 |
* </ul>
|
|
91 |
*
|
|
92 |
* @param env Environment attributes.
|
|
93 |
* @param mbs The MBeanServer for which the connector server provides
|
|
94 |
* remote access.
|
|
95 |
*
|
|
96 |
* @return the connector server's default class loader.
|
|
97 |
*
|
|
98 |
* @exception IllegalArgumentException if one of the following is true:
|
|
99 |
* <ul>
|
|
100 |
* <li>both
|
|
101 |
* <code>jmx.remote.default.class.loader</code> and
|
|
102 |
* <code>jmx.remote.default.class.loader.name</code> are specified,
|
|
103 |
* </li>
|
|
104 |
* <li>or
|
|
105 |
* <code>jmx.remote.default.class.loader</code> is not
|
|
106 |
* an instance of {@link ClassLoader},
|
|
107 |
* </li>
|
|
108 |
* <li>or
|
|
109 |
* <code>jmx.remote.default.class.loader.name</code> is not
|
|
110 |
* an instance of {@link ObjectName},
|
|
111 |
* </li>
|
|
112 |
* <li>or
|
|
113 |
* <code>jmx.remote.default.class.loader.name</code> is specified
|
|
114 |
* but <var>mbs</var> is null.
|
|
115 |
* </li>
|
|
116 |
* @exception InstanceNotFoundException if
|
|
117 |
* <code>jmx.remote.default.class.loader.name</code> is specified
|
|
118 |
* and the ClassLoader MBean is not found in <var>mbs</var>.
|
|
119 |
*/
|
|
120 |
public static ClassLoader resolveServerClassLoader(Map env,
|
|
121 |
MBeanServer mbs)
|
|
122 |
throws InstanceNotFoundException {
|
|
123 |
|
|
124 |
if (env == null)
|
|
125 |
return Thread.currentThread().getContextClassLoader();
|
|
126 |
|
|
127 |
Object loader = env.get(DEFAULT_CLASS_LOADER);
|
|
128 |
Object name = env.get(DEFAULT_CLASS_LOADER_NAME);
|
|
129 |
|
|
130 |
if (loader != null && name != null) {
|
|
131 |
final String msg = "Only one of " +
|
|
132 |
DEFAULT_CLASS_LOADER + " or " +
|
|
133 |
DEFAULT_CLASS_LOADER_NAME +
|
|
134 |
" should be specified.";
|
|
135 |
throw new IllegalArgumentException(msg);
|
|
136 |
}
|
|
137 |
|
|
138 |
if (loader == null && name == null)
|
|
139 |
return Thread.currentThread().getContextClassLoader();
|
|
140 |
|
|
141 |
if (loader != null) {
|
|
142 |
if (loader instanceof ClassLoader) {
|
|
143 |
return (ClassLoader) loader;
|
|
144 |
} else {
|
|
145 |
final String msg =
|
|
146 |
"ClassLoader object is not an instance of " +
|
|
147 |
ClassLoader.class.getName() + " : " +
|
|
148 |
loader.getClass().getName();
|
|
149 |
throw new IllegalArgumentException(msg);
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
|
153 |
ObjectName on;
|
|
154 |
if (name instanceof ObjectName) {
|
|
155 |
on = (ObjectName) name;
|
|
156 |
} else {
|
|
157 |
final String msg =
|
|
158 |
"ClassLoader name is not an instance of " +
|
|
159 |
ObjectName.class.getName() + " : " +
|
|
160 |
name.getClass().getName();
|
|
161 |
throw new IllegalArgumentException(msg);
|
|
162 |
}
|
|
163 |
|
|
164 |
if (mbs == null)
|
|
165 |
throw new IllegalArgumentException("Null MBeanServer object");
|
|
166 |
|
|
167 |
return mbs.getClassLoader(on);
|
|
168 |
}
|
|
169 |
|
|
170 |
/**
|
|
171 |
* Get the Connector Client default class loader.
|
|
172 |
* <p>
|
|
173 |
* Returns:
|
|
174 |
* <p>
|
|
175 |
* <ul>
|
|
176 |
* <li>
|
|
177 |
* The ClassLoader object found in <var>env</var> for
|
|
178 |
* <code>jmx.remote.default.class.loader</code>, if any.
|
|
179 |
* </li>
|
|
180 |
* <li>The <tt>Thread.currentThread().getContextClassLoader()</tt>
|
|
181 |
* otherwise.
|
|
182 |
* </li>
|
|
183 |
* </ul>
|
|
184 |
* <p>
|
|
185 |
* Usually a Connector Client will call
|
|
186 |
* <pre>
|
|
187 |
* ClassLoader dcl = EnvHelp.resolveClientClassLoader(env);
|
|
188 |
* </pre>
|
|
189 |
* in its <code>connect(Map env)</code> method.
|
|
190 |
*
|
|
191 |
* @return The connector client default class loader.
|
|
192 |
*
|
|
193 |
* @exception IllegalArgumentException if
|
|
194 |
* <code>jmx.remote.default.class.loader</code> is specified
|
|
195 |
* and is not an instance of {@link ClassLoader}.
|
|
196 |
*/
|
|
197 |
public static ClassLoader resolveClientClassLoader(Map env) {
|
|
198 |
|
|
199 |
if (env == null)
|
|
200 |
return Thread.currentThread().getContextClassLoader();
|
|
201 |
|
|
202 |
Object loader = env.get(DEFAULT_CLASS_LOADER);
|
|
203 |
|
|
204 |
if (loader == null)
|
|
205 |
return Thread.currentThread().getContextClassLoader();
|
|
206 |
|
|
207 |
if (loader instanceof ClassLoader) {
|
|
208 |
return (ClassLoader) loader;
|
|
209 |
} else {
|
|
210 |
final String msg =
|
|
211 |
"ClassLoader object is not an instance of " +
|
|
212 |
ClassLoader.class.getName() + " : " +
|
|
213 |
loader.getClass().getName();
|
|
214 |
throw new IllegalArgumentException(msg);
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|
|
218 |
/**
|
|
219 |
* Initialize the cause field of a {@code Throwable} object.
|
|
220 |
*
|
|
221 |
* @param throwable The {@code Throwable} on which the cause is set.
|
|
222 |
* @param cause The cause to set on the supplied {@code Throwable}.
|
|
223 |
* @return the {@code Throwable} with the cause field initialized.
|
|
224 |
*/
|
|
225 |
public static <T extends Throwable> T initCause(T throwable,
|
|
226 |
Throwable cause) {
|
|
227 |
throwable.initCause(cause);
|
|
228 |
return throwable;
|
|
229 |
}
|
|
230 |
|
|
231 |
/**
|
|
232 |
* Returns the cause field of a {@code Throwable} object.
|
|
233 |
* The cause field can be got only if <var>t</var> has an
|
|
234 |
* {@link Throwable#getCause()} method (JDK Version >= 1.4)
|
|
235 |
* @param t {@code Throwable} on which the cause must be set.
|
|
236 |
* @return the cause if getCause() succeeded and the got value is not
|
|
237 |
* null, otherwise return the <var>t</var>.
|
|
238 |
*/
|
|
239 |
public static Throwable getCause(Throwable t) {
|
|
240 |
Throwable ret = t;
|
|
241 |
|
|
242 |
try {
|
|
243 |
java.lang.reflect.Method getCause =
|
|
244 |
t.getClass().getMethod("getCause", (Class[]) null);
|
|
245 |
ret = (Throwable)getCause.invoke(t, (Object[]) null);
|
|
246 |
|
|
247 |
} catch (Exception e) {
|
|
248 |
// OK.
|
|
249 |
// it must be older than 1.4.
|
|
250 |
}
|
|
251 |
return (ret != null) ? ret: t;
|
|
252 |
}
|
|
253 |
|
|
254 |
|
|
255 |
/**
|
|
256 |
* <p>Name of the attribute that specifies the size of a notification
|
|
257 |
* buffer for a connector server. The default value is 1000.
|
|
258 |
*/
|
|
259 |
public static final String BUFFER_SIZE_PROPERTY =
|
|
260 |
"jmx.remote.x.notification.buffer.size";
|
|
261 |
|
|
262 |
|
|
263 |
/**
|
|
264 |
* Returns the size of a notification buffer for a connector server.
|
|
265 |
* The default value is 1000.
|
|
266 |
*/
|
|
267 |
public static int getNotifBufferSize(Map env) {
|
|
268 |
int defaultQueueSize = 1000; // default value
|
|
269 |
|
|
270 |
// keep it for the compability for the fix:
|
|
271 |
// 6174229: Environment parameter should be notification.buffer.size
|
|
272 |
// instead of buffer.size
|
|
273 |
final String oldP = "jmx.remote.x.buffer.size";
|
|
274 |
|
|
275 |
// the default value re-specified in the system
|
|
276 |
try {
|
|
277 |
GetPropertyAction act = new GetPropertyAction(BUFFER_SIZE_PROPERTY);
|
|
278 |
String s = AccessController.doPrivileged(act);
|
|
279 |
if (s != null) {
|
|
280 |
defaultQueueSize = Integer.parseInt(s);
|
|
281 |
} else { // try the old one
|
|
282 |
act = new GetPropertyAction(oldP);
|
|
283 |
s = AccessController.doPrivileged(act);
|
|
284 |
if (s != null) {
|
|
285 |
defaultQueueSize = Integer.parseInt(s);
|
|
286 |
}
|
|
287 |
}
|
|
288 |
} catch (RuntimeException e) {
|
|
289 |
logger.warning("getNotifBufferSize",
|
|
290 |
"Can't use System property "+
|
|
291 |
BUFFER_SIZE_PROPERTY+ ": " + e);
|
|
292 |
logger.debug("getNotifBufferSize", e);
|
|
293 |
}
|
|
294 |
|
|
295 |
int queueSize = defaultQueueSize;
|
|
296 |
|
|
297 |
try {
|
|
298 |
if (env.containsKey(BUFFER_SIZE_PROPERTY)) {
|
|
299 |
queueSize = (int)EnvHelp.getIntegerAttribute(env,BUFFER_SIZE_PROPERTY,
|
|
300 |
defaultQueueSize,0,
|
|
301 |
Integer.MAX_VALUE);
|
|
302 |
} else { // try the old one
|
|
303 |
queueSize = (int)EnvHelp.getIntegerAttribute(env,oldP,
|
|
304 |
defaultQueueSize,0,
|
|
305 |
Integer.MAX_VALUE);
|
|
306 |
}
|
|
307 |
} catch (RuntimeException e) {
|
|
308 |
logger.warning("getNotifBufferSize",
|
|
309 |
"Can't determine queuesize (using default): "+
|
|
310 |
e);
|
|
311 |
logger.debug("getNotifBufferSize", e);
|
|
312 |
}
|
|
313 |
|
|
314 |
return queueSize;
|
|
315 |
}
|
|
316 |
|
|
317 |
/**
|
|
318 |
* <p>Name of the attribute that specifies the maximum number of
|
|
319 |
* notifications that a client will fetch from its server.. The
|
|
320 |
* value associated with this attribute should be an
|
|
321 |
* <code>Integer</code> object. The default value is 1000.</p>
|
|
322 |
*/
|
|
323 |
public static final String MAX_FETCH_NOTIFS =
|
|
324 |
"jmx.remote.x.notification.fetch.max";
|
|
325 |
|
|
326 |
/**
|
|
327 |
* Returns the maximum notification number which a client will
|
|
328 |
* fetch every time.
|
|
329 |
*/
|
|
330 |
public static int getMaxFetchNotifNumber(Map env) {
|
|
331 |
return (int) getIntegerAttribute(env, MAX_FETCH_NOTIFS, 1000, 1,
|
|
332 |
Integer.MAX_VALUE);
|
|
333 |
}
|
|
334 |
|
|
335 |
/**
|
|
336 |
* <p>Name of the attribute that specifies the timeout for a
|
|
337 |
* client to fetch notifications from its server. The value
|
|
338 |
* associated with this attribute should be a <code>Long</code>
|
|
339 |
* object. The default value is 60000 milliseconds.</p>
|
|
340 |
*/
|
|
341 |
public static final String FETCH_TIMEOUT =
|
|
342 |
"jmx.remote.x.notification.fetch.timeout";
|
|
343 |
|
|
344 |
/**
|
|
345 |
* Returns the timeout for a client to fetch notifications.
|
|
346 |
*/
|
|
347 |
public static long getFetchTimeout(Map env) {
|
|
348 |
return getIntegerAttribute(env, FETCH_TIMEOUT, 60000L, 0,
|
1004
|
349 |
Long.MAX_VALUE);
|
|
350 |
}
|
|
351 |
|
|
352 |
/**
|
|
353 |
* <p>Name of the attribute that specifies an object that will check
|
|
354 |
* accesses to add/removeNotificationListener and also attempts to
|
|
355 |
* receive notifications. The value associated with this attribute
|
|
356 |
* should be a <code>NotificationAccessController</code> object.
|
|
357 |
* The default value is null.</p>
|
|
358 |
* This field is not public because of its com.sun dependency.
|
|
359 |
*/
|
|
360 |
public static final String NOTIF_ACCESS_CONTROLLER =
|
|
361 |
"com.sun.jmx.remote.notification.access.controller";
|
|
362 |
|
|
363 |
public static NotificationAccessController getNotificationAccessController(
|
|
364 |
Map env) {
|
|
365 |
return (env == null) ? null :
|
|
366 |
(NotificationAccessController) env.get(NOTIF_ACCESS_CONTROLLER);
|
2
|
367 |
}
|
|
368 |
|
|
369 |
/**
|
|
370 |
* Get an integer-valued attribute with name <code>name</code>
|
|
371 |
* from <code>env</code>. If <code>env</code> is null, or does
|
|
372 |
* not contain an entry for <code>name</code>, return
|
|
373 |
* <code>defaultValue</code>. The value may be a Number, or it
|
|
374 |
* may be a String that is parsable as a long. It must be at
|
|
375 |
* least <code>minValue</code> and at most<code>maxValue</code>.
|
|
376 |
*
|
|
377 |
* @throws IllegalArgumentException if <code>env</code> contains
|
|
378 |
* an entry for <code>name</code> but it does not meet the
|
|
379 |
* constraints above.
|
|
380 |
*/
|
|
381 |
public static long getIntegerAttribute(Map env, String name,
|
|
382 |
long defaultValue, long minValue,
|
|
383 |
long maxValue) {
|
|
384 |
final Object o;
|
|
385 |
|
|
386 |
if (env == null || (o = env.get(name)) == null)
|
|
387 |
return defaultValue;
|
|
388 |
|
|
389 |
final long result;
|
|
390 |
|
|
391 |
if (o instanceof Number)
|
|
392 |
result = ((Number) o).longValue();
|
|
393 |
else if (o instanceof String) {
|
|
394 |
result = Long.parseLong((String) o);
|
|
395 |
/* May throw a NumberFormatException, which is an
|
|
396 |
IllegalArgumentException. */
|
|
397 |
} else {
|
|
398 |
final String msg =
|
|
399 |
"Attribute " + name + " value must be Integer or String: " + o;
|
|
400 |
throw new IllegalArgumentException(msg);
|
|
401 |
}
|
|
402 |
|
|
403 |
if (result < minValue) {
|
|
404 |
final String msg =
|
|
405 |
"Attribute " + name + " value must be at least " + minValue +
|
|
406 |
": " + result;
|
|
407 |
throw new IllegalArgumentException(msg);
|
|
408 |
}
|
|
409 |
|
|
410 |
if (result > maxValue) {
|
|
411 |
final String msg =
|
|
412 |
"Attribute " + name + " value must be at most " + maxValue +
|
|
413 |
": " + result;
|
|
414 |
throw new IllegalArgumentException(msg);
|
|
415 |
}
|
|
416 |
|
|
417 |
return result;
|
|
418 |
}
|
|
419 |
|
|
420 |
public static final String DEFAULT_ORB="java.naming.corba.orb";
|
|
421 |
|
|
422 |
/* Check that all attributes have a key that is a String.
|
|
423 |
Could make further checks, e.g. appropriate types for attributes. */
|
|
424 |
public static void checkAttributes(Map attributes) {
|
|
425 |
for (Iterator it = attributes.keySet().iterator(); it.hasNext(); ) {
|
|
426 |
Object key = it.next();
|
|
427 |
if (!(key instanceof String)) {
|
|
428 |
final String msg =
|
|
429 |
"Attributes contain key that is not a string: " + key;
|
|
430 |
throw new IllegalArgumentException(msg);
|
|
431 |
}
|
|
432 |
}
|
|
433 |
}
|
|
434 |
|
|
435 |
/* Return a writable map containing only those attributes that are
|
|
436 |
serializable, and that are not hidden by
|
|
437 |
jmx.remote.x.hidden.attributes or the default list of hidden
|
|
438 |
attributes. */
|
|
439 |
public static <V> Map<String, V> filterAttributes(Map<String, V> attributes) {
|
|
440 |
if (logger.traceOn()) {
|
|
441 |
logger.trace("filterAttributes", "starts");
|
|
442 |
}
|
|
443 |
|
|
444 |
SortedMap<String, V> map = new TreeMap<String, V>(attributes);
|
|
445 |
purgeUnserializable(map.values());
|
|
446 |
hideAttributes(map);
|
|
447 |
return map;
|
|
448 |
}
|
|
449 |
|
|
450 |
/**
|
|
451 |
* Remove from the given Collection any element that is not a
|
|
452 |
* serializable object.
|
|
453 |
*/
|
|
454 |
private static void purgeUnserializable(Collection<?> objects) {
|
|
455 |
logger.trace("purgeUnserializable", "starts");
|
|
456 |
ObjectOutputStream oos = null;
|
|
457 |
int i = 0;
|
|
458 |
for (Iterator it = objects.iterator(); it.hasNext(); i++) {
|
|
459 |
Object v = it.next();
|
|
460 |
|
|
461 |
if (v == null || v instanceof String) {
|
|
462 |
if (logger.traceOn()) {
|
|
463 |
logger.trace("purgeUnserializable",
|
|
464 |
"Value trivially serializable: " + v);
|
|
465 |
}
|
|
466 |
continue;
|
|
467 |
}
|
|
468 |
|
|
469 |
try {
|
|
470 |
if (oos == null)
|
|
471 |
oos = new ObjectOutputStream(new SinkOutputStream());
|
|
472 |
oos.writeObject(v);
|
|
473 |
if (logger.traceOn()) {
|
|
474 |
logger.trace("purgeUnserializable",
|
|
475 |
"Value serializable: " + v);
|
|
476 |
}
|
|
477 |
} catch (IOException e) {
|
|
478 |
if (logger.traceOn()) {
|
|
479 |
logger.trace("purgeUnserializable",
|
|
480 |
"Value not serializable: " + v + ": " +
|
|
481 |
e);
|
|
482 |
}
|
|
483 |
it.remove();
|
|
484 |
oos = null; // ObjectOutputStream invalid after exception
|
|
485 |
}
|
|
486 |
}
|
|
487 |
}
|
|
488 |
|
|
489 |
/**
|
1004
|
490 |
* The value of this attribute, if present, is a string specifying
|
|
491 |
* what other attributes should not appear in
|
|
492 |
* JMXConnectorServer.getAttributes(). It is a space-separated
|
|
493 |
* list of attribute patterns, where each pattern is either an
|
|
494 |
* attribute name, or an attribute prefix followed by a "*"
|
|
495 |
* character. The "*" has no special significance anywhere except
|
|
496 |
* at the end of a pattern. By default, this list is added to the
|
|
497 |
* list defined by {@link #DEFAULT_HIDDEN_ATTRIBUTES} (which
|
|
498 |
* uses the same format). If the value of this attribute begins
|
|
499 |
* with an "=", then the remainder of the string defines the
|
|
500 |
* complete list of attribute patterns.
|
2
|
501 |
*/
|
|
502 |
public static final String HIDDEN_ATTRIBUTES =
|
|
503 |
"jmx.remote.x.hidden.attributes";
|
|
504 |
|
|
505 |
/**
|
1004
|
506 |
* Default list of attributes not to show.
|
|
507 |
* @see #HIDDEN_ATTRIBUTES
|
2
|
508 |
*/
|
|
509 |
/* This list is copied directly from the spec, plus
|
|
510 |
java.naming.security.*. Most of the attributes here would have
|
|
511 |
been eliminated from the map anyway because they are typically
|
|
512 |
not serializable. But just in case they are, we list them here
|
|
513 |
to conform to the spec. */
|
|
514 |
public static final String DEFAULT_HIDDEN_ATTRIBUTES =
|
|
515 |
"java.naming.security.* " +
|
|
516 |
"jmx.remote.authenticator " +
|
|
517 |
"jmx.remote.context " +
|
|
518 |
"jmx.remote.default.class.loader " +
|
|
519 |
"jmx.remote.message.connection.server " +
|
|
520 |
"jmx.remote.object.wrapping " +
|
|
521 |
"jmx.remote.rmi.client.socket.factory " +
|
|
522 |
"jmx.remote.rmi.server.socket.factory " +
|
|
523 |
"jmx.remote.sasl.callback.handler " +
|
|
524 |
"jmx.remote.tls.socket.factory " +
|
|
525 |
"jmx.remote.x.access.file " +
|
|
526 |
"jmx.remote.x.password.file ";
|
|
527 |
|
|
528 |
private static final SortedSet<String> defaultHiddenStrings =
|
|
529 |
new TreeSet<String>();
|
|
530 |
private static final SortedSet<String> defaultHiddenPrefixes =
|
|
531 |
new TreeSet<String>();
|
|
532 |
|
|
533 |
private static void hideAttributes(SortedMap<String, ?> map) {
|
|
534 |
if (map.isEmpty())
|
|
535 |
return;
|
|
536 |
|
|
537 |
final SortedSet<String> hiddenStrings;
|
|
538 |
final SortedSet<String> hiddenPrefixes;
|
|
539 |
|
|
540 |
String hide = (String) map.get(HIDDEN_ATTRIBUTES);
|
|
541 |
if (hide != null) {
|
|
542 |
if (hide.startsWith("="))
|
|
543 |
hide = hide.substring(1);
|
|
544 |
else
|
|
545 |
hide += " " + DEFAULT_HIDDEN_ATTRIBUTES;
|
|
546 |
hiddenStrings = new TreeSet<String>();
|
|
547 |
hiddenPrefixes = new TreeSet<String>();
|
|
548 |
parseHiddenAttributes(hide, hiddenStrings, hiddenPrefixes);
|
|
549 |
} else {
|
|
550 |
hide = DEFAULT_HIDDEN_ATTRIBUTES;
|
|
551 |
synchronized (defaultHiddenStrings) {
|
|
552 |
if (defaultHiddenStrings.isEmpty()) {
|
|
553 |
parseHiddenAttributes(hide,
|
|
554 |
defaultHiddenStrings,
|
|
555 |
defaultHiddenPrefixes);
|
|
556 |
}
|
|
557 |
hiddenStrings = defaultHiddenStrings;
|
|
558 |
hiddenPrefixes = defaultHiddenPrefixes;
|
|
559 |
}
|
|
560 |
}
|
|
561 |
|
|
562 |
/* Construct a string that is greater than any key in the map.
|
|
563 |
Setting a string-to-match or a prefix-to-match to this string
|
|
564 |
guarantees that we will never call next() on the corresponding
|
|
565 |
iterator. */
|
|
566 |
String sentinelKey = map.lastKey() + "X";
|
|
567 |
Iterator keyIterator = map.keySet().iterator();
|
|
568 |
Iterator stringIterator = hiddenStrings.iterator();
|
|
569 |
Iterator prefixIterator = hiddenPrefixes.iterator();
|
|
570 |
|
|
571 |
String nextString;
|
|
572 |
if (stringIterator.hasNext())
|
|
573 |
nextString = (String) stringIterator.next();
|
|
574 |
else
|
|
575 |
nextString = sentinelKey;
|
|
576 |
String nextPrefix;
|
|
577 |
if (prefixIterator.hasNext())
|
|
578 |
nextPrefix = (String) prefixIterator.next();
|
|
579 |
else
|
|
580 |
nextPrefix = sentinelKey;
|
|
581 |
|
|
582 |
/* Read each key in sorted order and, if it matches a string
|
|
583 |
or prefix, remove it. */
|
|
584 |
keys:
|
|
585 |
while (keyIterator.hasNext()) {
|
|
586 |
String key = (String) keyIterator.next();
|
|
587 |
|
|
588 |
/* Continue through string-match values until we find one
|
|
589 |
that is either greater than the current key, or equal
|
|
590 |
to it. In the latter case, remove the key. */
|
|
591 |
int cmp = +1;
|
|
592 |
while ((cmp = nextString.compareTo(key)) < 0) {
|
|
593 |
if (stringIterator.hasNext())
|
|
594 |
nextString = (String) stringIterator.next();
|
|
595 |
else
|
|
596 |
nextString = sentinelKey;
|
|
597 |
}
|
|
598 |
if (cmp == 0) {
|
|
599 |
keyIterator.remove();
|
|
600 |
continue keys;
|
|
601 |
}
|
|
602 |
|
|
603 |
/* Continue through the prefix values until we find one
|
|
604 |
that is either greater than the current key, or a
|
|
605 |
prefix of it. In the latter case, remove the key. */
|
|
606 |
while (nextPrefix.compareTo(key) <= 0) {
|
|
607 |
if (key.startsWith(nextPrefix)) {
|
|
608 |
keyIterator.remove();
|
|
609 |
continue keys;
|
|
610 |
}
|
|
611 |
if (prefixIterator.hasNext())
|
|
612 |
nextPrefix = (String) prefixIterator.next();
|
|
613 |
else
|
|
614 |
nextPrefix = sentinelKey;
|
|
615 |
}
|
|
616 |
}
|
|
617 |
}
|
|
618 |
|
|
619 |
private static void parseHiddenAttributes(String hide,
|
|
620 |
SortedSet<String> hiddenStrings,
|
|
621 |
SortedSet<String> hiddenPrefixes) {
|
|
622 |
final StringTokenizer tok = new StringTokenizer(hide);
|
|
623 |
while (tok.hasMoreTokens()) {
|
|
624 |
String s = tok.nextToken();
|
|
625 |
if (s.endsWith("*"))
|
|
626 |
hiddenPrefixes.add(s.substring(0, s.length() - 1));
|
|
627 |
else
|
|
628 |
hiddenStrings.add(s);
|
|
629 |
}
|
|
630 |
}
|
|
631 |
|
|
632 |
/**
|
|
633 |
* <p>Name of the attribute that specifies the timeout to keep a
|
|
634 |
* server side connection after answering last client request.
|
|
635 |
* The default value is 120000 milliseconds.</p>
|
|
636 |
*/
|
|
637 |
public static final String SERVER_CONNECTION_TIMEOUT =
|
|
638 |
"jmx.remote.x.server.connection.timeout";
|
|
639 |
|
|
640 |
/**
|
|
641 |
* Returns the server side connection timeout.
|
|
642 |
*/
|
|
643 |
public static long getServerConnectionTimeout(Map env) {
|
|
644 |
return getIntegerAttribute(env, SERVER_CONNECTION_TIMEOUT, 120000L,
|
|
645 |
0, Long.MAX_VALUE);
|
|
646 |
}
|
|
647 |
|
|
648 |
/**
|
|
649 |
* <p>Name of the attribute that specifies the period in
|
|
650 |
* millisecond for a client to check its connection. The default
|
|
651 |
* value is 60000 milliseconds.</p>
|
|
652 |
*/
|
|
653 |
public static final String CLIENT_CONNECTION_CHECK_PERIOD =
|
|
654 |
"jmx.remote.x.client.connection.check.period";
|
|
655 |
|
|
656 |
/**
|
|
657 |
* Returns the client connection check period.
|
|
658 |
*/
|
|
659 |
public static long getConnectionCheckPeriod(Map env) {
|
|
660 |
return getIntegerAttribute(env, CLIENT_CONNECTION_CHECK_PERIOD, 60000L,
|
|
661 |
0, Long.MAX_VALUE);
|
|
662 |
}
|
|
663 |
|
|
664 |
/**
|
|
665 |
* Computes a boolean value from a string value retrieved from a
|
|
666 |
* property in the given map.
|
|
667 |
*
|
|
668 |
* @param env the environment map.
|
|
669 |
* @param prop the name of the property in the environment map whose
|
|
670 |
* returned string value must be converted into a boolean value.
|
1004
|
671 |
* @param systemProperty if true, consult a system property of the
|
|
672 |
* same name if there is no entry in the environment map.
|
2
|
673 |
*
|
|
674 |
* @return
|
|
675 |
* <ul>
|
|
676 |
* <li>{@code false} if {@code env.get(prop)} is {@code null}</li>
|
|
677 |
* <li>{@code false} if
|
|
678 |
* {@code ((String)env.get(prop)).equalsIgnoreCase("false")}
|
|
679 |
* is {@code true}</li>
|
|
680 |
* <li>{@code true} if
|
|
681 |
* {@code ((String)env.get(prop)).equalsIgnoreCase("true")}
|
|
682 |
* is {@code true}</li>
|
|
683 |
* </ul>
|
|
684 |
*
|
|
685 |
* @throws IllegalArgumentException if {@code env} is {@code null} or
|
|
686 |
* {@code env.get(prop)} is not {@code null} and
|
|
687 |
* {@code ((String)env.get(prop)).equalsIgnoreCase("false")} and
|
|
688 |
* {@code ((String)env.get(prop)).equalsIgnoreCase("true")} are
|
|
689 |
* {@code false}.
|
|
690 |
* @throws ClassCastException if {@code env.get(prop)} cannot be cast
|
|
691 |
* to {@code String}.
|
|
692 |
*/
|
1004
|
693 |
public static boolean computeBooleanFromString(
|
|
694 |
Map env, String prop, boolean systemProperty) {
|
|
695 |
|
|
696 |
if (env == null)
|
|
697 |
throw new IllegalArgumentException("env map cannot be null");
|
|
698 |
|
|
699 |
// returns a default value of 'false' if no property is found...
|
|
700 |
return computeBooleanFromString(env,prop,systemProperty,false);
|
|
701 |
}
|
|
702 |
|
|
703 |
/**
|
|
704 |
* Computes a boolean value from a string value retrieved from a
|
|
705 |
* property in the given map.
|
|
706 |
*
|
|
707 |
* @param env the environment map.
|
|
708 |
* @param prop the name of the property in the environment map whose
|
|
709 |
* returned string value must be converted into a boolean value.
|
|
710 |
* @param systemProperty if true, consult a system property of the
|
|
711 |
* same name if there is no entry in the environment map.
|
|
712 |
* @param defaultValue a default value to return in case no property
|
|
713 |
* was defined.
|
|
714 |
*
|
|
715 |
* @return
|
|
716 |
* <ul>
|
|
717 |
* <li>{@code defaultValue} if {@code env.get(prop)} is {@code null}
|
|
718 |
* and {@code systemProperty} is {@code false}</li>
|
|
719 |
* <li>{@code defaultValue} if {@code env.get(prop)} is {@code null}
|
|
720 |
* and {@code systemProperty} is {@code true} and
|
|
721 |
* {@code System.getProperty(prop)} is {@code null}</li>
|
|
722 |
* <li>{@code false} if {@code env.get(prop)} is {@code null}
|
|
723 |
* and {@code systemProperty} is {@code true} and
|
|
724 |
* {@code System.getProperty(prop).equalsIgnoreCase("false")}
|
|
725 |
* is {@code true}</li>
|
|
726 |
* <li>{@code true} if {@code env.get(prop)} is {@code null}
|
|
727 |
* and {@code systemProperty} is {@code true} and
|
|
728 |
* {@code System.getProperty(prop).equalsIgnoreCase("true")}
|
|
729 |
* is {@code true}</li>
|
|
730 |
* <li>{@code false} if
|
|
731 |
* {@code ((String)env.get(prop)).equalsIgnoreCase("false")}
|
|
732 |
* is {@code true}</li>
|
|
733 |
* <li>{@code true} if
|
|
734 |
* {@code ((String)env.get(prop)).equalsIgnoreCase("true")}
|
|
735 |
* is {@code true}</li>
|
|
736 |
* </ul>
|
|
737 |
*
|
|
738 |
* @throws IllegalArgumentException if {@code env} is {@code null} or
|
|
739 |
* {@code env.get(prop)} is not {@code null} and
|
|
740 |
* {@code ((String)env.get(prop)).equalsIgnoreCase("false")} and
|
|
741 |
* {@code ((String)env.get(prop)).equalsIgnoreCase("true")} are
|
|
742 |
* {@code false}.
|
|
743 |
* @throws ClassCastException if {@code env.get(prop)} cannot be cast
|
|
744 |
* to {@code String}.
|
|
745 |
*/
|
|
746 |
public static boolean computeBooleanFromString(
|
|
747 |
Map env, String prop, boolean systemProperty, boolean defaultValue) {
|
2
|
748 |
|
|
749 |
if (env == null)
|
|
750 |
throw new IllegalArgumentException("env map cannot be null");
|
|
751 |
|
|
752 |
String stringBoolean = (String) env.get(prop);
|
1004
|
753 |
if (stringBoolean == null && systemProperty) {
|
|
754 |
stringBoolean =
|
|
755 |
AccessController.doPrivileged(new GetPropertyAction(prop));
|
|
756 |
}
|
2
|
757 |
|
|
758 |
if (stringBoolean == null)
|
1004
|
759 |
return defaultValue;
|
2
|
760 |
else if (stringBoolean.equalsIgnoreCase("true"))
|
|
761 |
return true;
|
|
762 |
else if (stringBoolean.equalsIgnoreCase("false"))
|
|
763 |
return false;
|
|
764 |
else
|
|
765 |
throw new IllegalArgumentException(prop +
|
|
766 |
" must be \"true\" or \"false\" instead of \"" +
|
|
767 |
stringBoolean + "\"");
|
|
768 |
}
|
|
769 |
|
|
770 |
/**
|
|
771 |
* Converts a map into a valid hash table, i.e.
|
|
772 |
* it removes all the 'null' values from the map.
|
|
773 |
*/
|
|
774 |
public static <K, V> Hashtable<K, V> mapToHashtable(Map<K, V> map) {
|
|
775 |
HashMap<K, V> m = new HashMap<K, V>(map);
|
|
776 |
if (m.containsKey(null)) m.remove(null);
|
|
777 |
for (Iterator i = m.values().iterator(); i.hasNext(); )
|
|
778 |
if (i.next() == null) i.remove();
|
|
779 |
return new Hashtable<K, V>(m);
|
|
780 |
}
|
|
781 |
|
1004
|
782 |
/**
|
|
783 |
* Returns true if the parameter JMXConnector.USE_EVENT_SERVICE is set to a
|
|
784 |
* String equals "true" by ignoring case in the map or in the System.
|
|
785 |
*/
|
|
786 |
public static boolean eventServiceEnabled(Map env) {
|
|
787 |
return computeBooleanFromString(env, JMXConnector.USE_EVENT_SERVICE, true);
|
|
788 |
}
|
|
789 |
|
|
790 |
/**
|
|
791 |
* Returns true if the parameter JMXConnectorServer.DELEGATE_TO_EVENT_SERVICE
|
|
792 |
* is set to a String equals "true" (ignores case).
|
|
793 |
* If the property DELEGATE_TO_EVENT_SERVICE is not set, returns
|
|
794 |
* a default value of "true".
|
|
795 |
*/
|
|
796 |
public static boolean delegateToEventService(Map env) {
|
|
797 |
return computeBooleanFromString(env,
|
|
798 |
JMXConnectorServer.DELEGATE_TO_EVENT_SERVICE, true, true);
|
|
799 |
}
|
|
800 |
|
|
801 |
// /**
|
|
802 |
// * <p>Name of the attribute that specifies an EventRelay object to use.
|
|
803 |
// */
|
|
804 |
// public static final String EVENT_RELAY =
|
|
805 |
// "jmx.remote.x.event.relay";
|
|
806 |
//
|
|
807 |
//
|
|
808 |
// /**
|
|
809 |
// * Returns an EventRelay object. The default one is FetchingEventRelay.
|
|
810 |
// * If {@code EVENT_RELAY} is specified in {@code env} as a key,
|
|
811 |
// * its value will be returned as an EventRelay object, if the value is
|
|
812 |
// * not of type {@code EventRelay}, the default {@code FetchingEventRelay}
|
|
813 |
// * will be returned.
|
|
814 |
// * If {@code EVENT_RELAY} is not specified but {@code ENABLE_EVENT_RELAY}
|
|
815 |
// * is specified as a key and its value is <code true>, the default {@code FetchingEventRelay}
|
|
816 |
// * will be returned.
|
|
817 |
// */
|
|
818 |
// public static EventRelay getEventRelay(Map env) {
|
|
819 |
// Map info = env == null ?
|
|
820 |
// Collections.EMPTY_MAP : env;
|
|
821 |
//
|
|
822 |
// Object o = env.get(EVENT_RELAY);
|
|
823 |
// if (o instanceof EventRelay) {
|
|
824 |
// return (EventRelay)o;
|
|
825 |
// } else if (o != null) {
|
|
826 |
// logger.warning("getEventRelay",
|
|
827 |
// "The user specified object is not an EventRelay object, " +
|
|
828 |
// "using the default class FetchingEventRelay.");
|
|
829 |
//
|
|
830 |
// return new FetchingEventRelay();
|
|
831 |
// }
|
|
832 |
//
|
|
833 |
// if (enableEventRelay(env)) {
|
|
834 |
// return new FetchingEventRelay();
|
|
835 |
// }
|
|
836 |
//
|
|
837 |
// return null;
|
|
838 |
// }
|
|
839 |
|
|
840 |
|
2
|
841 |
private static final class SinkOutputStream extends OutputStream {
|
|
842 |
public void write(byte[] b, int off, int len) {}
|
|
843 |
public void write(int b) {}
|
|
844 |
}
|
|
845 |
|
|
846 |
private static final ClassLogger logger =
|
|
847 |
new ClassLogger("javax.management.remote.misc", "EnvHelp");
|
|
848 |
}
|