jdk/test/javax/management/namespace/VirtualNamespaceQueryTest.java
changeset 4159 9e3aae7675f1
parent 4158 0b4d21bc8b5c
parent 4156 acaa49a2768a
child 4160 bda0a85afcb7
equal deleted inserted replaced
4158:0b4d21bc8b5c 4159:9e3aae7675f1
     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  *
       
    26  * @test VirtualNamespaceQueryTest.java
       
    27  * @summary General VirtualNamespaceQueryTest test.
       
    28  * @author Daniel Fuchs
       
    29  * @bug 5072476
       
    30  * @run clean VirtualNamespaceQueryTest Wombat WombatMBean
       
    31  *            NamespaceController NamespaceControllerMBean
       
    32  *            JMXRemoteTargetNamespace
       
    33  * @compile -XDignore.symbol.file=true VirtualNamespaceQueryTest.java
       
    34  *          Wombat.java WombatMBean.java
       
    35  *          NamespaceController.java NamespaceControllerMBean.java
       
    36  *          JMXRemoteTargetNamespace.java
       
    37  * @run main VirtualNamespaceQueryTest
       
    38  */
       
    39 
       
    40 import java.util.Arrays;
       
    41 import java.util.Collections;
       
    42 import java.util.Set;
       
    43 import javax.management.DynamicMBean;
       
    44 import javax.management.InstanceNotFoundException;
       
    45 import javax.management.JMX;
       
    46 import javax.management.MBeanServer;
       
    47 import javax.management.MBeanServerFactory;
       
    48 import javax.management.NotificationEmitter;
       
    49 import javax.management.ObjectInstance;
       
    50 import javax.management.ObjectName;
       
    51 import javax.management.StandardMBean;
       
    52 import javax.management.namespace.JMXNamespace;
       
    53 import javax.management.namespace.JMXNamespaces;
       
    54 import javax.management.namespace.MBeanServerSupport;
       
    55 
       
    56 /**
       
    57  *
       
    58  * @author dfuchs
       
    59  */
       
    60 public class VirtualNamespaceQueryTest {
       
    61     public static class WombatRepository extends MBeanServerSupport {
       
    62         final Wombat wombat;
       
    63         final StandardMBean mbean;
       
    64         final ObjectName wombatName;
       
    65 
       
    66         public WombatRepository(ObjectName wombatName) {
       
    67             try {
       
    68                 wombat = new Wombat();
       
    69                 mbean  = wombat;
       
    70                 this.wombatName = wombatName;
       
    71                 wombat.preRegister(null,wombatName);
       
    72             } catch (Exception x) {
       
    73                 throw new IllegalArgumentException(x);
       
    74             }
       
    75         }
       
    76 
       
    77         @Override
       
    78         public DynamicMBean getDynamicMBeanFor(ObjectName name)
       
    79             throws InstanceNotFoundException {
       
    80             if (wombatName.equals(name)) return mbean;
       
    81             else throw new InstanceNotFoundException(String.valueOf(name));
       
    82         }
       
    83 
       
    84         @Override
       
    85         protected Set<ObjectName> getNames() {
       
    86             final Set<ObjectName> res = Collections.singleton(wombatName);
       
    87             return res;
       
    88         }
       
    89 
       
    90         @Override
       
    91         public NotificationEmitter getNotificationEmitterFor(
       
    92                 ObjectName name) throws InstanceNotFoundException {
       
    93             DynamicMBean mb = getDynamicMBeanFor(name);
       
    94             if (mb instanceof NotificationEmitter)
       
    95                 return (NotificationEmitter)mb;
       
    96             return null;
       
    97         }
       
    98     }
       
    99     public static class WombatNamespace extends JMXNamespace {
       
   100         public WombatNamespace(ObjectName wombatName) {
       
   101             super(new WombatRepository(wombatName));
       
   102         }
       
   103     }
       
   104 
       
   105     public static void simpleTest() throws Exception {
       
   106         final MBeanServer  server = MBeanServerFactory.newMBeanServer();
       
   107         final ObjectName   wombatName = new ObjectName("burrow:type=Wombat");
       
   108         final JMXNamespace ns = new WombatNamespace(wombatName);
       
   109         server.registerMBean(ns, JMXNamespaces.getNamespaceObjectName("wombats"));
       
   110         final Set<ObjectName> dirs =
       
   111                 server.queryNames(new ObjectName("wombats//*//:type=JMXNamespace"),
       
   112                 wombatName);
       
   113         System.out.println("all dirs: "+dirs);
       
   114         if (dirs.size()>0)
       
   115             throw new RuntimeException("Unexpected ObjectNames returned: "+dirs);
       
   116 
       
   117         final ObjectInstance inst = NamespaceController.createInstance(server);
       
   118         final NamespaceControllerMBean controller =
       
   119                 JMX.newMBeanProxy(server, inst.getObjectName(),
       
   120                 NamespaceControllerMBean.class);
       
   121         final String[] dirNames = controller.findNamespaces(null,null,2);
       
   122         System.err.println(Arrays.toString(dirNames));
       
   123     }
       
   124 
       
   125     public static void main(String[] args) throws Exception {
       
   126         simpleTest();
       
   127     }
       
   128 }