jdk/test/javax/management/eventService/NotSerializableNotifTest.java
changeset 4159 9e3aae7675f1
parent 4158 0b4d21bc8b5c
parent 4156 acaa49a2768a
child 4160 bda0a85afcb7
equal deleted inserted replaced
4158:0b4d21bc8b5c 4159:9e3aae7675f1
     1 /*
       
     2  * Copyright 2007-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 NotSerializableNotifTest.java 1.5 08/01/22
       
    27  * @bug 5108776
       
    28  * @summary Basic test for EventClient.
       
    29  * @author Shanliang JIANG
       
    30  * @run clean NotSerializableNotifTest
       
    31  * @run build NotSerializableNotifTest
       
    32  * @run main NotSerializableNotifTest
       
    33  */
       
    34 
       
    35 
       
    36 // JMX imports
       
    37 //
       
    38 import javax.management.* ;
       
    39 import javax.management.event.EventClient;
       
    40 import javax.management.event.EventClientDelegate;
       
    41 import javax.management.event.EventClientDelegateMBean;
       
    42 import javax.management.event.EventRelay;
       
    43 import javax.management.event.FetchingEventRelay;
       
    44 
       
    45 import javax.management.remote.*;
       
    46 import javax.management.remote.JMXServiceURL;
       
    47 
       
    48 public class NotSerializableNotifTest {
       
    49     private static MBeanServer mbeanServer =
       
    50         MBeanServerFactory.createMBeanServer();
       
    51     private static ObjectName emitter;
       
    52     private static int port = 2468;
       
    53 
       
    54     private static String[] protocols;
       
    55 
       
    56     private static final int sentNotifs = 50;
       
    57 
       
    58     public static void main(String[] args) throws Exception {
       
    59         System.out.println(">>> Test to send a not serializable notification");
       
    60 
       
    61         // for 1.5
       
    62         if (System.getProperty("java.version").startsWith("1.5") &&
       
    63                 !mbeanServer.isRegistered(EventClientDelegateMBean.OBJECT_NAME)) {
       
    64             System.out.print("Working on "+System.getProperty("java.version")+
       
    65                     " register "+EventClientDelegateMBean.OBJECT_NAME);
       
    66 
       
    67             mbeanServer.registerMBean(EventClientDelegate.
       
    68                     getEventClientDelegate(mbeanServer),
       
    69                     EventClientDelegateMBean.OBJECT_NAME);
       
    70         }
       
    71 
       
    72         NotificationEmitter nm = new NotificationEmitter();
       
    73         emitter = new ObjectName("Default:name=NotificationEmitter");
       
    74         mbeanServer.registerMBean(nm, emitter);
       
    75         String proto = "rmi";
       
    76 
       
    77         System.out.println(">>> Test for protocol " + proto);
       
    78 
       
    79         JMXServiceURL url = new JMXServiceURL(proto, null, 0);
       
    80 
       
    81         JMXConnectorServer server =
       
    82             JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);
       
    83 
       
    84         server.start();
       
    85 
       
    86         url = server.getAddress();
       
    87         JMXConnector conn = JMXConnectorFactory.connect(url, null);
       
    88         MBeanServerConnection client = conn.getMBeanServerConnection();
       
    89 
       
    90         EventClientDelegateMBean ecd = EventClientDelegate.getProxy(client);
       
    91         EventRelay eventRelay = new FetchingEventRelay(
       
    92                 ecd,
       
    93                 FetchingEventRelay.DEFAULT_BUFFER_SIZE,
       
    94                 10,
       
    95                 FetchingEventRelay.DEFAULT_MAX_NOTIFICATIONS,
       
    96                 null);
       
    97         EventClient ec = new EventClient(ecd, eventRelay, null, null,
       
    98                 EventClient.DEFAULT_REQUESTED_LEASE_TIME);
       
    99 
       
   100         // add listener from the client side
       
   101         Listener listener = new Listener();
       
   102         ec.addNotificationListener(emitter, listener, null, null);
       
   103 
       
   104         LostListener lostListener = new LostListener();
       
   105         ec.addEventClientListener(lostListener, null, null);
       
   106 
       
   107         // ask to send one not serializable notif
       
   108         System.out.println(">>> sending not serializable notifs ...");
       
   109 
       
   110         Object[] params = new Object[] {new Integer(sentNotifs)};
       
   111         String[] signatures = new String[] {"java.lang.Integer"};
       
   112         client.invoke(emitter, "sendNotserializableNotifs", params, signatures);
       
   113 
       
   114 //      nm.sendNotserializableNotifs(sentNotifs);
       
   115 //      nm.sendNotifications(1);
       
   116 
       
   117         // waiting
       
   118         synchronized(lostListener) {
       
   119             if (lostListener.lostCount != sentNotifs) {
       
   120                 lostListener.wait(6000);
       
   121             }
       
   122         }
       
   123 
       
   124         Thread.sleep(100);
       
   125 
       
   126         if (lostListener.lostCount != sentNotifs) {
       
   127             System.out.println(">>> FAILED. Expected "+sentNotifs+", but got "+lostListener.lostCount);
       
   128             System.exit(1);
       
   129         }
       
   130 
       
   131         System.out.println(">>> Passed.");
       
   132 
       
   133         ec.close();
       
   134         conn.close();
       
   135         server.stop();
       
   136     }
       
   137 
       
   138 
       
   139 //--------------------------
       
   140 // private classes
       
   141 //--------------------------
       
   142     private static class Listener implements NotificationListener {
       
   143         public void handleNotification(Notification n, Object handback) {
       
   144             System.out.println(">>> Listener: receive: "+n);
       
   145         }
       
   146     }
       
   147 
       
   148 
       
   149     private static class LostListener implements NotificationListener {
       
   150         public void handleNotification(Notification n, Object handback) {
       
   151              if (!EventClient.NOTIFS_LOST.equals(n.getType())) {
       
   152                 return;
       
   153             }
       
   154 
       
   155             if (!(n.getUserData() instanceof Long)) {
       
   156                 System.out.println(">>> Listener: JMXConnectionNotification userData " +
       
   157                                    "not a Long: " + n.getUserData());
       
   158                 System.exit(1);
       
   159             } else {
       
   160                 int lost = ((Long) n.getUserData()).intValue();
       
   161                 lostCount += lost;
       
   162                 if (lostCount >= sentNotifs) {
       
   163                     synchronized(this) {
       
   164                         this.notifyAll();
       
   165                     }
       
   166                 }
       
   167             }
       
   168 
       
   169         }
       
   170 
       
   171 
       
   172         private int lostCount = 0;
       
   173     }
       
   174 
       
   175     public static class NotificationEmitter extends NotificationBroadcasterSupport
       
   176         implements NotificationEmitterMBean {
       
   177 
       
   178         public MBeanNotificationInfo[] getNotificationInfo() {
       
   179             final String[] ntfTypes = {myType};
       
   180 
       
   181             final MBeanNotificationInfo[] ntfInfoArray  = {
       
   182                 new MBeanNotificationInfo(ntfTypes,
       
   183                                           "javax.management.Notification",
       
   184                                           "Notifications sent by the NotificationEmitter")};
       
   185 
       
   186             return ntfInfoArray;
       
   187         }
       
   188 
       
   189         /**
       
   190          * Send not serializable Notifications.
       
   191          *
       
   192          * @param nb The number of notifications to send
       
   193          */
       
   194         public void sendNotserializableNotifs(Integer nb) {
       
   195 
       
   196             Notification notif;
       
   197             for (int i=1; i<=nb.intValue(); i++) {
       
   198                 notif = new Notification(myType, this, i);
       
   199 
       
   200                 notif.setUserData(new Object());
       
   201                 sendNotification(notif);
       
   202             }
       
   203         }
       
   204 
       
   205         /**
       
   206          * Send Notification objects.
       
   207          *
       
   208          * @param nb The number of notifications to send
       
   209          */
       
   210         public void sendNotifications(Integer nb) {
       
   211             Notification notif;
       
   212             for (int i=1; i<=nb.intValue(); i++) {
       
   213                 notif = new Notification(myType, this, i);
       
   214 
       
   215                 sendNotification(notif);
       
   216             }
       
   217         }
       
   218 
       
   219         private final String myType = "notification.my_notification";
       
   220     }
       
   221 
       
   222     public interface NotificationEmitterMBean {
       
   223         public void sendNotifications(Integer nb);
       
   224 
       
   225         public void sendNotserializableNotifs(Integer nb);
       
   226     }
       
   227 }