jdk/src/share/classes/com/sun/jmx/namespace/RoutingProxy.java
author dfuchs
Wed, 10 Sep 2008 16:27:13 +0200
changeset 1227 4546977d0d66
parent 1222 78e3d021d528
child 1570 4165709c91e3
permissions -rw-r--r--
6746754: jmx namespace: test for leading separator missing 6669137: RFE: InstanceNotFoundException should have a constructor that takes an ObjectName 6746796: jmx namespaces: Several tests are missing an @bug or @run keyword Summary: Note on 6669137: first implementation of 6669137 was actually pushed with 5072476 - here we only have a small update and a test case. Also re-fixes 6737133: Compilation failure of test/javax/management/eventService/LeaseManagerDeadlockTest.java which had failed. Reviewed-by: emcmanus, yjoan
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 com.sun.jmx.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 java.io.IOException;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    30
import java.util.logging.Level;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    31
import java.util.logging.Logger;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    32
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    33
import javax.management.MBeanException;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    34
import javax.management.MBeanRegistrationException;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    35
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    36
import javax.management.MBeanServerConnection;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    37
import javax.management.MalformedObjectNameException;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    38
import javax.management.ObjectName;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    39
import javax.management.namespace.JMXNamespaces;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    40
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    41
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    42
/**
1222
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    43
 * A RoutingProxy narrows on a given name space in a
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    44
 * source object implementing MBeanServerConnection.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    45
 * It is used to implement
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    46
 * {@code JMXNamespaces.narrowToNamespace(...)}.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    47
 * This abstract class has two concrete subclasses:
1222
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    48
 * <p>{@link RoutingConnectionProxy}: to narrow down into an
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    49
 *    MBeanServerConnection.</p>
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    50
 * <p>{@link RoutingServerProxy}: to narrow down into an MBeanServer.</p>
1227
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    51
 *
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    52
 * <p>This class can also be used to "broaden" from a namespace.  The same
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    53
 * class is used for both purposes because in both cases all that happens
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    54
 * is that ObjectNames are rewritten in one way on the way in (e.g. the
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    55
 * parameter of getMBeanInfo) and another way on the way out (e.g. the
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    56
 * return value of queryNames).</p>
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    57
 *
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    58
 * <p>Specifically, if you narrow into "a//" then you want to add the
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    59
 * "a//" prefix to ObjectNames on the way in and subtract it on the way
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    60
 * out.  But ClientContext uses this class to subtract the
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    61
 * "jmx.context//foo=bar//" prefix on the way in and add it back on the
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    62
 * way out.</p>
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
    63
 *
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    64
 * <p><b>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    65
 * This API is a Sun internal API and is subject to changes without notice.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    66
 * </b></p>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    67
 * @since 1.7
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
    68
 */
1222
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    69
//
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    70
// RoutingProxies are client side objects which are used to narrow down
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    71
// into a namespace. They are used to perform ObjectName translation,
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    72
// adding the namespace to the routing ObjectName before sending it over
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    73
// to the source connection, and removing that prefix from results of
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    74
// queries, createMBean, registerMBean, and getObjectInstance.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    75
// This translation is the opposite to that which is performed by
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    76
// NamespaceInterceptors.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    77
//
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    78
// There is however a special case where routing proxies are used on the
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    79
// 'server' side to remove a namespace - rather than to add it:
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    80
// This the case of ClientContext.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    81
// When an ObjectName like "jmx.context//c1=v1,c2=v2//D:k=v" reaches the
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    82
// jmx.context namespace, a routing proxy is used to remove the prefix
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    83
// c1=v1,c2=v2// from the routing objectname.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    84
//
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    85
// For a RoutingProxy used in a narrowDownToNamespace operation, we have:
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    86
//     targetNs="" // targetNS is the namespace 'to remove'
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    87
//     sourceNS=<namespace-we-narrow-down-to> // namespace 'to add'
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    88
//
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    89
// For a RoutingProxy used in a ClientContext operation, we have:
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    90
//     targetNs=<encoded-context> // context must be removed from object name
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    91
//     sourceNs="" // nothing to add...
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    92
//
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    93
// RoutingProxies can also be used on the client side to implement
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    94
// "withClientContext" operations. In that case, the boolean parameter
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    95
// 'forwards context' is set to true, targetNs is "", and sourceNS may
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    96
// also be "". When forwardsContext is true, the RoutingProxy dynamically
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    97
// creates an ObjectNameRouter for each operation - in order to dynamically add
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    98
// the context attached to the thread to the routing ObjectName. This is
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
    99
