author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 32834 | jdk/src/java.base/share/classes/java/io/DeleteOnExitHook.java@e1dca5fe4de3 |
child 52427 | 3c6aa484536c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7803
diff
changeset
|
2 |
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package java.io; |
|
26 |
||
27 |
import java.util.*; |
|
28 |
import java.io.File; |
|
32834
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
25859
diff
changeset
|
29 |
import jdk.internal.misc.SharedSecrets; |
2 | 30 |
|
31 |
/** |
|
32 |
* This class holds a set of filenames to be deleted on VM exit through a shutdown hook. |
|
33 |
* A set is used both to prevent double-insertion of the same file as well as offer |
|
34 |
* quick removal. |
|
35 |
*/ |
|
36 |
||
37 |
class DeleteOnExitHook { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
38 |
private static LinkedHashSet<String> files = new LinkedHashSet<>(); |
2277
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
39 |
static { |
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
40 |
// DeleteOnExitHook must be the last shutdown hook to be invoked. |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
41 |
// Application shutdown hooks may add the first file to the |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
42 |
// delete on exit list and cause the DeleteOnExitHook to be |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
43 |
// registered during shutdown in progress. So set the |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
44 |
// registerShutdownInProgress parameter to true. |
32834
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
25859
diff
changeset
|
45 |
SharedSecrets.getJavaLangAccess() |
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
46 |
.registerShutdownHook(2 /* Shutdown hook invocation order */, |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
47 |
true /* register even if shutdown in progress */, |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
48 |
new Runnable() { |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
49 |
public void run() { |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
50 |
runHooks(); |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
51 |
} |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
52 |
} |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
53 |
); |
2277
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
54 |
} |
2 | 55 |
|
56 |
private DeleteOnExitHook() {} |
|
57 |
||
58 |
static synchronized void add(String file) { |
|
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
59 |
if(files == null) { |
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
60 |
// DeleteOnExitHook is running. Too late to add a file |
2 | 61 |
throw new IllegalStateException("Shutdown in progress"); |
2703
acd4d6a53e3e
6829503: addShutdownHook fails if called after shutdown has commenced.
mchung
parents:
2277
diff
changeset
|
62 |
} |
2 | 63 |
|
64 |
files.add(file); |
|
65 |
} |
|
66 |
||
2277
445a331b4a8b
6810254: Lazily instantiate the shared secret access objects
mchung
parents:
2
diff
changeset
|
67 |
static void runHooks() { |
2 | 68 |
LinkedHashSet<String> theFiles; |
69 |
||
70 |
synchronized (DeleteOnExitHook.class) { |
|
71 |
theFiles = files; |
|
72 |
files = null; |
|
73 |
} |
|
74 |
||
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
75 |
ArrayList<String> toBeDeleted = new ArrayList<>(theFiles); |
2 | 76 |
|
77 |
// reverse the list to maintain previous jdk deletion order. |
|
78 |
// Last in first deleted. |
|
79 |
Collections.reverse(toBeDeleted); |
|
80 |
for (String filename : toBeDeleted) { |
|
81 |
(new File(filename)).delete(); |
|
82 |
} |
|
83 |
} |
|
84 |
} |