restoreProfile("one-issue-per-line.xml");
orchestrator.getServer().provisionProject("sample", "xoo-sample");
orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line");
- SonarRunner runner = configureRunnerIssues("shared/xoo-sample");
+ SonarRunner runner = configureRunnerIssues("shared/xoo-sample", "sonar.verbose", "true");
BuildResult result = orchestrator.executeBuild(runner);
assertThat(ItUtils.countIssuesInJsonReport(result, true)).isEqualTo(17);
}
import org.sonar.api.utils.UriReader;
import org.sonar.batch.analysis.AnalysisProperties;
import org.sonar.batch.analysis.DefaultAnalysisMode;
-import org.sonar.batch.cache.PersistentCacheProvider;
+import org.sonar.batch.cache.GlobalPersistentCacheProvider;
import org.sonar.batch.cache.ProjectSyncContainer;
import org.sonar.batch.cache.StrategyWSLoaderProvider;
import org.sonar.batch.cache.WSLoader.LoadStrategy;
BatchPluginPredicate.class,
ExtensionInstaller.class,
- CachesManager.class,
+ CachesManager.class,
GlobalMode.class,
GlobalSettings.class,
new RulesProvider(),
DefaultHttpDownloader.class,
UriReader.class,
new FileCacheProvider(),
- new PersistentCacheProvider(),
+ new GlobalPersistentCacheProvider(),
System2.INSTANCE,
new GlobalRepositoriesProvider(),
UuidFactoryImpl.INSTANCE);
*/
package org.sonar.batch.bootstrap;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
-import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
-import java.io.InputStreamReader;
import java.net.URI;
-import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
@BatchSide
public class ServerClient {
private static final String GET = "GET";
- private static final Logger LOG = LoggerFactory.getLogger(ServerClient.class);
private GlobalProperties props;
private DefaultHttpDownloader.BaseHttpDownloader downloader;
public String getURL() {
return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/");
}
-
- public String getServerVersion() {
- InputStream is = this.getClass().getClassLoader().getResourceAsStream("sq-version.txt");
- if (is == null) {
- LOG.warn("Failed to get SQ version");
- return null;
- }
- try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
- return br.readLine();
- } catch (IOException e) {
- LOG.warn("Failed to get SQ version", e);
- return null;
- }
- }
public URI getURI(String pathStartingWithSlash) {
Preconditions.checkArgument(pathStartingWithSlash.startsWith("/"), "Path must start with slash /: " + pathStartingWithSlash);
*/
package org.sonar.batch.cache;
-import javax.annotation.Nullable;
+import org.apache.commons.io.FileUtils;
-import org.sonar.batch.bootstrap.ServerClient;
import org.sonar.home.cache.PersistentCache;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Date;
public class DefaultProjectCacheStatus implements ProjectCacheStatus {
- private static final String STATUS_PREFIX = "cache-sync-status-";
+ private static final String STATUS_FILENAME = "cache-sync-status";
private PersistentCache cache;
- private ServerClient client;
- public DefaultProjectCacheStatus(PersistentCache cache, ServerClient client) {
+ public DefaultProjectCacheStatus(PersistentCache cache) {
this.cache = cache;
- this.client = client;
}
@Override
- public void save(@Nullable String projectKey) {
+ public void save() {
Date now = new Date();
try {
- ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
- try (ObjectOutputStream objOutput = new ObjectOutputStream(byteOutput)) {
+ FileOutputStream fos = new FileOutputStream(getStatusFilePath().toFile());
+ try (ObjectOutputStream objOutput = new ObjectOutputStream(fos)) {
objOutput.writeObject(now);
}
- cache.put(getKey(projectKey), byteOutput.toByteArray());
+
} catch (IOException e) {
throw new IllegalStateException("Failed to write cache sync status", e);
}
}
@Override
- public void delete(@Nullable String projectKey) {
- try {
- cache.put(getKey(projectKey), new byte[0]);
- } catch (IOException e) {
- throw new IllegalStateException("Failed to delete cache sync status", e);
- }
+ public void delete() {
+ cache.clear();
+ FileUtils.deleteQuietly(getStatusFilePath().toFile());
}
@Override
- public Date getSyncStatus(@Nullable String projectKey) {
+ public Date getSyncStatus() {
+ Path p = getStatusFilePath();
try {
- byte[] status = cache.get(getKey(projectKey), null);
- if (status == null || status.length == 0) {
+ if (!Files.isRegularFile(p)) {
return null;
}
- ByteArrayInputStream byteInput = new ByteArrayInputStream(status);
- try (ObjectInputStream objInput = new ObjectInputStream(byteInput)) {
+ InputStream is = new FileInputStream(p.toFile());
+ try (ObjectInputStream objInput = new ObjectInputStream(is)) {
return (Date) objInput.readObject();
}
} catch (IOException | ClassNotFoundException e) {
+ FileUtils.deleteQuietly(p.toFile());
throw new IllegalStateException("Failed to read cache sync status", e);
}
}
- private String getKey(@Nullable String projectKey) {
- if (projectKey != null) {
- return STATUS_PREFIX + client.getURL() + "-" + client.getServerVersion() + "-" + projectKey;
- } else {
- return STATUS_PREFIX + client.getURL() + "-" + client.getServerVersion();
- }
+ private Path getStatusFilePath() {
+ return cache.getDirectory().resolve(STATUS_FILENAME);
}
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch.cache;
+
+import org.apache.commons.lang.StringUtils;
+import org.sonar.batch.bootstrap.Slf4jLogger;
+import org.sonar.batch.util.BatchUtils;
+import org.sonar.home.cache.PersistentCacheBuilder;
+
+import java.nio.file.Paths;
+
+import org.sonar.batch.bootstrap.GlobalProperties;
+import org.sonar.home.cache.PersistentCache;
+import org.picocontainer.injectors.ProviderAdapter;
+
+public class GlobalPersistentCacheProvider extends ProviderAdapter {
+ private PersistentCache cache;
+
+ public PersistentCache provide(GlobalProperties props) {
+ if (cache == null) {
+ PersistentCacheBuilder builder = new PersistentCacheBuilder(new Slf4jLogger());
+ String home = props.property("sonar.userHome");
+ String serverUrl = getServerUrl(props);
+
+ if (home != null) {
+ builder.setSonarHome(Paths.get(home));
+ }
+
+ builder.setAreaForGlobal(serverUrl, BatchUtils.getServerVersion());
+ cache = builder.build();
+ }
+
+ return cache;
+ }
+
+ private String getServerUrl(GlobalProperties props) {
+ return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/");
+ }
+}
}
public void execute(boolean force) {
- Date lastSync = cacheStatus.getSyncStatus(null);
+ Date lastSync = cacheStatus.getSyncStatus();
if (lastSync != null) {
if (!force) {
} else {
LOG.info("-- Found cache [{}], synchronizing data..", lastSync);
}
- cacheStatus.delete(null);
+ cacheStatus.delete();
} else {
LOG.info("-- Cache not found, synchronizing data..");
}
loadData();
- saveStatus();
+
+ cacheStatus.save();
+ LOG.info("-- Succesfully synchronized cache");
}
private static Collection<String> getKeys(Collection<QProfile> qProfiles) {
return list;
}
- private void saveStatus() {
- cacheStatus.save(null);
- LOG.info("-- Succesfully synchronized cache");
- }
-
private void loadData() {
Profiler profiler = Profiler.create(Loggers.get(ProjectCacheSynchronizer.class));
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.batch.cache;
-
-import org.sonar.batch.bootstrap.Slf4jLogger;
-import org.sonar.batch.bootstrap.UserProperties;
-
-import org.sonar.api.utils.log.Logger;
-import org.sonar.api.utils.log.Loggers;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Paths;
-
-import org.picocontainer.injectors.ProviderAdapter;
-import org.sonar.home.cache.PersistentCache;
-import org.sonar.home.cache.PersistentCacheBuilder;
-
-public class PersistentCacheProvider extends ProviderAdapter {
- private static final Logger LOG = Loggers.get(PersistentCacheProvider.class);
- private PersistentCache cache;
-
- public PersistentCache provide(UserProperties props) {
- if (cache == null) {
- PersistentCacheBuilder builder = new PersistentCacheBuilder(new Slf4jLogger());
-
- String home = props.property("sonar.userHome");
- if (home != null) {
- builder.setSonarHome(Paths.get(home));
- }
-
- builder.setVersion(getServerVersion());
- cache = builder.build();
- }
-
- return cache;
- }
-
- private String getServerVersion() {
- InputStream is = this.getClass().getClassLoader().getResourceAsStream("sq-version.txt");
- if (is == null) {
- LOG.warn("Failed to get SQ version");
- return null;
- }
- try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
- return br.readLine();
- } catch (IOException e) {
- LOG.warn("Failed to get SQ version", e);
- return null;
- }
- }
-}
*/
package org.sonar.batch.cache;
-import javax.annotation.Nullable;
-
import java.util.Date;
public interface ProjectCacheStatus {
- void save(@Nullable String projectKey);
+ void save();
- void delete(@Nullable String projectKey);
+ void delete();
- Date getSyncStatus(@Nullable String projectKey);
+ Date getSyncStatus();
}
}
public void load(String projectKey, boolean force) {
- Date lastSync = cacheStatus.getSyncStatus(projectKey);
+ Date lastSync = cacheStatus.getSyncStatus();
if (lastSync != null) {
if (!force) {
} else {
LOG.info("-- Found project [{}] cache [{}], synchronizing data..", projectKey, lastSync);
}
- cacheStatus.delete(projectKey);
+ cacheStatus.delete();
} else {
LOG.info("-- Cache for project [{}] not found, synchronizing data..", projectKey);
}
loadData(projectKey);
- saveStatus(projectKey);
+ saveStatus();
}
- private void saveStatus(String projectKey) {
- cacheStatus.save(projectKey);
+ private void saveStatus() {
+ cacheStatus.save();
LOG.info("-- Succesfully synchronized project cache");
}
profiler.stopInfo();
Collection<String> profileKeys = getKeys(qProfiles);
-
+
profiler.startInfo("Load project active rules");
activeRulesLoader.load(profileKeys, projectKey);
profiler.stopInfo();
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch.cache;
+
+import org.sonar.api.batch.bootstrap.ProjectKey;
+
+public class ProjectKeySupplier implements ProjectKey {
+ private final String key;
+
+ ProjectKeySupplier(String key) {
+ this.key = key;
+ }
+
+ @Override
+ public String get() {
+ return key;
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch.cache;
+
+import org.sonar.api.batch.bootstrap.ProjectKey;
+
+import org.sonar.batch.util.BatchUtils;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.batch.bootstrap.GlobalProperties;
+import com.google.common.base.Preconditions;
+import org.sonar.batch.analysis.DefaultAnalysisMode;
+import org.sonar.batch.bootstrap.Slf4jLogger;
+
+import java.nio.file.Paths;
+
+import org.picocontainer.injectors.ProviderAdapter;
+import org.sonar.home.cache.PersistentCache;
+import org.sonar.home.cache.PersistentCacheBuilder;
+
+public class ProjectPersistentCacheProvider extends ProviderAdapter {
+ private PersistentCache cache;
+
+ public PersistentCache provide(GlobalProperties props, DefaultAnalysisMode mode, ProjectKey key) {
+ if (cache == null) {
+ PersistentCacheBuilder builder = new PersistentCacheBuilder(new Slf4jLogger());
+ String projectKey = key.get();
+ String home = props.property("sonar.userHome");
+ String serverUrl = getServerUrl(props);
+
+ if (home != null) {
+ builder.setSonarHome(Paths.get(home));
+ }
+
+ if (mode.isNotAssociated()) {
+ builder.setAreaForLocalProject(serverUrl, BatchUtils.getServerVersion());
+ } else {
+ Preconditions.checkNotNull(projectKey);
+ builder.setAreaForProject(serverUrl, BatchUtils.getServerVersion(), projectKey);
+ }
+
+ cache = builder.build();
+ }
+
+ return cache;
+ }
+
+ private String getServerUrl(GlobalProperties props) {
+ return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/");
+ }
+}
import org.sonar.batch.repository.ProjectRepositoriesFactoryProvider;
import org.sonar.batch.analysis.DefaultAnalysisMode;
import org.sonar.api.CoreProperties;
-import com.google.common.collect.ImmutableMap;
+import java.util.HashMap;
import java.util.Map;
import org.sonar.batch.analysis.AnalysisProperties;
}
}
- private static DefaultAnalysisMode createIssuesAnalysisMode() {
- Map<String, String> props = ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES);
+ private static DefaultAnalysisMode createIssuesAnalysisMode(@Nullable String projectKey) {
+ Map<String, String> props = new HashMap<>();
+ props.put(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_ISSUES);
+ if (projectKey != null) {
+ props.put(CoreProperties.PROJECT_KEY_PROPERTY, projectKey);
+ }
GlobalProperties globalProps = new GlobalProperties(props);
AnalysisProperties analysisProps = new AnalysisProperties(props);
return new DefaultAnalysisMode(globalProps, analysisProps);
private void addComponents() {
add(new StrategyWSLoaderProvider(LoadStrategy.SERVER_ONLY),
+ new ProjectKeySupplier(projectKey),
projectKey != null ? ProjectCacheSynchronizer.class : NonAssociatedCacheSynchronizer.class,
UserRepositoryLoader.class,
new ProjectRepositoriesFactoryProvider(projectKey),
- createIssuesAnalysisMode());
+ new ProjectPersistentCacheProvider(),
+ createIssuesAnalysisMode(projectKey));
addIfMissing(DefaultProjectCacheStatus.class, ProjectCacheStatus.class);
addIfMissing(DefaultProjectRepositoriesLoader.class, ProjectRepositoriesLoader.class);
*/
package org.sonar.batch.cache;
-import com.google.common.io.ByteSource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
+
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
import org.apache.commons.io.IOUtils;
import org.sonar.api.utils.HttpDownloader;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.batch.bootstrap.ServerClient;
import org.sonar.home.cache.PersistentCache;
-
import static org.sonar.batch.cache.WSLoader.ServerStatus.ACCESSIBLE;
import static org.sonar.batch.cache.WSLoader.ServerStatus.NOT_ACCESSIBLE;
import static org.sonar.batch.cache.WSLoader.ServerStatus.UNKNOWN;
}
@Nonnull
- public WSLoaderResult<ByteSource> loadSource(String id) {
- WSLoaderResult<byte[]> byteResult = load(id, defautLoadStrategy);
- return new WSLoaderResult<ByteSource>(ByteSource.wrap(byteResult.get()), byteResult.isFromCache());
+ public WSLoaderResult<InputStream> loadStream(String id) {
+ return load(id, defautLoadStrategy, createStreamLoaderServer(), createStreamLoaderCache());
}
@Nonnull
@Nonnull
public WSLoaderResult<String> loadString(String id, WSLoader.LoadStrategy strategy) {
- WSLoaderResult<byte[]> byteResult = load(id, strategy);
- return new WSLoaderResult<String>(new String(byteResult.get(), StandardCharsets.UTF_8), byteResult.isFromCache());
- }
-
- @Nonnull
- public WSLoaderResult<byte[]> load(String id) {
- return load(id, defautLoadStrategy);
+ return load(id, strategy, createStringLoaderServer(), createStringLoaderCache());
}
@Nonnull
- public WSLoaderResult<byte[]> load(String id, WSLoader.LoadStrategy strategy) {
+ private <T> WSLoaderResult<T> load(String id, WSLoader.LoadStrategy strategy, DataLoader<T> serverLoader, DataLoader<T> cacheLoader) {
switch (strategy) {
case CACHE_FIRST:
- return loadFromCacheFirst(id, true);
+ return loadFromCacheFirst(id, cacheLoader, serverLoader);
case CACHE_ONLY:
- return loadFromCacheFirst(id, false);
+ return loadFromCacheFirst(id, cacheLoader, null);
case SERVER_FIRST:
- return loadFromServerFirst(id, true);
+ return loadFromServerFirst(id, serverLoader, cacheLoader);
case SERVER_ONLY:
default:
- return loadFromServerFirst(id, false);
+ return loadFromServerFirst(id, serverLoader, null);
}
}
- public LoadStrategy getStrategy() {
+ public LoadStrategy getDefaultStrategy() {
return this.defautLoadStrategy;
}
return serverStatus == NOT_ACCESSIBLE;
}
- private void updateCache(String id, byte[] value) {
- try {
- cache.put(client.getURI(id).toString(), value);
- } catch (IOException e) {
- throw new IllegalStateException("Error saving to WS cache", e);
- }
- }
-
@Nonnull
- private WSLoaderResult<byte[]> loadFromCacheFirst(String id, boolean fallback) {
+ private <T> WSLoaderResult<T> loadFromCacheFirst(String id, DataLoader<T> cacheLoader, @Nullable DataLoader<T> serverLoader) {
try {
- return loadFromCache(id);
+ return loadFromCache(id, cacheLoader);
} catch (NotAvailableException cacheNotAvailable) {
- if (fallback) {
+ if (serverLoader != null) {
try {
- return loadFromServer(id);
+ return loadFromServer(id, serverLoader);
} catch (NotAvailableException serverNotAvailable) {
throw new IllegalStateException(FAIL_MSG, serverNotAvailable.getCause());
}
}
@Nonnull
- private WSLoaderResult<byte[]> loadFromServerFirst(String id, boolean fallback) {
+ private <T> WSLoaderResult<T> loadFromServerFirst(String id, DataLoader<T> serverLoader, @Nullable DataLoader<T> cacheLoader) {
try {
- return loadFromServer(id);
+ return loadFromServer(id, serverLoader);
} catch (NotAvailableException serverNotAvailable) {
- if (fallback) {
+ if (cacheLoader != null) {
try {
- return loadFromCache(id);
+ return loadFromCache(id, cacheLoader);
} catch (NotAvailableException cacheNotAvailable) {
throw new IllegalStateException(FAIL_MSG, serverNotAvailable.getCause());
}
}
}
+ interface DataLoader<T> {
+ T load(String id) throws IOException;
+ }
+
@Nonnull
- private WSLoaderResult<byte[]> loadFromCache(String id) throws NotAvailableException {
+ private <T> WSLoaderResult<T> loadFromCache(String id, DataLoader<T> loader) throws NotAvailableException {
+ T result = null;
+
try {
- byte[] result = cache.get(client.getURI(id).toString(), null);
- if (result == null) {
- throw new NotAvailableException("resource not cached");
- }
- return new WSLoaderResult<byte[]>(result, true);
+ result = loader.load(id);
} catch (IOException e) {
// any exception on the cache should fail fast
throw new IllegalStateException(e);
}
+ if (result == null) {
+ throw new NotAvailableException("resource not cached");
+ }
+ return new WSLoaderResult<T>(result, true);
}
@Nonnull
- private WSLoaderResult<byte[]> loadFromServer(String id) throws NotAvailableException {
+ private <T> WSLoaderResult<T> loadFromServer(String id, DataLoader<T> loader) throws NotAvailableException {
if (isOffline()) {
throw new NotAvailableException("Server not available");
}
try {
- InputStream is = client.load(id, REQUEST_METHOD, true, CONNECT_TIMEOUT, READ_TIMEOUT);
+ T t = loader.load(id);
switchToOnline();
- byte[] value = IOUtils.toByteArray(is);
- updateCache(id, value);
- return new WSLoaderResult<byte[]>(value, false);
+ return new WSLoaderResult<T>(t, false);
} catch (IllegalStateException e) {
if (e.getCause() instanceof HttpDownloader.HttpException) {
// fail fast if it could connect but there was a application-level error
}
}
+ private DataLoader<String> createStringLoaderServer() {
+ return new DataLoader<String>() {
+ @Override
+ public String load(String id) throws IOException {
+ InputStream is = client.load(id, REQUEST_METHOD, true, CONNECT_TIMEOUT, READ_TIMEOUT);
+ String str = IOUtils.toString(is, StandardCharsets.UTF_8);
+ try {
+ cache.put(id, str.getBytes(StandardCharsets.UTF_8));
+ } catch (IOException e) {
+ throw new IllegalStateException("Error saving to WS cache", e);
+ }
+ return str;
+ }
+ };
+ }
+
+ private DataLoader<String> createStringLoaderCache() {
+ return new DataLoader<String>() {
+ @Override
+ public String load(String id) throws IOException {
+ return cache.getString(id);
+ }
+ };
+ }
+
+ private DataLoader<InputStream> createStreamLoaderServer() {
+ return new DataLoader<InputStream>() {
+ @Override
+ public InputStream load(String id) throws IOException {
+ InputStream is = client.load(id, REQUEST_METHOD, true, CONNECT_TIMEOUT, READ_TIMEOUT);
+ try {
+ cache.put(id, is);
+ } catch (IOException e) {
+ throw new IllegalStateException("Error saving to WS cache", e);
+ }
+ is.close();
+ return cache.getStream(id);
+ }
+ };
+ }
+
+ private DataLoader<InputStream> createStreamLoaderCache() {
+ return new DataLoader<InputStream>() {
+ @Override
+ public InputStream load(String id) throws IOException {
+ return cache.getStream(id);
+ }
+ };
+ }
+
private class NotAvailableException extends Exception {
private static final long serialVersionUID = 1L;
*/
package org.sonar.batch.repository;
-import org.sonar.batch.cache.WSLoaderResult;
-
-import org.sonar.batch.cache.WSLoader;
-import org.sonar.batch.util.BatchUtils;
-import com.google.common.io.ByteSource;
import com.google.common.base.Function;
-import org.sonar.batch.protocol.input.BatchInput.ServerIssue;
-
+import com.google.common.io.ByteSource;
import java.io.IOException;
import java.io.InputStream;
+import org.apache.commons.io.IOUtils;
+import org.sonar.batch.cache.WSLoader;
+import org.sonar.batch.cache.WSLoaderResult;
+import org.sonar.batch.protocol.input.BatchInput.ServerIssue;
+import org.sonar.batch.util.BatchUtils;
public class DefaultServerIssuesLoader implements ServerIssuesLoader {
return result.isFromCache();
}
- private static void parseIssues(ByteSource input, Function<ServerIssue, Void> consumer) {
- try (InputStream is = input.openBufferedStream()) {
+ private static void parseIssues(InputStream is, Function<ServerIssue, Void> consumer) {
+ try {
ServerIssue previousIssue = ServerIssue.parseDelimitedFrom(is);
while (previousIssue != null) {
consumer.apply(previousIssue);
}
} catch (IOException e) {
throw new IllegalStateException("Unable to get previous issues", e);
+ } finally {
+ IOUtils.closeQuietly(is);
}
}
}
*/
package org.sonar.batch.repository.user;
-import org.sonar.batch.cache.WSLoaderResult;
+import org.apache.commons.io.IOUtils;
+import org.sonar.batch.cache.WSLoaderResult;
import org.sonar.batch.cache.WSLoader;
import javax.annotation.Nullable;
import com.google.common.collect.Lists;
import com.google.common.base.Joiner;
import org.sonar.batch.util.BatchUtils;
-import com.google.common.io.ByteSource;
import com.google.common.base.Function;
import org.sonar.batch.protocol.input.BatchInput;
public BatchInput.User load(String userLogin) {
return load(userLogin, null);
}
-
+
public BatchInput.User load(String userLogin, @Nullable MutableBoolean fromCache) {
- ByteSource byteSource = loadQuery(new UserEncodingFunction().apply(userLogin), fromCache);
- return parseUser(byteSource);
+ InputStream is = loadQuery(new UserEncodingFunction().apply(userLogin), fromCache);
+ return parseUser(is);
}
public Collection<BatchInput.User> load(List<String> userLogins) {
return load(userLogins, null);
}
-
+
/**
* Not cache friendly. Should not be used if a cache hit is expected.
*/
if (userLogins.isEmpty()) {
return Collections.emptyList();
}
- ByteSource byteSource = loadQuery(Joiner.on(',').join(Lists.transform(userLogins, new UserEncodingFunction())), fromCache);
+ InputStream is = loadQuery(Joiner.on(',').join(Lists.transform(userLogins, new UserEncodingFunction())), fromCache);
- return parseUsers(byteSource);
+ return parseUsers(is);
}
- private ByteSource loadQuery(String loginsQuery, @Nullable MutableBoolean fromCache) {
- WSLoaderResult<ByteSource> result = wsLoader.loadSource("/scanner/users?logins=" + loginsQuery);
+ private InputStream loadQuery(String loginsQuery, @Nullable MutableBoolean fromCache) {
+ WSLoaderResult<InputStream> result = wsLoader.loadStream("/scanner/users?logins=" + loginsQuery);
if (fromCache != null) {
fromCache.setValue(result.isFromCache());
}
}
}
- private static BatchInput.User parseUser(ByteSource input) {
- try (InputStream is = input.openStream()) {
+ private static BatchInput.User parseUser(InputStream is) {
+ try {
return BatchInput.User.parseDelimitedFrom(is);
} catch (IOException e) {
throw new IllegalStateException("Unable to get user details from server", e);
+ } finally {
+ IOUtils.closeQuietly(is);
}
}
- private static Collection<BatchInput.User> parseUsers(ByteSource input) {
+ private static Collection<BatchInput.User> parseUsers(InputStream is) {
List<BatchInput.User> users = new ArrayList<>();
- try (InputStream is = input.openStream()) {
+ try {
BatchInput.User user = BatchInput.User.parseDelimitedFrom(is);
while (user != null) {
users.add(user);
}
} catch (IOException e) {
throw new IllegalStateException("Unable to get user details from server", e);
+ } finally {
+ IOUtils.closeQuietly(is);
}
return users;
*/
package org.sonar.batch.rule;
-import org.sonar.batch.cache.WSLoaderResult;
+import org.apache.commons.io.IOUtils;
+import org.sonar.batch.cache.WSLoaderResult;
import org.sonar.batch.cache.WSLoader;
import javax.annotation.Nullable;
import org.apache.commons.lang.mutable.MutableBoolean;
import org.sonarqube.ws.Rules.ListResponse.Rule;
-import com.google.common.io.ByteSource;
import org.sonarqube.ws.Rules.ListResponse;
import java.io.IOException;
@Override
public List<Rule> load(@Nullable MutableBoolean fromCache) {
- WSLoaderResult<ByteSource> result = wsLoader.loadSource(RULES_SEARCH_URL);
- ListResponse list = loadFromSource(result.get());
+ WSLoaderResult<InputStream> result = wsLoader.loadStream(RULES_SEARCH_URL);
+ ListResponse list = loadFromStream(result.get());
if (fromCache != null) {
fromCache.setValue(result.isFromCache());
}
return list.getRulesList();
}
- private static ListResponse loadFromSource(ByteSource input) {
- try (InputStream is = input.openStream()) {
+ private static ListResponse loadFromStream(InputStream is) {
+ try {
return ListResponse.parseFrom(is);
} catch (IOException e) {
throw new IllegalStateException("Unable to get rules", e);
+ } finally {
+ IOUtils.closeQuietly(is);
}
}
*/
package org.sonar.batch.scan;
+import org.sonar.batch.bootstrap.Slf4jLogger;
+
+import org.sonar.home.cache.DirectoryLock;
import org.picocontainer.Startable;
import org.sonar.api.batch.bootstrap.ProjectReactor;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import java.io.IOException;
-import java.io.RandomAccessFile;
-import java.nio.channels.FileChannel;
-import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
-import java.nio.file.Files;
import java.nio.file.Path;
public class ProjectLock implements Startable {
- private static final Logger LOG = LoggerFactory.getLogger(ProjectLock.class);
static final String LOCK_FILE_NAME = ".sonar_lock";
- private final Path lockFilePath;
- private RandomAccessFile lockRandomAccessFile;
- private FileChannel lockChannel;
- private FileLock lockFile;
+ private DirectoryLock lock;
public ProjectLock(ProjectReactor projectReactor) {
Path directory = projectReactor.getRoot().getBaseDir().toPath();
- this.lockFilePath = directory.resolve(LOCK_FILE_NAME).toAbsolutePath();
+ this.lock = new DirectoryLock(directory.toAbsolutePath(), new Slf4jLogger());
}
public void tryLock() {
try {
- lockRandomAccessFile = new RandomAccessFile(lockFilePath.toFile(), "rw");
- lockChannel = lockRandomAccessFile.getChannel();
- lockFile = lockChannel.tryLock(0, 1024, false);
-
- if (lockFile == null) {
+ if (!lock.tryLock()) {
failAlreadyInProgress(null);
}
} catch (OverlappingFileLockException e) {
failAlreadyInProgress(e);
- } catch (IOException e) {
- throw new IllegalStateException("Failed to create project lock in " + lockFilePath.toString(), e);
}
}
@Override
public void stop() {
- if (lockFile != null) {
- try {
- lockFile.release();
- lockFile = null;
- } catch (IOException e) {
- LOG.error("Error releasing lock", e);
- }
- }
- if (lockChannel != null) {
- try {
- lockChannel.close();
- lockChannel = null;
- } catch (IOException e) {
- LOG.error("Error closing file channel", e);
- }
- }
- if (lockRandomAccessFile != null) {
- try {
- lockRandomAccessFile.close();
- lockRandomAccessFile = null;
- } catch (IOException e) {
- LOG.error("Error closing file", e);
- }
- }
-
- try {
- Files.delete(lockFilePath);
- } catch (IOException e) {
- // ignore, as an error happens if another process just started to acquire the same lock
- LOG.debug("Couldn't delete lock file: " + lockFilePath.toString(), e);
- }
+ lock.unlock();
}
@Override
public void start() {
// nothing to do
}
-
}
*/
package org.sonar.batch.scan;
-import org.sonar.batch.issue.tracking.LocalIssueTracking;
+import org.sonar.batch.cache.ProjectPersistentCacheProvider;
+import org.sonar.batch.issue.tracking.LocalIssueTracking;
import org.sonar.batch.issue.tracking.IssueTransition;
import org.sonar.batch.repository.DefaultProjectRepositoriesFactory;
import org.sonar.batch.repository.QualityProfileProvider;
BatchComponentCache.class,
DefaultIssueCallback.class,
new ProjectSettingsProvider(),
+ new ProjectPersistentCacheProvider(),
// temp
new AnalysisTempFolderProvider(),
package org.sonar.batch.util;
import com.google.common.base.Strings;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class BatchUtils {
+ private static final Logger LOG = LoggerFactory.getLogger(BatchUtils.class);
private BatchUtils() {
}
String cleanKey = StringUtils.deleteWhitespace(projectKey);
return StringUtils.replace(cleanKey, ":", "_");
}
-
+
public static String encodeForUrl(@Nullable String url) {
try {
return URLEncoder.encode(Strings.nullToEmpty(url), "UTF-8");
return o.getClass().getName();
}
+
+ public static String getServerVersion() {
+ InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("sq-version.txt");
+ if (is == null) {
+ LOG.warn("Failed to get SQ version");
+ return null;
+ }
+ try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
+ return br.readLine();
+ } catch (IOException e) {
+ LOG.warn("Failed to get SQ version", e);
+ return null;
+ }
+ }
}
props = new AnalysisProperties(propMap, null);
WSLoader loader = loaderProvider.provide(props, mode, cache, client);
- assertThat(loader.getStrategy()).isEqualTo(LoadStrategy.SERVER_ONLY);
+ assertThat(loader.getDefaultStrategy()).isEqualTo(LoadStrategy.SERVER_ONLY);
}
}
thrown.expect(IllegalStateException.class);
WSLoader wsLoader = mock(WSLoader.class);
- doThrow(new IllegalStateException()).when(wsLoader).load("/deploy/plugins/index.txt");
+ doThrow(new IllegalStateException()).when(wsLoader).loadString("/deploy/plugins/index.txt");
new BatchPluginInstaller(wsLoader, serverClient, fileCache, pluginPredicate).installRemotes();
}
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.assertj.core.api.Assertions.assertThat;
-
-import org.sonar.home.cache.PersistentCacheLoader;
-
+import com.google.common.io.Files;
import org.junit.rules.ExpectedException;
+import java.io.File;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.Date;
-import static org.mockito.Mockito.doThrow;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Matchers.any;
import org.junit.Test;
-import org.sonar.home.cache.Logger;
import org.junit.rules.TemporaryFolder;
import org.junit.Rule;
import org.junit.Before;
import org.sonar.home.cache.PersistentCache;
public class DefaultProjectCacheStatusTest {
- private static final String PROJ_KEY = "project1";
@Rule
public TemporaryFolder tmp = new TemporaryFolder();
-
+
@Rule
public ExpectedException exception = ExpectedException.none();
@Before
public void setUp() {
- cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null);
client = mock(ServerClient.class);
- when(client.getServerVersion()).thenReturn("5.2");
- when(client.getURL()).thenReturn("localhost");
- cacheStatus = new DefaultProjectCacheStatus(cache, client);
- }
-
- @Test
- public void errorDelete() throws IOException {
cache = mock(PersistentCache.class);
- doThrow(IOException.class).when(cache).put(anyString(), any(byte[].class));
- cacheStatus = new DefaultProjectCacheStatus(cache, client);
-
- exception.expect(IllegalStateException.class);
- exception.expectMessage("Failed to delete cache sync status");
- cacheStatus.delete(PROJ_KEY);
+ when(cache.getDirectory()).thenReturn(tmp.getRoot().toPath());
+ cacheStatus = new DefaultProjectCacheStatus(cache);
}
-
+
@Test
public void errorSave() throws IOException {
- cache = mock(PersistentCache.class);
- doThrow(IOException.class).when(cache).put(anyString(), any(byte[].class));
- cacheStatus = new DefaultProjectCacheStatus(cache, client);
+ when(cache.getDirectory()).thenReturn(tmp.getRoot().toPath().resolve("unexistent_folder"));
+ cacheStatus = new DefaultProjectCacheStatus(cache);
exception.expect(IllegalStateException.class);
exception.expectMessage("Failed to write cache sync status");
- cacheStatus.save(PROJ_KEY);
- }
-
- @Test
- public void useServerVersionAsKey() {
- cacheStatus.save(PROJ_KEY);
- assertThat(cacheStatus.getSyncStatus(PROJ_KEY)).isNotNull();
- assertThat(age(cacheStatus.getSyncStatus(PROJ_KEY))).isLessThan(2000);
-
- when(client.getServerVersion()).thenReturn("5.1");
-
- assertThat(cacheStatus.getSyncStatus(PROJ_KEY)).isNull();
+ cacheStatus.save();
}
-
+
@Test
public void errorStatus() throws IOException {
- cache = mock(PersistentCache.class);
- doThrow(IOException.class).when(cache).get(anyString(), any(PersistentCacheLoader.class));
- cacheStatus = new DefaultProjectCacheStatus(cache, client);
+ Files.write("trash".getBytes(StandardCharsets.UTF_8), new File(tmp.getRoot(), "cache-sync-status"));
+ cacheStatus = new DefaultProjectCacheStatus(cache);
exception.expect(IllegalStateException.class);
exception.expectMessage("Failed to read cache sync status");
- cacheStatus.getSyncStatus(PROJ_KEY);
+ cacheStatus.getSyncStatus();
}
-
+
@Test
public void testSave() {
- cacheStatus.save(PROJ_KEY);
- assertThat(cacheStatus.getSyncStatus(PROJ_KEY)).isNotNull();
- assertThat(age(cacheStatus.getSyncStatus(PROJ_KEY))).isLessThan(2000);
- assertThat(cacheStatus.getSyncStatus(PROJ_KEY + "1")).isNull();
+ cacheStatus.save();
+ assertThat(cacheStatus.getSyncStatus()).isNotNull();
+ assertThat(age(cacheStatus.getSyncStatus())).isLessThan(2000);
}
@Test
public void testDelete() {
- cacheStatus.save(PROJ_KEY);
- cacheStatus.delete(PROJ_KEY);
- assertThat(cacheStatus.getSyncStatus(PROJ_KEY)).isNull();
+ cacheStatus.save();
+ cacheStatus.delete();
+ assertThat(cacheStatus.getSyncStatus()).isNull();
}
private long age(Date date) {
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch.cache;
+
+import org.sonar.batch.util.BatchUtils;
+
+import org.sonar.home.cache.PersistentCache;
+
+import java.util.HashMap;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import org.sonar.batch.bootstrap.GlobalProperties;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
+
+public class GlobalPersistentCacheProviderTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ private GlobalPersistentCacheProvider provider;
+ private GlobalProperties globalProperties;
+
+ @Before
+ public void setUp() {
+ HashMap<String, String> map = new HashMap<String, String>();
+ map.put("sonar.userHome", temp.getRoot().getAbsolutePath());
+ globalProperties = new GlobalProperties(map);
+ provider = new GlobalPersistentCacheProvider();
+ }
+
+ @Test
+ public void test_path() {
+ PersistentCache cache = provider.provide(globalProperties);
+ assertThat(cache.getDirectory()).isEqualTo(temp.getRoot().toPath()
+ .resolve("ws_cache")
+ .resolve("http%3A%2F%2Flocalhost%3A9000-" + BatchUtils.getServerVersion())
+ .resolve("global"));
+ }
+}
@Test
public void dont_sync_if_exists() {
- when(cacheStatus.getSyncStatus(null)).thenReturn(new Date());
+ when(cacheStatus.getSyncStatus()).thenReturn(new Date());
synchronizer.execute(false);
verifyNoMoreInteractions(qualityProfileLoader, activeRulesLoader);
}
@Test
public void always_sync_if_force() {
- when(cacheStatus.getSyncStatus(null)).thenReturn(new Date());
+ when(cacheStatus.getSyncStatus()).thenReturn(new Date());
synchronizer.execute(true);
checkSync();
}
}
private void checkSync() {
- verify(cacheStatus).getSyncStatus(null);
- verify(cacheStatus).save(null);
+ verify(cacheStatus).getSyncStatus();
+ verify(cacheStatus).save();
verify(qualityProfileLoader).load(null, null);
verify(activeRulesLoader).load(ImmutableList.of("profile"), null);
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.batch.cache;
-
-import org.junit.Rule;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.batch.bootstrap.GlobalProperties;
-import org.sonar.batch.cache.PersistentCacheProvider;
-
-import java.io.File;
-import java.util.Collections;
-
-import org.junit.Before;
-import static org.assertj.core.api.Assertions.assertThat;
-import org.junit.Test;
-
-public class PersistentCacheProviderTest {
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- private PersistentCacheProvider provider = null;
- private GlobalProperties props = null;
-
- @Before
- public void prepare() {
- props = new GlobalProperties(Collections.<String, String>emptyMap());
- provider = new PersistentCacheProvider();
- }
-
- @Test
- public void test_singleton() {
- assertThat(provider.provide(props)).isEqualTo(provider.provide(props));
- }
-
- @Test
- public void test_cache_dir() {
- assertThat(provider.provide(props).getBaseDirectory().toFile()).exists().isDirectory();
- }
-
- @Test
- public void test_home() {
- File f = temp.getRoot();
- props.properties().put("sonar.userHome", f.getAbsolutePath());
- assertThat(provider.provide(props).getBaseDirectory()).isEqualTo(f.toPath().resolve("ws_cache"));
- }
-}
*/
package org.sonar.batch.cache;
+import static org.mockito.Mockito.when;
+import org.sonar.batch.repository.ProjectRepositoriesFactory;
+import org.sonar.batch.repository.DefaultProjectRepositoriesFactory;
+import org.junit.Rule;
+import org.junit.rules.ExpectedException;
+import org.sonar.batch.repository.ProjectSettingsRepo;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
-import com.google.common.io.ByteSource;
-import com.google.common.io.Resources;
+import org.sonar.batch.protocol.input.ActiveRule;
+import org.sonar.batch.protocol.input.QProfile;
+import org.apache.commons.lang.mutable.MutableBoolean;
+import org.sonar.batch.repository.DefaultProjectSettingsLoader;
+import org.sonar.batch.rule.DefaultActiveRulesLoader;
+import org.sonar.batch.repository.DefaultQualityProfileLoader;
+import org.sonar.batch.repository.ProjectSettingsLoader;
+import org.sonar.batch.rule.ActiveRulesLoader;
+import org.sonar.batch.repository.QualityProfileLoader;
+import org.sonar.batch.analysis.DefaultAnalysisMode;
+import org.sonar.batch.analysis.AnalysisProperties;
+import org.sonar.batch.protocol.input.ProjectRepositories;
+import org.sonar.batch.repository.DefaultServerIssuesLoader;
+import org.sonar.batch.repository.DefaultProjectRepositoriesLoader;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
+
import java.io.IOException;
+import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Date;
MockitoAnnotations.initMocks(this);
String batchProject = getResourceAsString("batch_project.json");
- ByteSource issues = getResourceAsByteSource("batch_issues.protobuf");
+ InputStream issues = getResourceAsInputStream("batch_issues.protobuf");
when(ws.loadString(BATCH_PROJECT)).thenReturn(new WSLoaderResult<>(batchProject, false));
- when(ws.loadSource(ISSUES)).thenReturn(new WSLoaderResult<>(issues, false));
+ when(ws.loadStream(ISSUES)).thenReturn(new WSLoaderResult<>(issues, false));
when(analysisMode.isIssues()).thenReturn(true);
when(properties.properties()).thenReturn(new HashMap<String, String>());
sync.load(PROJECT_KEY, false);
verify(ws).loadString(BATCH_PROJECT);
- verify(ws).loadSource(ISSUES);
+ verify(ws).loadStream(ISSUES);
verifyNoMoreInteractions(ws);
- verify(cacheStatus).save(anyString());
+ verify(cacheStatus).save();
}
@Test
ProjectCacheSynchronizer sync = create(mockedProjectRepositories);
sync.load(PROJECT_KEY, true);
- verify(cacheStatus).save(PROJECT_KEY);
+ verify(cacheStatus).save();
}
@Test
public void testDontSyncIfNotForce() {
- when(cacheStatus.getSyncStatus(PROJECT_KEY)).thenReturn(new Date());
+ when(cacheStatus.getSyncStatus()).thenReturn(new Date());
ProjectCacheSynchronizer sync = create(null);
sync.load(PROJECT_KEY, false);
return Resources.toString(resource, StandardCharsets.UTF_8);
}
- private ByteSource getResourceAsByteSource(String name) throws IOException {
+ private InputStream getResourceAsInputStream(String name) throws IOException {
URL resource = this.getClass().getResource(getClass().getSimpleName() + "/" + name);
- return Resources.asByteSource(resource);
+ return Resources.asByteSource(resource).openBufferedStream();
}
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.batch.cache;
+
+import org.sonar.api.batch.bootstrap.ProjectKey;
+
+import org.sonar.batch.util.BatchUtils;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.batch.analysis.DefaultAnalysisMode;
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.batch.bootstrap.GlobalProperties;
+import org.sonar.batch.cache.ProjectPersistentCacheProvider;
+
+import java.io.File;
+import java.nio.file.Path;
+import java.util.Collections;
+
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.mock;
+import org.junit.Before;
+import static org.assertj.core.api.Assertions.assertThat;
+import org.junit.Test;
+
+public class ProjectPersistentCacheProviderTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ private ProjectPersistentCacheProvider provider = null;
+ private GlobalProperties props = null;
+ private DefaultAnalysisMode mode = null;
+ private ProjectKey key = null;
+
+ @Before
+ public void prepare() {
+ key = new ProjectKeySupplier("proj");
+ props = new GlobalProperties(Collections.<String, String>emptyMap());
+ mode = mock(DefaultAnalysisMode.class);
+ provider = new ProjectPersistentCacheProvider();
+ }
+
+ @Test
+ public void test_singleton() {
+ assertThat(provider.provide(props, mode, key)).isEqualTo(provider.provide(props, mode, key));
+ }
+
+ @Test
+ public void test_cache_dir() {
+ assertThat(provider.provide(props, mode, key).getDirectory().toFile()).exists().isDirectory();
+ }
+
+ @Test
+ public void test_home() {
+ File f = temp.getRoot();
+ props.properties().put("sonar.userHome", f.getAbsolutePath());
+ Path expected = f.toPath()
+ .resolve("ws_cache")
+ .resolve("http%3A%2F%2Flocalhost%3A9000-" + BatchUtils.getServerVersion())
+ .resolve("projects")
+ .resolve("proj");
+
+ assertThat(provider.provide(props, mode, key).getDirectory()).isEqualTo(expected);
+ }
+}
StrategyWSLoaderProvider provider = new StrategyWSLoaderProvider(LoadStrategy.CACHE_FIRST);
WSLoader wsLoader = provider.provide(cache, client);
- assertThat(wsLoader.getStrategy()).isEqualTo(LoadStrategy.CACHE_FIRST);
+ assertThat(wsLoader.getDefaultStrategy()).isEqualTo(LoadStrategy.CACHE_FIRST);
}
@Test
import org.mockito.InOrder;
import java.io.IOException;
+import java.io.InputStream;
import java.net.URI;
import static org.junit.Assert.*;
import org.mockito.Mock;
public class WSLoaderTest {
- private final static String ID = "/dummy";
+ private final static String ID = "dummy";
private final static String cacheValue = "cache";
private final static String serverValue = "server";
public void setUp() throws IOException {
MockitoAnnotations.initMocks(this);
when(client.load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt())).thenReturn(IOUtils.toInputStream(serverValue));
- when(cache.get(ID, null)).thenReturn(cacheValue.getBytes());
+ when(cache.getString(ID)).thenReturn(cacheValue);
when(client.getURI(anyString())).thenAnswer(new Answer<URI>() {
@Override
public URI answer(InvocationOnMock invocation) throws Throwable {
assertUsedCache(2);
}
+ @Test
+ public void get_stream_from_cache() throws IOException {
+ InputStream is = mock(InputStream.class);
+ when(cache.getStream(ID)).thenReturn(is);
+ WSLoader loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, client);
+ WSLoaderResult<InputStream> result = loader.loadStream(ID);
+ assertThat(result.get()).isEqualTo(is);
+ verify(cache).getStream(ID);
+
+ verifyNoMoreInteractions(cache, client);
+ }
+
+ @Test
+ public void put_stream_in_cache() throws IOException {
+ InputStream is1 = mock(InputStream.class);
+ InputStream is2 = mock(InputStream.class);
+
+ when(client.load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt())).thenReturn(is1);
+ when(cache.getStream(ID)).thenReturn(is2);
+
+ WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client);
+ WSLoaderResult<InputStream> result = loader.loadStream(ID);
+ assertThat(result.get()).isEqualTo(is2);
+
+ verify(client).load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt());
+ verify(cache).put(ID, is1);
+ verify(cache).getStream(ID);
+
+ verifyNoMoreInteractions(cache, client);
+ }
+
@Test
public void test_cache_strategy_fallback() throws IOException {
turnCacheEmpty();
WSLoader loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, client);
- assertResult(loader.load(ID), serverValue.getBytes(), false);
+ assertResult(loader.loadString(ID), serverValue, false);
InOrder inOrder = Mockito.inOrder(client, cache);
- inOrder.verify(cache).get(ID, null);
+ inOrder.verify(cache).getString(ID);
inOrder.verify(client).load(eq(ID), anyString(), anyBoolean(), anyInt(), anyInt());
}
InOrder inOrder = Mockito.inOrder(client, cache);
inOrder.verify(client).load(eq(ID), anyString(), anyBoolean(), anyInt(), anyInt());
- inOrder.verify(cache).get(ID, null);
+ inOrder.verify(cache).getString(ID);
}
@Test
public void test_put_cache() throws IOException {
WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client);
- loader.load(ID);
+ loader.loadString(ID);
verify(cache).put(ID, serverValue.getBytes());
}
public void test_throw_cache_exception_fallback() throws IOException {
turnServerOffline();
- when(cache.get(ID, null)).thenThrow(new NullPointerException());
+ when(cache.getString(ID)).thenThrow(new NullPointerException());
WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client);
try {
- loader.load(ID);
+ loader.loadString(ID);
fail("NPE expected");
} catch (NullPointerException e) {
assertUsedServer(1);
@Test
public void test_throw_cache_exception() throws IOException {
- when(cache.get(ID, null)).thenThrow(new IllegalStateException());
+ when(cache.getString(ID)).thenThrow(new IllegalStateException());
WSLoader loader = new WSLoader(LoadStrategy.CACHE_FIRST, cache, client);
try {
- loader.load(ID);
+ loader.loadString(ID);
fail("IllegalStateException expected");
} catch (IllegalStateException e) {
assertUsedServer(0);
WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client);
try {
- loader.load(ID);
+ loader.loadString(ID);
fail("IllegalStateException expected");
} catch (IllegalStateException e) {
// cache should not be used
exception.expectMessage(Matchers.is("Server is not available"));
WSLoader loader = new WSLoader(LoadStrategy.SERVER_ONLY, cache, client);
- loader.load(ID);
+ loader.loadString(ID);
}
@Test
exception.expectMessage(Matchers.is("Server is not accessible and data is not cached"));
WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client);
- loader.load(ID);
+ loader.loadString(ID);
}
@Test
exception.expectMessage(Matchers.is("Data is not cached"));
WSLoader loader = new WSLoader(LoadStrategy.CACHE_ONLY, cache, client);
- loader.load(ID);
+ loader.loadString(ID);
}
@Test
public void test_server_strategy() throws IOException {
WSLoader loader = new WSLoader(LoadStrategy.SERVER_FIRST, cache, client);
- assertResult(loader.load(ID), serverValue.getBytes(), false);
+ assertResult(loader.loadString(ID), serverValue, false);
// should not fetch from cache
verify(cache).put(ID, serverValue.getBytes());
public void test_server_only() throws IOException {
turnServerOffline();
WSLoader loader = new WSLoader(LoadStrategy.SERVER_ONLY, cache, client);
- loader.load(ID);
+ loader.loadString(ID);
}
@Test
}
private void assertUsedCache(int times) throws IOException {
- verify(cache, times(times)).get(ID, null);
+ verify(cache, times(times)).getString(ID);
}
private void assertUsedServer(int times) {
verify(client, times(times)).load(anyString(), anyString(), anyBoolean(), anyInt(), anyInt());
}
- private <T> void assertResult(WSLoaderResult<T> result, T expected, boolean fromCache) {
+ private void assertResult(WSLoaderResult<InputStream> result, byte[] expected, boolean fromCache) throws IOException {
+ byte[] content = IOUtils.toByteArray(result.get());
+ assertThat(result).isNotNull();
+ assertThat(content).isEqualTo(expected);
+ assertThat(result.isFromCache()).isEqualTo(fromCache);
+ }
+
+ private void assertResult(WSLoaderResult<String> result, String expected, boolean fromCache) {
assertThat(result).isNotNull();
assertThat(result.get()).isEqualTo(expected);
assertThat(result.isFromCache()).isEqualTo(fromCache);
}
private void turnCacheEmpty() throws IOException {
- when(cache.get(ID, null)).thenReturn(null);
+ when(cache.getString(ID)).thenReturn(null);
}
}
private Batch batch;
private static Path workingDir = null;
private static Path globalWorkingDir = null;
-
+
private static void createWorkingDirs() throws IOException {
destroyWorkingDirs();
-
+
workingDir = java.nio.file.Files.createTempDirectory("mediumtest-working-dir");
globalWorkingDir = java.nio.file.Files.createTempDirectory("mediumtest-global-working-dir");
}
-
+
private static void destroyWorkingDirs() throws IOException {
- if(workingDir != null) {
+ if (workingDir != null) {
FileUtils.deleteDirectory(workingDir.toFile());
workingDir = null;
}
-
- if(globalWorkingDir != null) {
+
+ if (globalWorkingDir != null) {
FileUtils.deleteDirectory(globalWorkingDir.toFile());
globalWorkingDir = null;
}
-
+
}
public static BatchMediumTesterBuilder builder() {
} catch (IOException e) {
e.printStackTrace();
}
-
+
BatchMediumTesterBuilder builder = new BatchMediumTesterBuilder().registerCoreMetrics();
builder.bootstrapProperties.put(MEDIUM_TEST_ENABLED, "true");
builder.bootstrapProperties.put(ReportPublisher.KEEP_REPORT_PROP_KEY, "true");
private static class FakeProjectCacheStatus implements ProjectCacheStatus {
@Override
- public void save(String projectKey) {
+ public void save() {
}
@Override
- public void delete(String projectKey) {
+ public void delete() {
}
@Override
- public Date getSyncStatus(String projectKey) {
+ public Date getSyncStatus() {
return new Date();
}
package org.sonar.batch.repository;
import org.sonar.batch.cache.WSLoaderResult;
-
import org.sonar.batch.cache.WSLoader;
-import com.google.common.io.ByteSource;
import com.google.common.base.Function;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@Test
public void loadFromWs() throws Exception {
- ByteSource bs = mock(ByteSource.class);
- when(wsLoader.loadSource("/scanner/issues?key=foo")).thenReturn(new WSLoaderResult<>(bs, true));
-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ServerIssue.newBuilder().setKey("ab1").build()
ServerIssue.newBuilder().setKey("ab2").build()
.writeDelimitedTo(bos);
- when(bs.openBufferedStream()).thenReturn(new ByteArrayInputStream(bos.toByteArray()));
+ InputStream is = new ByteArrayInputStream(bos.toByteArray());
+ when(wsLoader.loadStream("/scanner/issues?key=foo")).thenReturn(new WSLoaderResult<>(is, true));
final List<ServerIssue> result = new ArrayList<>();
loader.load("foo", new Function<BatchInput.ServerIssue, Void>() {
@Test(expected = IllegalStateException.class)
public void testError() throws IOException {
+<<<<<<< HEAD
ByteSource source = mock(ByteSource.class);
when(source.openBufferedStream()).thenThrow(IOException.class);
when(wsLoader.loadSource("/scanner/issues?key=foo")).thenReturn(new WSLoaderResult<ByteSource>(source, true));
+=======
+ InputStream is = mock(InputStream.class);
+ when(is.read()).thenThrow(IOException.class);
+ when(wsLoader.loadStream("/batch/issues?key=foo")).thenReturn(new WSLoaderResult<InputStream>(is, true));
+>>>>>>> SONAR-6777 Project cache sync
loader.load("foo", mock(Function.class));
}
}
import org.junit.rules.ExpectedException;
import org.junit.Rule;
import org.mockito.Mockito;
-import com.google.common.io.ByteSource;
import org.junit.Test;
import org.sonar.batch.protocol.input.BatchInput;
public void testLoadEmptyList() {
assertThat(userRepo.load(Lists.<String>emptyList())).isEmpty();
}
-
+
@Test
public void testLoad() throws IOException {
Map<String, String> userMap = ImmutableMap.of("fmallet", "Freddy Mallet", "sbrandhof", "Simon");
- WSLoaderResult<ByteSource> res = new WSLoaderResult<>(createUsersMock(userMap), true);
- when(wsLoader.loadSource("/scanner/users?logins=fmallet,sbrandhof")).thenReturn(res);
+ WSLoaderResult<InputStream> res = new WSLoaderResult<>(createUsersMock(userMap), true);
+ when(wsLoader.loadStream("/scanner/users?logins=fmallet,sbrandhof")).thenReturn(res);
assertThat(userRepo.load(Arrays.asList("fmallet", "sbrandhof"))).extracting("login", "name").containsOnly(tuple("fmallet", "Freddy Mallet"), tuple("sbrandhof", "Simon"));
}
@Test
public void testFromCache() throws IOException {
- WSLoaderResult<ByteSource> res = new WSLoaderResult<>(createUsersMock(ImmutableMap.of("fmallet", "Freddy Mallet")), true);
- when(wsLoader.loadSource(anyString())).thenReturn(res);
+ WSLoaderResult<InputStream> res = new WSLoaderResult<>(createUsersMock(ImmutableMap.of("fmallet", "Freddy Mallet")), true);
+ when(wsLoader.loadStream(anyString())).thenReturn(res);
MutableBoolean fromCache = new MutableBoolean();
userRepo.load("", fromCache);
assertThat(fromCache.booleanValue()).isTrue();
@Test
public void testLoadSingleUser() throws IOException {
+<<<<<<< HEAD
WSLoaderResult<ByteSource> res = new WSLoaderResult<>(createUsersMock(ImmutableMap.of("fmallet", "Freddy Mallet")), true);
when(wsLoader.loadSource("/scanner/users?logins=fmallet")).thenReturn(res);
+=======
+ WSLoaderResult<InputStream> res = new WSLoaderResult<>(createUsersMock(ImmutableMap.of("fmallet", "Freddy Mallet")), true);
+ when(wsLoader.loadStream("/batch/users?logins=fmallet")).thenReturn(res);
+>>>>>>> SONAR-6777 Project cache sync
assertThat(userRepo.load("fmallet").getName()).isEqualTo("Freddy Mallet");
}
- private ByteSource createUsersMock(Map<String, String> users) throws IOException {
+ private InputStream createUsersMock(Map<String, String> users) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (Map.Entry<String, String> user : users.entrySet()) {
BatchInput.User.Builder builder = BatchInput.User.newBuilder();
builder.setLogin(user.getKey()).setName(user.getValue()).build().writeDelimitedTo(out);
}
- ByteSource source = mock(ByteSource.class);
- when(source.openStream()).thenReturn(new ByteArrayInputStream(out.toByteArray()));
- return source;
+ return new ByteArrayInputStream(out.toByteArray());
}
@Test
public void testInputStreamError() throws IOException {
+<<<<<<< HEAD
ByteSource source = mock(ByteSource.class);
WSLoaderResult<ByteSource> res = new WSLoaderResult<>(source, true);
when(wsLoader.loadSource("/scanner/users?logins=fmallet,sbrandhof")).thenReturn(res);
+=======
+ InputStream is = mock(InputStream.class);
+ Mockito.doThrow(IOException.class).when(is).read();
+ WSLoaderResult<InputStream> res = new WSLoaderResult<>(is, true);
+>>>>>>> SONAR-6777 Project cache sync
- InputStream errorInputStream = mock(InputStream.class);
- Mockito.doThrow(IOException.class).when(errorInputStream).read();
- when(source.openStream()).thenReturn(errorInputStream);
+ when(wsLoader.loadStream("/batch/users?logins=fmallet,sbrandhof")).thenReturn(res);
exception.expect(IllegalStateException.class);
exception.expectMessage("Unable to get user details from server");
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-
import org.junit.rules.ExpectedException;
-
import org.sonar.batch.cache.WSLoaderResult;
import org.sonar.batch.cache.WSLoader;
import org.apache.commons.lang.mutable.MutableBoolean;
import com.google.common.io.Resources;
import java.io.IOException;
+import java.io.InputStream;
import java.util.List;
import static org.mockito.Matchers.anyString;
@Test
public void testParseServerResponse() throws IOException {
WSLoader wsLoader = mock(WSLoader.class);
- ByteSource source = Resources.asByteSource(this.getClass().getResource("DefaultRulesLoader/response.protobuf"));
- when(wsLoader.loadSource(anyString())).thenReturn(new WSLoaderResult<>(source, true));
+ InputStream is = Resources.asByteSource(this.getClass().getResource("DefaultRulesLoader/response.protobuf")).openBufferedStream();
+ when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, true));
DefaultRulesLoader loader = new DefaultRulesLoader(wsLoader);
List<Rule> ruleList = loader.load(null);
assertThat(ruleList).hasSize(318);
}
@Test
- public void testLoadedFromCache() {
+ public void testLoadedFromCache() throws IOException {
WSLoader wsLoader = mock(WSLoader.class);
- ByteSource source = Resources.asByteSource(this.getClass().getResource("DefaultRulesLoader/response.protobuf"));
- when(wsLoader.loadSource(anyString())).thenReturn(new WSLoaderResult<>(source, true));
+ InputStream is = Resources.asByteSource(this.getClass().getResource("DefaultRulesLoader/response.protobuf")).openBufferedStream();
+ when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, true));
DefaultRulesLoader loader = new DefaultRulesLoader(wsLoader);
MutableBoolean fromCache = new MutableBoolean();
loader.load(fromCache);
}
@Test
- public void testError() {
+ public void testError() throws IOException {
WSLoader wsLoader = mock(WSLoader.class);
- ByteSource source = ByteSource.wrap(new String("trash").getBytes());
- when(wsLoader.loadSource(anyString())).thenReturn(new WSLoaderResult<>(source, true));
+ InputStream is = ByteSource.wrap(new String("trash").getBytes()).openBufferedStream();
+ when(wsLoader.loadStream(anyString())).thenReturn(new WSLoaderResult<>(is, true));
DefaultRulesLoader loader = new DefaultRulesLoader(wsLoader);
exception.expect(IllegalStateException.class);
public void errorLock() {
lock = setUpTest(Paths.get("path", "that", "wont", "exist", "ever").toFile());
exception.expect(IllegalStateException.class);
- exception.expectMessage("Failed to create project lock in");
+ exception.expectMessage("Failed to create lock in");
lock.tryLock();
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+public class DeleteFileOnCloseInputStream extends InputStream {
+ private final InputStream is;
+ private final Path p;
+
+ public DeleteFileOnCloseInputStream(InputStream stream, Path p) {
+ this.is = stream;
+ this.p = p;
+ }
+
+ @Override
+ public int read() throws IOException {
+ return is.read();
+ }
+
+ @Override
+ public int read(byte[] b) throws IOException {
+ return is.read(b);
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ return is.read(b, off, len);
+ }
+
+ @Override
+ public long skip(long n) throws IOException {
+ return is.skip(n);
+ }
+
+ @Override
+ public synchronized void mark(int readlimit) {
+ is.mark(readlimit);
+ }
+
+ @Override
+ public synchronized void reset() throws IOException {
+ is.reset();
+ }
+
+ @Override
+ public int available() throws IOException {
+ return is.available();
+ }
+
+ @Override
+ public boolean markSupported() {
+ return is.markSupported();
+ }
+
+ @Override
+ public void close() throws IOException {
+ try {
+ super.close();
+ } finally {
+ Files.delete(p);
+ }
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.io.PrintWriter;
+import java.io.RandomAccessFile;
+import java.io.StringWriter;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+public class DirectoryLock {
+ static final String LOCK_FILE_NAME = ".sonar_lock";
+ private final Path lockFilePath;
+ private final Logger logger;
+
+ private RandomAccessFile lockRandomAccessFile;
+ private FileChannel lockChannel;
+ private FileLock lockFile;
+
+ public DirectoryLock(Path directory, Logger logger) {
+ this.logger = logger;
+ this.lockFilePath = directory.resolve(LOCK_FILE_NAME).toAbsolutePath();
+ }
+
+ public String getFileLockName() {
+ return LOCK_FILE_NAME;
+ }
+
+ public void lock() {
+ try {
+ lockRandomAccessFile = new RandomAccessFile(lockFilePath.toFile(), "rw");
+ lockChannel = lockRandomAccessFile.getChannel();
+ lockFile = lockChannel.lock(0, 1024, false);
+ } catch (IOException e) {
+ throw new IllegalStateException("Failed to create lock in " + lockFilePath.toString(), e);
+ }
+ }
+
+ public boolean tryLock() {
+ try {
+ lockRandomAccessFile = new RandomAccessFile(lockFilePath.toFile(), "rw");
+ lockChannel = lockRandomAccessFile.getChannel();
+ lockFile = lockChannel.tryLock(0, 1024, false);
+
+ return lockFile != null;
+ } catch (IOException e) {
+ throw new IllegalStateException("Failed to create lock in " + lockFilePath.toString(), e);
+ }
+ }
+
+ public void unlock() {
+ if (lockFile != null) {
+ try {
+ lockFile.release();
+ lockFile = null;
+ } catch (IOException e) {
+ logger.error("Error releasing lock", e);
+ }
+ }
+ if (lockChannel != null) {
+ try {
+ lockChannel.close();
+ lockChannel = null;
+ } catch (IOException e) {
+ logger.error("Error closing file channel", e);
+ }
+ }
+ if (lockRandomAccessFile != null) {
+ try {
+ lockRandomAccessFile.close();
+ lockRandomAccessFile = null;
+ } catch (IOException e) {
+ logger.error("Error closing file", e);
+ }
+ }
+
+ try {
+ Files.delete(lockFilePath);
+ } catch (IOException e) {
+ // ignore, as an error happens if another process just started to acquire the same lock
+ StringWriter errors = new StringWriter();
+ e.printStackTrace(new PrintWriter(errors));
+ logger.debug("Couldn't delete lock file: " + lockFilePath.toString() + " " + errors.toString());
+ }
+ }
+}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.RandomAccessFile;
-import java.nio.channels.FileChannel;
-import java.nio.channels.FileLock;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
private static final Charset ENCODING = StandardCharsets.UTF_8;
private static final String DIGEST_ALGO = "MD5";
- private static final String LOCK_FNAME = ".lock";
// eviction strategy is to expire entries after modification once a time duration has elapsed
private final long defaultDurationToExpireMs;
private final Logger logger;
- private final String version;
- private final Path baseDir;
+ private final Path dir;
+ private DirectoryLock lock;
- public PersistentCache(Path baseDir, long defaultDurationToExpireMs, Logger logger, String version) {
- this.baseDir = baseDir;
+ public PersistentCache(Path dir, long defaultDurationToExpireMs, Logger logger, DirectoryLock lock) {
+ this.dir = dir;
this.defaultDurationToExpireMs = defaultDurationToExpireMs;
this.logger = logger;
- this.version = version;
+ this.lock = lock;
reconfigure();
- logger.debug("cache: " + baseDir + ", default expiration time (ms): " + defaultDurationToExpireMs);
+ logger.debug("cache: " + dir + ", default expiration time (ms): " + defaultDurationToExpireMs);
}
public synchronized void reconfigure() {
try {
- Files.createDirectories(baseDir);
+ Files.createDirectories(dir);
} catch (IOException e) {
throw new IllegalStateException("failed to create cache dir", e);
}
}
- public Path getBaseDirectory() {
- return baseDir;
+ public Path getDirectory() {
+ return dir;
}
@CheckForNull
- public synchronized String getString(@Nonnull String obj, @Nullable final PersistentCacheLoader<String> valueLoader) throws IOException {
- ValueLoaderDecoder decoder = valueLoader != null ? new ValueLoaderDecoder(valueLoader) : null;
-
- byte[] cached = get(obj, decoder);
+ public synchronized String getString(@Nonnull String obj) throws IOException {
+ byte[] cached = get(obj);
if (cached == null) {
return null;
try {
lock();
Path path = getCacheCopy(key);
- return new DeleteOnCloseInputStream(new FileInputStream(path.toFile()), path);
+ return new DeleteFileOnCloseInputStream(new FileInputStream(path.toFile()), path);
} finally {
unlock();
}
@CheckForNull
- public synchronized byte[] get(@Nonnull String obj, @Nullable PersistentCacheLoader<byte[]> valueLoader) throws IOException {
+ public synchronized byte[] get(@Nonnull String obj) throws IOException {
String key = getKey(obj);
try {
}
logger.debug("cache miss for " + obj + " -> " + key);
-
- if (valueLoader != null) {
- byte[] value = valueLoader.get();
- if (value != null) {
- putCache(key, value);
- }
- return value;
- }
} finally {
unlock();
}
logger.info("cache: clearing");
try {
lock();
- deleteCacheEntries(new DirectoryClearFilter());
+ deleteCacheEntries(new DirectoryClearFilter(lock.getFileLockName()));
} catch (IOException e) {
logger.error("Error clearing cache", e);
} finally {
logger.info("cache: cleaning");
try {
lock();
- deleteCacheEntries(new DirectoryCleanFilter(defaultDurationToExpireMs));
+ deleteCacheEntries(new DirectoryCleanFilter(defaultDurationToExpireMs, lock.getFileLockName()));
} catch (IOException e) {
logger.error("Error cleaning cache", e);
} finally {
}
private void lock() throws IOException {
- lockRandomAccessFile = new RandomAccessFile(getLockPath().toFile(), "rw");
- lockChannel = lockRandomAccessFile.getChannel();
- lockFile = lockChannel.lock();
+ lock.lock();
}
- private RandomAccessFile lockRandomAccessFile;
- private FileChannel lockChannel;
- private FileLock lockFile;
-
private void unlock() {
- if (lockFile != null) {
- try {
- lockFile.release();
- } catch (IOException e) {
- logger.error("Error releasing lock", e);
- }
- }
- if (lockChannel != null) {
- try {
- lockChannel.close();
- } catch (IOException e) {
- logger.error("Error closing file channel", e);
- }
- }
- if (lockRandomAccessFile != null) {
- try {
- lockRandomAccessFile.close();
- } catch (IOException e) {
- logger.error("Error closing file", e);
- }
- }
-
- lockFile = null;
- lockRandomAccessFile = null;
- lockChannel = null;
+ lock.unlock();
}
private String getKey(String uri) {
try {
String key = uri;
- if (version != null) {
- key += version;
- }
MessageDigest digest = MessageDigest.getInstance(DIGEST_ALGO);
digest.update(key.getBytes(StandardCharsets.UTF_8));
return byteArrayToHex(digest.digest());
}
private void deleteCacheEntries(DirectoryStream.Filter<Path> filter) throws IOException {
- try (DirectoryStream<Path> stream = Files.newDirectoryStream(baseDir, filter)) {
+ try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) {
for (Path p : stream) {
try {
Files.delete(p);
}
}
- private static class ValueLoaderDecoder implements PersistentCacheLoader<byte[]> {
- PersistentCacheLoader<String> valueLoader;
+ private static class DirectoryClearFilter implements DirectoryStream.Filter<Path> {
+ private String lockFileName;
- ValueLoaderDecoder(PersistentCacheLoader<String> valueLoader) {
- this.valueLoader = valueLoader;
+ DirectoryClearFilter(String lockFileName) {
+ this.lockFileName = lockFileName;
}
- @Override
- public byte[] get() throws IOException {
- String s = valueLoader.get();
- if (s != null) {
- return s.getBytes(ENCODING);
- }
- return null;
- }
- }
-
- private static class DirectoryClearFilter implements DirectoryStream.Filter<Path> {
@Override
public boolean accept(Path entry) throws IOException {
- return !LOCK_FNAME.equals(entry.getFileName().toString());
+ return !lockFileName.equals(entry.getFileName().toString());
}
}
private static class DirectoryCleanFilter implements DirectoryStream.Filter<Path> {
private long defaultDurationToExpireMs;
+ private String lockFileName;
- DirectoryCleanFilter(long defaultDurationToExpireMs) {
+ DirectoryCleanFilter(long defaultDurationToExpireMs, String lockFileName) {
this.defaultDurationToExpireMs = defaultDurationToExpireMs;
+ this.lockFileName = lockFileName;
}
@Override
public boolean accept(Path entry) throws IOException {
- if (LOCK_FNAME.equals(entry.getFileName().toString())) {
+ if (lockFileName.equals(entry.getFileName().toString())) {
return false;
}
return temp;
}
- private static class DeleteOnCloseInputStream extends InputStream {
- private final InputStream stream;
- private final Path p;
-
- private DeleteOnCloseInputStream(InputStream stream, Path p) {
- this.stream = stream;
- this.p = p;
- }
-
- @Override
- public int read() throws IOException {
- return stream.read();
- }
-
- @Override
- public void close() throws IOException {
- stream.close();
- Files.delete(p);
- }
- }
-
private boolean validateCacheEntry(Path cacheEntryPath, long durationToExpireMs) throws IOException {
if (!Files.exists(cacheEntryPath)) {
return false;
return false;
}
- private Path getLockPath() {
- return baseDir.resolve(LOCK_FNAME);
- }
-
private Path getCacheEntryPath(String key) {
- return baseDir.resolve(key);
+ return dir.resolve(key);
}
public static String byteArrayToHex(byte[] bytes) {
*/
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>
+ * <sonar_home>/ws_cache/<server_url>-<version>/projects/<project>/
+ * <sonar_home>/ws_cache/<server_url>-<version>/global/
+ * <sonar_home>/ws_cache/<server_url>-<version>/local/
+ * </pre>
+ */
public class PersistentCacheBuilder {
private static final long DEFAULT_EXPIRE_DURATION = TimeUnit.MILLISECONDS.convert(1L, TimeUnit.DAYS);
private static final String DIR_NAME = "ws_cache";
- private Path cachePath;
+ private Path cacheBasePath;
+ private Path relativePath;
private final Logger logger;
- private String version;
public PersistentCacheBuilder(Logger logger) {
this.logger = logger;
}
- public PersistentCache build() {
- if (cachePath == null) {
- setSonarHome(findHome());
- }
-
- return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, logger, version);
+ public PersistentCacheBuilder setAreaForProject(String serverUrl, String serverVersion, String projectKey) {
+ relativePath = Paths.get(sanitizeFilename(serverUrl + "-" + serverVersion))
+ .resolve("projects")
+ .resolve(sanitizeFilename(projectKey));
+ return this;
}
-
- public PersistentCacheBuilder setVersion(String version) {
- this.version = version;
+
+ public PersistentCacheBuilder setAreaForGlobal(String serverUrl, String serverVersion) {
+ relativePath = Paths.get(sanitizeFilename(serverUrl + "-" + serverVersion))
+ .resolve("global");
return this;
}
-
+
+ public PersistentCacheBuilder setAreaForLocalProject(String serverUrl, String serverVersion) {
+ relativePath = Paths.get(sanitizeFilename(serverUrl + "-" + serverVersion))
+ .resolve("local");
+ return this;
+ }
+
public PersistentCacheBuilder setSonarHome(@Nullable Path p) {
if (p != null) {
- this.cachePath = p.resolve(DIR_NAME);
+ 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);
+ return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, logger, lock);
+ }
+
private static Path findHome() {
String home = System.getenv("SONAR_USER_HOME");
home = System.getProperty("user.home");
return Paths.get(home, ".sonar");
}
+
+ private 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);
+ }
+ }
}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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;
-
-public interface PersistentCacheLoader<T> {
- T get() throws IOException;
-}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.io.InputStream;
+
+public class UnlockOnCloseInputStream extends InputStream {
+ private final DirectoryLock lock;
+ private final InputStream is;
+
+ public UnlockOnCloseInputStream(InputStream stream, DirectoryLock lock) {
+ this.is = stream;
+ this.lock = lock;
+ }
+
+ @Override
+ public int read() throws IOException {
+ return is.read();
+ }
+
+ @Override
+ public int read(byte[] b) throws IOException {
+ return is.read(b);
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ return is.read(b, off, len);
+ }
+
+ @Override
+ public long skip(long n) throws IOException {
+ return is.skip(n);
+ }
+
+ @Override
+ public synchronized void mark(int readlimit) {
+ is.mark(readlimit);
+ }
+
+ @Override
+ public synchronized void reset() throws IOException {
+ is.reset();
+ }
+
+ @Override
+ public int available() throws IOException {
+ return is.available();
+ }
+
+ @Override
+ public boolean markSupported() {
+ return is.markSupported();
+ }
+
+ @Override
+ public void close() throws IOException {
+ try {
+ super.close();
+ } finally {
+ lock.unlock();
+ }
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 static org.mockito.Mockito.mock;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.rules.ExpectedException;
+
+import java.nio.channels.OverlappingFileLockException;
+import java.nio.file.Paths;
+
+import org.junit.Test;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.rules.TemporaryFolder;
+
+public class DirectoryLockTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+ private DirectoryLock lock;
+
+ @Before
+ public void setUp() {
+ lock = new DirectoryLock(temp.getRoot().toPath(), mock(Logger.class));
+ }
+
+ @Test
+ public void lock() {
+ assertThat(temp.getRoot().list()).isEmpty();
+ lock.lock();
+ assertThat(temp.getRoot().toPath().resolve(".sonar_lock")).exists();
+ lock.unlock();
+ assertThat(temp.getRoot().list()).isEmpty();
+ }
+
+ @Test
+ public void tryLock() {
+ assertThat(temp.getRoot().list()).isEmpty();
+ lock.tryLock();
+ assertThat(temp.getRoot().toPath().resolve(".sonar_lock")).exists();
+ lock.unlock();
+ assertThat(temp.getRoot().list()).isEmpty();
+ }
+
+ @Test(expected = OverlappingFileLockException.class)
+ public void error_2locks() {
+ assertThat(temp.getRoot().list()).isEmpty();
+ lock.lock();
+ lock.lock();
+ }
+
+ @Test
+ public void unlockWithoutLock() {
+ lock.unlock();
+ }
+
+ @Test
+ public void errorCreatingLock() {
+ lock = new DirectoryLock(Paths.get("non", "existing", "path"), mock(Logger.class));
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Failed to create lock");
+ lock.lock();
+ }
+
+ @Test
+ public void errorTryLock() {
+ lock = new DirectoryLock(Paths.get("non", "existing", "path"), mock(Logger.class));
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Failed to create lock");
+ lock.tryLock();
+ }
+}
package org.sonar.home.cache;
import java.nio.file.Files;
+import java.nio.file.Paths;
+
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
@Test
public void user_home_property_can_be_null() {
- PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setSonarHome(null).build();
- assertTrue(Files.isDirectory(cache.getBaseDirectory()));
- assertThat(cache.getBaseDirectory().getFileName().toString()).isEqualTo("ws_cache");
+ PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setSonarHome(null).setAreaForGlobal("url", "0").build();
+ assertTrue(Files.isDirectory(cache.getDirectory()));
+ assertThat(cache.getDirectory()).endsWith(Paths.get("url-0", "global"));
}
@Test
public void set_user_home() {
- PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setSonarHome(temp.getRoot().toPath()).build();
+ PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setSonarHome(temp.getRoot().toPath()).setAreaForGlobal("url", "0").build();
- assertThat(cache.getBaseDirectory().getParent().toString()).isEqualTo(temp.getRoot().toPath().toString());
- assertTrue(Files.isDirectory(cache.getBaseDirectory()));
+ assertThat(cache.getDirectory()).isDirectory();
+ assertThat(cache.getDirectory()).startsWith(temp.getRoot().toPath());
+ assertTrue(Files.isDirectory(cache.getDirectory()));
}
@Test
System.setProperty("user.home", temp.getRoot().getAbsolutePath());
- PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).build();
- assertTrue(Files.isDirectory(cache.getBaseDirectory()));
- assertThat(cache.getBaseDirectory().getFileName().toString()).isEqualTo("ws_cache");
+ PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setAreaForGlobal("url", "0").build();
+ assertTrue(Files.isDirectory(cache.getDirectory()));
+ assertThat(cache.getDirectory()).startsWith(temp.getRoot().toPath());
+ }
+
+ @Test
+ public void directories() {
+ System.setProperty("user.home", temp.getRoot().getAbsolutePath());
+
+ PersistentCache cache = new PersistentCacheBuilder(mock(Logger.class)).setAreaForProject("url", "0", "proj").build();
+ assertThat(cache.getDirectory()).endsWith(Paths.get(".sonar", "ws_cache", "url-0", "projects", "proj"));
+
+ cache = new PersistentCacheBuilder(mock(Logger.class)).setAreaForLocalProject("url", "0").build();
+ assertThat(cache.getDirectory()).endsWith(Paths.get(".sonar", "ws_cache", "url-0", "local"));
- String expectedSonarHome = temp.getRoot().toPath().resolve(".sonar").toString();
- assertThat(cache.getBaseDirectory().getParent().toString()).isEqualTo(expectedSonarHome);
+ cache = new PersistentCacheBuilder(mock(Logger.class)).setAreaForGlobal("url", "0").build();
+ assertThat(cache.getDirectory()).endsWith(Paths.get(".sonar", "ws_cache", "url-0", "global"));
}
}
package org.sonar.home.cache;
import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.atLeast;
+import static org.mockito.Mockito.verify;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
public class PersistentCacheTest {
private final static String URI = "key1";
private final static String VALUE = "cache content";
private PersistentCache cache = null;
+ private DirectoryLock lock = null;
@Rule
public TemporaryFolder tmp = new TemporaryFolder();
@Before
public void setUp() {
- cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null);
+ lock = mock(DirectoryLock.class);
+ when(lock.getFileLockName()).thenReturn("lock");
+ cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), lock);
}
@Test
assertCacheHit(false);
}
- @Test
- public void testNullLoader() throws Exception {
- assertThat(cache.get(URI, null)).isNull();
- assertCacheHit(false);
- }
-
- @Test
- public void testNullLoaderString() throws Exception {
- assertThat(cache.getString(URI, null)).isNull();
- assertCacheHit(false);
- }
-
- @Test
- public void testNullValue() throws Exception {
- // mocks have their methods returning null by default
- PersistentCacheLoader<byte[]> c = mock(PersistentCacheLoader.class);
- assertThat(cache.get(URI, c)).isNull();
- verify(c).get();
- assertCacheHit(false);
- }
-
@Test
public void testClean() throws Exception {
+ Path lockFile = cache.getDirectory().resolve("lock");
// puts entry
- assertCacheHit(false);
+ cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
+ Files.write(lockFile, "test".getBytes(StandardCharsets.UTF_8));
+ assertCacheHit(true);
// negative time to make sure it is expired
- cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), null);
+ cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), lock);
cache.clean();
assertCacheHit(false);
+ // lock file should not get deleted
+ assertThat(new String(Files.readAllBytes(lockFile), StandardCharsets.UTF_8)).isEqualTo("test");
}
@Test
public void testClear() throws Exception {
- assertCacheHit(false);
+ Path lockFile = cache.getDirectory().resolve("lock");
+ cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
+ Files.write(lockFile, "test".getBytes(StandardCharsets.UTF_8));
+ assertCacheHit(true);
cache.clear();
assertCacheHit(false);
+ // lock file should not get deleted
+ assertThat(new String(Files.readAllBytes(lockFile), StandardCharsets.UTF_8)).isEqualTo("test");
}
@Test
public void testCacheHit() throws Exception {
- assertCacheHit(false);
- assertCacheHit(true);
- }
-
- @Test
- public void testPut() throws Exception {
- cache.put(URI, VALUE.getBytes());
+ cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
assertCacheHit(true);
}
@Test
public void testReconfigure() throws Exception {
- cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null);
+ cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), lock);
assertCacheHit(false);
+ cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
assertCacheHit(true);
File root = tmp.getRoot();
assertThat(root).exists();
assertCacheHit(false);
+ cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
assertCacheHit(true);
}
@Test
public void testExpiration() throws Exception {
- // negative time to make sure it is expired on the second call
- cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), null);
- assertCacheHit(false);
- assertCacheHit(false);
- }
-
- @Test
- public void testDifferentServerVersions() throws Exception {
+ // negative time to make sure it is expired
+ cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), lock);
+ cache.put(URI, VALUE.getBytes(StandardCharsets.UTF_8));
assertCacheHit(false);
- assertCacheHit(true);
-
- PersistentCache cache2 = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), "5.2");
- assertCacheHit(cache2, false);
- assertCacheHit(cache2, true);
-
}
private void assertCacheHit(boolean hit) throws Exception {
}
private void assertCacheHit(PersistentCache pCache, boolean hit) throws Exception {
- CacheFillerString c = new CacheFillerString();
- assertThat(pCache.getString(URI, c)).isEqualTo(VALUE);
- assertThat(c.wasCalled).isEqualTo(!hit);
- }
-
- private class CacheFillerString implements PersistentCacheLoader<String> {
- public boolean wasCalled = false;
-
- @Override
- public String get() {
- wasCalled = true;
- return VALUE;
- }
- }
-
- /**
- * WSCache should be transparent regarding exceptions: if an exception is thrown by the value loader, it should pass through
- * the cache to the original caller using the cache.
- * @throws Exception
- */
- @Test(expected = ArithmeticException.class)
- public void testExceptions() throws Exception {
- PersistentCacheLoader<byte[]> c = mock(PersistentCacheLoader.class);
- when(c.get()).thenThrow(ArithmeticException.class);
- cache.get(URI, c);
+ String expected = hit ? VALUE : null;
+ assertThat(pCache.getString(URI)).isEqualTo(expected);
+ verify(lock, atLeast(1)).unlock();
}
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.api.batch.bootstrap;
+
+public interface ProjectKey {
+ String get();
+}
* @since 2.9
*/
@BatchSide
-public class ProjectReactor {
+public class ProjectReactor implements ProjectKey {
private ProjectDefinition root;
}
return null;
}
+
+ @Override
+ public String get() {
+ if (root != null) {
+ return root.getKeyWithBranch();
+ }
+ return null;
+ }
}