KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > TmpCleaner


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.verifier;
25
26 import java.io.BufferedReader JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileReader JavaDoc;
29
30 /**
31  * Reads the list of file names from the file cleandirs.txt and
32  * calls deleteFile to recursively delete the directories.
33  * NOTE : This independent class gets called only on W2K, where the
34  * Normal cleanAll() call from doit() in verifier fails.
35  */

36 public class TmpCleaner {
37
38     private final static String JavaDoc TMPDIR = System.getProperty("java.io.tmpdir");
39
40     public void run() {
41
42         // read the file
43
try {
44             String JavaDoc cleandirs = TMPDIR + File.separator + "cleandirs.txt"; // NOI18N
45
File JavaDoc tmpfile = new File JavaDoc(cleandirs);
46             if (!tmpfile.exists())
47                 return;
48             BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(cleandirs));
49
50             try {
51                 do {
52                     String JavaDoc str = br.readLine();
53                     String JavaDoc file = TMPDIR + File.separator + str;
54                     File JavaDoc toDelete = new File JavaDoc(file);
55                     deleteFile(toDelete);
56                     toDelete.deleteOnExit();
57                 } while (br.ready());
58             } catch (Exception JavaDoc e) {
59             }
60
61
62             br.close();
63             File JavaDoc f = new File JavaDoc(cleandirs);
64             f.delete();
65         } catch (Exception JavaDoc e) {
66         }
67     }
68
69     private void deleteFile(File JavaDoc p_file) {
70         String JavaDoc FILE_SEPARATOR = System.getProperty("file.separator");
71         // If it is a directory, empty it first
72
if (p_file.isDirectory()) {
73             String JavaDoc[] dirList = p_file.list();
74             for (int i = 0; i < dirList.length; i++) {
75
76                 File JavaDoc aFile = new File JavaDoc(
77                         p_file.getPath() + FILE_SEPARATOR + dirList[i]);
78                 if (aFile.isDirectory()) {
79                     deleteFile(aFile);
80                 }
81                 aFile.delete();
82             }
83         }
84         p_file.delete();
85     }
86
87
88     public static void main(String JavaDoc[] args) {
89         try {
90             TmpCleaner t = new TmpCleaner();
91             System.gc();
92             t.run();
93         } catch (Exception JavaDoc e) {
94         }
95         System.exit(0);
96     }
97
98 }
99
Popular Tags