jdk/src/java.management/share/classes/com/sun/jmx/remote/util/EnvHelp.java
author avstepan
Tue, 19 May 2015 16:04:14 +0400
changeset 30655 d83f50188ca9
parent 25859 3317bb8137f4
child 32034 05676cfd40b5
permissions -rw-r--r--
8080422: some docs cleanup for core libs Summary: some docs cleanup Reviewed-by: rriggs, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1706
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
     1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14210
diff changeset
     3
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     8
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    10
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    23
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    24
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package com.sun.jmx.remote.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.SortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.SortedSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.TreeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.TreeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.remote.JMXConnectorFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import com.sun.jmx.mbeanserver.GetPropertyAction;
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    51
import com.sun.jmx.remote.security.NotificationAccessController;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    52
import javax.management.remote.JMXConnector;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    53
import javax.management.remote.JMXConnectorServer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class EnvHelp {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    58
     * Name of the attribute that specifies a default class loader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * object.
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    60
     * The value associated with this attribute is a ClassLoader object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final String DEFAULT_CLASS_LOADER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        JMXConnectorFactory.DEFAULT_CLASS_LOADER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    66
     * Name of the attribute that specifies a default class loader
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    67
     * ObjectName.
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
    68
     * The value associated with this attribute is an ObjectName object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final String DEFAULT_CLASS_LOADER_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        JMXConnectorServerFactory.DEFAULT_CLASS_LOADER_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Get the Connector Server default class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Returns:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *     The ClassLoader object found in <var>env</var> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *     <code>jmx.remote.default.class.loader</code>, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *     The ClassLoader pointed to by the ObjectName found in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *     <var>env</var> for <code>jmx.remote.default.class.loader.name</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *     and registered in <var>mbs</var> if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *     The current thread's context classloader otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param env Environment attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param mbs The MBeanServer for which the connector server provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * remote access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @return the connector server's default class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception IllegalArgumentException if one of the following is true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <li>both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *     <code>jmx.remote.default.class.loader</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *     <code>jmx.remote.default.class.loader.name</code> are specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <li>or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *     <code>jmx.remote.default.class.loader</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *     an instance of {@link ClassLoader},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <li>or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *     <code>jmx.remote.default.class.loader.name</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *     an instance of {@link ObjectName},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <li>or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *     <code>jmx.remote.default.class.loader.name</code> is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *     but <var>mbs</var> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * </li>
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   116
     * </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @exception InstanceNotFoundException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <code>jmx.remote.default.class.loader.name</code> is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * and the ClassLoader MBean is not found in <var>mbs</var>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   121
    public static ClassLoader resolveServerClassLoader(Map<String, ?> env,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                                       MBeanServer mbs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (env == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Object loader = env.get(DEFAULT_CLASS_LOADER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Object name   = env.get(DEFAULT_CLASS_LOADER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (loader != null && name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            final String msg = "Only one of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                DEFAULT_CLASS_LOADER + " or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                DEFAULT_CLASS_LOADER_NAME +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                " should be specified.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (loader == null && name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (loader instanceof ClassLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                return (ClassLoader) loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    "ClassLoader object is not an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    ClassLoader.class.getName() + " : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    loader.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        ObjectName on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (name instanceof ObjectName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            on = (ObjectName) name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                "ClassLoader name is not an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                ObjectName.class.getName() + " : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                name.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (mbs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new IllegalArgumentException("Null MBeanServer object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return mbs.getClassLoader(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Get the Connector Client default class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Returns:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *     The ClassLoader object found in <var>env</var> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *     <code>jmx.remote.default.class.loader</code>, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <li>The <tt>Thread.currentThread().getContextClassLoader()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *     otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Usually a Connector Client will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * ClassLoader dcl = EnvHelp.resolveClientClassLoader(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * in its <code>connect(Map env)</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return The connector client default class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @exception IllegalArgumentException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <code>jmx.remote.default.class.loader</code> is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * and is not an instance of {@link ClassLoader}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   197
    public static ClassLoader resolveClientClassLoader(Map<String, ?> env) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (env == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        Object loader = env.get(DEFAULT_CLASS_LOADER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (loader instanceof ClassLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return (ClassLoader) loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                "ClassLoader object is not an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                ClassLoader.class.getName() + " : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                loader.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Initialize the cause field of a {@code Throwable} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param throwable The {@code Throwable} on which the cause is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param cause The cause to set on the supplied {@code Throwable}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return the {@code Throwable} with the cause field initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public static <T extends Throwable> T initCause(T throwable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                                    Throwable cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        throwable.initCause(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return throwable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Returns the cause field of a {@code Throwable} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * The cause field can be got only if <var>t</var> has an
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   234
     * {@link Throwable#getCause()} method (JDK Version {@literal >=} 1.4)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param t {@code Throwable} on which the cause must be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @return the cause if getCause() succeeded and the got value is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * null, otherwise return the <var>t</var>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public static Throwable getCause(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        Throwable ret = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            java.lang.reflect.Method getCause =
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   244
                t.getClass().getMethod("getCause", (Class<?>[]) null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            ret = (Throwable)getCause.invoke(t, (Object[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // it must be older than 1.4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return (ret != null) ? ret: t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   256
     * Name of the attribute that specifies the size of a notification
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * buffer for a connector server. The default value is 1000.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public static final String BUFFER_SIZE_PROPERTY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        "jmx.remote.x.notification.buffer.size";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Returns the size of a notification buffer for a connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * The default value is 1000.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   267
    public static int getNotifBufferSize(Map<String, ?> env) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        int defaultQueueSize = 1000; // default value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // keep it for the compability for the fix:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // 6174229: Environment parameter should be notification.buffer.size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // instead of buffer.size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        final String oldP = "jmx.remote.x.buffer.size";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // the default value re-specified in the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            GetPropertyAction act = new GetPropertyAction(BUFFER_SIZE_PROPERTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            String s = AccessController.doPrivileged(act);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                defaultQueueSize = Integer.parseInt(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            } else { // try the old one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                act = new GetPropertyAction(oldP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                s = AccessController.doPrivileged(act);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    defaultQueueSize = Integer.parseInt(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            logger.warning("getNotifBufferSize",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                           "Can't use System property "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                           BUFFER_SIZE_PROPERTY+ ": " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
              logger.debug("getNotifBufferSize", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int queueSize = defaultQueueSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (env.containsKey(BUFFER_SIZE_PROPERTY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                queueSize = (int)EnvHelp.getIntegerAttribute(env,BUFFER_SIZE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                            defaultQueueSize,0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                            Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            } else { // try the old one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                queueSize = (int)EnvHelp.getIntegerAttribute(env,oldP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                            defaultQueueSize,0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                            Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            logger.warning("getNotifBufferSize",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                           "Can't determine queuesize (using default): "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                           e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            logger.debug("getNotifBufferSize", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return queueSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   318
     * Name of the attribute that specifies the maximum number of
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   319
     * notifications that a client will fetch from its server. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * value associated with this attribute should be an
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   321
     * {@code Integer} object.  The default value is 1000.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public static final String MAX_FETCH_NOTIFS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        "jmx.remote.x.notification.fetch.max";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Returns the maximum notification number which a client will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * fetch every time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   330
    public static int getMaxFetchNotifNumber(Map<String, ?> env) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return (int) getIntegerAttribute(env, MAX_FETCH_NOTIFS, 1000, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                         Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   336
     * Name of the attribute that specifies the timeout for a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * client to fetch notifications from its server. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * associated with this attribute should be a <code>Long</code>
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   339
     * object.  The default value is 60000 milliseconds.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public static final String FETCH_TIMEOUT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        "jmx.remote.x.notification.fetch.timeout";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Returns the timeout for a client to fetch notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   347
    public static long getFetchTimeout(Map<String, ?> env) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return getIntegerAttribute(env, FETCH_TIMEOUT, 60000L, 0,
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   349
                Long.MAX_VALUE);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   350
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   351
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   352
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   353
     * Name of the attribute that specifies an object that will check
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   354
     * accesses to add/removeNotificationListener and also attempts to
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   355
     * receive notifications.  The value associated with this attribute
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   356
     * should be a <code>NotificationAccessController</code> object.
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   357
     * The default value is null.
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   358
     * <p>
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   359
     * This field is not public because of its com.sun dependency.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   360
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   361
    public static final String NOTIF_ACCESS_CONTROLLER =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   362
            "com.sun.jmx.remote.notification.access.controller";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   363
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   364
    public static NotificationAccessController getNotificationAccessController(
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   365
            Map<String, ?> env) {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   366
        return (env == null) ? null :
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   367
            (NotificationAccessController) env.get(NOTIF_ACCESS_CONTROLLER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Get an integer-valued attribute with name <code>name</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * from <code>env</code>.  If <code>env</code> is null, or does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * not contain an entry for <code>name</code>, return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <code>defaultValue</code>.  The value may be a Number, or it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * may be a String that is parsable as a long.  It must be at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * least <code>minValue</code> and at most<code>maxValue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @throws IllegalArgumentException if <code>env</code> contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * an entry for <code>name</code> but it does not meet the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * constraints above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   382
    public static long getIntegerAttribute(Map<String, ?> env, String name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                           long defaultValue, long minValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                           long maxValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        final Object o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (env == null || (o = env.get(name)) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        final long result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (o instanceof Number)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            result = ((Number) o).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        else if (o instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            result = Long.parseLong((String) o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            /* May throw a NumberFormatException, which is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
               IllegalArgumentException.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                "Attribute " + name + " value must be Integer or String: " + o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (result < minValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                "Attribute " + name + " value must be at least " + minValue +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                ": " + result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (result > maxValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                "Attribute " + name + " value must be at most " + maxValue +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                ": " + result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public static final String DEFAULT_ORB="java.naming.corba.orb";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /* Check that all attributes have a key that is a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
       Could make further checks, e.g. appropriate types for attributes.  */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   425
    public static void checkAttributes(Map<?, ?> attributes) {
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   426
        for (Object key : attributes.keySet()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (!(key instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    "Attributes contain key that is not a string: " + key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /* Return a writable map containing only those attributes that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
       serializable, and that are not hidden by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
       jmx.remote.x.hidden.attributes or the default list of hidden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
       attributes.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public static <V> Map<String, V> filterAttributes(Map<String, V> attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            logger.trace("filterAttributes", "starts");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        SortedMap<String, V> map = new TreeMap<String, V>(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        purgeUnserializable(map.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        hideAttributes(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Remove from the given Collection any element that is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * serializable object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    private static void purgeUnserializable(Collection<?> objects) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        logger.trace("purgeUnserializable", "starts");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        ObjectOutputStream oos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        int i = 0;
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   458
        for (Iterator<?> it = objects.iterator(); it.hasNext(); i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            Object v = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (v == null || v instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    logger.trace("purgeUnserializable",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                 "Value trivially serializable: " + v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                if (oos == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    oos = new ObjectOutputStream(new SinkOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                oos.writeObject(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    logger.trace("purgeUnserializable",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                                 "Value serializable: " + v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                if (logger.traceOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    logger.trace("purgeUnserializable",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                 "Value not serializable: " + v + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                                 e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                oos = null; // ObjectOutputStream invalid after exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /**
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   490
     * The value of this attribute, if present, is a string specifying
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   491
     * what other attributes should not appear in
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   492
     * JMXConnectorServer.getAttributes().  It is a space-separated
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   493
     * list of attribute patterns, where each pattern is either an
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   494
     * attribute name, or an attribute prefix followed by a "*"
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   495
     * character.  The "*" has no special significance anywhere except
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   496
     * at the end of a pattern.  By default, this list is added to the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   497
     * list defined by {@link #DEFAULT_HIDDEN_ATTRIBUTES} (which
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   498
     * uses the same format).  If the value of this attribute begins
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   499
     * with an "=", then the remainder of the string defines the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   500
     * complete list of attribute patterns.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public static final String HIDDEN_ATTRIBUTES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        "jmx.remote.x.hidden.attributes";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   506
     * Default list of attributes not to show.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   507
     * @see #HIDDEN_ATTRIBUTES
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /* This list is copied directly from the spec, plus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
       java.naming.security.*.  Most of the attributes here would have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
       been eliminated from the map anyway because they are typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
       not serializable.  But just in case they are, we list them here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
       to conform to the spec.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    public static final String DEFAULT_HIDDEN_ATTRIBUTES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        "java.naming.security.* " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        "jmx.remote.authenticator " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        "jmx.remote.context " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        "jmx.remote.default.class.loader " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        "jmx.remote.message.connection.server " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        "jmx.remote.object.wrapping " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        "jmx.remote.rmi.client.socket.factory " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        "jmx.remote.rmi.server.socket.factory " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        "jmx.remote.sasl.callback.handler " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        "jmx.remote.tls.socket.factory " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        "jmx.remote.x.access.file " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        "jmx.remote.x.password.file ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    private static final SortedSet<String> defaultHiddenStrings =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            new TreeSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    private static final SortedSet<String> defaultHiddenPrefixes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            new TreeSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    private static void hideAttributes(SortedMap<String, ?> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (map.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        final SortedSet<String> hiddenStrings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        final SortedSet<String> hiddenPrefixes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        String hide = (String) map.get(HIDDEN_ATTRIBUTES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (hide != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            if (hide.startsWith("="))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                hide = hide.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                hide += " " + DEFAULT_HIDDEN_ATTRIBUTES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            hiddenStrings = new TreeSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            hiddenPrefixes = new TreeSet<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            parseHiddenAttributes(hide, hiddenStrings, hiddenPrefixes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            hide = DEFAULT_HIDDEN_ATTRIBUTES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            synchronized (defaultHiddenStrings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                if (defaultHiddenStrings.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    parseHiddenAttributes(hide,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                          defaultHiddenStrings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                                          defaultHiddenPrefixes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                hiddenStrings = defaultHiddenStrings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                hiddenPrefixes = defaultHiddenPrefixes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        /* Construct a string that is greater than any key in the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
           Setting a string-to-match or a prefix-to-match to this string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
           guarantees that we will never call next() on the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
           iterator.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        String sentinelKey = map.lastKey() + "X";
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   567
        Iterator<String> keyIterator = map.keySet().iterator();
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   568
        Iterator<String> stringIterator = hiddenStrings.iterator();
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   569
        Iterator<String> prefixIterator = hiddenPrefixes.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        String nextString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if (stringIterator.hasNext())
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   573
            nextString = stringIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            nextString = sentinelKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        String nextPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        if (prefixIterator.hasNext())
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   578
            nextPrefix = prefixIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            nextPrefix = sentinelKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        /* Read each key in sorted order and, if it matches a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
           or prefix, remove it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    keys:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        while (keyIterator.hasNext()) {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   586
            String key = keyIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            /* Continue through string-match values until we find one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
               that is either greater than the current key, or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
               to it.  In the latter case, remove the key.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            int cmp = +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            while ((cmp = nextString.compareTo(key)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                if (stringIterator.hasNext())
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   594
                    nextString = stringIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    nextString = sentinelKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            if (cmp == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                keyIterator.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                continue keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            /* Continue through the prefix values until we find one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
               that is either greater than the current key, or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
               prefix of it.  In the latter case, remove the key.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            while (nextPrefix.compareTo(key) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                if (key.startsWith(nextPrefix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    keyIterator.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    continue keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                if (prefixIterator.hasNext())
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   612
                    nextPrefix = prefixIterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    nextPrefix = sentinelKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    private static void parseHiddenAttributes(String hide,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                              SortedSet<String> hiddenStrings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                              SortedSet<String> hiddenPrefixes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        final StringTokenizer tok = new StringTokenizer(hide);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        while (tok.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            String s = tok.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            if (s.endsWith("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                hiddenPrefixes.add(s.substring(0, s.length() - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                hiddenStrings.add(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   633
     * Name of the attribute that specifies the timeout to keep a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * server side connection after answering last client request.
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   635
     * The default value is 120000 milliseconds.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public static final String SERVER_CONNECTION_TIMEOUT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        "jmx.remote.x.server.connection.timeout";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * Returns the server side connection timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   643
    public static long getServerConnectionTimeout(Map<String, ?> env) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return getIntegerAttribute(env, SERVER_CONNECTION_TIMEOUT, 120000L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                   0, Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   649
     * Name of the attribute that specifies the period in
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   650
     * millisecond for a client to check its connection. The default
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   651
     * value is 60000 milliseconds.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public static final String CLIENT_CONNECTION_CHECK_PERIOD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        "jmx.remote.x.client.connection.check.period";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * Returns the client connection check period.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   659
    public static long getConnectionCheckPeriod(Map<String, ?> env) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        return getIntegerAttribute(env, CLIENT_CONNECTION_CHECK_PERIOD, 60000L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                                   0, Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * Computes a boolean value from a string value retrieved from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * property in the given map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   668
     * @param stringBoolean the string value that must be converted
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   669
     * into a boolean value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *   <ul>
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   673
     *   <li>{@code false} if {@code stringBoolean} is {@code null}</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *   <li>{@code false} if
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   675
     *       {@code stringBoolean.equalsIgnoreCase("false")}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *       is {@code true}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *   <li>{@code true} if
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   678
     *       {@code stringBoolean.equalsIgnoreCase("true")}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *       is {@code true}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *   </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   682
     * @throws IllegalArgumentException if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * {@code ((String)env.get(prop)).equalsIgnoreCase("false")} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * {@code ((String)env.get(prop)).equalsIgnoreCase("true")} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   687
    public static boolean computeBooleanFromString(String stringBoolean) {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   688
        // returns a default value of 'false' if no property is found...
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   689
        return computeBooleanFromString(stringBoolean,false);
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   690
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   691
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   692
    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   693
     * Computes a boolean value from a string value retrieved from a
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   694
     * property in the given map.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   695
     *
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   696
     * @param stringBoolean the string value that must be converted
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   697
     * into a boolean value.
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   698
     * @param defaultValue a default value to return in case no property
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   699
     *        was defined.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   700
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   701
     * @return
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   702
     *   <ul>
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   703
     *   <li>{@code defaultValue} if {@code stringBoolean}
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   704
     *   is {@code null}</li>
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   705
     *   <li>{@code false} if
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   706
     *       {@code stringBoolean.equalsIgnoreCase("false")}
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   707
     *       is {@code true}</li>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   708
     *   <li>{@code true} if
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   709
     *       {@code stringBoolean.equalsIgnoreCase("true")}
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   710
     *       is {@code true}</li>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   711
     *   </ul>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   712
     *
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   713
     * @throws IllegalArgumentException if
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   714
     * {@code ((String)env.get(prop)).equalsIgnoreCase("false")} and
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   715
     * {@code ((String)env.get(prop)).equalsIgnoreCase("true")} are
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   716
     * {@code false}.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   717
     */
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   718
    public static boolean computeBooleanFromString( String stringBoolean, boolean defaultValue) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        if (stringBoolean == null)
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   720
            return defaultValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        else if (stringBoolean.equalsIgnoreCase("true"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        else if (stringBoolean.equalsIgnoreCase("false"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        else
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   726
            throw new IllegalArgumentException(
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   727
                "Property value must be \"true\" or \"false\" instead of \"" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                stringBoolean + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * Converts a map into a valid hash table, i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * it removes all the 'null' values from the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    public static <K, V> Hashtable<K, V> mapToHashtable(Map<K, V> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        HashMap<K, V> m = new HashMap<K, V>(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (m.containsKey(null)) m.remove(null);
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1247
diff changeset
   738
        for (Iterator<?> i = m.values().iterator(); i.hasNext(); )
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            if (i.next() == null) i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        return new Hashtable<K, V>(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   743
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   744
     * Name of the attribute that specifies whether a connector server
1706
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   745
     * should not prevent the VM from exiting
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   746
     */
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   747
    public static final String JMX_SERVER_DAEMON = "jmx.remote.x.daemon";
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   748
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   749
    /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 25859
diff changeset
   750
     * Returns true if {@value JMX_SERVER_DAEMON} is specified in the {@code env}
1706
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   751
     * as a key and its value is a String and it is equal to true ignoring case.
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   752
     *
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   753
     * @param env
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   754
     * @return
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   755
     */
1708
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1706
diff changeset
   756
    public static boolean isServerDaemon(Map<String, ?> env) {
1706
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   757
        return (env != null) &&
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   758
                ("true".equalsIgnoreCase((String)env.get(JMX_SERVER_DAEMON)));
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   759
    }
4a382455c04d 6760712: Provide a connector server option that causes it not to prevent the VM from exiting
sjiang
parents: 1510
diff changeset
   760
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    private static final class SinkOutputStream extends OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        public void write(byte[] b, int off, int len) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        public void write(int b) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        new ClassLogger("javax.management.remote.misc", "EnvHelp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
}