1 17 package org.alfresco.util; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 22 import org.alfresco.error.AlfrescoRuntimeException; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.quartz.Job; 26 import org.quartz.JobExecutionContext; 27 import org.quartz.JobExecutionException; 28 29 35 public class TempFileProvider 36 { 37 38 public static final String ALFRESCO_TEMP_FILE_DIR = "Alfresco"; 39 40 41 public static final String SYSTEM_KEY_TEMP_DIR = "java.io.tmpdir"; 42 43 private static final Log logger = LogFactory.getLog(TempFileProvider.class); 44 45 48 private TempFileProvider() 49 { 50 } 51 52 55 public static File getTempDir() 56 { 57 String systemTempDirPath = System.getProperty(SYSTEM_KEY_TEMP_DIR); 59 if (systemTempDirPath == null) 60 { 61 throw new AlfrescoRuntimeException("System property not available: " + SYSTEM_KEY_TEMP_DIR); 62 } 63 File systemTempDir = new File (systemTempDirPath); 64 File tempDir = new File (systemTempDir, ALFRESCO_TEMP_FILE_DIR); 72 if (tempDir.exists()) 74 { 75 } else 77 { 78 if (!tempDir.mkdirs()) 80 { 81 throw new AlfrescoRuntimeException("Failed to create temp directory: " + tempDir); 82 } 83 if (logger.isDebugEnabled()) 84 { 85 logger.debug("Created temp directory: " + tempDir); 86 } 87 } 88 return tempDir; 90 } 91 92 99 public static File createTempFile(String prefix, String suffix) 100 { 101 File tempDir = TempFileProvider.getTempDir(); 102 return createTempFile(prefix, suffix, tempDir); 104 } 105 106 113 public static File createTempFile(String prefix, String suffix, File directory) 114 { 115 try 116 { 117 File tempFile = File.createTempFile(prefix, suffix, directory); 118 return tempFile; 119 } catch (IOException e) 120 { 121 throw new AlfrescoRuntimeException("Failed to created temp file: \n" + 122 " prefix: " + prefix + "\n" 123 + " suffix: " + suffix + "\n" + 124 " directory: " + directory, 125 e); 126 } 127 } 128 129 140 public static class TempFileCleanerJob implements Job 141 { 142 public static final String KEY_PROTECT_HOURS = "protectHours"; 143 144 148 public void execute(JobExecutionContext context) throws JobExecutionException 149 { 150 String strProtectHours = (String ) context.getJobDetail().getJobDataMap().get(KEY_PROTECT_HOURS); 152 if (strProtectHours == null) 153 { 154 throw new JobExecutionException("Missing job data: " + KEY_PROTECT_HOURS); 155 } 156 int protectHours = -1; 157 try 158 { 159 protectHours = Integer.parseInt(strProtectHours); 160 } 161 catch (NumberFormatException e) 162 { 163 throw new JobExecutionException("Invalid job data " + KEY_PROTECT_HOURS + ": " + strProtectHours); 164 } 165 if (protectHours < 0 || protectHours > 8760) 166 { 167 throw new JobExecutionException("Hours to protect temp files must be 0 <= x <= 8760"); 168 } 169 170 long now = System.currentTimeMillis(); 171 long aFewHoursBack = now - (3600L * 1000L * protectHours); 172 173 File tempDir = TempFileProvider.getTempDir(); 174 int count = removeFiles(tempDir, aFewHoursBack, false); if (logger.isDebugEnabled()) 177 { 178 logger.debug("Removed " + count + " files from temp directory: " + tempDir); 179 } 180 } 181 182 190 public static int removeFiles(long removeBefore) 191 { 192 File tempDir = TempFileProvider.getTempDir(); 193 return removeFiles(tempDir, removeBefore, false); 194 } 195 196 202 private static int removeFiles(File directory, long removeBefore, boolean removeDir) 203 { 204 if (!directory.isDirectory()) 205 { 206 throw new IllegalArgumentException ("Expected a directory to clear: " + directory); 207 } 208 if (!directory.exists()) 210 { 211 return 0; 212 } 213 File [] files = directory.listFiles(); 215 int count = 0; 216 for (File file : files) 217 { 218 if (file.isDirectory()) 219 { 220 removeFiles(file, removeBefore, true); 222 } 223 else 224 { 225 if (file.lastModified() > removeBefore) 227 { 228 continue; 230 } 231 try 233 { 234 file.delete(); 235 count++; 236 } 237 catch (Throwable e) 238 { 239 logger.info("Failed to remove temp file: " + file); 240 } 241 } 242 } 243 if (removeDir && directory.listFiles().length == 0) 245 { 246 try 248 { 249 directory.delete(); 250 } 251 catch (Throwable e) 252 { 253 logger.info("Failed to remove temp directory: " + directory); 254 } 255 } 256 return count; 258 } 259 } 260 } 261 | Popular Tags |