jdk/src/share/classes/com/sun/jmx/remote/util/EnvHelp.java
author sjiang
Thu, 31 Jul 2008 15:31:13 +0200
changeset 1004 5ba8217eb504
parent 2 90ce3da70b43
child 1247 b4c26443dee5
permissions -rw-r--r--
5108776: Add reliable event handling to the JMX API 6218920: API bug - impossible to delete last MBeanServerForwarder on a connector Reviewed-by: emcmanus
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jmx.remote.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.SortedMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.SortedSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.TreeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.TreeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.remote.JMXConnectorFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import com.sun.jmx.mbeanserver.GetPropertyAction;
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    50
import com.sun.jmx.remote.security.NotificationAccessController;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    51
import javax.management.remote.JMXConnector;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
    52
import javax.management.remote.JMXConnectorServer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class EnvHelp {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * <p>Name of the attribute that specifies a default class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * The value associated with this attribute is a ClassLoader object</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final String DEFAULT_CLASS_LOADER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        JMXConnectorFactory.DEFAULT_CLASS_LOADER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <p>Name of the attribute that specifies a default class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *    ObjectName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * The value associated with this attribute is an ObjectName object</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final String DEFAULT_CLASS_LOADER_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        JMXConnectorServerFactory.DEFAULT_CLASS_LOADER_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Get the Connector Server default class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <p>
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @exception InstanceNotFoundException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <code>jmx.remote.default.class.loader.name</code> is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * and the ClassLoader MBean is not found in <var>mbs</var>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static ClassLoader resolveServerClassLoader(Map env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                                       MBeanServer mbs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        throws InstanceNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (env == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        Object loader = env.get(DEFAULT_CLASS_LOADER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Object name   = env.get(DEFAULT_CLASS_LOADER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (loader != null && name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            final String msg = "Only one of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                DEFAULT_CLASS_LOADER + " or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                DEFAULT_CLASS_LOADER_NAME +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                " should be specified.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (loader == null && name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (loader instanceof ClassLoader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                return (ClassLoader) loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    "ClassLoader object is not an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    ClassLoader.class.getName() + " : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    loader.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        ObjectName on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (name instanceof ObjectName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            on = (ObjectName) name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                "ClassLoader name is not an instance of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                ObjectName.class.getName() + " : " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                name.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (mbs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new IllegalArgumentException("Null MBeanServer object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return mbs.getClassLoader(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Get the Connector Client default class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Returns:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <p>
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public static ClassLoader resolveClientClassLoader(Map env) {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * {@link Throwable#getCause()} method (JDK Version >= 1.4)
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 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                t.getClass().getMethod("getCause", (Class[]) null);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <p>Name of the attribute that specifies the size of a notification
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public static int getNotifBufferSize(Map env) {
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <p>Name of the attribute that specifies the maximum number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * notifications that a client will fetch from its server.. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * value associated with this attribute should be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <code>Integer</code> object.  The default value is 1000.</p>
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public static int getMaxFetchNotifNumber(Map env) {
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * <p>Name of the attribute that specifies the timeout for a
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * object.  The default value is 60000 milliseconds.</p>
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public static long getFetchTimeout(Map env) {
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
    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   353
     * <p>Name of the attribute that specifies an object that will check
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.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   357
     * The default value is null.</p>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   358
     * 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
   359
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   360
    public static final String NOTIF_ACCESS_CONTROLLER =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   361
            "com.sun.jmx.remote.notification.access.controller";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   362
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   363
    public static NotificationAccessController getNotificationAccessController(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   364
            Map env) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   365
        return (env == null) ? null :
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   366
            (NotificationAccessController) env.get(NOTIF_ACCESS_CONTROLLER);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Get an integer-valued attribute with name <code>name</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * from <code>env</code>.  If <code>env</code> is null, or does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * not contain an entry for <code>name</code>, return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <code>defaultValue</code>.  The value may be a Number, or it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * may be a String that is parsable as a long.  It must be at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * least <code>minValue</code> and at most<code>maxValue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @throws IllegalArgumentException if <code>env</code> contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * an entry for <code>name</code> but it does not meet the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * constraints above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public static long getIntegerAttribute(Map env, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                           long defaultValue, long minValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                           long maxValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        final Object o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (env == null || (o = env.get(name)) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        final long result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (o instanceof Number)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            result = ((Number) o).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        else if (o instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            result = Long.parseLong((String) o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            /* May throw a NumberFormatException, which is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
               IllegalArgumentException.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                "Attribute " + name + " value must be Integer or String: " + o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (result < minValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                "Attribute " + name + " value must be at least " + minValue +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                ": " + result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (result > maxValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                "Attribute " + name + " value must be at most " + maxValue +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                ": " + result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public static final String DEFAULT_ORB="java.naming.corba.orb";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /* Check that all attributes have a key that is a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
       Could make further checks, e.g. appropriate types for attributes.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public static void checkAttributes(Map attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        for (Iterator it = attributes.keySet().iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            Object key = it.next();
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        for (Iterator it = objects.iterator(); it.hasNext(); i++) {
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";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        Iterator keyIterator = map.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        Iterator stringIterator = hiddenStrings.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        Iterator prefixIterator = hiddenPrefixes.iterator();
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())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            nextString = (String) stringIterator.next();
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())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            nextPrefix = (String) prefixIterator.next();
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()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            String key = (String) keyIterator.next();
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())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    nextString = (String) stringIterator.next();
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())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    nextPrefix = (String) prefixIterator.next();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * <p>Name of the attribute that specifies the timeout to keep a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * server side connection after answering last client request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * The default value is 120000 milliseconds.</p>
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    public static long getServerConnectionTimeout(Map env) {
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * <p>Name of the attribute that specifies the period in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * millisecond for a client to check its connection.  The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * value is 60000 milliseconds.</p>
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    public static long getConnectionCheckPeriod(Map env) {
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @param env the environment map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @param prop the name of the property in the environment map whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * returned string value must be converted into a boolean value.
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   671
     * @param systemProperty if true, consult a system property of the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   672
     * same name if there is no entry in the environment map.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *   <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *   <li>{@code false} if {@code env.get(prop)} is {@code null}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *   <li>{@code false} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *       {@code ((String)env.get(prop)).equalsIgnoreCase("false")}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *       is {@code true}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *   <li>{@code true} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *       {@code ((String)env.get(prop)).equalsIgnoreCase("true")}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *       is {@code true}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *   </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @throws IllegalArgumentException if {@code env} is {@code null} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * {@code env.get(prop)} is not {@code null} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * {@code ((String)env.get(prop)).equalsIgnoreCase("false")} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * {@code ((String)env.get(prop)).equalsIgnoreCase("true")} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @throws ClassCastException if {@code env.get(prop)} cannot be cast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * to {@code String}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     */
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   693
    public static boolean computeBooleanFromString(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   694
            Map env, String prop, boolean systemProperty) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   695
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   696
        if (env == null)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   697
            throw new IllegalArgumentException("env map cannot be null");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   698
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   699
        // returns a default value of 'false' if no property is found...
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   700
        return computeBooleanFromString(env,prop,systemProperty,false);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   701
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   702
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   703
    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   704
     * 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
   705
     * property in the given map.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   706
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   707
     * @param env the environment map.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   708
     * @param prop the name of the property in the environment map whose
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   709
     * returned string value must be converted into a boolean value.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   710
     * @param systemProperty if true, consult a system property of the
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   711
     * same name if there is no entry in the environment map.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   712
     * @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
   713
     *        was defined.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   714
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   715
     * @return
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   716
     *   <ul>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   717
     *   <li>{@code defaultValue} if {@code env.get(prop)} is {@code null}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   718
     *       and {@code systemProperty} is {@code false}</li>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   719
     *   <li>{@code defaultValue} if {@code env.get(prop)} is {@code null}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   720
     *       and {@code systemProperty} is {@code true} and
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   721
     *       {@code System.getProperty(prop)} is {@code null}</li>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   722
     *   <li>{@code false} if {@code env.get(prop)} is {@code null}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   723
     *       and {@code systemProperty} is {@code true} and
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   724
     *       {@code System.getProperty(prop).equalsIgnoreCase("false")}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   725
     *       is {@code true}</li>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   726
     *   <li>{@code true} if {@code env.get(prop)} is {@code null}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   727
     *       and {@code systemProperty} is {@code true} and
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   728
     *       {@code System.getProperty(prop).equalsIgnoreCase("true")}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   729
     *       is {@code true}</li>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   730
     *   <li>{@code false} if
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   731
     *       {@code ((String)env.get(prop)).equalsIgnoreCase("false")}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   732
     *       is {@code true}</li>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   733
     *   <li>{@code true} if
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   734
     *       {@code ((String)env.get(prop)).equalsIgnoreCase("true")}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   735
     *       is {@code true}</li>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   736
     *   </ul>
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   737
     *
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   738
     * @throws IllegalArgumentException if {@code env} is {@code null} or
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   739
     * {@code env.get(prop)} is not {@code null} and
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   740
     * {@code ((String)env.get(prop)).equalsIgnoreCase("false")} and
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   741
     * {@code ((String)env.get(prop)).equalsIgnoreCase("true")} are
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   742
     * {@code false}.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   743
     * @throws ClassCastException if {@code env.get(prop)} cannot be cast
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   744
     * to {@code String}.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   745
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   746
    public static boolean computeBooleanFromString(
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   747
            Map env, String prop, boolean systemProperty, boolean defaultValue) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if (env == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            throw new IllegalArgumentException("env map cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        String stringBoolean = (String) env.get(prop);
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   753
        if (stringBoolean == null && systemProperty) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   754
            stringBoolean =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   755
                    AccessController.doPrivileged(new GetPropertyAction(prop));
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   756
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        if (stringBoolean == null)
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   759
            return defaultValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        else if (stringBoolean.equalsIgnoreCase("true"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        else if (stringBoolean.equalsIgnoreCase("false"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            throw new IllegalArgumentException(prop +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                " must be \"true\" or \"false\" instead of \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                stringBoolean + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * Converts a map into a valid hash table, i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * it removes all the 'null' values from the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    public static <K, V> Hashtable<K, V> mapToHashtable(Map<K, V> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        HashMap<K, V> m = new HashMap<K, V>(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        if (m.containsKey(null)) m.remove(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        for (Iterator i = m.values().iterator(); i.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            if (i.next() == null) i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        return new Hashtable<K, V>(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   782
    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   783
     * Returns true if the parameter JMXConnector.USE_EVENT_SERVICE is set to a
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   784
     * String equals "true" by ignoring case in the map or in the System.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   785
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   786
    public static boolean eventServiceEnabled(Map env) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   787
        return computeBooleanFromString(env, JMXConnector.USE_EVENT_SERVICE, true);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   788
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   789
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   790
    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   791
     * Returns true if the parameter JMXConnectorServer.DELEGATE_TO_EVENT_SERVICE
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   792
     * is set to a String equals "true" (ignores case).
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   793
     * If the property DELEGATE_TO_EVENT_SERVICE is not set, returns
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   794
     * a default value of "true".
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   795
     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   796
    public static boolean delegateToEventService(Map env) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   797
        return computeBooleanFromString(env,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   798
                JMXConnectorServer.DELEGATE_TO_EVENT_SERVICE, true, true);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   799
    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   800
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   801
//    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   802
//     * <p>Name of the attribute that specifies an EventRelay object to use.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   803
//     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   804
//    public static final String EVENT_RELAY =
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   805
//            "jmx.remote.x.event.relay";
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   806
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   807
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   808
//    /**
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   809
//     * Returns an EventRelay object. The default one is FetchingEventRelay.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   810
//     * If {@code EVENT_RELAY} is specified in {@code env} as a key,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   811
//     * its value will be returned as an EventRelay object, if the value is
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   812
//     * not of type {@code EventRelay}, the default {@code FetchingEventRelay}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   813
//     * will be returned.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   814
//     * If {@code EVENT_RELAY} is not specified but {@code ENABLE_EVENT_RELAY}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   815
//     * is specified as a key and its value is <code true>, the default {@code FetchingEventRelay}
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   816
//     * will be returned.
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   817
//     */
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   818
//    public static EventRelay getEventRelay(Map env) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   819
//        Map info = env == null ?
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   820
//            Collections.EMPTY_MAP : env;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   821
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   822
//        Object o = env.get(EVENT_RELAY);
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   823
//        if (o instanceof EventRelay) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   824
//            return (EventRelay)o;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   825
//        } else if (o != null) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   826
//            logger.warning("getEventRelay",
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   827
//                    "The user specified object is not an EventRelay object, " +
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   828
//                    "using the default class FetchingEventRelay.");
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   829
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   830
//            return new FetchingEventRelay();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   831
//        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   832
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   833
//        if (enableEventRelay(env)) {
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   834
//            return new FetchingEventRelay();
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   835
//        }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   836
//
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   837
//        return null;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   838
//    }
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   839
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   840
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    private static final class SinkOutputStream extends OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        public void write(byte[] b, int off, int len) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        public void write(int b) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        new ClassLogger("javax.management.remote.misc", "EnvHelp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
}