1 24 25 package org.objectweb.jonas_lib.genbase.utils; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Vector ; 32 33 42 public class TempRepository { 43 44 45 private static TempRepository instance = null; 46 47 48 private List directories; 49 50 53 private TempRepository() { 54 directories = new Vector (); 55 } 56 57 62 public static TempRepository getInstance() { 63 if (instance == null) { 64 instance = new TempRepository(); 65 } 66 67 return instance; 68 } 69 70 75 public void addDir(File file) { 76 directories.add(file); 77 } 78 79 85 public boolean deleteAll() { 86 boolean result = true; 87 88 for (Iterator i = directories.iterator(); i.hasNext();) { 89 File dir = (File ) i.next(); 90 i.remove(); 91 92 if (dir.exists()) { 94 result &= delete(dir); 95 } 96 } 97 98 return result; 99 } 100 101 108 private boolean delete(File f) { 109 if (f.isFile()) { 110 return f.delete(); 111 } else { 112 File [] childs = f.listFiles(); 113 if (childs == null) { 114 return f.delete(); 116 } else { 117 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 135 public File createDir() throws IOException { 136 File tmp = File.createTempFile("wsgen", null); 138 139 if (!tmp.delete()) { 140 throw new IOException ("Cannot delete temporary file"); 141 } 142 143 if (!tmp.mkdir()) { 145 throw new IOException ("Cannot create temporary directory"); 146 } 147 148 addDir(tmp); 150 151 return tmp; 152 } 153 } | Popular Tags |