jdk/test/javax/management/namespace/SourceNamespaceTest.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 SourceNamespaceTest.java
       
    26  * @summary Test how queryNames works with Namespaces.
       
    27  * @author Daniel Fuchs
       
    28  * @run clean SourceNamespaceTest Wombat WombatMBean
       
    29  * @run build SourceNamespaceTest Wombat WombatMBean
       
    30  * @run main SourceNamespaceTest
       
    31  */
       
    32 
       
    33 
       
    34 import javax.management.JMException;
       
    35 import javax.management.JMX;
       
    36 import javax.management.MBeanServer;
       
    37 import javax.management.MBeanServerFactory;
       
    38 import javax.management.ObjectName;
       
    39 import javax.management.namespace.JMXNamespace;
       
    40 import javax.management.namespace.JMXNamespaces;
       
    41 
       
    42 /**
       
    43  * A simple test to test the source directory parameter...
       
    44  * @author dfuchs
       
    45  */
       
    46 public class SourceNamespaceTest {
       
    47 
       
    48 
       
    49     public static void localTest() throws JMException {
       
    50         final JMXNamespace adir =
       
    51                 new JMXNamespace(MBeanServerFactory.newMBeanServer());
       
    52 
       
    53         // put a wombat in adir...
       
    54         final Wombat w1 = new Wombat();
       
    55         final ObjectName wn1 = new ObjectName("wilderness:type=Wombat,name=gloups");
       
    56         adir.getSourceServer().registerMBean(w1,wn1);
       
    57 
       
    58         // register adir
       
    59         final MBeanServer server = MBeanServerFactory.newMBeanServer();
       
    60         server.registerMBean(adir, JMXNamespaces.getNamespaceObjectName("adir"));
       
    61 
       
    62         if (! (server.isRegistered(JMXNamespaces.insertPath("adir", wn1))))
       
    63             throw new RuntimeException("Test failed: " +
       
    64                     JMXNamespaces.insertPath("adir", wn1) + " not found");
       
    65 
       
    66         System.out.println("Wombat gloups correctly registered...");
       
    67 
       
    68         // put another wombat in adir...
       
    69         final Wombat w2 = new Wombat();
       
    70         final ObjectName wn2 =
       
    71                 new ObjectName("wilderness:type=Wombat,name=pasgloups");
       
    72         server.registerMBean(w2,JMXNamespaces.insertPath("adir", wn2));
       
    73 
       
    74         if (! (server.isRegistered(JMXNamespaces.insertPath("adir", wn2))))
       
    75             throw new RuntimeException("Test failed: " +
       
    76                     JMXNamespaces.insertPath("adir", wn2) + " not found");
       
    77 
       
    78         System.out.println("Wombat pasgloups correctly registered...");
       
    79 
       
    80 
       
    81         // make an alias
       
    82         final JMXNamespace alias = new JMXNamespace(
       
    83                 JMXNamespaces.narrowToNamespace(server,"adir"));
       
    84         server.registerMBean(alias,
       
    85                 JMXNamespaces.getNamespaceObjectName("alias"));
       
    86 
       
    87         if (! (server.isRegistered(JMXNamespaces.insertPath("alias", wn1))))
       
    88             throw new RuntimeException("Test failed: " +
       
    89                     JMXNamespaces.insertPath("alias", wn1) + " not found");
       
    90 
       
    91         System.out.println("Wombat gloups accessible through alias...");
       
    92 
       
    93         if (! (server.isRegistered(JMXNamespaces.insertPath("alias", wn2))))
       
    94             throw new RuntimeException("Test failed: " +
       
    95                     JMXNamespaces.insertPath("alias", wn2) + " not found");
       
    96 
       
    97         System.out.println("Wombat pasgloups accessible through alias...");
       
    98 
       
    99         final WombatMBean wp2 = JMX.newMBeanProxy(server,
       
   100                 JMXNamespaces.insertPath("alias",wn2), WombatMBean.class);
       
   101         System.out.println(JMXNamespaces.insertPath("alias",wn2).toString()
       
   102                 +" says: "+wp2.getCaption());
       
   103 
       
   104         // We're going to make another alias, but register it in a different
       
   105         // MBeanServer. This is to make sure that source server and target
       
   106         // server are not mixed up.
       
   107         //
       
   108         final MBeanServer server2 = MBeanServerFactory.newMBeanServer();
       
   109         final JMXNamespace alias2 = new JMXNamespace(
       
   110                 JMXNamespaces.narrowToNamespace(server,"adir"));
       
   111         server2.registerMBean(alias2,
       
   112                 JMXNamespaces.getNamespaceObjectName("alias2"));
       
   113 
       
   114 
       
   115         if (! (server2.isRegistered(JMXNamespaces.insertPath("alias2", wn1))))
       
   116             throw new RuntimeException("Test failed: " +
       
   117                     JMXNamespaces.insertPath("alias2", wn1) + " not found");
       
   118 
       
   119         System.out.println("Wombat gloups accessible through alias2...");
       
   120 
       
   121         if (! (server2.isRegistered(JMXNamespaces.insertPath("alias2", wn2))))
       
   122             throw new RuntimeException("Test failed: " +
       
   123                     JMXNamespaces.insertPath("alias2", wn2) + " not found");
       
   124 
       
   125         System.out.println("Wombat pasgloups accessible through alias...");
       
   126 
       
   127         final WombatMBean wp22 = JMX.newMBeanProxy(server2,
       
   128                 JMXNamespaces.insertPath("alias2",wn2), WombatMBean.class);
       
   129         System.out.println(JMXNamespaces.insertPath("alias2",wn2).toString()
       
   130                 +" says: "+wp22.getCaption());
       
   131 
       
   132 
       
   133 
       
   134     }
       
   135 
       
   136     public static void main(String[] args) throws Exception {
       
   137         localTest();
       
   138     }
       
   139 
       
   140 }