aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/sonar-cpd-plugin
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2012-03-26 12:18:44 +0600
committerEvgeny Mandrikov <mandrikov@gmail.com>2012-03-26 12:54:14 +0600
commit0d41c77bf1d0d84b0ad908b9b0e675de5562d072 (patch)
treefe93638e84f153730c7224b74ff4eae81503e5d9 /plugins/sonar-cpd-plugin
parent594da410d3152a76ca4dc92aecb4d05e599284c9 (diff)
downloadsonarqube-0d41c77bf1d0d84b0ad908b9b0e675de5562d072.tar.gz
sonarqube-0d41c77bf1d0d84b0ad908b9b0e675de5562d072.zip
SONAR-3347 Limit time of analysis of single file for bridge between PMD and Sonar CPD
Diffstat (limited to 'plugins/sonar-cpd-plugin')
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java47
-rw-r--r--plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java2
2 files changed, 38 insertions, 11 deletions
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java
index 7aee8b8ed4a..026893d031c 100644
--- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java
@@ -22,12 +22,14 @@ package org.sonar.plugins.cpd;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.sonar.api.batch.CpdMapping;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.resources.*;
+import org.sonar.api.utils.SonarException;
import org.sonar.duplications.DuplicationPredicates;
import org.sonar.duplications.block.Block;
-import org.sonar.duplications.detector.suffixtree.SuffixTreeCloneDetectionAlgorithm;
import org.sonar.duplications.index.CloneGroup;
import org.sonar.duplications.internal.pmd.TokenizerBridge;
import org.sonar.plugins.cpd.index.IndexFactory;
@@ -35,9 +37,17 @@ import org.sonar.plugins.cpd.index.SonarDuplicationsIndex;
import java.util.Collection;
import java.util.List;
+import java.util.concurrent.*;
public class SonarBridgeEngine extends CpdEngine {
+ private static final Logger LOG = LoggerFactory.getLogger(SonarBridgeEngine.class);
+
+ /**
+ * Limit of time to analyse one file (in seconds).
+ */
+ private static final int TIMEOUT = 5 * 60;
+
private final IndexFactory indexFactory;
private final CpdMapping[] mappings;
@@ -71,6 +81,7 @@ public class SonarBridgeEngine extends CpdEngine {
TokenizerBridge bridge = new TokenizerBridge(mapping.getTokenizer(), fileSystem.getSourceCharset().name(), getBlockSize(project));
for (InputFile inputFile : inputFiles) {
+ LOG.debug("Populating index from {}", inputFile.getFile());
Resource resource = mapping.createResource(inputFile.getFile(), fileSystem.getSourceDirs());
String resourceId = SonarEngine.getFullKey(project, resource);
List<Block> blocks = bridge.chunk(resourceId, inputFile.getFile());
@@ -80,16 +91,32 @@ public class SonarBridgeEngine extends CpdEngine {
// Detect
Predicate<CloneGroup> minimumTokensPredicate = DuplicationPredicates.numberOfUnitsNotLessThan(PmdEngine.getMinimumTokens(project));
- for (InputFile inputFile : inputFiles) {
- Resource resource = mapping.createResource(inputFile.getFile(), fileSystem.getSourceDirs());
- String resourceKey = SonarEngine.getFullKey(project, resource);
-
- Collection<Block> fileBlocks = index.getByResource(resource, resourceKey);
- List<CloneGroup> duplications = SuffixTreeCloneDetectionAlgorithm.detect(index, fileBlocks);
-
- Iterable<CloneGroup> filtered = Iterables.filter(duplications, minimumTokensPredicate);
+ ExecutorService executorService = Executors.newSingleThreadExecutor();
+ try {
+ for (InputFile inputFile : inputFiles) {
+ LOG.debug("Detection of duplications for {}", inputFile.getFile());
+ Resource resource = mapping.createResource(inputFile.getFile(), fileSystem.getSourceDirs());
+ String resourceKey = SonarEngine.getFullKey(project, resource);
+
+ Collection<Block> fileBlocks = index.getByResource(resource, resourceKey);
+
+ Iterable<CloneGroup> filtered;
+ try {
+ List<CloneGroup> duplications = executorService.submit(new SonarEngine.Task(index, fileBlocks)).get(TIMEOUT, TimeUnit.SECONDS);
+ filtered = Iterables.filter(duplications, minimumTokensPredicate);
+ } catch (TimeoutException e) {
+ filtered = null;
+ LOG.warn("Timeout during detection of duplications for " + inputFile.getFile(), e);
+ } catch (InterruptedException e) {
+ throw new SonarException(e);
+ } catch (ExecutionException e) {
+ throw new SonarException(e);
+ }
- SonarEngine.save(context, resource, filtered);
+ SonarEngine.save(context, resource, filtered);
+ }
+ } finally {
+ executorService.shutdown();
}
}
diff --git a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java
index 460fdd10fa0..88c2c90656a 100644
--- a/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java
+++ b/plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarEngine.java
@@ -156,7 +156,7 @@ public class SonarEngine extends CpdEngine {
}
}
- private static class Task implements Callable<List<CloneGroup>> {
+ static class Task implements Callable<List<CloneGroup>> {
private final CloneIndex index;
private final Collection<Block> fileBlocks;