jdk/src/share/classes/javax/management/namespace/JMXNamespaces.java
author emcmanus
Fri, 07 Nov 2008 11:48:07 +0100
changeset 1570 4165709c91e3
parent 1222 78e3d021d528
child 1709 392dd6db361a
permissions -rw-r--r--
5072267: A way to communicate client context such as locale to the JMX server Summary: Support for client contexts and also for localization of descriptions Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     1
/*
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     4
 *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    10
 *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    15
 * accompanied this code).
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    16
 *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    20
 *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    23
 * have any questions.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    24
 */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    25
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    26
package javax.management.namespace;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    27
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    28
import com.sun.jmx.defaults.JmxProperties;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    29
import com.sun.jmx.namespace.ObjectNameRouter;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    30
import com.sun.jmx.namespace.serial.RewritingProcessor;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    31
import com.sun.jmx.namespace.RoutingConnectionProxy;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    32
import com.sun.jmx.namespace.RoutingServerProxy;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    33
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    34
import java.util.logging.Level;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    35
import java.util.logging.Logger;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    36
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
    37
import javax.management.InstanceNotFoundException;
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    38
import javax.management.MBeanServer;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    39
import javax.management.MBeanServerConnection;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    40
import javax.management.MalformedObjectNameException;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    41
import javax.management.ObjectName;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    42
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    43
/**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    44
 * Static constants and utility methods to help work with
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    45
 * JMX name spaces.  There are no instances of this class.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    46
 * @since 1.7
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    47
 */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    48
