aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2018-08-21 14:14:48 +0200
committerSonarTech <sonartech@sonarsource.com>2018-08-21 20:21:04 +0200
commit2fdab0c755a5f4cf22a3cd15843703d5947c9b15 (patch)
tree3cd3f357a5c8b76b42991144b8029b560ffa0410 /sonar-plugin-api
parent01e36514435cede2ecbb6acd28266fa608168fb9 (diff)
downloadsonarqube-2fdab0c755a5f4cf22a3cd15843703d5947c9b15.tar.gz
sonarqube-2fdab0c755a5f4cf22a3cd15843703d5947c9b15.zip
SONAR-11169 make log interceptor concurrent and thread safe
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ListInterceptor.java32
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java6
2 files changed, 19 insertions, 19 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ListInterceptor.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ListInterceptor.java
index 3bd9d5a6928..3d2a23d99a0 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ListInterceptor.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ListInterceptor.java
@@ -19,19 +19,19 @@
*/
package org.sonar.api.utils.log;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.ListMultimap;
-import java.util.ArrayList;
+import com.google.common.collect.ImmutableList;
+import java.util.Collections;
import java.util.List;
-import java.util.Optional;
-import java.util.function.Predicate;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
public class ListInterceptor implements LogInterceptor {
- private final List<LogAndArguments> logs = new ArrayList<>();
- private final ListMultimap<LoggerLevel, LogAndArguments> logsByLevel = ArrayListMultimap.create();
+ private final List<LogAndArguments> logs = new CopyOnWriteArrayList<>();
+ private final Map<LoggerLevel, List<LogAndArguments>> logsByLevel = new ConcurrentHashMap<>();
@Override
public void log(LoggerLevel level, String msg) {
@@ -68,19 +68,25 @@ public class ListInterceptor implements LogInterceptor {
private void add(LoggerLevel level, LogAndArguments l) {
logs.add(l);
- logsByLevel.put(level, l);
+ logsByLevel.compute(level, (key, existingList) -> {
+ if (existingList == null) {
+ return new CopyOnWriteArrayList<>(new LogAndArguments[] {l});
+ }
+ existingList.add(l);
+ return existingList;
+ });
}
public List<String> logs() {
return logs.stream().map(LogAndArguments::getFormattedMsg).collect(Collectors.toList());
}
- public Optional<String> findFirst(Predicate<String> logPredicate) {
- return logs.stream().map(LogAndArguments::getFormattedMsg).filter(logPredicate).findFirst();
- }
-
public List<String> logs(LoggerLevel level) {
- return logsByLevel.get(level).stream().map(LogAndArguments::getFormattedMsg).collect(Collectors.toList());
+ List<LogAndArguments> res = logsByLevel.get(level);
+ if (res == null) {
+ return Collections.emptyList();
+ }
+ return ImmutableList.copyOf(res).stream().map(LogAndArguments::getFormattedMsg).collect(Collectors.toList());
}
public List<LogAndArguments> getLogs() {
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java
index a58bbe8f30c..2b7ddc8e9ac 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java
@@ -20,8 +20,6 @@
package org.sonar.api.utils.log;
import java.util.List;
-import java.util.Optional;
-import java.util.function.Predicate;
import org.junit.rules.ExternalResource;
/**
@@ -92,10 +90,6 @@ public class LogTester extends ExternalResource {
return ((ListInterceptor) LogInterceptors.get()).logs();
}
- public Optional<String> findFirstLog(Predicate<String> msgPredicate) {
- return ((ListInterceptor) LogInterceptors.get()).findFirst(msgPredicate);
- }
-
/**
* Logs in chronological order (item at index 0 is the oldest one) for
* a given level