KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 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: FileManager.java,v 1.5 2005/04/25 16:14:13 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_lib.deployment.work;
27
28 import java.io.File JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.text.SimpleDateFormat JavaDoc;
34 import java.util.Date JavaDoc;
35
36 import org.objectweb.jonas.common.Log;
37
38 import org.objectweb.util.monolog.api.BasicLevel;
39 import org.objectweb.util.monolog.api.Logger;
40
41 /**
42  * JOnAS File manager.<br>
43  * This class provides a way for managing the working package files.
44  * @author Florent Benoit
45  */

46
47 public class FileManager {
48
49     /**
50      * Format of a timestamp dir.
51      */

52     private static final String JavaDoc TIMESTAMP_FORMAT = "_yyyy.MM.dd-HH.mm.ss";
53
54     /**
55      * Size of the buffer.
56      */

57     private static final int BUFFER_SIZE = 2048;
58
59     /**
60      * Logger
61      */

62     private static Logger logger = Log.getLogger(Log.JONAS_DEPLOY_WORK_PREFIX);
63
64     /**
65      * Constructor. Private as it is an utility class
66      */

67     protected FileManager() {
68
69     }
70
71     /**
72      * Give the unpack destination directory of the specified file.
73      * @param urlFileName the url of the name of the EAR file (ends with the
74      * .ear extension).
75      * @return the timestamp destination directory of the specified file.
76      * @throws FileManagerException if we can't get the timestamp.
77      */

78     public static String JavaDoc fileToTimeStampDir(URL JavaDoc urlFileName) throws FileManagerException {
79         return fileToTimeStampDir(urlFileName, "");
80     }
81
82     /**
83      * Give the unpack destination directory of the specified file.
84      * @param urlFileName the url of the name of the EAR file (ends with the
85      * .ear extension).
86      * @param ext the suffix to concatenate to the build name
87      * @return the timestamp destination directory of the specified file.
88      * @throws FileManagerException if we can't get the timestamp.
89      */

90     public static String JavaDoc fileToTimeStampDir(URL JavaDoc urlFileName, String JavaDoc ext) throws FileManagerException {
91
92         // check protocol
93
if (!urlFileName.getProtocol().equalsIgnoreCase("file")) {
94             throw new FileManagerException("Only the file:/ URL can be used");
95         }
96
97         File JavaDoc urlFile = null;
98         urlFile = new File JavaDoc(urlFileName.getFile());
99
100         if (!urlFile.exists()) {
101             throw new FileManagerException("The file " + urlFileName.getFile() + " was not found.");
102         }
103
104         // Create a format
105
SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(TIMESTAMP_FORMAT);
106
107         // get the date of the Ear file
108
long lastModified = urlFile.lastModified();
109
110         if (lastModified == 0L) {
111             throw new FileManagerException("Can't read the last modified time for the file" + urlFile + ".");
112         }
113
114         // Date
115
Date JavaDoc d = new Date JavaDoc(lastModified);
116
117         // Return String
118
String JavaDoc stReturn = urlFile.getName();
119         // Remove extension .ear
120
int lastIndex = stReturn.lastIndexOf(".");
121         if (lastIndex == -1) {
122             throw new FileManagerException("The specified file " + urlFileName.getFile()
123                     + " is not a file with the format XXX.ear.");
124         }
125
126         stReturn = stReturn.substring(0, lastIndex);
127
128         stReturn += sdf.format(d) + ext;
129
130         return stReturn;
131     }
132
133     /**
134      * Write an input stream to a given file.
135      * @param in the inputStream.
136      * @param earEntryFile the file where the inputstream must be dumped.
137      * @throws FileManagerException if the dump failed.
138      */

139     protected static void dump(InputStream JavaDoc in, File JavaDoc earEntryFile) throws FileManagerException {
140
141         try {
142             // File output
143
FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(earEntryFile);
144             int n = 0;
145             try {
146                 // buffer
147
byte[] buffer = new byte[BUFFER_SIZE];
148
149                 while ((n = in.read(buffer)) > 0) {
150                     out.write(buffer, 0, n);
151                 }
152             } finally {
153                 out.close();
154             }
155         } catch (IOException JavaDoc e) {
156             String JavaDoc err = "Error while uncompressing the file " + earEntryFile;
157             logger.log(BasicLevel.ERROR, err);
158             throw new FileManagerException(err, e);
159         }
160     }
161
162     }
Popular Tags