public class JMXNamespaces {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    49
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    50
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    51
     * A logger for this class.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    52
     **/
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    53
    private static final Logger LOG = JmxProperties.NAMESPACE_LOGGER;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    54
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    55
    /** Creates a new instance of JMXNamespaces */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    56
    private JMXNamespaces() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    57
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    58
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    59
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    60
     * The name space separator. This is an alias for {@link
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    61
     * ObjectName#NAMESPACE_SEPARATOR}.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    62
     **/
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    63
    public static final String NAMESPACE_SEPARATOR =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    64
            ObjectName.NAMESPACE_SEPARATOR;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    65
    private static final int NAMESPACE_SEPARATOR_LENGTH =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    66
            NAMESPACE_SEPARATOR.length();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    67
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    68
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    69
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    70
     * Creates a new {@code MBeanServerConnection} proxy on a
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    71
     * {@linkplain javax.management.namespace sub name space}
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    72
     * of the given parent.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    73
     *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    74
     * @param parent The parent {@code MBeanServerConnection} that contains
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    75
     *               the name space.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    76
     * @param namespace  The {@linkplain javax.management.namespace
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    77
     *               name space} in which to narrow.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    78
     * @return A new {@code MBeanServerConnection} proxy that shows the content
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    79
     *         of that name space.
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
    80
     * @throws IllegalArgumentException if either argument is null,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
    81
     * or the name space does not exist, or if a proxy for that name space
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
    82
     * cannot be created.  The {@linkplain Throwable#getCause() cause} of
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
    83
     * this exception will be an {@link InstanceNotFoundException} if and only
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
    84
     * if the name space is found not to exist.
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    85
     */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    86
    public static MBeanServerConnection narrowToNamespace(
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    87
                        MBeanServerConnection parent,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    88
                        String namespace) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    89
        if (LOG.isLoggable(Level.FINER))
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    90
            LOG.finer("Making MBeanServerConnection for: " +namespace);
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
    91
        return RoutingConnectionProxy.cd(parent, namespace, true);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    92
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    93
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    94
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    95
     * Creates a new {@code MBeanServer} proxy on a
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    96
     * {@linkplain javax.management.namespace sub name space}
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    97
     * of the given parent.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    98
     *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    99
     * @param parent The parent {@code MBeanServer} that contains
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   100
     *               the name space.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   101
     * @param namespace  The {@linkplain javax.management.namespace
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   102
     *               name space} in which to narrow.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   103
     * @return A new {@code MBeanServer} proxy that shows the content
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   104
     *         of that name space.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   105
     * @throws IllegalArgumentException if either argument is null,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   106
     * or the name space does not exist, or if a proxy for that name space
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
   107
     * cannot be created.  The {@linkplain Throwable#getCause() cause} of
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
   108
     * this exception will be an {@link InstanceNotFoundException} if and only
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
   109
     * if the name space is found not to exist.
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   110
     */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   111
    public static MBeanServer narrowToNamespace(MBeanServer parent,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   112
            String namespace) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   113
        if (LOG.isLoggable(Level.FINER))
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
   114
            LOG.finer("Making MBeanServer for: " +namespace);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
   115
        return RoutingServerProxy.cd(parent, namespace, true);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   116
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   117
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   118
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   119
     * Returns an object that is the same as the given object except that
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   120
     * any {@link ObjectName} it might contain has its domain modified.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   121
     * The returned object might be identical to the given object if it
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   122
     * does not contain any {@code ObjectName} values or if none of them
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   123
     * were modified.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   124
     * This method will replace a prefix ({@code toRemove}) from the path of
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   125
     * the ObjectNames contained in {@code obj} by another prefix
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   126
     * ({@code toAdd}).
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   127
     * Therefore, all contained ObjectNames must have a path that start with
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   128
     * the given {@code toRemove} prefix. If one of them doesn't, an {@link
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   129
     * IllegalArgumentException} is thrown.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   130
     * <p>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   131
     * For instance, if {@code obj} contains the ObjectName
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   132
     * {@code x//y//z//d:k=x}, and {@code toAdd} is {@code v//w}, and
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   133
     * {@code toRemove}
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   134
     *  is {@code x//y} this method will return a copy of {@code obj} that
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   135
     * contains {@code v//w//z//d:k=x}.<br>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   136
     * On the other hand, if {@code obj} contains the ObjectName
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   137
     * {@code x//y//z//d:k=x}, and {@code toAdd} is {@code v//w}, and
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   138
     * {@code toRemove} is {@code v} this method
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   139
     * will raise an exception, because {@code x//y//z//d:k=x} doesn't start
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   140
     * with {@code v}
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   141
     * </p>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   142
     * <p>Note: the default implementation of this method can use the
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   143
     *   Java serialization framework to clone and replace ObjectNames in the
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   144
     *   provided {@code obj}. It will usually fail if {@code obj} is not
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   145
     *   Java serializable, or contains objects which are not Java
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   146
     *   serializable.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   147
     * </p>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   148
     * @param obj    The object to deep-rewrite
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   149
     * @param toRemove a prefix already present in contained ObjectNames.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   150
     *        If {@code toRemove} is the empty string {@code ""}, nothing
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   151
     *        will be removed from the contained ObjectNames.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   152
     * @param toAdd the prefix that will replace (@code toRemove} in contained
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   153
     *  ObjectNames.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   154
     *        If {@code toAdd} is the empty string {@code ""}, nothing
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   155
     *        will be added to the contained ObjectNames.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   156
     * @return the rewritten object, or possibly {@code obj} if nothing needed
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   157
     * to be changed.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   158
     * @throws IllegalArgumentException if {@code obj} couldn't be rewritten or
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   159
     * if {@code toRemove} or {@code toAdd} is null.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   160
     **/
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   161
    public static <T> T deepReplaceHeadNamespace(T obj, String toRemove, String toAdd) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   162
        final RewritingProcessor processor =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   163
                RewritingProcessor.newRewritingProcessor(toAdd,toRemove);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   164
        return processor.rewriteOutput(obj);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   165
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   166
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   167
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   168
     * Appends {@code namespace} to {@code path}.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   169
     * This methods appends {@code namespace} to {@code path} to obtain a
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   170
     * a <i>full path</i>, and normalizes the result thus obtained:
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   171
     * <ul>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   172
     * <li>If {@code path} is empty, the full path is
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   173
     *     {@code namespace}.</li>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   174
     * <li>Otherwise, if {@code namespace} is empty,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   175
     *     the full path is {@code path}</li>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   176
     * <li>Otherwise, and this is the regular case, the full path is the
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   177
     *     result of the concatenation of
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   178
     *     {@code path}+{@value #NAMESPACE_SEPARATOR}+{@code namespace}</li>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   179
     * <li>finally, the full path is normalized: multiple consecutive
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   180
     *     occurrences of {@value #NAMESPACE_SEPARATOR} are replaced by a
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   181
     *     single {@value #NAMESPACE_SEPARATOR} in the result, and trailing
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   182
     *     occurences of {@value #NAMESPACE_SEPARATOR} are removed.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   183
     * </li>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   184
     * </ul>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   185
     * @param path a name space path prefix
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   186
     * @param namespace a name space name to append to the path
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   187
     * @return a syntactically valid name space path, or "" if both parameters
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   188
     * are null or empty.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   189
     * @throws IllegalArgumentException if either argument is null or ends with
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   190
     * an odd number of {@code /} characters.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   191
     **/
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   192
    public static String concat(String path, String namespace) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   193
        if (path == null || namespace == null)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   194
            throw new IllegalArgumentException("Null argument");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   195
        checkTrailingSlashes(path);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   196
        checkTrailingSlashes(namespace);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   197
        final String result;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   198
        if (path.equals("")) result=namespace;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   199
        else if (namespace.equals("")) result=path;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   200
        else result=path+NAMESPACE_SEPARATOR+namespace;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   201
        return ObjectNameRouter.normalizeNamespacePath(result,false,true,false);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   202
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   203
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   204
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   205
     * Returns a syntactically valid name space path.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   206
     * If the provided {@code namespace} ends with {@code "//"},
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   207
     * recursively strips trailing {@code "//"}.  Each sequence of an
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   208
     * even number of {@code "/"} characters is also replaced by {@code "//"},
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   209
     * for example {@code "foo//bar////baz/////buh"} will become
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   210
     * {@code "foo//bar//baz///buh"}.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   211
     *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   212
     * @param namespace A name space path
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   213
     * @return {@code ""} - if the provided {@code namespace} resolves to
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   214
     * the empty string; otherwise a syntactically valid name space string
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   215
     * stripped of trailing and redundant {@code "//"}.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   216
     * @throws IllegalArgumentException if {@code namespace} is null or
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   217
     * is not syntactically valid (e.g. it contains
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   218
     * invalid characters like ':', or it ends with an odd
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   219
     * number of '/').
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   220
     */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   221
    public static String normalizeNamespaceName(String namespace) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   222
        if (namespace == null)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   223
            throw new IllegalArgumentException("Null namespace");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   224
        final String sourcePath =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   225
                ObjectNameRouter.normalizeNamespacePath(namespace,false,true,false);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   226
        if (sourcePath.equals("")) return sourcePath;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   227
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   228
        // Will throw an IllegalArgumentException if the namespace name
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   229
        // is not syntactically valid...
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   230
        //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   231
        getNamespaceObjectName(sourcePath);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   232
        return sourcePath;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   233
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   234
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   235
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   236
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   237
     * Return a canonical handler name for the provided {@code namespace},
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   238
     * The handler name returned will be
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   239
     * {@link #normalizeNamespaceName normalizeNamespaceName}{@code (namespace) +
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   240
     * "//:type=JMXNamespace"}.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   241
     *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   242
     * @param namespace A name space path
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   243
     * @return a canonical ObjectName for a name space handler.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   244
     * @see #normalizeNamespaceName
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   245
     * @throws IllegalArgumentException if the provided
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   246
     *          {@code namespace} is null or not valid.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   247
     */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   248
    public static ObjectName getNamespaceObjectName(String namespace) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   249
        if (namespace == null || namespace.equals(""))
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   250
            throw new IllegalArgumentException("Null or empty namespace");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   251
        final String sourcePath =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   252
                ObjectNameRouter.normalizeNamespacePath(namespace,false,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   253
                            true,false);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   254
        try {
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1222
diff changeset
   255
            // We could use ObjectName.valueOf here - but throwing an
1222
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   256
            // IllegalArgumentException that contains just the supplied
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   257
            // namespace instead of the whole ObjectName seems preferable.
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   258
            return ObjectName.getInstance(sourcePath+
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   259
                    NAMESPACE_SEPARATOR+":"+
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   260
                    JMXNamespace.TYPE_ASSIGNMENT);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   261
        } catch (MalformedObjectNameException x) {
1222
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   262
            throw new IllegalArgumentException("Invalid namespace: " +
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   263
                                               namespace,x);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   264
        }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   265
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   266
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   267
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   268
     * Returns an ObjectName pattern that can be used to query for all MBeans
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   269
     * contained in the given name space.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   270
     * For instance, if {@code namespace="foo//bar"}, this method will
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   271
     * return {@code "foo//bar//*:*"}
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   272
     * @return an ObjectName pattern that selects all MBeans in the given
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   273
     *         name space.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   274
     **/
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   275
    public static ObjectName getWildcardFor(String namespace) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   276
            return insertPath(namespace,ObjectName.WILDCARD);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   277
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   278
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   279
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   280
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   281
     * Returns an ObjectName that can be used to access an MBean
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   282
     * contained in the given name space.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   283
     * For instance, if {@code path="foo//bar"}, and
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   284
     * {@code to="domain:type=Thing"} this method will
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   285
     * return {@code "foo//bar//domain:type=Thing"}
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   286
     * @return an ObjectName that can be used to invoke an MBean located in a
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   287
     *         sub name space.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   288
     * @throws IllegalArgumentException if {@code path} ends with an
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   289
     * odd number of {@code /} characters.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   290
     **/
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   291
    public static ObjectName insertPath(String path, ObjectName to) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   292
        if (path == null || to == null)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   293
            throw new IllegalArgumentException("Null argument");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   294
        checkTrailingSlashes(path);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   295
        try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   296
            String prefix = path;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   297
            if (!prefix.equals("")) prefix =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   298
                    ObjectNameRouter.normalizeNamespacePath(
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   299
                        prefix + NAMESPACE_SEPARATOR,false,false,false);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   300
            return to.withDomain(
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   301
                    ObjectNameRouter.normalizeDomain(
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   302
                        prefix+to.getDomain(),false));
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   303
        } catch (MalformedObjectNameException x) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   304
            throw new IllegalArgumentException(path+": "+x,x);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   305
        }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   306
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   307
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   308
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   309
     * Returns the normalized name space path of the name space expected to
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   310
     * contain {@code ObjectName}.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   311
     * For instance, for {@code "foo//domain:type=Thing"} this will be
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   312
     * {@code "foo"}. For {@code "//foo//bar//domain:type=Thing"} this will be
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   313
     * {@code "foo//bar"}. For {@code //foo//bar//baz//domain:type=Thing}
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   314
     * this will be {@code "foo//bar//baz"}. For
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   315
     * {@code //foo//bar//baz//:type=JMXNamespace}
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   316
     * this will be {@code "foo//bar"}.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   317
     *
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   318
     * @param name an {@code ObjectName}
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   319
     * @return the name space path of the name space that could contain such
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   320
     *         a name. If {@code name} has no name space, returns {@code ""}.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   321
     * @throws IllegalArgumentException if {@code name} is null.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   322
     **/
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   323
    public static String getContainingNamespace(ObjectName name) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   324
        return getNormalizedPath(name,true);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   325
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   326
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   327
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   328
    static String getNormalizedPath(ObjectName name,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   329
            boolean removeLeadingSep) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   330
        if (name == null)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   331
            throw new IllegalArgumentException("Null name");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   332
        String domain =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   333
                ObjectNameRouter.normalizeDomain(name.getDomain(),removeLeadingSep);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   334
        int end = domain.length();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   335
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   336
        // special case of domain part being a single '/'
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   337
        //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   338
        if (domain.endsWith(NAMESPACE_SEPARATOR+"/"))
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   339
            return domain.substring(0,end-NAMESPACE_SEPARATOR_LENGTH-1);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   340
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   341
        // special case of namespace handler
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   342
        //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   343
        if (domain.endsWith(NAMESPACE_SEPARATOR))
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   344
            domain = domain.substring(0,end-NAMESPACE_SEPARATOR_LENGTH);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   345
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   346
        int last = domain.lastIndexOf(NAMESPACE_SEPARATOR);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   347
        if (last < 0) return "";
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   348
        if (last == 0) return domain;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   349
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   350
        // special case of domain part starting with '/'
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   351
        // last=0 is not possible - we took care of this above.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   352
        if (domain.charAt(last-1) == '/') last--;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   353
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   354
        return domain.substring(0,last);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   355
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   356
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   357
    private static void checkTrailingSlashes(String path) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   358
        int i;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   359
        for (i = path.length() - 1; i >= 0 && path.charAt(i) == '/'; i--)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   360
            continue;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   361
        if (path.length() - i % 2 == 0)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   362
            throw new IllegalArgumentException("Path ends with odd number of /");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   363
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   364
}