1 25 26 package org.objectweb.jonas.web.lib; 27 28 import java.io.File ; 30 import java.io.IOException ; 31 import java.util.jar.JarFile ; 32 33 import org.objectweb.jonas_lib.files.FileUtils; 34 import org.objectweb.jonas_lib.files.FileUtilsException; 35 36 import org.objectweb.jonas.common.Log; 37 import org.objectweb.jonas.web.JWebContainerServiceException; 38 39 import org.objectweb.util.monolog.api.BasicLevel; 40 import org.objectweb.util.monolog.api.Logger; 41 42 48 public final class JarTools { 49 50 53 private static Logger logger = Log.getLogger(Log.JONAS_WEB_PREFIX); 54 55 58 private JarTools() { 59 60 } 61 62 69 public static void unpack(String fileName, String destDir) throws JWebContainerServiceException { 70 71 boolean doUnpack = false; 72 73 File jarFile = new File (fileName); 75 if (!jarFile.exists()) { 76 String err = "Cannot unpack " + fileName + " file does not exist"; 77 throw new JWebContainerServiceException(err); 78 } 79 80 File dest = new File (destDir); 82 if (dest.exists()) { 83 if (dest.lastModified() < jarFile.lastModified()) { 86 if (removeDirectory(dest)) { 87 doUnpack = true; 88 } else { 89 String err = "Cannot unpack " + fileName; 90 err = err + " cannot delete existing unpacked directory"; 91 new JWebContainerServiceException(err); 92 } 93 } } else { 95 doUnpack = true; 96 } 97 98 if (doUnpack) { 100 JarFile packedJar = null; 101 try { 102 packedJar = new JarFile (fileName); 103 FileUtils.unpack(packedJar, dest); 105 } catch (IOException e) { 106 String err = "Error while trying to create JarFile object on '" + fileName + "'"; 107 throw new JWebContainerServiceException(err, e); 108 } catch (FileUtilsException e) { 109 String err = "Error while unpacking jar '" + fileName + "'"; 110 throw new JWebContainerServiceException(err, e); 111 } finally { 112 try { 113 packedJar.close(); 114 } catch (IOException ioe) { 115 if (logger.isLoggable(BasicLevel.DEBUG)) { 116 logger.log(BasicLevel.DEBUG, "Cannot close jarfile '" + packedJar + "'."); 117 } 118 } 119 } 120 } 121 } 122 123 124 125 130 private static boolean removeDirectory(File file) { 131 boolean removeOk = true; 132 133 if (!file.exists()) { 135 return false; 136 } 137 138 if (file.isDirectory()) { 140 File [] childFiles = file.listFiles(); 142 for (int i = 0; i < childFiles.length; i++) { 143 removeOk = removeOk && removeDirectory(childFiles[i]); 144 } 145 } 146 removeOk = removeOk && file.delete(); 148 return removeOk; 149 } 150 } 151 | Popular Tags |