KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > deployment > work > EarFileManager


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 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  * Initial developer(s): Florent BENOIT & Ludovic BERT
22  * --------------------------------------------------------------------------
23  * $Id: EarFileManager.java,v 1.2 2004/07/16 13:57:50 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib.deployment.work;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.jar.JarEntry JavaDoc;
35 import java.util.jar.JarFile JavaDoc;
36
37 /**
38  * JOnAS Ear File manager. This class provides a way for managing the EAR files.
39  * @author Florent Benoit
40  * @author Ludovic Bert
41  * @author Nicolas Van Caneghem <nicolas.vancaneghem@openpricer.com>Allow the
42  * deployment of an exploded ear
43  */

44
45 public class EarFileManager extends FileManager {
46
47     /**
48      * Constructor. Private as it is an utility class
49      */

50     private EarFileManager() {
51         super();
52     }
53
54     /**
55      * true If an unpacked directory has the same timestamp than the EAR file,
56      * false otherwise.
57      * @param urlFileName the url of the name of the EAR file (ends with the
58      * .ear extension).
59      * @param urlDirName the url of the directory where the file must be
60      * unpacked.
61      * @return true If an unpacked directory has the same timestamp than the EAR
62      * file, false otherwise.
63      * @throws FileManagerException if the file doesn't exist
64      */

65     protected static boolean isUnpackedEar(URL JavaDoc urlFileName, URL JavaDoc urlDirName) throws FileManagerException {
66
67         File JavaDoc urlFile = new File JavaDoc(urlFileName.getFile());
68
69         if (!urlFile.exists()) {
70             throw new FileManagerException("File " + urlFileName + "doesn't exist");
71         }
72         String JavaDoc timeStampDir = fileToTimeStampDir(urlFileName);
73
74         File JavaDoc f = new File JavaDoc(urlDirName.getPath() + File.separator + timeStampDir);
75
76         return f.exists();
77     }
78
79
80     /**
81      * Unpack the given EAR file to the specified directory.
82      * @param urlFileName the url of the name of the EAR file to unpack.
83      * @param urlDirName the url of the destination directory where is unpacked
84      * the EAR file.
85      * @return the url of the unpacked directory
86      * @throws FileManagerException if we can't unpack the file.
87      */

88     public static URL JavaDoc unpackEar(URL JavaDoc urlFileName, URL JavaDoc urlDirName) throws FileManagerException {
89         return unpackEar(urlFileName, urlDirName, true);
90     }
91
92     /**
93      * Unpack the given EAR file to the specified directory.
94      * @param urlFileName the url of the name of the EAR file to unpack.
95      * @param urlDirName the url of the destination directory where is unpacked
96      * the EAR file.
97      * @param useTimeStamp use timestamping for unpacking the EAR or not
98      * @return the url of the unpacked directory
99      * @throws FileManagerException if we can't unpack the file.
100      */

101     public static URL JavaDoc unpackEar(URL JavaDoc urlFileName, URL JavaDoc urlDirName, boolean useTimeStamp) throws FileManagerException {
102
103         // Check protocol
104
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 ear is exploded
110
if (new File JavaDoc(urlFileName.getFile()).isDirectory()) {
111             return urlFileName;
112         }
113
114         // Get the dirName
115
String JavaDoc timeStampDir = fileToTimeStampDir(urlFileName);
116
117         boolean unPacked = false;
118         if (useTimeStamp) {
119             unPacked = isUnpackedEar(urlFileName, urlDirName);
120         }
121
122         // EAR file and directory
123
JarFile JavaDoc earFile = null;
124         File JavaDoc parentDirectoryFile = null;
125         URL JavaDoc parentDirectoryUrl = null;
126         try {
127             earFile = new JarFile JavaDoc(urlFileName.getFile());
128             if (useTimeStamp) {
129                 parentDirectoryFile = new File JavaDoc(urlDirName.getPath() + File.separator + timeStampDir);
130             } else {
131                 String JavaDoc stReturn = new File JavaDoc(urlFileName.getFile()).getName();
132                 String JavaDoc userName = System.getProperty("user.name", "default");
133                 //Remove extension .ear
134
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 JavaDoc(urlDirName.getPath() + File.separator + userName + "_" + stReturn);
142             }
143             //Protocol file: no .toURL() method because we have a / at the end
144
// of the url otherwise
145
parentDirectoryUrl = new URL JavaDoc("file:" + parentDirectoryFile.getPath());
146         } catch (IOException JavaDoc e) {
147             throw new FileManagerException("Error while creating file for reading the ear file :" + urlFileName
148                     + ": " + e.getMessage());
149         }
150
151         //Nothing to do if it's unpack
152
if (unPacked) {
153             return parentDirectoryUrl;
154         }
155
156         JarEntry JavaDoc earEntry = null;
157         try {
158             try {
159                 //get entries of the EAR file
160
for (Enumeration JavaDoc earEntries = earFile.entries(); earEntries.hasMoreElements();) {
161                     earEntry = (JarEntry JavaDoc) earEntries.nextElement();
162
163                     //File entry
164
File JavaDoc earEntryFile = new File JavaDoc(parentDirectoryFile, earEntry.getName());
165
166                     //Create directory
167
if (earEntry.isDirectory()) {
168                         if (!earEntryFile.exists()) {
169                             //create parent directories (with mkdirs)
170
if (!earEntryFile.mkdirs()) {
171                                 String JavaDoc err = "Can not create directory " + earEntryFile + ", Check the write access.";
172                                 throw new FileManagerException(err);
173                             }
174                         }
175                         continue;
176                     }
177
178                     //If it's a file, we must extract the file
179
//Ensure that the directory exists.
180
earEntryFile.getParentFile().mkdirs();
181
182                     InputStream JavaDoc is = null;
183                     try {
184                         //get the input stream
185
is = earFile.getInputStream(earEntry);
186
187                         //Dump to the file
188
dump(is, earEntryFile);
189                     } finally {
190                         is.close();
191                     }
192                 }
193             } finally {
194                 earFile.close();
195             }
196         } catch (IOException JavaDoc e) {
197             throw new FileManagerException("Error while uncompressing entry " + earEntry + ": " + e.getMessage());
198         }
199         return parentDirectoryUrl;
200     }
201
202 }
Popular Tags