001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar 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 3 of the License, or (at your option) any later version. 010 * 011 * Sonar 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 Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.batch.bootstrap; 021 022 import com.google.common.collect.Maps; 023 import org.apache.commons.io.FileUtils; 024 import org.apache.commons.io.filefilter.AgeFileFilter; 025 import org.apache.commons.io.filefilter.AndFileFilter; 026 import org.apache.commons.io.filefilter.DirectoryFileFilter; 027 import org.apache.commons.io.filefilter.PrefixFileFilter; 028 import org.apache.commons.lang.StringUtils; 029 import org.slf4j.LoggerFactory; 030 import org.sonar.api.utils.SonarException; 031 import org.sonar.api.utils.TempFileUtils; 032 033 import java.io.File; 034 import java.io.FileFilter; 035 import java.io.IOException; 036 import java.util.Arrays; 037 import java.util.Map; 038 039 public final class TempDirectories { 040 041 public static final String DIR_PREFIX = "sonar-batch"; 042 043 // this timeout must be greater than the longest analysis 044 public static final int AGE_BEFORE_DELETION = 24 * 60 * 60 * 1000; 045 046 private File rootDir; 047 private Map<String, File> directoriesByKey = Maps.newHashMap(); 048 049 public TempDirectories() throws IOException { 050 this.rootDir = TempFileUtils.createTempDirectory(DIR_PREFIX); 051 LoggerFactory.getLogger(getClass()).debug("Temporary directory: " + rootDir.getAbsolutePath()); 052 } 053 054 public File getRoot() { 055 return rootDir; 056 } 057 058 /** 059 * Get or create a working directory 060 */ 061 public File getDir(String key) { 062 if (StringUtils.isBlank(key)) { 063 return rootDir; 064 } 065 066 File dir = directoriesByKey.get(key); 067 if (dir == null) { 068 dir = new File(rootDir, key); 069 try { 070 FileUtils.forceMkdir(dir); 071 directoriesByKey.put(key, dir); 072 073 } catch (IOException e) { 074 throw new SonarException("Can not create the temp directory: " + dir, e); 075 } 076 } 077 return dir; 078 } 079 080 public File getFile(String directoryKey, String filename) { 081 File dir = getDir(directoryKey); 082 return new File(dir, filename); 083 } 084 085 /** 086 * This method is executed by picocontainer during shutdown. 087 */ 088 public void stop() { 089 directoriesByKey.clear(); 090 091 // Deleting temp directory does not work on MS Windows and Sun JVM because URLClassLoader locks JARs and resources. 092 // The workaround is that sonar deletes orphans itself. 093 094 // older than AGE_BEFORE_DELETION to be sure that the current dir is deleted on mac and linux. 095 rootDir.setLastModified(System.currentTimeMillis() - AGE_BEFORE_DELETION - 60 * 60 * 1000); 096 097 File[] directoriesToDelete = rootDir.getParentFile().listFiles((FileFilter) new AndFileFilter(Arrays.asList( 098 DirectoryFileFilter.DIRECTORY, new PrefixFileFilter(DIR_PREFIX), new AgeFileFilter(System.currentTimeMillis() - AGE_BEFORE_DELETION)))); 099 for (File dir : directoriesToDelete) { 100 LoggerFactory.getLogger(getClass()).debug("Delete temporary directory: " + dir); 101 FileUtils.deleteQuietly(dir); 102 } 103 } 104 }