EarFileManager.java

00001 
00027 package org.objectweb.jonas_lib.deployment.work;
00028 
00029 import java.io.File;
00030 import java.io.IOException;
00031 import java.io.InputStream;
00032 import java.net.URL;
00033 import java.util.Enumeration;
00034 import java.util.jar.JarEntry;
00035 import java.util.jar.JarFile;
00036 
00045 public class EarFileManager extends FileManager {
00046 
00050     private EarFileManager() {
00051         super();
00052     }
00053 
00065     protected static boolean isUnpackedEar(URL urlFileName, URL urlDirName) throws FileManagerException {
00066 
00067         File urlFile = new File(urlFileName.getFile());
00068 
00069         if (!urlFile.exists()) {
00070             throw new FileManagerException("File " + urlFileName + "doesn't exist");
00071         }
00072         String timeStampDir = fileToTimeStampDir(urlFileName);
00073 
00074         File f = new File(urlDirName.getPath() + File.separator + timeStampDir);
00075 
00076         return f.exists();
00077     }
00078 
00079 
00088     public static URL unpackEar(URL urlFileName, URL urlDirName) throws FileManagerException {
00089         return unpackEar(urlFileName, urlDirName, true);
00090     }
00091 
00101     public static URL unpackEar(URL urlFileName, URL urlDirName, boolean useTimeStamp) throws FileManagerException {
00102 
00103         // Check protocol
00104         if ((!urlFileName.getProtocol().equalsIgnoreCase("file"))
00105                 || (!urlDirName.getProtocol().equalsIgnoreCase("file"))) {
00106             throw new FileManagerException("Only the file:/ URL can be used");
00107         }
00108 
00109         // if ear is exploded
00110         if (new File(urlFileName.getFile()).isDirectory()) {
00111             return urlFileName;
00112         }
00113 
00114         // Get the dirName
00115         String timeStampDir = fileToTimeStampDir(urlFileName);
00116 
00117         boolean unPacked = false;
00118         if (useTimeStamp) {
00119             unPacked = isUnpackedEar(urlFileName, urlDirName);
00120         }
00121 
00122         // EAR file and directory
00123         JarFile earFile = null;
00124         File parentDirectoryFile = null;
00125         URL parentDirectoryUrl = null;
00126         try {
00127             earFile = new JarFile(urlFileName.getFile());
00128             if (useTimeStamp) {
00129                 parentDirectoryFile = new File(urlDirName.getPath() + File.separator + timeStampDir);
00130             } else {
00131                 String stReturn = new File(urlFileName.getFile()).getName();
00132                 String userName = System.getProperty("user.name", "default");
00133                 //Remove extension .ear
00134                 int lastIndex = stReturn.lastIndexOf(".");
00135                 if (lastIndex == -1) {
00136                     throw new FileManagerException("The specified file " + urlFileName.getFile()
00137                             + " is not a file with the format XXX.ear.");
00138                 }
00139                 stReturn = stReturn.substring(0, lastIndex);
00140 
00141                 parentDirectoryFile = new File(urlDirName.getPath() + File.separator + userName + "_" + stReturn);
00142             }
00143             //Protocol file: no .toURL() method because we have a / at the end
00144             // of the url otherwise
00145             parentDirectoryUrl = new URL("file:" + parentDirectoryFile.getPath());
00146         } catch (IOException e) {
00147             throw new FileManagerException("Error while creating file for reading the ear file :" + urlFileName
00148                     + ": " + e.getMessage());
00149         }
00150 
00151         //Nothing to do if it's unpack
00152         if (unPacked) {
00153             return parentDirectoryUrl;
00154         }
00155 
00156         JarEntry earEntry = null;
00157         try {
00158             try {
00159                 //get entries of the EAR file
00160                 for (Enumeration earEntries = earFile.entries(); earEntries.hasMoreElements();) {
00161                     earEntry = (JarEntry) earEntries.nextElement();
00162 
00163                     //File entry
00164                     File earEntryFile = new File(parentDirectoryFile, earEntry.getName());
00165 
00166                     //Create directory
00167                     if (earEntry.isDirectory()) {
00168                         if (!earEntryFile.exists()) {
00169                             //create parent directories (with mkdirs)
00170                             if (!earEntryFile.mkdirs()) {
00171                                 String err = "Can not create directory " + earEntryFile + ", Check the write access.";
00172                                 throw new FileManagerException(err);
00173                             }
00174                         }
00175                         continue;
00176                     }
00177 
00178                     //If it's a file, we must extract the file
00179                     //Ensure that the directory exists.
00180                     earEntryFile.getParentFile().mkdirs();
00181 
00182                     InputStream is = null;
00183                     try {
00184                         //get the input stream
00185                         is = earFile.getInputStream(earEntry);
00186 
00187                         //Dump to the file
00188                         dump(is, earEntryFile);
00189                     } finally {
00190                         is.close();
00191                     }
00192                 }
00193             } finally {
00194                 earFile.close();
00195             }
00196         } catch (IOException e) {
00197             throw new FileManagerException("Error while uncompressing entry " + earEntry + ": " + e.getMessage());
00198         }
00199         return parentDirectoryUrl;
00200     }
00201 
00202 }

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