KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > util > TmpDir


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: TmpDir.java,v 1.2 2005/03/24 10:51:25 slobodan Exp $
23  */

24
25
26
27
28
29 package com.lutris.util;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 /**
35  * Class for creating and manage a temporary directory.
36  *
37  * @version $Revision: 1.2 $
38  * @author Mark Diekhans
39  * @since Jolt1.0
40  */

41 class TmpDir {
42   File JavaDoc dir;
43
44   /**
45    * Create a tmp directory.
46    *
47    * @param dirPath Path of directory to generate the temporary file in.
48    * @param baseName Prefix name of the directory.
49    * @exception java.io.IOException if unable to create the directory.
50    */

51   public TmpDir (String JavaDoc path,
52                  String JavaDoc baseName)
53       throws IOException JavaDoc {
54     int dirNum = 0;
55
56     /*
57      * Generate a unique directory.
58      */

59     while (true) {
60       dir = new File JavaDoc (path, baseName + "." + System.currentTimeMillis () +
61                       "." + dirNum + ".tmp");
62       if (dir.mkdirs ()) {
63         break;
64       }
65       if (!dir.exists ()) {
66         /*
67          * Didn't fail because it already exists, must be an access error.
68          */

69         throw new IOException JavaDoc ("no permission to create directory " +
70                                 dir.getAbsolutePath ());
71       }
72       dirNum++;
73       if (dirNum >= 25) {
74         throw new IOException JavaDoc ("failed to create tmp directory named in the " +
75                                "form \"" + dir.getAbsolutePath () +
76                                "\" after " + dirNum + " trys");
77
78       }
79     }
80   }
81
82   /**
83    * Get the File object describing the directory.
84    *
85    * @return Reference to the file object.
86    */

87   public File JavaDoc file () {
88     return dir;
89   }
90   
91   /**
92    * Recursive delete files.
93    */

94   void recursiveDelete (File JavaDoc dirPath) {
95     String JavaDoc [] ls = dirPath.list ();
96
97     for (int idx = 0; idx < ls.length; idx++) {
98       File JavaDoc file = new File JavaDoc (dirPath, ls [idx]);
99       if (file.isDirectory ())
100         recursiveDelete (file);
101       file.delete ();
102     }
103   }
104
105
106   /**
107    * Delete the directory and its contents
108    *
109    * @exception java.io.IOException if unable to remove the directory.
110    */

111   public void delete ()
112       throws IOException JavaDoc {
113     recursiveDelete (dir);
114     if (dir.exists ()) {
115       throw new IOException JavaDoc ("Unable to delete directory hierarchy \"" +
116                              dir.getAbsolutePath () + "\"");
117     }
118   }
119
120   /**
121    * Create the specified directory in the tmp directory if it doesn't exist.
122    *
123    * @param path Path of directory relative to the tmp directory.
124    * @return A File naming the new directory.
125    * @exception java.io.IOException if unable to create the directory.
126    */

127   public File JavaDoc mkdirs (String JavaDoc path)
128       throws IOException JavaDoc {
129     File JavaDoc newDir = new File JavaDoc (dir.getAbsolutePath (), path);
130     newDir.mkdirs ();
131     if (!newDir.exists ()) {
132       throw new IOException JavaDoc ("Unable to create directory \"" +
133                              dir.getAbsolutePath () + "\"");
134     }
135     return newDir;
136   }
137 }
138
Popular Tags