1 /*
2  * Copyright (c) 2001, 2012, 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  */
27 
28 import java.rmi.Remote;
29 import java.rmi.registry.LocateRegistry;
30 import java.rmi.registry.Registry;
31 import java.rmi.server.UnicastRemoteObject;
32 
33 public class ShutdownImpl implements Shutdown {
34 
35     private static Remote impl;         // rooted here to prevent GC
36 
37     private final ShutdownMonitor monitor;
38 
ShutdownImpl(ShutdownMonitor monitor)39     private ShutdownImpl(ShutdownMonitor monitor) {
40         this.monitor = monitor;
41     }
42 
shutdown()43     public void shutdown() {
44         try {
45             System.err.println(
46                 "(ShutdownImpl.shutdown) shutdown method invoked:");
47 
48             UnicastRemoteObject.unexportObject(this, true);
49             System.err.println(
50                 "(ShutdownImpl.shutdown) shutdown object unexported");
51 
52             Thread.sleep(500);
53             System.err.println("(ShutDownImpl.shutdown) FEE");
54             Thread.sleep(500);
55             System.err.println("(ShutDownImpl.shutdown) FIE");
56             Thread.sleep(500);
57             System.err.println("(ShutDownImpl.shutdown) FOE");
58             Thread.sleep(500);
59             System.err.println("(ShutDownImpl.shutdown) FOO");
60 
61             monitor.declareStillAlive();
62             System.err.println("(ShutDownImpl.shutdown) still alive!");
63         } catch (Exception e) {
64             throw new RuntimeException(
65                 "unexpected exception occurred in shutdown method", e);
66         }
67     }
68 
main(String[] args)69     public static void main(String[] args) {
70         try {
71             int registryPort = Integer.parseInt(System.getProperty("rmi.registry.port"));
72             Registry registry =
73                 LocateRegistry.getRegistry("", registryPort);
74             ShutdownMonitor monitor = (ShutdownMonitor)
75                 registry.lookup(KeepAliveDuringCall.BINDING);
76             System.err.println("(ShutdownImpl) retrieved shutdown monitor");
77 
78             impl = new ShutdownImpl(monitor);
79             Shutdown stub = (Shutdown) UnicastRemoteObject.exportObject(impl);
80             System.err.println("(ShutdownImpl) exported shutdown object");
81 
82             monitor.submitShutdown(stub);
83             System.err.println("(ShutdownImpl) submitted shutdown object");
84 
85         } catch (Exception e) {
86             System.err.println("(ShutdownImpl) TEST SUBPROCESS FAILURE:");
87             e.printStackTrace();
88         }
89     }
90 }
91