1 /*
   2  * Copyright (c) 2003, 2008, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /*
  26  * @test
  27  * @bug 4917237
  28  * @summary test that process exit immediately after stop() / close() called
  29  * @author Jean Francois Denise
  30  * @run clean RMIExitTest
  31  * @run build RMIExitTest
  32  * @run main RMIExitTest
  33  */
  34 
  35 import java.net.MalformedURLException;
  36 import java.io.IOException;
  37 
  38 import javax.management.MBeanServerFactory;
  39 import javax.management.MBeanServer;
  40 import javax.management.ObjectName;
  41 import javax.management.MBeanServerConnection;
  42 import javax.management.NotificationListener;
  43 import javax.management.Notification;
  44 
  45 import javax.management.remote.JMXServiceURL;
  46 import javax.management.remote.JMXConnectorServer;
  47 import javax.management.remote.JMXConnector;
  48 import javax.management.remote.JMXConnectorServerFactory;
  49 import javax.management.remote.JMXConnectorFactory;
  50 
  51 /**
  52  * VM shutdown hook. Test that the hook is called less than 5 secs
  53  * after expected exit.
  54  */
  55 class TimeChecker extends Thread {
  56     @Override
  57     public void run() {
  58         System.out.println("shutdown hook called");
  59         long elapsedTime =
  60             System.currentTimeMillis() - RMIExitTest.exitStartTime;
  61         if(elapsedTime >= 5000) {
  62             System.out.println("BUG 4917237 not Fixed.");
  63             // Once in hook, to provide an exit status != 0, halt must
  64             // be called. Hooks are not called when halt is called.
  65             Runtime.getRuntime().halt(1);
  66         } else {
  67             System.out.println("BUG 4917237 Fixed");
  68         }
  69     }
  70 }
  71 
  72 /**
  73  * Start a server, connect a client, add/remove listeners, close client,
  74  * stop server. Check that VM exits in less than 5 secs.
  75  *
  76  */
  77 public class RMIExitTest {
  78     private static final MBeanServer mbs =
  79         MBeanServerFactory.createMBeanServer();
  80     public static long exitStartTime = 0;
  81 
  82     public static void main(String[] args) {
  83         System.out.println("Start test");
  84         Runtime.getRuntime().addShutdownHook(new TimeChecker());
  85         test();
  86         exitStartTime = System.currentTimeMillis();
  87         System.out.println("End test");
  88     }
  89 
  90     private static void test() {
  91         try {
  92             JMXServiceURL u = new JMXServiceURL("rmi", null, 0);
  93             JMXConnectorServer server;
  94             JMXServiceURL addr;
  95             JMXConnector client;
  96             MBeanServerConnection mserver;
  97 
  98             final ObjectName delegateName =
  99                 new ObjectName("JMImplementation:type=MBeanServerDelegate");
 100             final NotificationListener dummyListener =
 101                 new NotificationListener() {
 102                         public void handleNotification(Notification n,
 103                                                        Object o) {
 104                             // do nothing
 105                             return;
 106                         }
 107                     };
 108 
 109             server = JMXConnectorServerFactory.newJMXConnectorServer(u,
 110                                                                      null,
 111                                                                      mbs);
 112             server.start();
 113 
 114             addr = server.getAddress();
 115             client = JMXConnectorFactory.newJMXConnector(addr, null);
 116             client.connect(null);
 117 
 118             mserver = client.getMBeanServerConnection();
 119             String s1 = "1";
 120             String s2 = "2";
 121             String s3 = "3";
 122 
 123             mserver.addNotificationListener(delegateName,
 124                                             dummyListener, null, s1);
 125             mserver.addNotificationListener(delegateName,
 126                                             dummyListener, null, s2);
 127             mserver.addNotificationListener(delegateName,
 128                                             dummyListener, null, s3);
 129 
 130             mserver.removeNotificationListener(delegateName,
 131                                                dummyListener, null, s3);
 132             mserver.removeNotificationListener(delegateName,
 133                                                dummyListener, null, s2);
 134             mserver.removeNotificationListener(delegateName,
 135                                                dummyListener, null, s1);
 136             client.close();
 137 
 138             server.stop();
 139         } catch (Exception e) {
 140             System.out.println(e);
 141             e.printStackTrace();
 142             System.exit(1);
 143         }
 144     }
 145 }