1189N/A/*
3909N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
1189N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1189N/A *
1189N/A * This code is free software; you can redistribute it and/or modify it
1189N/A * under the terms of the GNU General Public License version 2 only, as
1189N/A * published by the Free Software Foundation.
1189N/A *
1189N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1189N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1189N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1189N/A * version 2 for more details (a copy is included in the LICENSE file that
1189N/A * accompanied this code).
1189N/A *
1189N/A * You should have received a copy of the GNU General Public License version
1189N/A * 2 along with this work; if not, write to the Free Software Foundation,
1189N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1189N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1189N/A */
1189N/A
1189N/A/*
1189N/A * @bug 6829503
1189N/A * @summary 1) Test Console and DeleteOnExitHook can be initialized
1189N/A * while shutdown is in progress
1189N/A * 2) Test if files that are added by the application shutdown
1189N/A * hook are deleted on exit during shutdown
1189N/A */
1189N/Aimport java.io.*;
1189N/Apublic class ShutdownHooks {
1189N/A private static File file;
1189N/A public static void main(String[] args) throws Exception {
1189N/A if (args.length != 2) {
1189N/A throw new IllegalArgumentException("Usage: ShutdownHooks <dir> <filename>");
1189N/A }
1189N/A
1189N/A // Add a shutdown hook
1189N/A Runtime.getRuntime().addShutdownHook(new Cleaner());
1189N/A
1189N/A File dir = new File(args[0]);
1189N/A file = new File(dir, args[1]);
1189N/A // write to file
1189N/A System.out.println("writing to "+ file);
3646N/A try (PrintWriter pw = new PrintWriter(file)) {
3646N/A pw.println("Shutdown begins");
3646N/A }
1189N/A }
1189N/A
1189N/A public static class Cleaner extends Thread {
1189N/A public void run() {
1189N/A // register the Console's shutdown hook while the application
1189N/A // shutdown hook is running
1189N/A Console cons = System.console();
1189N/A // register the DeleteOnExitHook while the application
1189N/A // shutdown hook is running
1189N/A file.deleteOnExit();
3646N/A try (PrintWriter pw = new PrintWriter(file)) {
1189N/A pw.println("file is being deleted");
1189N/A } catch (FileNotFoundException e) {
1189N/A throw new RuntimeException(e);
1189N/A }
1189N/A }
1189N/A }
1189N/A
1189N/A}