001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 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: FileManager.java 6641 2005-04-25 16:14:13Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_lib.deployment.work;
025:
026: import java.io.File;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.net.URL;
031: import java.text.SimpleDateFormat;
032: import java.util.Date;
033:
034: import org.objectweb.jonas.common.Log;
035:
036: import org.objectweb.util.monolog.api.BasicLevel;
037: import org.objectweb.util.monolog.api.Logger;
038:
039: /**
040: * JOnAS File manager.<br>
041: * This class provides a way for managing the working package files.
042: * @author Florent Benoit
043: */
044:
045: public class FileManager {
046:
047: /**
048: * Format of a timestamp dir.
049: */
050: private static final String TIMESTAMP_FORMAT = "_yyyy.MM.dd-HH.mm.ss";
051:
052: /**
053: * Size of the buffer.
054: */
055: private static final int BUFFER_SIZE = 2048;
056:
057: /**
058: * Logger
059: */
060: private static Logger logger = Log
061: .getLogger(Log.JONAS_DEPLOY_WORK_PREFIX);
062:
063: /**
064: * Constructor. Private as it is an utility class
065: */
066: protected FileManager() {
067:
068: }
069:
070: /**
071: * Give the unpack destination directory of the specified file.
072: * @param urlFileName the url of the name of the EAR file (ends with the
073: * .ear extension).
074: * @return the timestamp destination directory of the specified file.
075: * @throws FileManagerException if we can't get the timestamp.
076: */
077: public static String fileToTimeStampDir(URL urlFileName)
078: throws FileManagerException {
079: return fileToTimeStampDir(urlFileName, "");
080: }
081:
082: /**
083: * Give the unpack destination directory of the specified file.
084: * @param urlFileName the url of the name of the EAR file (ends with the
085: * .ear extension).
086: * @param ext the suffix to concatenate to the build name
087: * @return the timestamp destination directory of the specified file.
088: * @throws FileManagerException if we can't get the timestamp.
089: */
090: public static String fileToTimeStampDir(URL urlFileName, String ext)
091: throws FileManagerException {
092:
093: // check protocol
094: if (!urlFileName.getProtocol().equalsIgnoreCase("file")) {
095: throw new FileManagerException(
096: "Only the file:/ URL can be used");
097: }
098:
099: File urlFile = null;
100: urlFile = new File(urlFileName.getFile());
101:
102: if (!urlFile.exists()) {
103: throw new FileManagerException("The file "
104: + urlFileName.getFile() + " was not found.");
105: }
106:
107: // Create a format
108: SimpleDateFormat sdf = new SimpleDateFormat(TIMESTAMP_FORMAT);
109:
110: // get the date of the Ear file
111: long lastModified = urlFile.lastModified();
112:
113: if (lastModified == 0L) {
114: throw new FileManagerException(
115: "Can't read the last modified time for the file"
116: + urlFile + ".");
117: }
118:
119: // Date
120: Date d = new Date(lastModified);
121:
122: // Return String
123: String stReturn = urlFile.getName();
124: // Remove extension .ear
125: int lastIndex = stReturn.lastIndexOf(".");
126: if (lastIndex == -1) {
127: throw new FileManagerException("The specified file "
128: + urlFileName.getFile()
129: + " is not a file with the format XXX.ear.");
130: }
131:
132: stReturn = stReturn.substring(0, lastIndex);
133:
134: stReturn += sdf.format(d) + ext;
135:
136: return stReturn;
137: }
138:
139: /**
140: * Write an input stream to a given file.
141: * @param in the inputStream.
142: * @param earEntryFile the file where the inputstream must be dumped.
143: * @throws FileManagerException if the dump failed.
144: */
145: protected static void dump(InputStream in, File earEntryFile)
146: throws FileManagerException {
147:
148: try {
149: // File output
150: FileOutputStream out = new FileOutputStream(earEntryFile);
151: int n = 0;
152: try {
153: // buffer
154: byte[] buffer = new byte[BUFFER_SIZE];
155:
156: while ((n = in.read(buffer)) > 0) {
157: out.write(buffer, 0, n);
158: }
159: } finally {
160: out.close();
161: }
162: } catch (IOException e) {
163: String err = "Error while uncompressing the file "
164: + earEntryFile;
165: logger.log(BasicLevel.ERROR, err);
166: throw new FileManagerException(err, e);
167: }
168: }
169:
170: }
|