KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > web > lib > JarTools


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JarTools.java,v 1.9 2005/05/05 16:09:14 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.web.lib;
27
28 //import java
29
import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.jar.JarFile JavaDoc;
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 /**
43  * This class provides an implementation of tools methods than permit to unpack
44  * a jar.
45  * @author Ludovic Bert
46  * @author Florent Benoit
47  */

48 public final class JarTools {
49
50     /**
51      * Logger
52      */

53     private static Logger logger = Log.getLogger(Log.JONAS_WEB_PREFIX);
54
55     /**
56      * No public constructor : utility class
57      */

58     private JarTools() {
59
60     }
61
62     /**
63      * Unpack the specified jar to the specified directory.
64      * @param fileName the name of the archive file to unpack.
65      * @param destDir the destination directory of the unpacking.
66      * @throws JWebContainerServiceException if an error occors during the
67      * unpacking.
68      */

69     public static void unpack(String JavaDoc fileName, String JavaDoc destDir) throws JWebContainerServiceException {
70
71         boolean doUnpack = false;
72
73         // Check if the file to unpack exists.
74
File JavaDoc jarFile = new File JavaDoc(fileName);
75         if (!jarFile.exists()) {
76             String JavaDoc err = "Cannot unpack " + fileName + " file does not exist";
77             throw new JWebContainerServiceException(err);
78         }
79
80         // Check if the destination dir exists.
81
File JavaDoc dest = new File JavaDoc(destDir);
82         if (dest.exists()) {
83             // Check if the destination directory is up to date and unpack it
84
// if necessary.
85
if (dest.lastModified() < jarFile.lastModified()) {
86                 if (removeDirectory(dest)) {
87                     doUnpack = true;
88                 } else {
89                     String JavaDoc err = "Cannot unpack " + fileName;
90                     err = err + " cannot delete existing unpacked directory";
91                     new JWebContainerServiceException(err);
92                 }
93             } // else nothing to do the dest dir is up to date.
94
} else {
95             doUnpack = true;
96         }
97
98         // Unpack if necessary
99
if (doUnpack) {
100             JarFile JavaDoc packedJar = null;
101             try {
102                 packedJar = new JarFile JavaDoc(fileName);
103                 // redirect to FileUtils
104
FileUtils.unpack(packedJar, dest);
105             } catch (IOException JavaDoc e) {
106                 String JavaDoc err = "Error while trying to create JarFile object on '" + fileName + "'";
107                 throw new JWebContainerServiceException(err, e);
108             } catch (FileUtilsException e) {
109                 String JavaDoc err = "Error while unpacking jar '" + fileName + "'";
110                 throw new JWebContainerServiceException(err, e);
111             } finally {
112                 try {
113                     packedJar.close();
114                 } catch (IOException JavaDoc 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     /**
126      * Remove a directory with all its child (recursive remove).
127      * @param file the name of the file or directory to remove.
128      * @return true if only if the directory is removed.
129      */

130     private static boolean removeDirectory(File JavaDoc file) {
131         boolean removeOk = true;
132
133         //File or directory doesn't exists, exit.
134
if (!file.exists()) {
135             return false;
136         }
137
138         //Remove the child before the current file(directory)
139
if (file.isDirectory()) {
140             //remove all the children
141
File JavaDoc[] childFiles = file.listFiles();
142             for (int i = 0; i < childFiles.length; i++) {
143                 removeOk = removeOk && removeDirectory(childFiles[i]);
144             }
145         }
146         //Since all childs are removed , remove us
147
removeOk = removeOk && file.delete();
148         return removeOk;
149     }
150 }
151
Popular Tags