// performed in the getObjectNameRouter() method.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   100
//
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   101
// Finally, in order to avoid too many layers of wrapping,
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   102
// RoutingConnectionProxy and RoutingServerProxy can be created through a
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   103
// factory method that can concatenate namespace pathes in order to
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   104
// return a single RoutingProxy - rather than wrapping a RoutingProxy inside
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   105
// another RoutingProxy. See RoutingConnectionProxy.cd and
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   106
// RoutingServerProxy.cd
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   107
//
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   108
// The class hierarchy is as follows:
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   109
//
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   110
//                           RoutingMBeanServerConnection
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   111
//                   [abstract class for all routing interceptors,
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   112
//                    such as RoutingProxies and HandlerInterceptors]
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   113
//                            /                          \
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   114
//                           /                            \
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   115
//                    RoutingProxy                HandlerInterceptor
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   116
//          [base class for                   [base class for server side
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   117
//           client-side objects used          objects, created by
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   118
//           in narrowDownTo]                  DispatchInterceptors]
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   119
//           /                  \                   |          \
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   120
//  RoutingConnectionProxy       \                  |      NamespaceInterceptor
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   121
//  [wraps MBeanServerConnection  \                 |     [used to remove
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   122
//   objects]                      \                |      namespace prefix and
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   123
//                        RoutingServerProxy        |      wrap  JMXNamespace]
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   124
//                        [wraps MBeanServer        |
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   125
//                         Objects]                 |
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   126
//                                            DomainInterceptor
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   127
//                                            [used to wrap JMXDomain]
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   128
//
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   129
// RoutingProxies also differ from HandlerInterceptors in that they transform
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   130
// calls to MBeanServerConnection operations that do not have any parameters
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   131
// into a call to the underlying JMXNamespace MBean.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   132
// So for instance a call to:
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   133
//    JMXNamespaces.narrowDownToNamespace(conn,"foo").getDomains()
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   134
// is transformed into
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   135
//    conn.getAttribute("foo//type=JMXNamespace","Domains");
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   136
//
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   137
public abstract class RoutingProxy<T extends MBeanServerConnection>
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   138
        extends RoutingMBeanServerConnection<T> {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   139
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   140
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   141
     * A logger for this class.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   142
     **/
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   143
    private static final Logger LOG = JmxProperties.NAMESPACE_LOGGER;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   144
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   145
    // The source MBeanServerConnection
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   146
    private final T source;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   147
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   148
    // The name space we're narrowing to (usually some name space in
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   149
    // the source MBeanServerConnection
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   150
    private final String                sourceNs;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   151
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   152
    // The name space we pretend to be mounted in (usually "")
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   153
    private final String                targetNs;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   154
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   155
    // The name of the JMXNamespace that handles the source name space
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   156
    private final ObjectName            handlerName;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   157
    private final ObjectNameRouter      router;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   158
    final boolean forwardsContext;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   159
    private volatile String             defaultDomain = 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
    /**
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   162
     * Creates a new instance of RoutingProxy
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   163
     */
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   164
    protected RoutingProxy(T source,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   165
                          String sourceNs,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   166
                          String targetNs,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   167
                          boolean forwardsContext) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   168
        if (source == null) throw new IllegalArgumentException("null");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   169
        this.sourceNs = JMXNamespaces.normalizeNamespaceName(sourceNs);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   170
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   171
        // Usually sourceNs is not null, except when implementing
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   172
        // Client Contexts
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   173
        //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   174
        if (sourceNs.equals("")) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   175
            this.handlerName = null;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   176
        } else {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   177
            // System.err.println("sourceNs: "+sourceNs);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   178
            this.handlerName =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   179
                JMXNamespaces.getNamespaceObjectName(this.sourceNs);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   180
            try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   181
                // System.err.println("handlerName: "+handlerName);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   182
                if (!source.isRegistered(handlerName))
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   183
                    throw new IllegalArgumentException(sourceNs +
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   184
                            ": no such name space");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   185
            } catch (IOException x) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   186
                throw new IllegalArgumentException("source stale: "+x,x);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   187
            }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   188
        }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   189
        this.source = source;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   190
        this.targetNs = (targetNs==null?"":
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   191
            JMXNamespaces.normalizeNamespaceName(targetNs));
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   192
        this.router =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   193
                new ObjectNameRouter(this.targetNs,this.sourceNs);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   194
        this.forwardsContext = forwardsContext;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   195
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   196
        if (LOG.isLoggable(Level.FINER))
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   197
            LOG.finer("RoutingProxy for " + this.sourceNs + " created");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   198
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   199
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   200
    @Override
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   201
    public T source() { return source; }
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
    ObjectNameRouter getObjectNameRouter() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   204
