FileDocCategorySizeDatePackage
FileArchive.javaAPI DocGlassfish v2 API13112Mon Jul 09 10:30:02 BST 2007com.sun.enterprise.deployment.deploy.shared

FileArchive

public class FileArchive extends AbstractArchive
This implementation of the AbstractArchive interface maps to a directory/file structure.
author
Jerome Dochez

Fields Summary
File
archive
String
path
OutputStream
os
Constructors Summary
public FileArchive()
Creates a new instance of FileArchive

    
           
      
    
Methods Summary
public voidclose()
close the abstract archive

        // nothing to do 
    
public voidcloseEntry()
Closes the current entry

        if (os!=null) {
            os.flush();
            os.close();
            os = null;
        }
    
public voidcloseEntry(AbstractArchive os)
close a previously returned @see java.io.OutputStream returned by an addEntry call

param
the output stream to close

        os.close();
    
public voidcreate(java.lang.String path)
creates a new abstract archive with the given path

param
the path to create the archive

    
        this.path = path.replace('/", File.separatorChar);
        archive = new File(path);
        archive.mkdirs();
    
public booleandelete()
delete the archive

        // delete the directory structure...
        try {
            return deleteDir(archive);
        } catch (IOException e) {
            return false;
        }
    
private booleandeleteDir(java.io.File directory)
utility method for deleting a directory and all its content

        if (!directory.isDirectory()) {
            throw new FileNotFoundException(directory.getPath());
        }
        
        // delete contents
        File[] entries = directory.listFiles();
        for (int i=0;i<entries.length;i++) {
            if (entries[i].isDirectory()) {
                deleteDir(entries[i]);
            } else {
                FileUtils.deleteFile(entries[i]);
            }
        }
        // delete self
        return FileUtils.deleteFile(directory);
    
public booleandeleteEntry(java.lang.String name)
delete an entry in the archive

param
the entry name
return
true if the entry was successfully deleted

        name = name.replace('/", File.separatorChar);
        File input = new File(archive, name);
        if (!input.exists()) {
            return false;
        }
        return input.delete();
    
public java.util.Enumerationentries(java.lang.String prefix)
Returns an enumeration of the module file entries with the specified prefix. All elements in the enumeration are of type String. Each String represents a file name relative to the root of the module.

param
prefix the prefix of entries to be included
return
an enumeration of the archive file entries.

        prefix = prefix.replace('/", File.separatorChar);
        File file = new File(archive, prefix);
        Vector namesList = new Vector();
        getListOfFiles(file, namesList, null);
        return namesList.elements();
    
public java.util.Enumerationentries()

return
an @see java.util.Enumeration of entries in this abstract archive

        Vector namesList = new Vector();
        getListOfFiles(archive, namesList, null);
        return namesList.elements();
    
public java.util.Enumerationentries(java.util.Enumeration embeddedArchives)

return
an @see java.util.Enumeration of entries in this abstract archive, providing the list of embedded archive to not count their entries as part of this archive

     	Vector nameList = new Vector();
        List massagedNames = new ArrayList();
	while (embeddedArchives.hasMoreElements()) {
		String subArchiveName  = (String) embeddedArchives.nextElement();
		massagedNames.add(FileUtils.makeFriendlyFileName(subArchiveName));
	}        
     	getListOfFiles(archive, nameList, massagedNames);
     	return nameList.elements();
     
public booleanexists()

return
true if this archive exists

        return archive.exists();
    
public longgetArchiveSize()
Get the size of the archive

return
tje the size of this archive or -1 on error

        if(getArchiveUri() == null) {
            return -1;
        }
        File tmpFile = new File(getArchiveUri());
        return(tmpFile.length());
    
public java.lang.StringgetArchiveUri()

return
the archive uri

        return path;
    
public AbstractArchivegetEmbeddedArchive(java.lang.String name)
create or obtain an embedded archive within this abstraction.

param
the name of the embedded archive.

       // Convert name to native form. See bug #6345029 for more details.
       name = name.replace('/", File.separatorChar);
       File file = new File(name);
       File subDir;
       if (file.isAbsolute()) {
           subDir = file;
       } else {
           // first we try to see if a sub directory with the right file
           // name exist
           subDir = new File(archive, FileUtils.makeFriendlyFileName(name));
       	   if (!subDir.exists()) {       	  
               // now we try to open a sub jar file...
               subDir = new File(archive, name);
               if (!subDir.exists()) {
                   // ok, nothing worked, reassing the name to the 
                   // sub directory one
                  subDir = new File(archive, FileUtils.makeFriendlyFileName(name));
              }                  
       	   }
       }
       String subName = subDir.getPath();
       if (!subDir.exists()) {
           // time to create a new sub directory
           File newDir = new File(subName);
           newDir.mkdirs();           
       }
       AbstractArchive sub;
       if (subDir.isDirectory()) {
       	    sub = new FileArchive();
            ((FileArchive) sub).open(subName);
       } else {
       	    sub = new InputJarArchive();
       	    ((InputJarArchive) sub).open(subName);
       }
       return sub;
    
public java.io.InputStreamgetEntry(java.lang.String name)

return
a @see java.io.InputStream for an existing entry in the current abstract archive
param
the entry name

            
        name = name.replace('/", File.separatorChar);
        File input = new File(archive, name);
        if (!input.exists() || input.isDirectory()) {
            return null;
        }
        FileInputStream fis = new FileInputStream(input);
        try {
            BufferedInputStream bis = new BufferedInputStream(fis);
            return bis;
        } catch (Throwable tx) {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Throwable thr) {
                    IOException ioe = new IOException("Error closing FileInputStream after error opening BufferedInputStream for entry " + name);
                    ioe.initCause(thr);
                    throw ioe;
                }
            }
            IOException ioe = new IOException("Error opening BufferedInputStream for entry " + name);
            ioe.initCause(tx);
            throw ioe;
        }
    
private voidgetListOfFiles(java.io.File directory, java.util.Vector files, java.util.List embeddedArchives)
utility method for getting contents of directory and sub directories

        File[] list = directory.listFiles();
        if (list == null) {
            return;
        }
        for (int i=0;i<list.length;i++) {
	    String fileName = list[i].getAbsolutePath().substring(archive.getAbsolutePath().length()+1);
            if (!list[i].isDirectory()) {                
                fileName = fileName.replace(File.separatorChar, '/");
                if (!fileName.equals(JarFile.MANIFEST_NAME)) {
                    files.add(fileName);
                }
            } else {
		if (embeddedArchives!=null) {
			if (!embeddedArchives.contains(fileName)) {
				getListOfFiles(list[i], files, null);
			}            		
		} else {
	                getListOfFiles(list[i], files, null);
		}
            }
        }
    
public java.util.jar.ManifestgetManifest()

return
the manifest information for this abstract archive

        InputStream is = null;
        try {
            is = getEntry(JarFile.MANIFEST_NAME);
            if (is!=null) {
                Manifest m = new Manifest(is);
                return m;
            }
        } finally {
            if (is != null) {
                is.close();
            }
        }
        return null;
    
public java.net.URIgetURI()

        return archive.toURI();
    
public voidopen(java.lang.String path)
Open an abstract archive

param
the path to the archive

    
        this.path = path.replace('/", File.separatorChar);
        archive = new File(path);
        if (!archive.exists()) {
            throw new FileNotFoundException(path);
        }
    
public java.io.OutputStreamputNextEntry(java.lang.String name)

returns
an @see java.io.OutputStream for a new entry in this current abstract archive.
param
the entry name

        name = name.replace('/", File.separatorChar);
        
        File newFile = new File(archive, name);
        if (newFile.exists()) {
            if (!deleteEntry(name))
                throw new IOException(name + " already exists and cannot be deleted");
        }
        // if the entry name contains directory structure, we need
        // to create those directories first.
        if (name.lastIndexOf(File.separatorChar)!=-1) {            
            String dirs = name.substring(0, name.lastIndexOf(File.separatorChar));            
            (new File(archive, dirs)).mkdirs();
        }
        os = new BufferedOutputStream(new FileOutputStream(newFile));
        return os;   
    
public booleanrenameTo(java.lang.String name)
rename the archive

param
name the archive name

        return FileUtils.renameFile(archive, new File(name));
    
public booleansupportsElementsOverwriting()

return
true if this archive abstraction supports overwriting of elements

        return true;