jdk/src/share/classes/com/sun/jmx/namespace/RoutingConnectionProxy.java
changeset 1156 bbc2d15aaf7a
child 1222 78e3d021d528
equal deleted inserted replaced
1155:a9a142fcf1b5 1156:bbc2d15aaf7a
       
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package com.sun.jmx.namespace;
       
    27 
       
    28 
       
    29 import com.sun.jmx.defaults.JmxProperties;
       
    30 import java.util.logging.Level;
       
    31 import java.util.logging.Logger;
       
    32 
       
    33 import javax.management.MBeanServerConnection;
       
    34 import javax.management.namespace.JMXNamespaces;
       
    35 
       
    36 
       
    37 /**
       
    38  * A RoutingConnectionProxy is an MBeanServerConnection proxy that proxies a
       
    39  * source name space in a source MBeanServerConnection.
       
    40  * It wraps a source MBeanServerConnection, and rewrites routing
       
    41  * ObjectNames. It is used to implement
       
    42  * {@code JMXNamespaces.narrowToNamespace(MBeanServerConnection)}.
       
    43  * <p><b>
       
    44  * This API is a Sun internal API and is subject to changes without notice.
       
    45  * </b></p>
       
    46  * @since 1.7
       
    47  */
       
    48 public class RoutingConnectionProxy
       
    49         extends RoutingProxy<MBeanServerConnection> {
       
    50 
       
    51     /**
       
    52      * A logger for this class.
       
    53      **/
       
    54     private static final Logger LOG = JmxProperties.NAMESPACE_LOGGER;
       
    55 
       
    56 
       
    57     /**
       
    58      * Creates a new instance of RoutingConnectionProxy
       
    59      */
       
    60     public RoutingConnectionProxy(MBeanServerConnection source,
       
    61                                String sourceDir) {
       
    62         this(source,sourceDir,"",false);
       
    63     }
       
    64 
       
    65     /**
       
    66      * Creates a new instance of RoutingConnectionProxy
       
    67      */
       
    68     public RoutingConnectionProxy(MBeanServerConnection source,
       
    69                                String sourceDir,
       
    70                                String targetDir,
       
    71                                boolean forwardsContext) {
       
    72         super(source,sourceDir,targetDir,forwardsContext);
       
    73 
       
    74         if (LOG.isLoggable(Level.FINER))
       
    75             LOG.finer("RoutingConnectionProxy for " + getSourceNamespace() +
       
    76                       " created");
       
    77     }
       
    78 
       
    79     @Override
       
    80     public String toString() {
       
    81         final String targetNs = getTargetNamespace();
       
    82         final String sourceNs = getSourceNamespace();
       
    83         String wrapped = String.valueOf(source());
       
    84         if ("".equals(targetNs)) {
       
    85             if (forwardsContext)
       
    86                 wrapped = "ClientContext.withDynamicContext("+wrapped+")";
       
    87             return "JMXNamespaces.narrowToNamespace("+
       
    88                     wrapped+", \""+
       
    89                     sourceNs+"\")";
       
    90         }
       
    91         return this.getClass().getSimpleName()+"("+wrapped+", \""+
       
    92                sourceNs+"\", \""+
       
    93                targetNs+"\", "+forwardsContext+")";
       
    94     }
       
    95 
       
    96     public static MBeanServerConnection cd(MBeanServerConnection source,
       
    97             String sourcePath) {
       
    98         if (source == null) throw new IllegalArgumentException("null");
       
    99         if (source.getClass().equals(RoutingConnectionProxy.class)) {
       
   100             // cast is OK here, but findbugs complains unless we use class.cast
       
   101             final RoutingConnectionProxy other =
       
   102                     RoutingConnectionProxy.class.cast(source);
       
   103             final String target = other.getTargetNamespace();
       
   104 
       
   105             // Avoid multiple layers of serialization.
       
   106             //
       
   107             // We construct a new proxy from the original source instead of
       
   108             // stacking a new proxy on top of the old one.
       
   109             // - that is we replace
       
   110             //      cd ( cd ( x, dir1), dir2);
       
   111             // by
       
   112             //      cd (x, dir1//dir2);
       
   113             //
       
   114             // We can do this only when the source class is exactly
       
   115             //    NamespaceConnectionProxy.
       
   116             //
       
   117             if (target == null || target.equals("")) {
       
   118                 final String path =
       
   119                     JMXNamespaces.concat(other.getSourceNamespace(),
       
   120                     sourcePath);
       
   121                 return new RoutingConnectionProxy(other.source(),path,"",
       
   122                         other.forwardsContext);
       
   123             }
       
   124             // Note: we could do possibly something here - but it would involve
       
   125             //       removing part of targetDir, and possibly adding
       
   126             //       something to sourcePath.
       
   127             //       Too complex to bother! => simply default to stacking...
       
   128         }
       
   129         return new RoutingConnectionProxy(source,sourcePath);
       
   130     }
       
   131 
       
   132 }