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