001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 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: * Initial developer(s): Florent BENOIT & Ludovic BERT
022: * --------------------------------------------------------------------------
023: * $Id: EarFileManager.java 5141 2004-07-16 13:57:50Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_lib.deployment.work;
026:
027: import java.io.File;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.net.URL;
031: import java.util.Enumeration;
032: import java.util.jar.JarEntry;
033: import java.util.jar.JarFile;
034:
035: /**
036: * JOnAS Ear File manager. This class provides a way for managing the EAR files.
037: * @author Florent Benoit
038: * @author Ludovic Bert
039: * @author Nicolas Van Caneghem <nicolas.vancaneghem@openpricer.com>Allow the
040: * deployment of an exploded ear
041: */
042:
043: public class EarFileManager extends FileManager {
044:
045: /**
046: * Constructor. Private as it is an utility class
047: */
048: private EarFileManager() {
049: super ();
050: }
051:
052: /**
053: * true If an unpacked directory has the same timestamp than the EAR file,
054: * false otherwise.
055: * @param urlFileName the url of the name of the EAR file (ends with the
056: * .ear extension).
057: * @param urlDirName the url of the directory where the file must be
058: * unpacked.
059: * @return true If an unpacked directory has the same timestamp than the EAR
060: * file, false otherwise.
061: * @throws FileManagerException if the file doesn't exist
062: */
063: protected static boolean isUnpackedEar(URL urlFileName,
064: URL urlDirName) throws FileManagerException {
065:
066: File urlFile = new File(urlFileName.getFile());
067:
068: if (!urlFile.exists()) {
069: throw new FileManagerException("File " + urlFileName
070: + "doesn't exist");
071: }
072: String timeStampDir = fileToTimeStampDir(urlFileName);
073:
074: File f = new File(urlDirName.getPath() + File.separator
075: + timeStampDir);
076:
077: return f.exists();
078: }
079:
080: /**
081: * Unpack the given EAR file to the specified directory.
082: * @param urlFileName the url of the name of the EAR file to unpack.
083: * @param urlDirName the url of the destination directory where is unpacked
084: * the EAR file.
085: * @return the url of the unpacked directory
086: * @throws FileManagerException if we can't unpack the file.
087: */
088: public static URL unpackEar(URL urlFileName, URL urlDirName)
089: throws FileManagerException {
090: return unpackEar(urlFileName, urlDirName, true);
091: }
092:
093: /**
094: * Unpack the given EAR file to the specified directory.
095: * @param urlFileName the url of the name of the EAR file to unpack.
096: * @param urlDirName the url of the destination directory where is unpacked
097: * the EAR file.
098: * @param useTimeStamp use timestamping for unpacking the EAR or not
099: * @return the url of the unpacked directory
100: * @throws FileManagerException if we can't unpack the file.
101: */
102: public static URL unpackEar(URL urlFileName, URL urlDirName,
103: boolean useTimeStamp) throws FileManagerException {
104:
105: // Check protocol
106: if ((!urlFileName.getProtocol().equalsIgnoreCase("file"))
107: || (!urlDirName.getProtocol().equalsIgnoreCase("file"))) {
108: throw new FileManagerException(
109: "Only the file:/ URL can be used");
110: }
111:
112: // if ear is exploded
113: if (new File(urlFileName.getFile()).isDirectory()) {
114: return urlFileName;
115: }
116:
117: // Get the dirName
118: String timeStampDir = fileToTimeStampDir(urlFileName);
119:
120: boolean unPacked = false;
121: if (useTimeStamp) {
122: unPacked = isUnpackedEar(urlFileName, urlDirName);
123: }
124:
125: // EAR file and directory
126: JarFile earFile = null;
127: File parentDirectoryFile = null;
128: URL parentDirectoryUrl = null;
129: try {
130: earFile = new JarFile(urlFileName.getFile());
131: if (useTimeStamp) {
132: parentDirectoryFile = new File(urlDirName.getPath()
133: + File.separator + timeStampDir);
134: } else {
135: String stReturn = new File(urlFileName.getFile())
136: .getName();
137: String userName = System.getProperty("user.name",
138: "default");
139: //Remove extension .ear
140: int lastIndex = stReturn.lastIndexOf(".");
141: if (lastIndex == -1) {
142: throw new FileManagerException(
143: "The specified file "
144: + urlFileName.getFile()
145: + " is not a file with the format XXX.ear.");
146: }
147: stReturn = stReturn.substring(0, lastIndex);
148:
149: parentDirectoryFile = new File(urlDirName.getPath()
150: + File.separator + userName + "_" + stReturn);
151: }
152: //Protocol file: no .toURL() method because we have a / at the end
153: // of the url otherwise
154: parentDirectoryUrl = new URL("file:"
155: + parentDirectoryFile.getPath());
156: } catch (IOException e) {
157: throw new FileManagerException(
158: "Error while creating file for reading the ear file :"
159: + urlFileName + ": " + e.getMessage());
160: }
161:
162: //Nothing to do if it's unpack
163: if (unPacked) {
164: return parentDirectoryUrl;
165: }
166:
167: JarEntry earEntry = null;
168: try {
169: try {
170: //get entries of the EAR file
171: for (Enumeration earEntries = earFile.entries(); earEntries
172: .hasMoreElements();) {
173: earEntry = (JarEntry) earEntries.nextElement();
174:
175: //File entry
176: File earEntryFile = new File(parentDirectoryFile,
177: earEntry.getName());
178:
179: //Create directory
180: if (earEntry.isDirectory()) {
181: if (!earEntryFile.exists()) {
182: //create parent directories (with mkdirs)
183: if (!earEntryFile.mkdirs()) {
184: String err = "Can not create directory "
185: + earEntryFile
186: + ", Check the write access.";
187: throw new FileManagerException(err);
188: }
189: }
190: continue;
191: }
192:
193: //If it's a file, we must extract the file
194: //Ensure that the directory exists.
195: earEntryFile.getParentFile().mkdirs();
196:
197: InputStream is = null;
198: try {
199: //get the input stream
200: is = earFile.getInputStream(earEntry);
201:
202: //Dump to the file
203: dump(is, earEntryFile);
204: } finally {
205: is.close();
206: }
207: }
208: } finally {
209: earFile.close();
210: }
211: } catch (IOException e) {
212: throw new FileManagerException(
213: "Error while uncompressing entry " + earEntry
214: + ": " + e.getMessage());
215: }
216: return parentDirectoryUrl;
217: }
218:
219: }
|