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