1 25 26 package org.objectweb.jonas_lib.deployment.work; 27 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.net.URL ; 33 import java.text.SimpleDateFormat ; 34 import java.util.Date ; 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 46 47 public class FileManager { 48 49 52 private static final String TIMESTAMP_FORMAT = "_yyyy.MM.dd-HH.mm.ss"; 53 54 57 private static final int BUFFER_SIZE = 2048; 58 59 62 private static Logger logger = Log.getLogger(Log.JONAS_DEPLOY_WORK_PREFIX); 63 64 67 protected FileManager() { 68 69 } 70 71 78 public static String fileToTimeStampDir(URL urlFileName) throws FileManagerException { 79 return fileToTimeStampDir(urlFileName, ""); 80 } 81 82 90 public static String fileToTimeStampDir(URL urlFileName, String ext) throws FileManagerException { 91 92 if (!urlFileName.getProtocol().equalsIgnoreCase("file")) { 94 throw new FileManagerException("Only the file:/ URL can be used"); 95 } 96 97 File urlFile = null; 98 urlFile = new File (urlFileName.getFile()); 99 100 if (!urlFile.exists()) { 101 throw new FileManagerException("The file " + urlFileName.getFile() + " was not found."); 102 } 103 104 SimpleDateFormat sdf = new SimpleDateFormat (TIMESTAMP_FORMAT); 106 107 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 d = new Date (lastModified); 116 117 String stReturn = urlFile.getName(); 119 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 139 protected static void dump(InputStream in, File earEntryFile) throws FileManagerException { 140 141 try { 142 FileOutputStream out = new FileOutputStream (earEntryFile); 144 int n = 0; 145 try { 146 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 e) { 156 String 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 |