jdk/test/javax/management/namespace/JMXRemoteNamespaceTest.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  * @test JMXRemoteNamespaceTest.java
       
    26  * @summary Basic tests on a JMXRemoteNamespace.
       
    27  * @author Daniel Fuchs
       
    28  * @bug 5072476
       
    29  * @run clean JMXRemoteNamespaceTest Wombat WombatMBean
       
    30  * @run build JMXRemoteNamespaceTest Wombat WombatMBean
       
    31  * @run main JMXRemoteNamespaceTest
       
    32  */
       
    33 
       
    34 import javax.management.JMX;
       
    35 import javax.management.Notification;
       
    36 import javax.management.ObjectName;
       
    37 import javax.management.namespace.JMXNamespaces;
       
    38 import javax.management.MBeanServer;
       
    39 import javax.management.MBeanServerFactory;
       
    40 import javax.management.NotificationListener;
       
    41 import javax.management.namespace.JMXRemoteNamespace;
       
    42 import javax.management.remote.JMXConnectorServer;
       
    43 import javax.management.remote.JMXConnectorServerFactory;
       
    44 import javax.management.remote.JMXServiceURL;
       
    45 import java.util.List;
       
    46 import java.util.ArrayList;
       
    47 import java.util.Collections;
       
    48 import java.io.IOException;
       
    49 import javax.management.AttributeChangeNotification;
       
    50 
       
    51 /**
       
    52  * Test simple creation/registration of namespace.
       
    53  *
       
    54  */
       
    55 public class JMXRemoteNamespaceTest {
       
    56 
       
    57     static class MyConnect implements NotificationListener {
       
    58         private final JMXRemoteNamespace my;
       
    59         private final List<Notification> list;
       
    60         private volatile int connectCount=0;
       
    61         private int closeCount=0;
       
    62         private final ObjectName myname;
       
    63         public MyConnect(JMXRemoteNamespace my, ObjectName myname) {
       
    64             this.my=my;
       
    65             this.myname = myname;
       
    66             list = Collections.synchronizedList(new ArrayList<Notification>());
       
    67             my.addNotificationListener(this, null, null);
       
    68         }
       
    69 
       
    70         public synchronized void connect() throws IOException {
       
    71                 my.connect();
       
    72                 if (!my.isConnected())
       
    73                     throw new IOException(myname+" should be connected");
       
    74                 connectCount++;
       
    75         }
       
    76 
       
    77         public void close() throws IOException {
       
    78                 my.close();
       
    79                 if (my.isConnected())
       
    80                     throw new IOException(myname+" shouldn't be connected");
       
    81                 closeCount++;
       
    82         }
       
    83 
       
    84         public synchronized int getConnectCount() {
       
    85             return connectCount;
       
    86         }
       
    87         public synchronized int getClosedCount() {
       
    88             return closeCount;
       
    89         }
       
    90 
       
    91         public synchronized void handleNotification(Notification notification,
       
    92                 Object handback) {
       
    93             list.add(notification);
       
    94         }
       
    95 
       
    96         public synchronized void checkNotifs(int externalConnect,
       
    97                 int externalClosed) throws Exception {
       
    98             System.err.println("Connected: "+connectCount+" time"+
       
    99                     ((connectCount>1)?"s":""));
       
   100             System.err.println("Closed: "+closeCount+" time"+
       
   101                     ((closeCount>1)?"s":""));
       
   102             System.err.println("Received:");
       
   103             int cl=0;
       
   104             int co=0;
       
   105             for (Notification n : list) {
       
   106                 System.err.println("\t"+n);
       
   107                 if (!(n instanceof AttributeChangeNotification))
       
   108                     throw new Exception("Unexpected notif: "+n.getClass());
       
   109                 final AttributeChangeNotification acn =
       
   110                         (AttributeChangeNotification)n;
       
   111                 if (((Boolean)acn.getNewValue()).booleanValue())
       
   112                     co++;
       
   113                 else cl++;
       
   114                 if ((((Boolean)acn.getNewValue()).booleanValue())
       
   115                     == (((Boolean)acn.getOldValue()).booleanValue())) {
       
   116                     throw new Exception("Bad values: old=new");
       
   117                 }
       
   118             }
       
   119             if (! (list.size()==(closeCount+connectCount+
       
   120                     externalClosed+externalConnect))) {
       
   121                 throw new Exception("Bad notif count - got "+list.size());
       
   122             }
       
   123             if (cl!=(closeCount+externalClosed)) {
       
   124                 throw new Exception("Bad count of close notif: expected "
       
   125                         +(closeCount+externalClosed)+", got"+cl);
       
   126             }
       
   127             if (co!=(connectCount+externalConnect)) {
       
   128                 throw new Exception("Bad count of connect notif: expected "
       
   129                         +(connectCount+externalConnect)+", got"+co);
       
   130             }
       
   131         }
       
   132     }
       
   133 
       
   134     public static void testConnectClose() throws Exception {
       
   135         final MBeanServer myServer = MBeanServerFactory.newMBeanServer();
       
   136         final JMXConnectorServer myRMI =
       
   137                 JMXConnectorServerFactory.newJMXConnectorServer(
       
   138                 new JMXServiceURL("rmi",null,0), null, myServer);
       
   139         myRMI.start();
       
   140         try {
       
   141         final JMXRemoteNamespace my =
       
   142                 JMXRemoteNamespace.newJMXRemoteNamespace(
       
   143                 myRMI.getAddress(),null);
       
   144         final MBeanServer s = MBeanServerFactory.newMBeanServer();
       
   145         final ObjectName myname = JMXNamespaces.getNamespaceObjectName("my");
       
   146         final ObjectName wname = ObjectName.getInstance("backyard:type=Wombat");
       
   147         myServer.registerMBean(new Wombat(),wname);
       
   148         final MyConnect myc = new MyConnect(my,myname);
       
   149         myc.connect();
       
   150         myc.close();
       
   151         myc.connect();
       
   152         s.registerMBean(my,myname);
       
   153         myc.close();
       
   154         myc.connect();
       
   155         if (!s.queryNames(new ObjectName("my//b*:*"),null).contains(
       
   156                 JMXNamespaces.insertPath("my", wname))) {
       
   157             throw new RuntimeException("1: Wombat not found: "+wname);
       
   158         }
       
   159         myc.close();
       
   160         myc.connect();
       
   161         final MBeanServer cd = JMXNamespaces.narrowToNamespace(s, "my");
       
   162         if (!cd.queryNames(new ObjectName("b*:*"),null).contains(wname)) {
       
   163             throw new RuntimeException("2: Wombat not found: "+wname);
       
   164         }
       
   165         myc.close();
       
   166         myc.connect();
       
   167         System.out.println("Found a Wombat in my backyard.");
       
   168 
       
   169         final String deepThoughts = "I want to leave this backyard!";
       
   170         final WombatMBean w = JMX.newMBeanProxy(cd, wname, WombatMBean.class);
       
   171         w.setCaption(deepThoughts);
       
   172         if (!deepThoughts.equals(w.getCaption()))
       
   173                 throw new RuntimeException("4: Wombat is not thinking right: "+
       
   174                         w.getCaption());
       
   175         s.unregisterMBean(myname);
       
   176         if (my.isConnected())
       
   177             throw new Exception(myname+" shouldn't be connected");
       
   178         myc.connect();
       
   179         myc.close();
       
   180         myc.checkNotifs(0,1);
       
   181         } finally {
       
   182             myRMI.stop();
       
   183         }
       
   184 
       
   185     }
       
   186 
       
   187     public static void main(String... args) throws Exception {
       
   188         testConnectClose();
       
   189     }
       
   190 }