001: /**
002: * JOnAS : Java(TM) OpenSource Application Server
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Initial Developer : Guillaume Sauthier
020: * --------------------------------------------------------------------------
021: * $Id: TempRepository.java 6657 2005-04-27 12:28:21Z benoitf $
022: * --------------------------------------------------------------------------
023: */package org.objectweb.jonas_lib.genbase.utils;
024:
025: import java.io.File;
026: import java.io.IOException;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Vector;
030:
031: /**
032: * A <code>TempRepository</code> object is tye main container for temporary
033: * directories. When an object of WsGen or ClientStubGen wants to use a temporary directory, it
034: * ask TempRepository instance to create a new one. It is usefull when multiples
035: * tempo directories are created. Deletion is much more simpler. The
036: * TempRepository is a Singleton.
037: *
038: * @author Guillaume Sauthier
039: */
040: public class TempRepository {
041:
042: /** the TempRepository instance */
043: private static TempRepository instance = null;
044:
045: /** list of temporary directories */
046: private List directories;
047:
048: /**
049: * Create a new empty TempRepository.
050: */
051: private TempRepository() {
052: directories = new Vector();
053: }
054:
055: /**
056: * Return the only instance of TempRepository.
057: *
058: * @return the only instance of TempRepository.
059: */
060: public static TempRepository getInstance() {
061: if (instance == null) {
062: instance = new TempRepository();
063: }
064:
065: return instance;
066: }
067:
068: /**
069: * Add a temporary File in the repository (file or directory).
070: *
071: * @param file the temporary file or directory to add.
072: */
073: public void addDir(File file) {
074: directories.add(file);
075: }
076:
077: /**
078: * Delete all the files contained in the repository. Returns true when
079: * deletion is successfull, false otherwise.
080: *
081: * @return true when deletion is successfull, false otherwise.
082: */
083: public boolean deleteAll() {
084: boolean result = true;
085:
086: for (Iterator i = directories.iterator(); i.hasNext();) {
087: File dir = (File) i.next();
088: i.remove();
089:
090: // delete only if file exists
091: if (dir.exists()) {
092: result &= delete(dir);
093: }
094: }
095:
096: return result;
097: }
098:
099: /**
100: * Delete a file or directory recursively
101: *
102: * @param f file or directory to be deleted
103: *
104: * @return true if deletion ok, false otherweise.
105: */
106: private boolean delete(File f) {
107: if (f.isFile()) {
108: return f.delete();
109: } else {
110: File[] childs = f.listFiles();
111: if (childs == null) {
112: // no childs
113: return f.delete();
114: } else {
115: // childs
116: boolean result = true;
117: for (int i = 0; i < childs.length; i++) {
118: result &= delete(childs[i]);
119: }
120: return result && f.delete();
121: }
122: }
123: }
124:
125: /**
126: * Return an empty temporary directory and automatically add it into the
127: * repository.
128: *
129: * @return an empty temporary directory.
130: *
131: * @throws IOException when creation fails.
132: */
133: public File createDir() throws IOException {
134: // create file
135: File tmp = File.createTempFile("wsgen", null);
136:
137: if (!tmp.delete()) {
138: throw new IOException("Cannot delete temporary file");
139: }
140:
141: // create directory
142: if (!tmp.mkdir()) {
143: throw new IOException("Cannot create temporary directory");
144: }
145:
146: // add in repo
147: addDir(tmp);
148:
149: return tmp;
150: }
151: }
|