File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\main\java\org\jvnet\hudson\test\TemporaryDirectoryAllocator.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package org.jvnet.hudson.test;
25
26 import hudson.FilePath;
27 import hudson.util.IOException2;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.HashSet;
32 import java.util.Set;
33
34 /**
35 * Allocates temporary directories and cleans it up at the end.
36 * @author Kohsuke Kawaguchi
37 */
38 public class TemporaryDirectoryAllocator {
39 /**
40 * Remember allocated directories to delete them later.
41 */
42 private final Set<File> tmpDirectories = new HashSet<File>();
43
44 /**
45 * Directory in which we allocate temporary directories.
46 */
47 private final File base;
48
49 public TemporaryDirectoryAllocator(File base) {
50 this.base = base;
51 }
52
53 public TemporaryDirectoryAllocator() {
54 this(new File(System.getProperty("java.io.tmpdir")));
55 }
56
57 /**
58 * Allocates a new empty temporary directory and returns it.
59 *
60 * This directory will be wiped out when {@link TemporaryDirectoryAllocator} gets disposed.
61 * When this method returns, the directory already exists.
62 */
63 public synchronized File allocate() throws IOException {
64 try {
65 File f = File.createTempFile("hudson", "test", base);
66 f.delete();
67 f.mkdirs();
68 tmpDirectories.add(f);
69 return f;
70 } catch (IOException e) {
71 throw new IOException2("Failed to create a temporary directory in "+base,e);
72 }
73 }
74
75 /**
76 * Deletes all allocated temporary directories.
77 */
78 public synchronized void dispose() throws IOException, InterruptedException {
79 IOException x = null;
80 for (File dir : tmpDirectories)
81 try {
82 new FilePath(dir).deleteRecursive();
83 } catch (IOException e) {
84 x = e;
85 }
86 tmpDirectories.clear();
87 if (x!=null) throw new IOException2("Failed to clean up temp dirs",x);
88 }
89
90 /**
91 * Deletes all allocated temporary directories asynchronously.
92 */
93 public synchronized void disposeAsync() {
94 final Set<File> tbr = new HashSet<File>(tmpDirectories);
95 tmpDirectories.clear();
96
97 new Thread("Disposing "+base) {
98 public void run() {
99 for (File dir : tbr)
100 try {
101 new FilePath(dir).deleteRecursive();
102 } catch (IOException e) {
103 e.printStackTrace();
104 } catch (InterruptedException e) {
105 e.printStackTrace();
106 }
107 }
108 }.start();
109 }
110 }
111