aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-home/src/main/java/org
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2016-05-13 10:35:18 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2016-05-13 15:03:56 +0200
commit35997d8bbebb20e87f78ab1d7a7a77ab28cf16a9 (patch)
tree36655ed02200f6a120b2c3d2ddf435315efcf110 /sonar-home/src/main/java/org
parent1d0523368d43ccf4bef67ee9918a84419b4073b7 (diff)
downloadsonarqube-35997d8bbebb20e87f78ab1d7a7a77ab28cf16a9.tar.gz
sonarqube-35997d8bbebb20e87f78ab1d7a7a77ab28cf16a9.zip
SONAR-7367 Remove code previously used by SonarLint
Diffstat (limited to 'sonar-home/src/main/java/org')
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java281
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java110
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheInvalidation.java27
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/TTLCacheInvalidation.java48
4 files changed, 0 insertions, 466 deletions
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java
deleted file mode 100644
index 6a00267e9d8..00000000000
--- a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java
+++ /dev/null
@@ -1,281 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.home.cache;
-
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.DirectoryStream;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.StandardCopyOption;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-
-import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
-
-import static java.nio.file.StandardOpenOption.CREATE;
-import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
-import static java.nio.file.StandardOpenOption.WRITE;
-
-public class PersistentCache {
- private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
- private static final Charset ENCODING = StandardCharsets.UTF_8;
- private static final String DIGEST_ALGO = "MD5";
-
- private final PersistentCacheInvalidation invalidation;
- private final Logger logger;
- private final Path dir;
- private DirectoryLock lock;
-
- public PersistentCache(Path dir, PersistentCacheInvalidation invalidation, Logger logger, DirectoryLock lock) {
- this.dir = dir;
- this.invalidation = invalidation;
- this.logger = logger;
- this.lock = lock;
-
- reconfigure();
- logger.debug("cache: " + dir);
- }
-
- public synchronized void reconfigure() {
- try {
- Files.createDirectories(dir);
- } catch (IOException e) {
- throw new IllegalStateException("failed to create cache dir", e);
- }
- }
-
- public Path getDirectory() {
- return dir;
- }
-
- @CheckForNull
- public synchronized String getString(@Nonnull String obj) throws IOException {
- byte[] cached = get(obj);
-
- if (cached == null) {
- return null;
- }
-
- return new String(cached, ENCODING);
- }
-
- @CheckForNull
- public synchronized InputStream getStream(@Nonnull String obj) throws IOException {
- String key = getKey(obj);
-
- try {
- lock();
- Path path = getCacheCopy(key);
- if (path == null) {
- return null;
- }
- return new DeleteFileOnCloseInputStream(new FileInputStream(path.toFile()), path);
-
- } finally {
- unlock();
- }
- }
-
- @CheckForNull
- public synchronized byte[] get(@Nonnull String obj) throws IOException {
- String key = getKey(obj);
-
- try {
- lock();
-
- byte[] cached = getCache(key);
-
- if (cached != null) {
- logger.debug("cache hit for " + obj + " -> " + key);
- return cached;
- }
-
- logger.debug("cache miss for " + obj + " -> " + key);
- } finally {
- unlock();
- }
-
- return null;
- }
-
- public synchronized void put(@Nonnull String obj, @Nonnull InputStream stream) throws IOException {
- String key = getKey(obj);
- try {
- lock();
- putCache(key, stream);
- } finally {
- unlock();
- }
- }
-
- public synchronized void put(@Nonnull String obj, @Nonnull byte[] value) throws IOException {
- String key = getKey(obj);
- try {
- lock();
- putCache(key, value);
- } finally {
- unlock();
- }
- }
-
- /**
- * Deletes all cache entries
- */
- public synchronized void clear() {
- logger.info("cache: clearing");
- try {
- lock();
- deleteCacheEntries(new DirectoryClearFilter());
- } catch (IOException e) {
- logger.error("Error clearing cache", e);
- } finally {
- unlock();
- }
- }
-
- /**
- * Deletes cache entries that are no longer valid according to the default expiration time period.
- */
- public synchronized void clean() {
- logger.info("cache: cleaning");
- try {
- lock();
- deleteCacheEntries(new DirectoryCleanFilter());
- } catch (IOException e) {
- logger.error("Error cleaning cache", e);
- } finally {
- unlock();
- }
- }
-
- private void lock() throws IOException {
- lock.lock();
- }
-
- private void unlock() {
- lock.unlock();
- }
-
- private static String getKey(String uri) {
- try {
- String key = uri;
- MessageDigest digest = MessageDigest.getInstance(DIGEST_ALGO);
- digest.update(key.getBytes(StandardCharsets.UTF_8));
- return byteArrayToHex(digest.digest());
- } catch (NoSuchAlgorithmException e) {
- throw new IllegalStateException("Couldn't create hash", e);
- }
- }
-
- private void deleteCacheEntries(DirectoryStream.Filter<Path> filter) throws IOException {
- try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) {
- for (Path p : stream) {
- try {
- Files.delete(p);
- } catch (Exception e) {
- logger.error("Error deleting " + p, e);
- }
- }
- }
- }
-
- private class DirectoryClearFilter implements DirectoryStream.Filter<Path> {
- @Override
- public boolean accept(Path entry) throws IOException {
- return !lock.getFileLockName().equals(entry.getFileName().toString());
- }
- }
-
- private class DirectoryCleanFilter implements DirectoryStream.Filter<Path> {
- @Override
- public boolean accept(Path entry) throws IOException {
- if (lock.getFileLockName().equals(entry.getFileName().toString())) {
- return false;
- }
-
- return invalidation.test(entry);
- }
- }
-
- private void putCache(String key, byte[] value) throws IOException {
- Path cachePath = getCacheEntryPath(key);
- Files.write(cachePath, value, CREATE, WRITE, TRUNCATE_EXISTING);
- }
-
- private void putCache(String key, InputStream stream) throws IOException {
- Path cachePath = getCacheEntryPath(key);
- Files.copy(stream, cachePath, StandardCopyOption.REPLACE_EXISTING);
- }
-
- private byte[] getCache(String key) throws IOException {
- Path cachePath = getCacheEntryPath(key);
-
- if (!validateCacheEntry(cachePath)) {
- return null;
- }
-
- return Files.readAllBytes(cachePath);
- }
-
- private Path getCacheCopy(String key) throws IOException {
- Path cachePath = getCacheEntryPath(key);
-
- if (!validateCacheEntry(cachePath)) {
- return null;
- }
-
- Path temp = Files.createTempFile("sonar_cache", null);
- Files.copy(cachePath, temp, StandardCopyOption.REPLACE_EXISTING);
- return temp;
- }
-
- private boolean validateCacheEntry(Path cacheEntryPath) throws IOException {
- if (!Files.exists(cacheEntryPath)) {
- return false;
- }
-
- if (invalidation.test(cacheEntryPath)) {
- logger.debug("cache: evicting entry");
- Files.delete(cacheEntryPath);
- return false;
- }
-
- return true;
- }
-
- private Path getCacheEntryPath(String key) {
- return dir.resolve(key);
- }
-
- public static String byteArrayToHex(byte[] bytes) {
- char[] hexChars = new char[bytes.length * 2];
- for (int j = 0; j < bytes.length; j++) {
- int v = bytes[j] & 0xFF;
- hexChars[j * 2] = hexArray[v >>> 4];
- hexChars[j * 2 + 1] = hexArray[v & 0x0F];
- }
- return new String(hexChars);
- }
-}
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java
deleted file mode 100644
index b34ee09277d..00000000000
--- a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.home.cache;
-
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.concurrent.TimeUnit;
-
-import javax.annotation.Nullable;
-
-/**
- * Cache files will be placed in 3 areas:
- * <pre>
- * &lt;sonar_home&gt;/ws_cache/&lt;server_url&gt;-&lt;version&gt;/projects/&lt;project&gt;/
- * &lt;sonar_home&gt;/ws_cache/&lt;server_url&gt;-&lt;version&gt;/global/
- * &lt;sonar_home&gt;/ws_cache/&lt;server_url&gt;-&lt;version&gt;/local/
- * </pre>
- */
-public class PersistentCacheBuilder {
- private static final long DEFAULT_EXPIRE_DURATION = TimeUnit.MILLISECONDS.convert(9999L, TimeUnit.DAYS);
- private static final String DIR_NAME = "ws_cache";
-
- private Path cacheBasePath;
- private Path relativePath;
- private final Logger logger;
-
- public PersistentCacheBuilder(Logger logger) {
- this.logger = logger;
- }
-
- public PersistentCacheBuilder setAreaForProject(String serverUrl, String serverVersion, String projectKey) {
- relativePath = Paths.get(sanitizeFilename(serverUrl))
- .resolve(sanitizeFilename(serverVersion))
- .resolve("projects")
- .resolve(sanitizeFilename(projectKey));
- return this;
- }
-
- public PersistentCacheBuilder setAreaForGlobal(String serverUrl) {
- relativePath = Paths.get(sanitizeFilename(serverUrl))
- .resolve("global");
- return this;
- }
-
- public PersistentCacheBuilder setAreaForLocalProject(String serverUrl, String serverVersion) {
- relativePath = Paths.get(sanitizeFilename(serverUrl))
- .resolve(sanitizeFilename(serverVersion))
- .resolve("local");
- return this;
- }
-
- public PersistentCacheBuilder setSonarHome(@Nullable Path p) {
- if (p != null) {
- this.cacheBasePath = p.resolve(DIR_NAME);
- }
- return this;
- }
-
- public PersistentCache build() {
- if (relativePath == null) {
- throw new IllegalStateException("area must be set before building");
- }
- if (cacheBasePath == null) {
- setSonarHome(findHome());
- }
- Path cachePath = cacheBasePath.resolve(relativePath);
- DirectoryLock lock = new DirectoryLock(cacheBasePath, logger);
- PersistentCacheInvalidation criteria = new TTLCacheInvalidation(DEFAULT_EXPIRE_DURATION);
- return new PersistentCache(cachePath, criteria, logger, lock);
- }
-
- private static Path findHome() {
- String home = System.getenv("SONAR_USER_HOME");
-
- if (home != null) {
- return Paths.get(home);
- }
-
- home = System.getProperty("user.home");
- return Paths.get(home, ".sonar");
- }
-
- private static String sanitizeFilename(String name) {
- try {
- return URLEncoder.encode(name, StandardCharsets.UTF_8.name());
- } catch (UnsupportedEncodingException e) {
- throw new IllegalStateException("Couldn't sanitize filename: " + name, e);
- }
- }
-}
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheInvalidation.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheInvalidation.java
deleted file mode 100644
index 184730a4cca..00000000000
--- a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheInvalidation.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.home.cache;
-
-import java.io.IOException;
-import java.nio.file.Path;
-
-public interface PersistentCacheInvalidation {
- boolean test(Path cacheEntryPath) throws IOException;
-}
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/TTLCacheInvalidation.java b/sonar-home/src/main/java/org/sonar/home/cache/TTLCacheInvalidation.java
deleted file mode 100644
index e871e87bb0a..00000000000
--- a/sonar-home/src/main/java/org/sonar/home/cache/TTLCacheInvalidation.java
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.home.cache;
-
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.attribute.BasicFileAttributes;
-
-public class TTLCacheInvalidation implements PersistentCacheInvalidation {
- private final long durationToExpireMs;
-
- public TTLCacheInvalidation(long durationToExpireMs) {
- this.durationToExpireMs = durationToExpireMs;
- }
-
- @Override
- public boolean test(Path cacheEntryPath) throws IOException {
- BasicFileAttributes attr = Files.readAttributes(cacheEntryPath, BasicFileAttributes.class);
- long modTime = attr.lastModifiedTime().toMillis();
-
- long age = System.currentTimeMillis() - modTime;
-
- if (age > durationToExpireMs) {
- return true;
- }
-
- return false;
- }
-
-}