1 45 package org.openejb.util; 46 47 import java.io.File ; 48 import java.io.FileInputStream ; 49 import java.io.FileNotFoundException ; 50 import java.io.FileOutputStream ; 51 import java.io.IOException ; 52 import java.io.InputStream ; 53 import java.net.URL ; 54 import java.util.Hashtable ; 55 import java.util.Properties ; 56 57 import org.openejb.OpenEJBException; 58 import org.openejb.loader.SystemInstance; 59 60 public class FileUtils { 61 62 private static final java.util.Random _random = new java.util.Random (); 63 64 private File home; 65 66 private FileUtils(String homeDir, String defaultDir) { 67 this(homeDir, defaultDir, System.getProperties()); 68 } 69 70 public FileUtils(String homeDir, String defaultDir, Hashtable env) { 71 String homePath = null; 72 try { 73 homePath = (String ) env.get(homeDir); 74 if (homePath == null) { 75 homePath = (String ) env.get(defaultDir); 76 } 77 78 if (homePath == null) { 79 homePath = System.getProperty("user.dir"); 80 } 81 82 home = new File (homePath); 83 if (!home.exists() || (home.exists() && !home.isDirectory())) { 84 homePath = System.getProperty("user.dir"); 85 home = new File (homePath); 86 } 87 88 home = home.getAbsoluteFile(); 89 } catch (SecurityException e) { 90 } 92 } 93 94 97 public File getDirectory(String path) throws IOException { 98 return getDirectory(path, false); 99 } 100 101 public boolean equals(Object obj) { 102 if (!(obj instanceof FileUtils)) return false; 103 FileUtils that = (FileUtils)obj; 104 return this.getDirectory().equals(that.getDirectory()); 105 } 106 107 117 public File getDirectory(String path, boolean create) throws IOException { 118 File dir = null; 119 120 dir = new File (home, path); 121 dir = dir.getCanonicalFile(); 122 123 if (!dir.exists() && create) { 124 try { 125 if (!dir.mkdirs()) 126 throw new IOException ("Cannot create the directory " + dir.getPath()); 127 } catch (SecurityException e) { 128 throw new IOException ( 129 "Permission denied: Cannot create the directory " + dir.getPath() + " : " + e.getMessage()); 130 } 131 } else if (dir.exists() && !dir.isDirectory()) { 132 throw new IOException ("The path specified is not a valid directory: " + dir.getPath()); 133 } 134 135 return dir; 136 } 137 138 public File getDirectory() { 139 return home; 140 } 141 142 public void setDirectory(File dir) { 143 this.home = dir; 144 } 145 146 public File getFile(String path) throws java.io.FileNotFoundException , java.io.IOException { 147 return getFile(path, true); 148 } 149 150 public File getFile(String path, boolean validate) throws java.io.FileNotFoundException , java.io.IOException { 151 File file = null; 152 153 file = new File (path); 154 155 if (!file.isAbsolute()) { 156 file = new File (home, path); 157 } 158 159 if (validate && !file.exists()) { 160 throw new FileNotFoundException ("The path specified is not a valid file: " + file.getPath()); 161 } else if (validate && file.isDirectory()) { 162 throw new FileNotFoundException ("The path specified is a directory, not a file: " + file.getPath()); 163 } 164 165 return file; 166 } 167 168 177 public static File createTempDirectory(String pathPrefix) throws java.io.IOException { 178 for (int maxAttempts = 100; maxAttempts > 0; --maxAttempts) { 179 String path = pathPrefix + _random.nextLong(); 180 java.io.File tmpDir = new java.io.File (path); 181 if (tmpDir.exists()) { 182 continue; 183 } else { 184 tmpDir.mkdir(); 185 return tmpDir; 186 } 187 } 188 throw new java.io.IOException ("Can't create temporary directory."); 189 } 190 191 198 public static File createTempDirectory() throws java.io.IOException { 199 String prefix = System.getProperty("java.io.tmpdir", File.separator + "tmp") + File.separator + "openejb"; 200 return createTempDirectory(prefix); 201 } 202 203 214 public static void copyFile(File destination, File source) throws java.io.IOException { 215 copyFile(destination, source, false); 216 } 217 218 231 public static void copyFile(File destination, File source, boolean deleteSourceFile) throws java.io.IOException { 232 FileInputStream in = null; 233 FileOutputStream out = null; 234 try { 235 in = new FileInputStream (source); 236 out = new FileOutputStream (destination); 237 238 int len; 239 byte[] buffer = new byte[4096]; 240 while ((len = in.read(buffer)) != -1) { 241 out.write(buffer, 0, len); 242 } 243 } catch (java.io.IOException e) { 244 throw e; 245 } finally { 246 in.close(); 247 out.close(); 248 } 249 250 if (deleteSourceFile) { 251 source.delete(); 252 } 253 } 254 255 } 256 | Popular Tags |