You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TempCleaning.java 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * SonarQube Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * sonarqube@googlegroups.com
  5. *
  6. * This program 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 3 of the License, or (at your option) any later version.
  10. *
  11. * This program 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 program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner.impl;
  21. import java.io.File;
  22. import java.util.Collection;
  23. import org.apache.commons.io.FileUtils;
  24. import org.apache.commons.io.filefilter.AgeFileFilter;
  25. import org.apache.commons.io.filefilter.AndFileFilter;
  26. import org.apache.commons.io.filefilter.PrefixFileFilter;
  27. import org.sonar.home.cache.Logger;
  28. /**
  29. * The file sonar-runner-batch.jar is locked by the classloader on Windows and can't be dropped at the end of the execution.
  30. * See {@link IsolatedLauncherFactory}
  31. */
  32. class TempCleaning {
  33. static final int ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;
  34. final File tempDir;
  35. private final Logger logger;
  36. TempCleaning(Logger logger) {
  37. this(new File(System.getProperty("java.io.tmpdir")), logger);
  38. }
  39. /**
  40. * For unit tests
  41. */
  42. TempCleaning(File tempDir, Logger logger) {
  43. this.logger = logger;
  44. this.tempDir = tempDir;
  45. }
  46. void clean() {
  47. logger.debug("Start temp cleaning...");
  48. long cutoff = System.currentTimeMillis() - ONE_DAY_IN_MILLISECONDS;
  49. Collection<File> files = FileUtils.listFiles(tempDir, new AndFileFilter(
  50. new PrefixFileFilter("sonar-runner-batch"),
  51. new AgeFileFilter(cutoff)), null);
  52. for (File file : files) {
  53. FileUtils.deleteQuietly(file);
  54. }
  55. logger.debug("Temp cleaning done");
  56. }
  57. }