KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > genbase > utils > TempRepository


1 /**
2  * JOnAS : Java(TM) OpenSource Application Server
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Initial Developer : Guillaume Sauthier
20  * --------------------------------------------------------------------------
21  * $Id: TempRepository.java,v 1.2 2005/04/27 12:28:21 benoitf Exp $
22  * --------------------------------------------------------------------------
23  */

24
25 package org.objectweb.jonas_lib.genbase.utils;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 /**
34  * A <code>TempRepository</code> object is tye main container for temporary
35  * directories. When an object of WsGen or ClientStubGen wants to use a temporary directory, it
36  * ask TempRepository instance to create a new one. It is usefull when multiples
37  * tempo directories are created. Deletion is much more simpler. The
38  * TempRepository is a Singleton.
39  *
40  * @author Guillaume Sauthier
41  */

42 public class TempRepository {
43
44     /** the TempRepository instance */
45     private static TempRepository instance = null;
46
47     /** list of temporary directories */
48     private List JavaDoc directories;
49
50     /**
51      * Create a new empty TempRepository.
52      */

53     private TempRepository() {
54         directories = new Vector JavaDoc();
55     }
56
57     /**
58      * Return the only instance of TempRepository.
59      *
60      * @return the only instance of TempRepository.
61      */

62     public static TempRepository getInstance() {
63         if (instance == null) {
64             instance = new TempRepository();
65         }
66
67         return instance;
68     }
69
70     /**
71      * Add a temporary File in the repository (file or directory).
72      *
73      * @param file the temporary file or directory to add.
74      */

75     public void addDir(File JavaDoc file) {
76         directories.add(file);
77     }
78
79     /**
80      * Delete all the files contained in the repository. Returns true when
81      * deletion is successfull, false otherwise.
82      *
83      * @return true when deletion is successfull, false otherwise.
84      */

85     public boolean deleteAll() {
86         boolean result = true;
87
88         for (Iterator JavaDoc i = directories.iterator(); i.hasNext();) {
89             File JavaDoc dir = (File JavaDoc) i.next();
90             i.remove();
91
92             // delete only if file exists
93
if (dir.exists()) {
94                 result &= delete(dir);
95             }
96         }
97
98         return result;
99     }
100
101     /**
102      * Delete a file or directory recursively
103      *
104      * @param f file or directory to be deleted
105      *
106      * @return true if deletion ok, false otherweise.
107      */

108     private boolean delete(File JavaDoc f) {
109         if (f.isFile()) {
110             return f.delete();
111         } else {
112             File JavaDoc[] childs = f.listFiles();
113             if (childs == null) {
114                 // no childs
115
return f.delete();
116             } else {
117                 // childs
118
boolean result = true;
119                 for (int i = 0; i < childs.length; i++) {
120                     result &= delete(childs[i]);
121                 }
122                 return result && f.delete();
123             }
124         }
125     }
126
127     /**
128      * Return an empty temporary directory and automatically add it into the
129      * repository.
130      *
131      * @return an empty temporary directory.
132      *
133      * @throws IOException when creation fails.
134      */

135     public File JavaDoc createDir() throws IOException JavaDoc {
136         // create file
137
File JavaDoc tmp = File.createTempFile("wsgen", null);
138
139         if (!tmp.delete()) {
140             throw new IOException JavaDoc("Cannot delete temporary file");
141         }
142
143         // create directory
144
if (!tmp.mkdir()) {
145             throw new IOException JavaDoc("Cannot create temporary directory");
146         }
147
148         // add in repo
149
addDir(tmp);
150
151         return tmp;
152     }
153 }
Popular Tags