001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JarTools.java 10491 2007-05-31 11:52:26Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.web.lib;
025:
026: //import java
027: import java.io.File;
028: import java.io.IOException;
029: import java.util.jar.JarFile;
030:
031: import org.objectweb.jonas_lib.files.FileUtils;
032: import org.objectweb.jonas_lib.files.FileUtilsException;
033:
034: import org.objectweb.jonas.common.Log;
035: import org.objectweb.jonas.web.JWebContainerServiceException;
036:
037: import org.objectweb.util.monolog.api.BasicLevel;
038: import org.objectweb.util.monolog.api.Logger;
039:
040: /**
041: * This class provides an implementation of tools methods than permit to unpack
042: * a jar.
043: * @author Ludovic Bert
044: * @author Florent Benoit
045: */
046: public final class JarTools {
047:
048: /**
049: * Logger
050: */
051: private static Logger logger = Log.getLogger(Log.JONAS_WEB_PREFIX);
052:
053: /**
054: * No public constructor : utility class
055: */
056: private JarTools() {
057:
058: }
059:
060: /**
061: * Unpack the specified jar to the specified directory.
062: * @param fileName the name of the archive file to unpack.
063: * @param destDir the destination directory of the unpacking.
064: * @throws JWebContainerServiceException if an error occors during the
065: * unpacking.
066: */
067: public static void unpack(String fileName, String destDir)
068: throws JWebContainerServiceException {
069:
070: boolean doUnpack = false;
071:
072: // Check if the file to unpack exists.
073: File jarFile = new File(fileName);
074: if (!jarFile.exists()) {
075: String err = "Cannot unpack " + fileName
076: + " file does not exist";
077: throw new JWebContainerServiceException(err);
078: }
079:
080: // Check if the destination dir exists.
081: File dest = new File(destDir);
082: if (dest.exists()) {
083: // Check if the destination directory is up to date and unpack it
084: // if necessary.
085: if (dest.lastModified() < jarFile.lastModified()) {
086: if (removeDirectory(dest)) {
087: doUnpack = true;
088: } else {
089: String err = "Cannot unpack " + fileName;
090: err = err
091: + " cannot delete existing unpacked directory";
092: new JWebContainerServiceException(err);
093: }
094: } // else nothing to do the dest dir is up to date.
095: } else {
096: doUnpack = true;
097: }
098:
099: // Unpack if necessary
100: if (doUnpack) {
101: JarFile packedJar = null;
102: try {
103: packedJar = new JarFile(fileName);
104: // redirect to FileUtils
105: FileUtils.unpack(packedJar, dest);
106: } catch (IOException e) {
107: String err = "Error while trying to create JarFile object on '"
108: + fileName + "'";
109: throw new JWebContainerServiceException(err, e);
110: } catch (FileUtilsException e) {
111: String err = "Error while unpacking jar '" + fileName
112: + "'";
113: throw new JWebContainerServiceException(err, e);
114: } finally {
115: try {
116: if (packedJar != null) {
117: packedJar.close();
118: }
119: } catch (IOException ioe) {
120: if (logger.isLoggable(BasicLevel.DEBUG)) {
121: logger.log(BasicLevel.DEBUG,
122: "Cannot close jarfile '" + packedJar
123: + "'.");
124: }
125: }
126: }
127: }
128: }
129:
130: /**
131: * Remove a directory with all its child (recursive remove).
132: * @param file the name of the file or directory to remove.
133: * @return true if only if the directory is removed.
134: */
135: private static boolean removeDirectory(File file) {
136: boolean removeOk = true;
137:
138: //File or directory doesn't exists, exit.
139: if (!file.exists()) {
140: return false;
141: }
142:
143: //Remove the child before the current file(directory)
144: if (file.isDirectory()) {
145: //remove all the children
146: File[] childFiles = file.listFiles();
147: for (int i = 0; i < childFiles.length; i++) {
148: removeOk = removeOk && removeDirectory(childFiles[i]);
149: }
150: }
151: //Since all childs are removed , remove us
152: removeOk = removeOk && file.delete();
153: return removeOk;
154: }
155: }
|