// TODO: uncomment this when contexts are added
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   205
//        if (forwardsContext)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   206
//            return ObjectNameRouter.wrapWithContext(router);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   207
//        else
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   208
            return router;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   209
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   210
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   211
    @Override
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   212
    public ObjectName toSource(ObjectName targetName)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   213
        throws MalformedObjectNameException {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   214
        if (targetName == null) return null;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   215
        if (targetName.getDomain().equals("") && targetNs.equals("")) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   216
            try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   217
                if (defaultDomain == null)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   218
                    defaultDomain = getDefaultDomain();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   219
            } catch(Exception x) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   220
                LOG.log(Level.FINEST,"Failed to get default domain",x);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   221
            }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   222
            if (defaultDomain != null)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   223
                targetName = targetName.withDomain(defaultDomain);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   224
        }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   225
        final ObjectNameRouter r = getObjectNameRouter();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   226
        return r.toSourceContext(targetName,true);
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
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   229
    @Override
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   230
    protected ObjectName newSourceMBeanName(ObjectName targetName)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   231
        throws MBeanRegistrationException {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   232
        if (targetName != null) return super.newSourceMBeanName(targetName);
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
        // OK => we can accept null if sourceNs is empty.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   235
        if (sourceNs.equals("")) return null;
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
        throw new MBeanRegistrationException(
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   238
                new IllegalArgumentException(
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   239
                "Can't use null ObjectName with namespaces"));
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   240
    }
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
    @Override
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   243
    public ObjectName toTarget(ObjectName sourceName)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   244
        throws MalformedObjectNameException {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   245
        if (sourceName == null) return null;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   246
        final ObjectNameRouter r = getObjectNameRouter();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   247
        return r.toTargetContext(sourceName,false);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   248
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   249
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   250
    private Object getAttributeFromHandler(String attributeName)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   251
            throws IOException {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   252
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   253
        try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   254
            return source().getAttribute(handlerName,attributeName);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   255
         } catch (RuntimeException ex) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   256
            throw makeCompliantRuntimeException(ex);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   257
         } catch (IOException x) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   258
             throw x;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   259
         } catch (MBeanException ex) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   260
             throw new IOException("Failed to get "+attributeName+": "+
1227
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
   261
                     ex.getCause(),
4546977d0d66 6746754: jmx namespace: test for leading separator missing
dfuchs
parents: 1222
diff changeset
   262
                     ex.getCause());
