jdk/test/javax/management/namespace/RoutingServerProxyTest.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 RoutingServerProxyTest.java 1.6
       
    26  * @summary General RoutingServerProxyTest test.
       
    27  * @author Daniel Fuchs
       
    28  * @run clean RoutingServerProxyTest Wombat WombatMBean
       
    29  * @compile -XDignore.symbol.file=true RoutingServerProxyTest.java
       
    30  * @run build RoutingServerProxyTest Wombat WombatMBean
       
    31  * @run main RoutingServerProxyTest
       
    32  */
       
    33 
       
    34 import com.sun.jmx.namespace.RoutingServerProxy;
       
    35 import java.io.IOException;
       
    36 import java.util.Collections;
       
    37 import java.util.Map;
       
    38 import java.util.Set;
       
    39 import java.util.concurrent.ConcurrentHashMap;
       
    40 import java.util.logging.Logger;
       
    41 
       
    42 import javax.management.DynamicMBean;
       
    43 import javax.management.InstanceNotFoundException;
       
    44 import javax.management.JMException;
       
    45 import javax.management.JMX;
       
    46 import javax.management.MBeanInfo;
       
    47 import javax.management.MBeanRegistration;
       
    48 import javax.management.MBeanServer;
       
    49 import javax.management.MBeanServerFactory;
       
    50 import javax.management.MalformedObjectNameException;
       
    51 import javax.management.NotCompliantMBeanException;
       
    52 import javax.management.NotificationEmitter;
       
    53 import javax.management.ObjectInstance;
       
    54 import javax.management.ObjectName;
       
    55 import javax.management.StandardEmitterMBean;
       
    56 import javax.management.namespace.JMXNamespace;
       
    57 import javax.management.namespace.JMXNamespaces;
       
    58 import javax.management.namespace.MBeanServerSupport;
       
    59 
       
    60 /**
       
    61  * Class RoutingServerProxyTest
       
    62  *
       
    63  * @author Sun Microsystems, Inc.
       
    64  */
       
    65 public class RoutingServerProxyTest {
       
    66 
       
    67     /**
       
    68      * A logger for this class.
       
    69      **/
       
    70     private static final Logger LOG =
       
    71             Logger.getLogger(RoutingServerProxyTest.class.getName());
       
    72 
       
    73     /**
       
    74      * Creates a new instance of RoutingServerProxyTest
       
    75      */
       
    76     public RoutingServerProxyTest() {
       
    77     }
       
    78 
       
    79     public static class DynamicWombat extends StandardEmitterMBean {
       
    80         DynamicWombat(Wombat w) throws NotCompliantMBeanException {
       
    81             super(w,WombatMBean.class,w);
       
    82         }
       
    83 
       
    84         @Override
       
    85         public ObjectName preRegister(MBeanServer server, ObjectName name)
       
    86             throws Exception {
       
    87             final ObjectName myname = ((Wombat)getImplementation()).
       
    88                     preRegister(server,name);
       
    89             return super.preRegister(server,myname);
       
    90         }
       
    91 
       
    92         @Override
       
    93         public void postRegister(Boolean registrationDone) {
       
    94             try {
       
    95                 ((Wombat)getImplementation()).
       
    96                     postRegister(registrationDone);
       
    97             } finally {
       
    98                 super.postRegister(registrationDone);
       
    99             }
       
   100         }
       
   101 
       
   102         @Override
       
   103         public void preDeregister() throws Exception {
       
   104                 ((Wombat)getImplementation()).
       
   105                     preDeregister();
       
   106                 super.preDeregister();
       
   107 
       
   108         }
       
   109 
       
   110         @Override
       
   111         public void postDeregister() {
       
   112             try {
       
   113                 ((Wombat)getImplementation()).
       
   114                     postDeregister();
       
   115             } finally {
       
   116                 super.postDeregister();
       
   117             }
       
   118         }
       
   119     }
       
   120 
       
   121     public static class VirtualWombatHandler
       
   122             extends JMXNamespace {
       
   123 
       
   124         public static class VirtualWombatRepository
       
   125                 extends MBeanServerSupport {
       
   126 
       
   127             final Map<ObjectName, DynamicMBean> bush;
       
   128 
       
   129             VirtualWombatRepository(Map<ObjectName, DynamicMBean> bush) {
       
   130                 this.bush = bush;
       
   131             }
       
   132 
       
   133             @Override
       
   134             protected Set<ObjectName> getNames() {
       
   135                 return bush.keySet();
       
   136             }
       
   137 
       
   138             @Override
       
   139             public DynamicMBean getDynamicMBeanFor(ObjectName name)
       
   140                     throws InstanceNotFoundException {
       
   141                 final DynamicMBean mb = bush.get(name);
       
   142                 if (mb == null) {
       
   143                     throw new InstanceNotFoundException(String.valueOf(name));
       
   144                 }
       
   145                 return mb;
       
   146             }
       
   147 
       
   148             @Override
       
   149             public NotificationEmitter getNotificationEmitterFor(
       
   150                     ObjectName name) throws InstanceNotFoundException {
       
   151                 DynamicMBean mbean = getDynamicMBeanFor(name);
       
   152                 if (mbean instanceof NotificationEmitter) {
       
   153                     return (NotificationEmitter) mbean;
       
   154                 }
       
   155                 return null;
       
   156             }
       
   157         }
       
   158         VirtualWombatRepository bush;
       
   159 
       
   160         VirtualWombatHandler(Map<ObjectName, DynamicMBean> bush) {
       
   161             this(new VirtualWombatRepository(Collections.synchronizedMap(bush)));
       
   162         }
       
   163 
       
   164         private VirtualWombatHandler(VirtualWombatRepository repository) {
       
   165             super(repository);
       
   166             bush = repository;
       
   167         }
       
   168 
       
   169         @Override
       
   170         public ObjectName preRegister(MBeanServer server, ObjectName name)
       
   171                 throws Exception {
       
   172             final ObjectName myname = super.preRegister(server, name);
       
   173             return myname;
       
   174         }
       
   175 
       
   176         @Override
       
   177         public void postRegister(Boolean registrationDone) {
       
   178             if (!registrationDone.booleanValue()) {
       
   179                 return;
       
   180             }
       
   181             final MBeanServer me = JMXNamespaces.narrowToNamespace(getMBeanServer(),
       
   182                     getObjectName().getDomain());
       
   183             for (Map.Entry<ObjectName, DynamicMBean> e : bush.bush.entrySet()) {
       
   184                 final DynamicMBean obj = e.getValue();
       
   185                 try {
       
   186                     if (obj instanceof MBeanRegistration) {
       
   187                         ((MBeanRegistration) obj).preRegister(me, e.getKey());
       
   188                     }
       
   189                 } catch (Exception x) {
       
   190                     System.err.println("preRegister failed for " +
       
   191                             e.getKey() + ": " + x);
       
   192                     bush.bush.remove(e.getKey());
       
   193                 }
       
   194             }
       
   195             for (Map.Entry<ObjectName, DynamicMBean> e : bush.bush.entrySet()) {
       
   196                 final Object obj = e.getValue();
       
   197                 if (obj instanceof MBeanRegistration) {
       
   198                     ((MBeanRegistration) obj).postRegister(registrationDone);
       
   199                 }
       
   200             }
       
   201         }
       
   202 
       
   203         @Override
       
   204         public void preDeregister() throws Exception {
       
   205             for (Map.Entry<ObjectName, DynamicMBean> e : bush.bush.entrySet()) {
       
   206                 final Object obj = e.getValue();
       
   207                 if (obj instanceof MBeanRegistration) {
       
   208                     ((MBeanRegistration) obj).preDeregister();
       
   209                 }
       
   210             }
       
   211         }
       
   212 
       
   213         @Override
       
   214         public void postDeregister() {
       
   215             for (Map.Entry<ObjectName, DynamicMBean> e : bush.bush.entrySet()) {
       
   216                 final Object obj = e.getValue();
       
   217                 if (obj instanceof MBeanRegistration) {
       
   218                     ((MBeanRegistration) obj).postDeregister();
       
   219                 }
       
   220             }
       
   221         }
       
   222     }
       
   223 
       
   224     public static ObjectName getWombatName(String name)
       
   225         throws MalformedObjectNameException {
       
   226         return ObjectName.getInstance("australian.bush:type=Wombat,name="+name);
       
   227     }
       
   228 
       
   229     public static ObjectName addDir(String dir, ObjectName name)
       
   230         throws MalformedObjectNameException {
       
   231         return name.withDomain(
       
   232                 dir+JMXNamespaces.NAMESPACE_SEPARATOR+ name.getDomain());
       
   233     }
       
   234 
       
   235     public static void simpleTest()
       
   236         throws JMException, IOException {
       
   237         final MBeanServer master = MBeanServerFactory.createMBeanServer();
       
   238         final MBeanServer agent1 = MBeanServerFactory.createMBeanServer();
       
   239         final Wombat w1 = new Wombat();
       
   240         final Wombat w2 = new Wombat();
       
   241         final Wombat w3 = new Wombat();
       
   242         final Map<ObjectName,DynamicMBean> wombats =
       
   243                 new ConcurrentHashMap<ObjectName,DynamicMBean>();
       
   244         wombats.put(getWombatName("LittleWombat"),
       
   245                 new DynamicWombat(w2));
       
   246         wombats.put(getWombatName("BigWombat"),
       
   247                 new DynamicWombat(w3));
       
   248         final Wombat w4 = new Wombat();
       
   249         final Wombat w5 = new Wombat();
       
   250 
       
   251         final JMXNamespace agent2 =
       
   252                 new VirtualWombatHandler(wombats);
       
   253         agent1.registerMBean(w4,getWombatName("LittleWombat"));
       
   254         master.registerMBean(w1,getWombatName("LittleWombat"));
       
   255         master.registerMBean(new JMXNamespace(agent1),
       
   256                 JMXNamespaces.getNamespaceObjectName("south.east"));
       
   257         master.registerMBean(agent2,
       
   258                 JMXNamespaces.getNamespaceObjectName("north"));
       
   259         master.registerMBean(w5,addDir("south.east",
       
   260                 getWombatName("GrandWombat")));
       
   261 
       
   262         MBeanServer se = null;
       
   263 
       
   264         try {
       
   265             se = JMXNamespaces.narrowToNamespace(master,"south.easht");
       
   266         } catch (Exception x) {
       
   267             System.out.println("Caught expected exception: "+x);
       
   268         }
       
   269         if (se != null)
       
   270             throw new RuntimeException("Expected exception for "+
       
   271                     "cd(south.easht)");
       
   272         se = JMXNamespaces.narrowToNamespace(master,"south.east");
       
   273 
       
   274         MBeanServer nth = JMXNamespaces.narrowToNamespace(master,"north");
       
   275 
       
   276         final ObjectName ln = getWombatName("LittleWombat");
       
   277         MBeanInfo mb1 = master.getMBeanInfo(ln);
       
   278         MBeanInfo mb2 = se.getMBeanInfo(ln);
       
   279         MBeanInfo mb3 = nth.getMBeanInfo(ln);
       
   280 
       
   281         final WombatMBean grand = JMX.newMBeanProxy(se,
       
   282                 getWombatName("GrandWombat"),WombatMBean.class);
       
   283         final WombatMBean big = JMX.newMBeanProxy(nth,
       
   284                 getWombatName("BigWombat"),WombatMBean.class);
       
   285         grand.getCaption();
       
   286         big.getCaption();
       
   287         grand.setCaption("I am GrandWombat");
       
   288         big.setCaption("I am BigWombat");
       
   289 
       
   290         final WombatMBean grand2 =
       
   291                 JMX.newMBeanProxy(master,addDir("south.east",
       
   292                 getWombatName("GrandWombat")),WombatMBean.class);
       
   293         final WombatMBean big2 =
       
   294                 JMX.newMBeanProxy(master,addDir("north",
       
   295                 getWombatName("BigWombat")),WombatMBean.class);
       
   296         if (!"I am GrandWombat".equals(grand2.getCaption()))
       
   297             throw new RuntimeException("bad caption for GrandWombat"+
       
   298                     grand2.getCaption());
       
   299         if (!"I am BigWombat".equals(big2.getCaption()))
       
   300             throw new RuntimeException("bad caption for BigWombat"+
       
   301                     big2.getCaption());
       
   302 
       
   303 
       
   304         final Set<ObjectInstance> northWombats =
       
   305                 nth.queryMBeans(ObjectName.WILDCARD,null);
       
   306         final Set<ObjectInstance> seWombats =
       
   307                 se.queryMBeans(ObjectName.WILDCARD,null);
       
   308         if (!northWombats.equals(
       
   309                 agent2.getSourceServer().queryMBeans(ObjectName.WILDCARD,null))) {
       
   310             throw new RuntimeException("Bad Wombat census in northern territory: got "
       
   311                     +northWombats+", expected "+
       
   312                     agent2.getSourceServer().
       
   313                     queryMBeans(ObjectName.WILDCARD,null));
       
   314         }
       
   315         if (!seWombats.equals(
       
   316                 agent1.queryMBeans(ObjectName.WILDCARD,null))) {
       
   317             throw new RuntimeException("Bad Wombat census in south east: got "
       
   318                     +seWombats+", expected "+
       
   319                     agent1.
       
   320                     queryMBeans(ObjectName.WILDCARD,null));
       
   321         }
       
   322 
       
   323         final MBeanServer supermaster = MBeanServerFactory.createMBeanServer();
       
   324         supermaster.registerMBean(new JMXNamespace(master),
       
   325             JMXNamespaces.getNamespaceObjectName("australia"));
       
   326         final MBeanServer proxymaster =
       
   327                 JMXNamespaces.narrowToNamespace(supermaster,"australia");
       
   328         final MBeanServer sem =
       
   329                 JMXNamespaces.narrowToNamespace(proxymaster,"south.east");
       
   330         final MBeanServer nthm =
       
   331                 JMXNamespaces.narrowToNamespace(proxymaster,"north");
       
   332         final Set<ObjectInstance> northWombats2 =
       
   333                 nthm.queryMBeans(ObjectName.WILDCARD,null);
       
   334         final Set<ObjectInstance> seWombats2 =
       
   335                 sem.queryMBeans(ObjectName.WILDCARD,null);
       
   336         if (!northWombats2.equals(
       
   337                 agent2.getSourceServer().queryMBeans(ObjectName.WILDCARD,null))) {
       
   338             throw new RuntimeException("Bad Wombat census in " +
       
   339                     "Australia // North");
       
   340         }
       
   341         if (!seWombats2.equals(
       
   342                 agent1.queryMBeans(ObjectName.WILDCARD,null))) {
       
   343             throw new RuntimeException("Bad Wombat census in " +
       
   344                     "Australia // South East");
       
   345         }
       
   346         final WombatMBean grand3 =
       
   347                 JMX.newMBeanProxy(supermaster,
       
   348                 addDir("australia//south.east",
       
   349                 getWombatName("GrandWombat")),WombatMBean.class);
       
   350         final WombatMBean big3 =
       
   351                 JMX.newMBeanProxy(supermaster,addDir("australia//north",
       
   352                 getWombatName("BigWombat")),WombatMBean.class);
       
   353         if (!"I am GrandWombat".equals(grand3.getCaption()))
       
   354             throw new RuntimeException("bad caption for " +
       
   355                     "australia//south.east//GrandWombat"+
       
   356                     grand3.getCaption());
       
   357         if (!"I am BigWombat".equals(big3.getCaption()))
       
   358             throw new RuntimeException("bad caption for " +
       
   359                     "australia//north//BigWombat"+
       
   360                     big3.getCaption());
       
   361         final WombatMBean grand4 =
       
   362                 JMX.newMBeanProxy(sem,
       
   363                 getWombatName("GrandWombat"),WombatMBean.class);
       
   364         final WombatMBean big4 =
       
   365                 JMX.newMBeanProxy(nthm,
       
   366                 getWombatName("BigWombat"),WombatMBean.class);
       
   367         if (!"I am GrandWombat".equals(grand4.getCaption()))
       
   368             throw new RuntimeException("bad caption for " +
       
   369                     "[australia//south.east//] GrandWombat"+
       
   370                     grand4.getCaption());
       
   371         if (!"I am BigWombat".equals(big4.getCaption()))
       
   372             throw new RuntimeException("bad caption for " +
       
   373                     "[australia//north//] BigWombat"+
       
   374                     big4.getCaption());
       
   375 
       
   376         if (!(nthm instanceof RoutingServerProxy))
       
   377             throw new AssertionError("expected RoutingServerProxy for nthm");
       
   378         if (!(sem instanceof RoutingServerProxy))
       
   379             throw new AssertionError("expected RoutingServerProxy for sem");
       
   380 
       
   381         if (!"australia//north".equals((
       
   382                 (RoutingServerProxy)nthm).getSourceNamespace()))
       
   383             throw new RuntimeException("north territory should be in australia");
       
   384         if (!"australia//south.east".equals((
       
   385                 (RoutingServerProxy)sem).getSourceNamespace()))
       
   386             throw new RuntimeException("south east territory should be in australia");
       
   387 
       
   388     }
       
   389 
       
   390     public static  void main(String[] args) {
       
   391         try {
       
   392             simpleTest();
       
   393         } catch (Exception x) {
       
   394             System.err.println("SimpleTest failed: "+x);
       
   395             throw new RuntimeException(x);
       
   396         }
       
   397     }
       
   398 
       
   399 }