00001
00027 package org.objectweb.jonas.web.lib;
00028
00029
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
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
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
00080 File dest = new File(destDir);
00081 if (dest.exists()) {
00082
00083
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 }
00093 } else {
00094 doUnpack = true;
00095 }
00096
00097
00098 if (doUnpack) {
00099 JarFile packedJar = null;
00100 JarEntry entry = null;
00101 try {
00102 try {
00103 packedJar = new JarFile(fileName);
00104
00105
00106 Enumeration entries = packedJar.entries();
00107 while (entries.hasMoreElements()) {
00108 entry = (JarEntry) entries.nextElement();
00109
00110
00111 File entryFile = new File(destDir, entry.getName());
00112
00113
00114 if (entry.isDirectory()) {
00115 if (!entryFile.exists()) {
00116
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
00126
00127 entryFile.getParentFile().mkdirs();
00128
00129 InputStream is = null;
00130 try {
00131
00132 is = packedJar.getInputStream(entry);
00133
00134
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
00163 FileOutputStream out = new FileOutputStream(entryFile);
00164 int n = 0;
00165 try {
00166
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
00192 if (!file.exists()) {
00193 return false;
00194 }
00195
00196
00197 if (file.isDirectory()) {
00198
00199 File[] childFiles = file.listFiles();
00200 for (int i = 0; i < childFiles.length; i++) {
00201 removeOk = removeOk && removeDirectory(childFiles[i]);
00202 }
00203 }
00204
00205 removeOk = removeOk && file.delete();
00206 return removeOk;
00207 }
00208 }