1222
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   263
         } catch (Exception ex) {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   264
             throw new IOException("Failed to get "+attributeName+": "+
1222
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   265
                     ex,ex);
1156
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
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   269
    // We cannot call getMBeanCount() on the underlying
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   270
    // MBeanServerConnection, because it would return the number of
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   271
    // 'top-level' MBeans, not the number of MBeans in the name space
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   272
    // we are narrowing to. Instead we're calling getMBeanCount() on
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   273
    // the JMXNamespace that handles the source 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
    // There is however one particular case when the sourceNs is empty.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   276
    // In that case, there's no handler - and the 'source' is the top
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   277
    // level namespace. In that particular case, handlerName will be null,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   278
    // and we directly invoke the top level source().
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   279
    // This later complex case is only used when implementing ClientContexts.
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
    @Override
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   282
    public Integer getMBeanCount() throws IOException {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   283
        try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   284
            if (handlerName == null) return source().getMBeanCount();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   285
            return (Integer) getAttributeFromHandler("MBeanCount");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   286
         } catch (RuntimeException ex) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   287
            throw makeCompliantRuntimeException(ex);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   288
         }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   289
    }
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
    // We cannot call getDomains() on the underlying
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   292
    // MBeanServerConnection, because it would return the domains of
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   293
    // 'top-level' MBeans, not the domains of MBeans in the name space
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   294
    // we are narrowing to. Instead we're calling getDomains() on
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   295
    // the JMXNamespace that handles the source name space.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   296
    //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   297
    // There is however one particular case when the sourceNs is empty.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   298
    // In that case, there's no handler - and the 'source' is the top
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   299
    // level namespace. In that particular case, handlerName will be null,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   300
    // and we directly invoke the top level source().
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   301
    // This later complex case is only used when implementing ClientContexts.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   302
    //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   303
    @Override
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   304
    public String[] getDomains() throws IOException {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   305
        try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   306
            if (handlerName == null) return source().getDomains();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   307
            return (String[]) getAttributeFromHandler("Domains");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   308
        } catch (RuntimeException ex) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   309
            throw makeCompliantRuntimeException(ex);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   310
        }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   311
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   312
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   313
    // We cannot call getDefaultDomain() on the underlying
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   314
    // MBeanServerConnection, because it would return the default domain of
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   315
    // 'top-level' namespace, not the default domain in the name space
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   316
    // we are narrowing to. Instead we're calling getDefaultDomain() on
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   317
    // the JMXNamespace that handles the source name space.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   318
    //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   319
    // There is however one particular case when the sourceNs is empty.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   320
    // In that case, there's no handler - and the 'source' is the top
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   321
    // level namespace. In that particular case, handlerName will be null,
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   322
    // and we directly invoke the top level source().
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   323
    // This later complex case is only used when implementing ClientContexts.
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   324
    //
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   325
    @Override
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   326
    public String getDefaultDomain() throws IOException {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   327
        try {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   328
            if (handlerName == null) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   329
                defaultDomain = source().getDefaultDomain();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   330
            } else {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   331
                defaultDomain =(String)
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   332
                        getAttributeFromHandler("DefaultDomain");
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   333
            }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   334
            return defaultDomain;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   335
        } catch (RuntimeException ex) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   336
            throw makeCompliantRuntimeException(ex);
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
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   339
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   340
    public String getSourceNamespace() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   341
        return sourceNs;
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
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   344
    public String getTargetNamespace() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   345
        return targetNs;
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   346
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   347
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   348
    @Override
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   349
    public String toString() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   350
        return super.toString()+", sourceNs="+
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   351
                sourceNs + (targetNs.equals("")?"":
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   352
                    (" mounted on targetNs="+targetNs));
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
1222
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   355
    // Creates an instance of a subclass 'R' of RoutingProxy<T>
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   356
    // RoutingServerProxy and RoutingConnectionProxy have their own factory
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   357
    // instance.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   358
    static interface RoutingProxyFactory<T extends MBeanServerConnection,
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   359
            R extends RoutingProxy<T>> {
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   360
            R newInstance(T source,
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   361
                    String sourcePath, String targetPath,
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   362
                    boolean forwardsContext);
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   363
            R newInstance(T source,
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   364
                    String sourcePath);
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   365
    }
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   366
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   367
    // Performs a narrowDownToNamespace operation.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   368
    // This method will attempt to merge two RoutingProxies in a single
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   369
    // one if they are of the same class.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   370
    //
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   371
    // This method is never called directly - it should be called only by
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   372
    // subclasses of RoutingProxy.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   373
    //
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   374
    // As for now it is called by:
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   375
    // RoutingServerProxy.cd and RoutingConnectionProxy.cd.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   376
    //
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   377
    static <T extends MBeanServerConnection, R extends RoutingProxy<T>>
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   378
           R cd(Class<R> routingProxyClass,
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   379
              RoutingProxyFactory<T,R> factory,
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   380
              T source, String sourcePath) {
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   381
        if (source == null) throw new IllegalArgumentException("null");
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   382
        if (source.getClass().equals(routingProxyClass)) {
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   383
            // cast is OK here, but findbugs complains unless we use class.cast
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   384
            final R other = routingProxyClass.cast(source);
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   385
            final String target = other.getTargetNamespace();
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   386
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   387
            // Avoid multiple layers of serialization.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   388
            //
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   389
            // We construct a new proxy from the original source instead of
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   390
            // stacking a new proxy on top of the old one.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   391
            // - that is we replace
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   392
            //      cd ( cd ( x, dir1), dir2);
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   393
            // by
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   394
            //      cd (x, dir1//dir2);
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   395
            //
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   396
            // We can do this only when the source class is exactly
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   397
            //    RoutingServerProxy.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   398
            //
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   399
            if (target == null || target.equals("")) {
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   400
                final String path =
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   401
                    JMXNamespaces.concat(other.getSourceNamespace(),
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   402
                    sourcePath);
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   403
                return factory.newInstance(other.source(),path,"",
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   404
                                           other.forwardsContext);
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   405
            }
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   406
            // Note: we could do possibly something here - but it would involve
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   407
            //       removing part of targetDir, and possibly adding
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   408
            //       something to sourcePath.
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   409
            //       Too complex to bother! => simply default to stacking...
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   410
        }
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   411
        return factory.newInstance(source,sourcePath);
78e3d021d528 6745832: jmx namespaces: Some refactoring/commenting would improve code readability.
dfuchs
parents: 1156
diff changeset
   412
    }
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents:
diff changeset
   413
}