jdk/test/javax/management/namespace/ExportNamespaceTest.java
changeset 1156 bbc2d15aaf7a
child 1227 4546977d0d66
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.
       
     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  *
       
    25  * @test ExportNamespaceTest.java
       
    26  * @summary Test that you can export a single namespace through a
       
    27  *          JMXConnectorServer.
       
    28  * @author Daniel Fuchs
       
    29  * @run clean ExportNamespaceTest Wombat WombatMBean
       
    30  * @run build ExportNamespaceTest Wombat WombatMBean
       
    31  * @run main ExportNamespaceTest
       
    32  */
       
    33 
       
    34 import javax.management.JMX;
       
    35 import javax.management.ObjectName;
       
    36 import javax.management.namespace.JMXNamespace;
       
    37 import javax.management.namespace.JMXNamespaces;
       
    38 import javax.management.MBeanServer;
       
    39 import javax.management.MBeanServerConnection;
       
    40 import javax.management.MBeanServerFactory;
       
    41 import javax.management.remote.JMXConnector;
       
    42 import javax.management.remote.JMXConnectorFactory;
       
    43 import javax.management.remote.JMXConnectorServer;
       
    44 import javax.management.remote.JMXConnectorServerFactory;
       
    45 import javax.management.remote.JMXServiceURL;
       
    46 
       
    47 
       
    48 /**
       
    49  * Test simple creation/registration of namespace.
       
    50  *
       
    51  */
       
    52 public class ExportNamespaceTest {
       
    53 
       
    54     public static void testExport() throws Exception {
       
    55         final JMXNamespace my =
       
    56                 new JMXNamespace(MBeanServerFactory.newMBeanServer());
       
    57         final MBeanServer s = MBeanServerFactory.newMBeanServer();
       
    58         final ObjectName myname = JMXNamespaces.getNamespaceObjectName("my");
       
    59         final ObjectName wname = ObjectName.getInstance("backyard:type=Wombat");
       
    60         my.getSourceServer().registerMBean(new Wombat(),wname);
       
    61         s.registerMBean(my,myname);
       
    62 
       
    63         if (!s.queryNames(new ObjectName("my//b*:*"),null).contains(
       
    64                 JMXNamespaces.insertPath("my", wname))) {
       
    65             throw new RuntimeException("1: Wombat not found: "+wname);
       
    66         }
       
    67 
       
    68         final MBeanServer cd = JMXNamespaces.narrowToNamespace(s, "my");
       
    69         if (!cd.queryNames(new ObjectName("b*:*"),null).contains(wname)) {
       
    70             throw new RuntimeException("2: Wombat not found: "+wname);
       
    71         }
       
    72 
       
    73         final JMXServiceURL url = new JMXServiceURL("rmi",null,0);
       
    74         final JMXConnectorServer server =
       
    75                 JMXConnectorServerFactory.newJMXConnectorServer(url, null, cd);
       
    76         server.start();
       
    77 
       
    78         final JMXConnector jc = JMXConnectorFactory.
       
    79                 connect(server.getAddress(),null);
       
    80         final MBeanServerConnection mbsc = jc.getMBeanServerConnection();
       
    81 
       
    82         if (!mbsc.queryNames(new ObjectName("b*:*"),null).contains(wname)) {
       
    83             throw new RuntimeException("3: Wombat not found: "+wname);
       
    84         }
       
    85         System.out.println("Found a Wombat in my backyard.");
       
    86 
       
    87         final String deepThoughts = "I want to leave this backyard!";
       
    88         final WombatMBean w = JMX.newMBeanProxy(mbsc, wname, WombatMBean.class);
       
    89         w.setCaption(deepThoughts);
       
    90         if (!deepThoughts.equals(w.getCaption()))
       
    91                 throw new RuntimeException("4: Wombat is not thinking right: "+
       
    92                         w.getCaption());
       
    93 
       
    94     }
       
    95 
       
    96     public static void main(String... args) throws Exception {
       
    97         testExport();
       
    98     }
       
    99 }