jdk/test/javax/management/namespace/MXBeanRefTest.java
changeset 1156 bbc2d15aaf7a
equal deleted inserted replaced
1155:a9a142fcf1b5 1156:bbc2d15aaf7a
       
     1 /*
       
     2  * Copyright 2007-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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 /*
       
    24  * @test MXBeanRefTest.java
       
    25  * @bug 5072476
       
    26  * @summary Test that MXBean proxy references work correctly in the presence
       
    27  * of namespaces.
       
    28  * @author Eamonn Mcmanus
       
    29  */
       
    30 
       
    31 /**
       
    32  * The idea is that we will create a hierarchy like this:
       
    33  * a//
       
    34  *   X
       
    35  *   b//
       
    36  *     Y
       
    37  *     Z
       
    38  * and we will use MXBean references so we have links like this:
       
    39  * a//
       
    40  *   X----+
       
    41  *   b//  |
       
    42  *       /
       
    43  *     Y
       
    44  *      \
       
    45  *      /
       
    46  *     Z
       
    47  * In other words, X.getY() will return a proxy for Y, which the MXBean
       
    48  * framework will map to b//Y.  A proxy for a//X should then map this
       
    49  * into a proxy for a//b//Y.  That's easy.  But then if we call getZ()
       
    50  * on this proxy, the MXBean framework will return just Z, and the proxy
       
    51  * must map that into a proxy for a//b//Z.
       
    52  */
       
    53 
       
    54 import java.lang.management.ManagementFactory;
       
    55 import java.lang.reflect.InvocationHandler;
       
    56 import java.lang.reflect.Proxy;
       
    57 import java.lang.reflect.UndeclaredThrowableException;
       
    58 import javax.management.JMX;
       
    59 import javax.management.MBeanServer;
       
    60 import javax.management.MBeanServerConnection;
       
    61 import javax.management.MBeanServerFactory;
       
    62 import javax.management.MBeanServerInvocationHandler;
       
    63 import javax.management.ObjectName;
       
    64 import javax.management.namespace.JMXNamespace;
       
    65 import javax.management.namespace.JMXNamespaces;
       
    66 import javax.management.openmbean.OpenDataException;
       
    67 
       
    68 public class MXBeanRefTest {
       
    69 
       
    70     public static interface ZMXBean {
       
    71         public void success();
       
    72     }
       
    73     public static class ZImpl implements ZMXBean {
       
    74         public void success() {}
       
    75     }
       
    76 
       
    77     public static interface YMXBean {
       
    78         public ZMXBean getZ();
       
    79         public void setZ(ZMXBean z);
       
    80     }
       
    81     public static class YImpl implements YMXBean {
       
    82         private ZMXBean z;
       
    83 
       
    84         public YImpl(ZMXBean z) {
       
    85             this.z = z;
       
    86         }
       
    87 
       
    88         public ZMXBean getZ() {
       
    89             return z;
       
    90         }
       
    91 
       
    92         public void setZ(ZMXBean z) {
       
    93             this.z = z;
       
    94         }
       
    95     }
       
    96 
       
    97     public static interface XMXBean {
       
    98         public YMXBean getY();
       
    99     }
       
   100     public static class XImpl implements XMXBean {
       
   101         private final YMXBean yProxy;
       
   102 
       
   103         public XImpl(YMXBean yProxy) {
       
   104             this.yProxy = yProxy;
       
   105         }
       
   106 
       
   107         public YMXBean getY() {
       
   108             return yProxy;
       
   109         }
       
   110     }
       
   111 
       
   112     public static void main(String[] args) throws Exception {
       
   113         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
       
   114 
       
   115         // Set up namespace hierarchy a//b//
       
   116         MBeanServer ambs = MBeanServerFactory.newMBeanServer();
       
   117         MBeanServer bmbs = MBeanServerFactory.newMBeanServer();
       
   118         JMXNamespace bHandler = new JMXNamespace(bmbs);
       
   119         ObjectName bHandlerName = JMXNamespaces.getNamespaceObjectName("b");
       
   120         System.out.println(bHandlerName);
       
   121         ambs.registerMBean(bHandler, bHandlerName);
       
   122         JMXNamespace aHandler = new JMXNamespace(ambs);
       
   123         ObjectName aHandlerName = JMXNamespaces.getNamespaceObjectName("a");
       
   124         mbs.registerMBean(aHandler, aHandlerName);
       
   125 
       
   126         ZMXBean z = new ZImpl();
       
   127         ObjectName zName = new ObjectName("foo:type=Z");
       
   128         bmbs.registerMBean(z, zName);
       
   129 
       
   130         YMXBean y = new YImpl(z);
       
   131         ObjectName yName = new ObjectName("foo:type=Y");
       
   132         bmbs.registerMBean(y, yName);
       
   133 
       
   134         ObjectName yNameInA = new ObjectName("b//" + yName);
       
   135         System.out.println("MBeanInfo for Y as seen from a//:");
       
   136         System.out.println(ambs.getMBeanInfo(yNameInA));
       
   137         YMXBean yProxyInA = JMX.newMXBeanProxy(ambs, yNameInA, YMXBean.class);
       
   138         XMXBean x = new XImpl(yProxyInA);
       
   139         ObjectName xName = new ObjectName("foo:type=X");
       
   140         ambs.registerMBean(x, xName);
       
   141 
       
   142         ObjectName xNameFromTop = new ObjectName("a//" + xName);
       
   143         XMXBean xProxy = JMX.newMXBeanProxy(mbs, xNameFromTop, XMXBean.class);
       
   144         System.out.println("Name of X Proxy: " + proxyName(xProxy));
       
   145         YMXBean yProxy = xProxy.getY();
       
   146         System.out.println("Name of Y Proxy: " + proxyName(yProxy));
       
   147         ZMXBean zProxy = yProxy.getZ();
       
   148         System.out.println("Name of Z Proxy: " + proxyName(zProxy));
       
   149 
       
   150         System.out.println("Operation through Z proxy...");
       
   151         zProxy.success();
       
   152 
       
   153         System.out.println("Changing Y's ref to Z...");
       
   154         yProxy.setZ(zProxy);
       
   155         zProxy = yProxy.getZ();
       
   156         System.out.println("Name of Z Proxy now: " + proxyName(zProxy));
       
   157         System.out.println("Operation through Z proxy again...");
       
   158         zProxy.success();
       
   159 
       
   160         System.out.println("Changing Y's ref to a bogus one...");
       
   161         ZMXBean zProxyBad = JMX.newMXBeanProxy(mbs, zName, ZMXBean.class);
       
   162         try {
       
   163             yProxy.setZ(zProxyBad);
       
   164         } catch (UndeclaredThrowableException e) {
       
   165             Throwable cause = e.getCause();
       
   166             if (cause instanceof OpenDataException) {
       
   167                 System.out.println("...correctly got UndeclaredThrowableException");
       
   168                 System.out.println("...wrapping: " + cause);
       
   169             } else
       
   170                 throw new Exception("FAILED: wrong exception: " + cause);
       
   171         }
       
   172 
       
   173         System.out.println("Test passed");
       
   174     }
       
   175 
       
   176     private static ObjectName proxyName(Object proxy) {
       
   177         InvocationHandler ih = Proxy.getInvocationHandler(proxy);
       
   178         MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler) ih;
       
   179         return mbsih.getObjectName();
       
   180     }
       
   181 }