1 26 27 package org.objectweb.jonas_lib.deployment.work; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.net.URL ; 33 import java.util.Enumeration ; 34 import java.util.jar.JarEntry ; 35 import java.util.jar.JarFile ; 36 37 44 45 public class EarFileManager extends FileManager { 46 47 50 private EarFileManager() { 51 super(); 52 } 53 54 65 protected static boolean isUnpackedEar(URL urlFileName, URL urlDirName) throws FileManagerException { 66 67 File urlFile = new File (urlFileName.getFile()); 68 69 if (!urlFile.exists()) { 70 throw new FileManagerException("File " + urlFileName + "doesn't exist"); 71 } 72 String timeStampDir = fileToTimeStampDir(urlFileName); 73 74 File f = new File (urlDirName.getPath() + File.separator + timeStampDir); 75 76 return f.exists(); 77 } 78 79 80 88 public static URL unpackEar(URL urlFileName, URL urlDirName) throws FileManagerException { 89 return unpackEar(urlFileName, urlDirName, true); 90 } 91 92 101 public static URL unpackEar(URL urlFileName, URL urlDirName, boolean useTimeStamp) throws FileManagerException { 102 103 if ((!urlFileName.getProtocol().equalsIgnoreCase("file")) 105 || (!urlDirName.getProtocol().equalsIgnoreCase("file"))) { 106 throw new FileManagerException("Only the file:/ URL can be used"); 107 } 108 109 if (new File (urlFileName.getFile()).isDirectory()) { 111 return urlFileName; 112 } 113 114 String timeStampDir = fileToTimeStampDir(urlFileName); 116 117 boolean unPacked = false; 118 if (useTimeStamp) { 119 unPacked = isUnpackedEar(urlFileName, urlDirName); 120 } 121 122 JarFile earFile = null; 124 File parentDirectoryFile = null; 125 URL parentDirectoryUrl = null; 126 try { 127 earFile = new JarFile (urlFileName.getFile()); 128 if (useTimeStamp) { 129 parentDirectoryFile = new File (urlDirName.getPath() + File.separator + timeStampDir); 130 } else { 131 String stReturn = new File (urlFileName.getFile()).getName(); 132 String userName = System.getProperty("user.name", "default"); 133 int lastIndex = stReturn.lastIndexOf("."); 135 if (lastIndex == -1) { 136 throw new FileManagerException("The specified file " + urlFileName.getFile() 137 + " is not a file with the format XXX.ear."); 138 } 139 stReturn = stReturn.substring(0, lastIndex); 140 141 parentDirectoryFile = new File (urlDirName.getPath() + File.separator + userName + "_" + stReturn); 142 } 143 parentDirectoryUrl = new URL ("file:" + parentDirectoryFile.getPath()); 146 } catch (IOException e) { 147 throw new FileManagerException("Error while creating file for reading the ear file :" + urlFileName 148 + ": " + e.getMessage()); 149 } 150 151 if (unPacked) { 153 return parentDirectoryUrl; 154 } 155 156 JarEntry earEntry = null; 157 try { 158 try { 159 for (Enumeration earEntries = earFile.entries(); earEntries.hasMoreElements();) { 161 earEntry = (JarEntry ) earEntries.nextElement(); 162 163 File earEntryFile = new File (parentDirectoryFile, earEntry.getName()); 165 166 if (earEntry.isDirectory()) { 168 if (!earEntryFile.exists()) { 169 if (!earEntryFile.mkdirs()) { 171 String err = "Can not create directory " + earEntryFile + ", Check the write access."; 172 throw new FileManagerException(err); 173 } 174 } 175 continue; 176 } 177 178 earEntryFile.getParentFile().mkdirs(); 181 182 InputStream is = null; 183 try { 184 is = earFile.getInputStream(earEntry); 186 187 dump(is, earEntryFile); 189 } finally { 190 is.close(); 191 } 192 } 193 } finally { 194 earFile.close(); 195 } 196 } catch (IOException e) { 197 throw new FileManagerException("Error while uncompressing entry " + earEntry + ": " + e.getMessage()); 198 } 199 return parentDirectoryUrl; 200 } 201 202 } | Popular Tags |