aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-home/src
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2013-11-26 15:28:00 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2013-11-26 15:28:46 +0100
commit25fbc1708b8cc7787db574ff4f124b706930802d (patch)
treeaa0fb1a3e0d7350a9f04049dff9de891913a2c2e /sonar-home/src
parent145913a4689d0e95f41ddecff499582607cf4b36 (diff)
downloadsonarqube-25fbc1708b8cc7787db574ff4f124b706930802d.tar.gz
sonarqube-25fbc1708b8cc7787db574ff4f124b706930802d.zip
SONAR-4830 Extract plugin dependencies in the cache
Diffstat (limited to 'sonar-home/src')
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/FileCache.java38
1 files changed, 38 insertions, 0 deletions
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java b/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java
index 0f51957c1c9..bed2671dc6a 100644
--- a/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java
+++ b/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java
@@ -20,6 +20,7 @@
package org.sonar.home.cache;
import org.apache.commons.io.FileUtils;
+import org.sonar.api.utils.ZipUtils;
import org.sonar.home.log.Log;
import javax.annotation.CheckForNull;
@@ -27,6 +28,7 @@ import javax.annotation.CheckForNull;
import java.io.File;
import java.io.IOException;
import java.util.Random;
+import java.util.zip.ZipEntry;
/**
* This class is responsible for managing Sonar batch file cache. You can put file into cache and
@@ -36,6 +38,8 @@ import java.util.Random;
public class FileCache {
private static final int TEMP_FILE_ATTEMPTS = 1000;
+ /** Maximum loop count when creating temp directories. */
+ private static final int TEMP_DIR_ATTEMPTS = 10000;
private final File dir, tmpDir;
private final FileHashes hashes;
@@ -147,6 +151,18 @@ public class FileCache {
throw new IllegalStateException("Fail to create temporary file in " + tmpDir);
}
+ private File createTempDir() {
+ String baseName = System.currentTimeMillis() + "-";
+
+ for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) {
+ File tempDir = new File(tmpDir, baseName + counter);
+ if (tempDir.mkdir()) {
+ return tempDir;
+ }
+ }
+ throw new IllegalStateException("Failed to create directory in " + tmpDir);
+ }
+
private File createDir(File dir, Log log, String debugTitle) {
if (!dir.isDirectory() || !dir.exists()) {
log.debug("Create : " + dir.getAbsolutePath());
@@ -159,4 +175,26 @@ public class FileCache {
return dir;
}
+ /**
+ * Unzip a cached file. Unzip is done only the first time.
+ * @param cachedFile
+ * @return directory where cachedFile was unzipped
+ * @throws IOException
+ */
+ public File unzip(File cachedFile) throws IOException {
+ String filename = cachedFile.getName();
+ File destDir = new File(cachedFile.getParentFile(), filename + "_unzip");
+ if (!destDir.exists()) {
+ File tempDir = createTempDir();
+ ZipUtils.unzip(cachedFile, tempDir, new LibFilter());
+ FileUtils.moveDirectory(tempDir, destDir);
+ }
+ return destDir;
+ }
+
+ private static final class LibFilter implements ZipUtils.ZipEntryFilter {
+ public boolean accept(ZipEntry entry) {
+ return entry.getName().startsWith("META-INF/lib");
+ }
+ }
}