00001
00027 package org.objectweb.jonas_lib.deployment.work;
00028
00029 import java.io.File;
00030 import java.io.FileOutputStream;
00031 import java.io.IOException;
00032 import java.io.InputStream;
00033 import java.io.BufferedInputStream;
00034 import java.io.BufferedOutputStream;
00035 import java.io.FileInputStream;
00036
00037 import java.net.URL;
00038 import java.text.SimpleDateFormat;
00039 import java.util.Date;
00040
00050 public class FileManager {
00051
00055 private static final String TIMESTAMP_FORMAT = "_yyyy.MM.dd-HH.mm.ss";
00056
00060 private static final int BUFFER_SIZE = 2048;
00061
00065 protected FileManager() {
00066
00067 }
00068
00076 public static String fileToTimeStampDir(URL urlFileName) throws FileManagerException {
00077 return fileToTimeStampDir(urlFileName, "");
00078 }
00079
00088 public static String fileToTimeStampDir(URL urlFileName, String ext) throws FileManagerException {
00089
00090
00091 if (!urlFileName.getProtocol().equalsIgnoreCase("file")) {
00092 throw new FileManagerException("Only the file:/ URL can be used");
00093 }
00094
00095 File urlFile = null;
00096 urlFile = new File(urlFileName.getFile());
00097
00098 if (!urlFile.exists()) {
00099 throw new FileManagerException("The file " + urlFileName.getFile() + " was not found.");
00100 }
00101
00102
00103 SimpleDateFormat sdf = new SimpleDateFormat(TIMESTAMP_FORMAT);
00104
00105
00106 long lastModified = urlFile.lastModified();
00107
00108 if (lastModified == 0L) {
00109 throw new FileManagerException("Can't read the last modified time for the file" + urlFile + ".");
00110 }
00111
00112
00113 Date d = new Date(lastModified);
00114
00115
00116 String stReturn = urlFile.getName();
00117
00118 int lastIndex = stReturn.lastIndexOf(".");
00119 if (lastIndex == -1) {
00120 throw new FileManagerException("The specified file " + urlFileName.getFile()
00121 + " is not a file with the format XXX.ear.");
00122 }
00123
00124 stReturn = stReturn.substring(0, lastIndex);
00125
00126 stReturn += sdf.format(d) + ext;
00127
00128 return stReturn;
00129 }
00130
00137 protected static void dump(InputStream in, File earEntryFile) throws FileManagerException {
00138
00139 try {
00140
00141 FileOutputStream out = new FileOutputStream(earEntryFile);
00142 int n = 0;
00143 try {
00144
00145 byte[] buffer = new byte[BUFFER_SIZE];
00146
00147 while ((n = in.read(buffer)) > 0) {
00148 out.write(buffer, 0, n);
00149 }
00150 } finally {
00151 out.close();
00152 }
00153 } catch (IOException e) {
00154 throw new FileManagerException("Error while uncompressing the file " + earEntryFile + ": " + e.getMessage());
00155 }
00156 }
00157
00164 public static void copyFile(String pSrcFilename, String pDstFilename) throws FileManagerException {
00165 try {
00166
00167
00168 File sourceFile = new File(pSrcFilename);
00169 FileInputStream fis = new FileInputStream(sourceFile);
00170 BufferedInputStream bis = new BufferedInputStream(fis);
00171 long l = sourceFile.length();
00172
00173
00174 FileOutputStream fos = new FileOutputStream(pDstFilename);
00175 BufferedOutputStream bos = new BufferedOutputStream(fos);
00176
00177
00178 for (long i = 0; i < l; i++) {
00179 bos.write(bis.read());
00180 }
00181
00182
00183 bos.flush();
00184 bos.close();
00185 bis.close();
00186
00187 } catch (Exception e) {
00188
00189 throw new FileManagerException("Error during copy file : " + pSrcFilename + " -> " + pDstFilename);
00190
00191 }
00192
00193 }
00194 }