JarTools.java

00001 
00027 package org.objectweb.jonas.web.lib;
00028 
00029 //import java
00030 import java.io.File;
00031 import java.io.IOException;
00032 import java.io.InputStream;
00033 import java.io.FileOutputStream;
00034 import java.util.jar.JarEntry;
00035 import java.util.jar.JarFile;
00036 import java.util.Enumeration;
00037 
00038 //import jonas
00039 import org.objectweb.jonas.web.JWebContainerServiceException;
00040 
00047 public class JarTools {
00048 
00052     private JarTools() {
00053 
00054     }
00055 
00059     private static final int BUFFER_SIZE = 2048;
00060 
00068     public static void unpack(String fileName, String destDir) throws JWebContainerServiceException {
00069 
00070         boolean doUnpack = false;
00071 
00072         // Check if the file to unpack exists.
00073         File jarFile = new File(fileName);
00074         if (!jarFile.exists()) {
00075             String err = "Cannot unpack " + fileName + " file does not exist";
00076             throw new JWebContainerServiceException(err);
00077         }
00078 
00079         // Check if the destination dir exists.
00080         File dest = new File(destDir);
00081         if (dest.exists()) {
00082             // Check if the destination directory is up to date and unpack it
00083             // if necessary.
00084             if (dest.lastModified() < jarFile.lastModified()) {
00085                 if (removeDirectory(dest)) {
00086                     doUnpack = true;
00087                 } else {
00088                     String err = "Cannot unpack " + fileName;
00089                     err = err + " cannot delete existing unpacked directory";
00090                     new JWebContainerServiceException(err);
00091                 }
00092             } // else nothing to do the dest dir is up to date.
00093         } else {
00094             doUnpack = true;
00095         }
00096 
00097         // Unpack if necessary
00098         if (doUnpack) {
00099             JarFile packedJar = null;
00100             JarEntry entry = null;
00101             try {
00102                 try {
00103                     packedJar = new JarFile(fileName);
00104 
00105                     //get entries of the jar file
00106                     Enumeration entries = packedJar.entries();
00107                     while (entries.hasMoreElements()) {
00108                         entry = (JarEntry) entries.nextElement();
00109 
00110                         //File entry
00111                         File entryFile = new File(destDir, entry.getName());
00112 
00113                         //Create directory
00114                         if (entry.isDirectory()) {
00115                             if (!entryFile.exists()) {
00116                                 //create parent directories (with mkdirs)
00117                                 if (!entryFile.mkdirs()) {
00118                                     String err = "Can not create directory " + entryFile + ", Check the write access.";
00119                                     throw new JWebContainerServiceException(err);
00120                                 }
00121                             }
00122                             continue;
00123                         }
00124 
00125                         //If it's a file, we must extract the file
00126                         //Ensure that the directory exists.
00127                         entryFile.getParentFile().mkdirs();
00128 
00129                         InputStream is = null;
00130                         try {
00131                             //get the input stream
00132                             is = packedJar.getInputStream(entry);
00133 
00134                             //Dump to the file
00135                             dump(is, entryFile);
00136                         } finally {
00137                             is.close();
00138                         }
00139                     }
00140                 } finally {
00141                     if (packedJar != null) {
00142                         packedJar.close();
00143                     }
00144                 }
00145             } catch (IOException e) {
00146                 String err = "Error while unpacking entry " + entry + " : ";
00147                 err = err + e.getMessage();
00148                 throw new JWebContainerServiceException(err);
00149             }
00150         }
00151     }
00152 
00159     private static void dump(InputStream in, File entryFile) throws JWebContainerServiceException {
00160 
00161         try {
00162             //File output
00163             FileOutputStream out = new FileOutputStream(entryFile);
00164             int n = 0;
00165             try {
00166                 //buffer
00167                 byte[] buffer = new byte[BUFFER_SIZE];
00168                 n = in.read(buffer);
00169                 while (n > 0) {
00170                     out.write(buffer, 0, n);
00171                     n = in.read(buffer);
00172                 }
00173             } finally {
00174                 out.close();
00175             }
00176         } catch (IOException e) {
00177             String err = "Error while unpacking entry " + entryFile + " : ";
00178             err = err + e.getMessage();
00179             throw new JWebContainerServiceException(err);
00180         }
00181     }
00182 
00188     private static boolean removeDirectory(File file) {
00189         boolean removeOk = true;
00190 
00191         //File or directory doesn't exists, exit.
00192         if (!file.exists()) {
00193             return false;
00194         }
00195 
00196         //Remove the child before the current file(directory)
00197         if (file.isDirectory()) {
00198             //remove all the children
00199             File[] childFiles = file.listFiles();
00200             for (int i = 0; i < childFiles.length; i++) {
00201                 removeOk = removeOk && removeDirectory(childFiles[i]);
00202             }
00203         }
00204         //Since all childs are removed , remove us
00205         removeOk = removeOk && file.delete();
00206         return removeOk;
00207     }
00208 }

Generated on Tue Feb 15 15:05:25 2005 for JOnAS by  doxygen 1.3.9.1