1 23 24 package com.sun.enterprise.deployment.deploy.shared; 25 26 import java.io.*; 27 import java.util.Vector ; 28 import java.util.Enumeration ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.jar.Manifest ; 33 import java.util.jar.JarFile ; 34 import java.net.URI ; 35 36 import com.sun.enterprise.util.io.FileUtils; 37 import com.sun.enterprise.deployment.deploy.shared.Archive; 38 39 45 public class FileArchive extends AbstractArchive { 46 47 File archive; 49 String path; 50 51 OutputStream os=null; 53 54 55 public FileArchive() { 56 } 57 58 62 public void open(String path) throws IOException { 63 this.path = path.replace('/', File.separatorChar); 64 archive = new File (path); 65 if (!archive.exists()) { 66 throw new FileNotFoundException(path); 67 } 68 } 69 70 74 public long getArchiveSize() throws NullPointerException , SecurityException { 75 if(getArchiveUri() == null) { 76 return -1; 77 } 78 File tmpFile = new File (getArchiveUri()); 79 return(tmpFile.length()); 80 } 81 82 86 public void create(String path) throws IOException { 87 this.path = path.replace('/', File.separatorChar); 88 archive = new File (path); 89 archive.mkdirs(); 90 } 91 92 95 public void close() throws IOException { 96 } 98 99 105 public void closeEntry(AbstractArchive os) throws IOException { 106 os.close(); 107 } 108 109 112 public boolean delete() { 113 try { 115 return deleteDir(archive); 116 } catch (IOException e) { 117 return false; 118 } 119 } 120 121 125 public Enumeration entries() { 126 Vector namesList = new Vector (); 127 getListOfFiles(archive, namesList, null); 128 return namesList.elements(); 129 } 130 131 136 public Enumeration entries(Enumeration embeddedArchives) { 137 Vector nameList = new Vector (); 138 List massagedNames = new ArrayList (); 139 while (embeddedArchives.hasMoreElements()) { 140 String subArchiveName = (String ) embeddedArchives.nextElement(); 141 massagedNames.add(FileUtils.makeFriendlyFileName(subArchiveName)); 142 } 143 getListOfFiles(archive, nameList, massagedNames); 144 return nameList.elements(); 145 } 146 147 156 public Enumeration entries(String prefix) { 157 prefix = prefix.replace('/', File.separatorChar); 158 File file = new File (archive, prefix); 159 Vector namesList = new Vector (); 160 getListOfFiles(file, namesList, null); 161 return namesList.elements(); 162 } 163 164 167 public boolean exists() { 168 return archive.exists(); 169 } 170 171 174 public String getArchiveUri() { 175 return path; 176 } 177 178 183 public AbstractArchive getEmbeddedArchive(String name) throws IOException { 184 name = name.replace('/', File.separatorChar); 186 File file = new File (name); 187 File subDir; 188 if (file.isAbsolute()) { 189 subDir = file; 190 } else { 191 subDir = new File (archive, FileUtils.makeFriendlyFileName(name)); 194 if (!subDir.exists()) { 195 subDir = new File (archive, name); 197 if (!subDir.exists()) { 198 subDir = new File (archive, FileUtils.makeFriendlyFileName(name)); 201 } 202 } 203 } 204 String subName = subDir.getPath(); 205 if (!subDir.exists()) { 206 File newDir = new File (subName); 208 newDir.mkdirs(); 209 } 210 AbstractArchive sub; 211 if (subDir.isDirectory()) { 212 sub = new FileArchive(); 213 ((FileArchive) sub).open(subName); 214 } else { 215 sub = new InputJarArchive(); 216 ((InputJarArchive) sub).open(subName); 217 } 218 return sub; 219 } 220 221 226 public InputStream getEntry(String name) throws IOException { 227 228 name = name.replace('/', File.separatorChar); 229 File input = new File (archive, name); 230 if (!input.exists()) { 231 return null; 232 } 233 FileInputStream fis = new FileInputStream(input); 234 try { 235 BufferedInputStream bis = new BufferedInputStream(fis); 236 return bis; 237 } catch (Throwable tx) { 238 if (fis != null) { 239 try { 240 fis.close(); 241 } catch (Throwable thr) { 242 IOException ioe = new IOException("Error closing FileInputStream after error opening BufferedInputStream for entry " + name); 243 ioe.initCause(thr); 244 throw ioe; 245 } 246 } 247 IOException ioe = new IOException("Error opening BufferedInputStream for entry " + name); 248 ioe.initCause(tx); 249 throw ioe; 250 } 251 } 252 253 256 public Manifest getManifest() throws IOException { 257 InputStream is = null; 258 try { 259 is = getEntry(JarFile.MANIFEST_NAME); 260 if (is!=null) { 261 Manifest m = new Manifest (is); 262 return m; 263 } 264 } finally { 265 if (is != null) { 266 is.close(); 267 } 268 } 269 return null; 270 } 271 272 277 public boolean renameTo(String name) { 278 return FileUtils.renameFile(archive, new File (name)); 279 } 280 281 282 285 private boolean deleteDir(File directory) throws IOException { 286 if (!directory.isDirectory()) { 287 throw new FileNotFoundException(directory.getPath()); 288 } 289 290 File [] entries = directory.listFiles(); 292 for (int i=0;i<entries.length;i++) { 293 if (entries[i].isDirectory()) { 294 deleteDir(entries[i]); 295 } else { 296 FileUtils.deleteFile(entries[i]); 297 } 298 } 299 return FileUtils.deleteFile(directory); 301 } 302 303 307 private void getListOfFiles(File directory, Vector files, List embeddedArchives) { 308 File [] list = directory.listFiles(); 309 for (int i=0;i<list.length;i++) { 310 String fileName = list[i].getAbsolutePath().substring(archive.getAbsolutePath().length()+1); 311 if (!list[i].isDirectory()) { 312 fileName = fileName.replace(File.separatorChar, '/'); 313 if (!fileName.equals(JarFile.MANIFEST_NAME)) { 314 files.add(fileName); 315 } 316 } else { 317 if (embeddedArchives!=null) { 318 if (!embeddedArchives.contains(fileName)) { 319 getListOfFiles(list[i], files, null); 320 } 321 } else { 322 getListOfFiles(list[i], files, null); 323 } 324 } 325 } 326 } 327 328 331 public boolean supportsElementsOverwriting() { 332 return true; 333 } 334 335 340 public boolean deleteEntry(String name) { 341 name = name.replace('/', File.separatorChar); 342 File input = new File (archive, name); 343 if (!input.exists()) { 344 return false; 345 } 346 return input.delete(); 347 } 348 349 352 public void closeEntry() throws IOException { 353 if (os!=null) { 354 os.flush(); 355 os.close(); 356 os = null; 357 } 358 } 359 360 public URI getURI() { 361 return archive.toURI(); 362 } 363 364 369 public OutputStream putNextEntry(String name) throws java.io.IOException { 370 name = name.replace('/', File.separatorChar); 371 372 File newFile = new File (archive, name); 373 if (newFile.exists()) { 374 if (!deleteEntry(name)) 375 throw new IOException(name + " already exists and cannot be deleted"); 376 } 377 if (name.lastIndexOf(File.separatorChar)!=-1) { 380 String dirs = name.substring(0, name.lastIndexOf(File.separatorChar)); 381 (new File (archive, dirs)).mkdirs(); 382 } 383 os = new BufferedOutputStream(new FileOutputStream(newFile)); 384 return os; 385 } 386 387 } 388 | Popular Tags |