import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.resources.ProjectFileSystem;
+import javax.annotation.Nullable;
+
import java.io.File;
import java.util.List;
this(def, projectFileSystem, null);
}
- public DefaultProjectClasspath(ProjectDefinition def, ProjectFileSystem projectFileSystem, MavenProject pom) {
+ public DefaultProjectClasspath(ProjectDefinition def, ProjectFileSystem projectFileSystem, @Nullable MavenProject pom) {
super(pom);
this.def = def;
this.projectFileSystem = projectFileSystem;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.analyzer.Analyzer;
import org.sonar.api.batch.analyzer.AnalyzerContext;
-import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.resources.Project;
import org.sonar.batch.scan.SensorWrapper;
*/
public class BatchExtensionDictionnary extends org.sonar.api.batch.BatchExtensionDictionnary {
- private FileSystem fs;
private AnalyzerContext context;
private AnalyzerOptimizer analyzerOptimizer;
- public BatchExtensionDictionnary(ComponentContainer componentContainer, FileSystem fs, AnalyzerContext context, AnalyzerOptimizer analyzerOptimizer) {
+ public BatchExtensionDictionnary(ComponentContainer componentContainer, AnalyzerContext context, AnalyzerOptimizer analyzerOptimizer) {
super(componentContainer);
- this.fs = fs;
this.context = context;
this.analyzerOptimizer = analyzerOptimizer;
}
List<T> result = Lists.newArrayList();
for (Object extension : getExtensions(type)) {
if (type == Sensor.class && extension instanceof Analyzer) {
- extension = new SensorWrapper((Analyzer) extension, context, fs, analyzerOptimizer);
+ extension = new SensorWrapper((Analyzer) extension, context, analyzerOptimizer);
}
if (shouldKeep(type, extension, project, matcher)) {
result.add((T) extension);
import com.google.common.collect.Lists;
import org.sonar.api.resources.Resource;
+import javax.annotation.Nullable;
+
import java.util.Collections;
import java.util.List;
return resource;
}
- public Bucket setParent(Bucket parent) {
+ public Bucket setParent(@Nullable Bucket parent) {
this.parent = parent;
if (parent != null) {
parent.addChild(this);
import org.sonar.api.security.ResourcePermissions;
import org.sonar.api.utils.SonarException;
+import javax.annotation.Nullable;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;
this.resourceCache = resourceCache;
}
- public Snapshot saveProject(Project project, Project parent) {
+ public Snapshot saveProject(Project project, @Nullable Project parent) {
Snapshot snapshot = snapshotsByResource.get(project);
if (snapshot == null) {
snapshot = persistProject(project, parent);
}
}
- private Snapshot persistProject(Project project, Project parent) {
+ private Snapshot persistProject(Project project, @Nullable Project parent) {
// temporary hack
project.setEffectiveKey(project.getKey());
return saveResource(project, resource, null);
}
- public Snapshot saveResource(Project project, Resource resource, Resource parent) {
+ public Snapshot saveResource(Project project, Resource resource, @Nullable Resource parent) {
Snapshot snapshot = snapshotsByResource.get(resource);
if (snapshot == null) {
snapshot = persist(project, resource, parent);
return snapshot;
}
- private Snapshot persist(Project project, Resource resource, Resource parent) {
+ private Snapshot persist(Project project, Resource resource, @Nullable Resource parent) {
Snapshot snapshot;
if (resource instanceof Project) {
// should not occur, please use the method saveProject()
/**
* Everything except project and library
*/
- private Snapshot persistFileOrDirectory(Project project, Resource resource, Resource parentReference) {
+ private Snapshot persistFileOrDirectory(Project project, Resource resource, @Nullable Resource parentReference) {
Snapshot moduleSnapshot = snapshotsByResource.get(project);
Integer moduleId = moduleSnapshot.getResourceId();
ResourceModel model = findOrCreateModel(resource);
@VisibleForTesting
static boolean shouldPersistMeasure(@Nullable Resource resource, @Nullable Measure measure) {
- return resource != null && measure != null && measure.getPersistenceMode().useDatabase() &&
+ if (resource == null || measure == null) {
+ return false;
+ }
+ return measure.getPersistenceMode().useDatabase() &&
!(ResourceUtils.isEntity(resource) && measure.isBestValue()) && isMeasureNotEmpty(measure);
}
import org.sonar.api.resources.ProjectLink;
import org.sonar.api.resources.Resource;
+import javax.annotation.Nullable;
+
import java.util.List;
public interface PersistenceManager {
void clear();
- void saveProject(Project project, Project parent);
+ void saveProject(Project project, @Nullable Project parent);
Snapshot saveResource(Project project, Resource resource, Resource parent);
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
+import javax.annotation.Nullable;
+
public interface ResourcePersister {
- Snapshot saveProject(Project project, Project parent);
+ Snapshot saveProject(Project project, @Nullable Project parent);
/**
* Persist a resource in database. Returns null if the resource must not be persisted (scope lower than file)
}
public AnalyzerMediumTesterBuilder registerMetric(Metric<?> metric) {
- metricFinder.add(metricId++, metric);
+ metricFinder.add(metricId, metric);
+ metricId++;
return this;
}
}
- public void start() throws Throwable {
+ public void start() {
batch.start();
}
this.issueExclusionsLoader = issueExclusionsLoader;
}
- public PhaseExecutor(Phases phases, DecoratorsExecutor decoratorsExecutor,
- MavenPluginsConfigurator mavenPluginsConfigurator, InitializersExecutor initializersExecutor,
- PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor,
- PersistenceManager persistenceManager, SensorContext sensorContext, DefaultIndex index,
- EventBus eventBus, ProjectInitializer pi, ScanPersister[] persisters, FileSystemLogger fsLogger, JsonReport jsonReport,
- DefaultModuleFileSystem fs, QProfileVerifier profileVerifier, IssueExclusionsLoader issueExclusionsLoader) {
- this(phases, decoratorsExecutor, mavenPluginsConfigurator, initializersExecutor, postJobsExecutor,
- sensorsExecutor, persistenceManager, sensorContext, index, eventBus, null, pi, persisters, fsLogger, jsonReport, fs, profileVerifier, issueExclusionsLoader);
- }
-
public static Collection<Class> getPhaseClasses() {
return Lists.<Class>newArrayList(DecoratorsExecutor.class, MavenPluginsConfigurator.class,
PostJobsExecutor.class, SensorsExecutor.class,
import org.sonar.api.batch.analyzer.Analyzer;
import org.sonar.api.batch.analyzer.AnalyzerContext;
import org.sonar.api.batch.analyzer.internal.DefaultAnalyzerDescriptor;
-import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.measure.Metric;
import org.sonar.api.resources.Project;
import org.sonar.batch.scan2.AnalyzerOptimizer;
private Analyzer analyzer;
private AnalyzerContext adaptor;
- private FileSystem fs;
private DefaultAnalyzerDescriptor descriptor;
private AnalyzerOptimizer optimizer;
- public SensorWrapper(Analyzer analyzer, AnalyzerContext adaptor, FileSystem fs, AnalyzerOptimizer optimizer) {
+ public SensorWrapper(Analyzer analyzer, AnalyzerContext adaptor, AnalyzerOptimizer optimizer) {
this.analyzer = analyzer;
this.optimizer = optimizer;
descriptor = new DefaultAnalyzerDescriptor();
analyzer.describe(descriptor);
this.adaptor = adaptor;
- this.fs = fs;
}
@DependedUpon
--- /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.scan2;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
+import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.fs.FileSystem;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.config.Settings;
+import org.sonar.api.utils.ZipUtils;
+import org.sonar.api.utils.text.JsonWriter;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Properties;
+
+public final class AnalysisPublisher {
+
+ private static final Logger LOG = LoggerFactory.getLogger(AnalysisPublisher.class);
+ private final Settings settings;
+ private final FileSystem fs;
+ private final AnalyzerMeasureCache measureCache;
+ private final ProjectDefinition def;
+ private AnalyzerIssueCache issueCache;
+
+ public AnalysisPublisher(ProjectDefinition def, Settings settings, FileSystem fs, AnalyzerMeasureCache measureCache, AnalyzerIssueCache analyzerIssueCache) {
+ this.def = def;
+ this.settings = settings;
+ this.fs = fs;
+ this.measureCache = measureCache;
+ this.issueCache = analyzerIssueCache;
+ }
+
+ public void execute() {
+ if (settings.getBoolean("sonar.skipPublish")) {
+ LOG.debug("Publishing of results is skipped");
+ return;
+ }
+ File exportDir = prepareExportDir();
+
+ exportAnalysisProperties(exportDir);
+
+ exportSourceFiles(exportDir);
+
+ exportMeasures(exportDir);
+
+ exportIssues(exportDir);
+
+ createZip(exportDir);
+
+ }
+
+ private void createZip(File exportDir) {
+ File exportZip = new File(fs.workDir(), def.getKey() + "-export.zip");
+ try {
+ ZipUtils.zipDir(exportDir, exportZip);
+ FileUtils.deleteDirectory(exportDir);
+ } catch (IOException e) {
+ throw unableToExport(e);
+ }
+ LOG.info("Results packaged in " + exportZip);
+ }
+
+ private IllegalStateException unableToExport(IOException e) {
+ return new IllegalStateException("Unable to export result of analyzis", e);
+ }
+
+ private void exportIssues(File exportDir) {
+ File issuesFile = new File(exportDir, "issues.json");
+ FileWriter issueWriter = null;
+ try {
+ issueWriter = new FileWriter(issuesFile);
+ JsonWriter jsonWriter = JsonWriter.of(issueWriter);
+ jsonWriter
+ .beginObject().name("issues")
+ .beginArray();
+ for (AnalyzerIssue issue : issueCache.byModule(def.getKey())) {
+ jsonWriter.beginObject()
+ .prop("repository", issue.ruleKey().repository())
+ .prop("rule", issue.ruleKey().rule());
+ if (issue.inputFile() != null) {
+ jsonWriter.prop("filePath", issue.inputFile().relativePath());
+ }
+ jsonWriter.prop("message", issue.message())
+ .prop("effortToFix", issue.effortToFix())
+ .prop("line", issue.line())
+ .endObject();
+ }
+ jsonWriter.endArray()
+ .endObject()
+ .close();
+ } catch (IOException e) {
+ throw unableToExport(e);
+ } finally {
+ IOUtils.closeQuietly(issueWriter);
+ }
+ }
+
+ private void exportMeasures(File exportDir) {
+ File measuresFile = new File(exportDir, "measures.json");
+ FileWriter measureWriter = null;
+ try {
+ measureWriter = new FileWriter(measuresFile);
+ JsonWriter jsonWriter = JsonWriter.of(measureWriter);
+ jsonWriter
+ .beginObject().name("measures")
+ .beginArray();
+ for (AnalyzerMeasure<?> measure : measureCache.byModule(def.getKey())) {
+ jsonWriter.beginObject()
+ .prop("metricKey", measure.metric().key());
+ if (measure.inputFile() != null) {
+ jsonWriter.prop("filePath", measure.inputFile().relativePath());
+ }
+ jsonWriter.prop("value", String.valueOf(measure.value()))
+ .endObject();
+ }
+ jsonWriter.endArray()
+ .endObject()
+ .close();
+ } catch (IOException e) {
+ throw unableToExport(e);
+ } finally {
+ IOUtils.closeQuietly(measureWriter);
+ }
+ }
+
+ private void exportSourceFiles(File exportDir) {
+ File sourceDir = new File(exportDir, "sources");
+ for (InputFile inputFile : fs.inputFiles(fs.predicates().all())) {
+ File dest = new File(sourceDir, inputFile.relativePath());
+ try {
+ FileUtils.copyFile(inputFile.file(), dest);
+ } catch (IOException e) {
+ throw unableToExport(e);
+ }
+ }
+ }
+
+ private void exportAnalysisProperties(File exportDir) {
+ File propsFile = new File(exportDir, "analysis.properties");
+ Properties props = new Properties();
+ props.putAll(settings.getProperties());
+ FileWriter writer = null;
+ try {
+ writer = new FileWriter(propsFile);
+ props.store(writer, "SonarQube batch");
+ } catch (IOException e) {
+ throw unableToExport(e);
+ } finally {
+ IOUtils.closeQuietly(writer);
+ }
+ }
+
+ private File prepareExportDir() {
+ File exportDir = new File(fs.workDir(), "export");
+ try {
+ if (exportDir.exists()) {
+ FileUtils.forceDelete(exportDir);
+ }
+ FileUtils.forceMkdir(exportDir);
+ } catch (IOException e) {
+ throw unableToExport(e);
+ }
+ return exportDir;
+ }
+}
+++ /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.scan2;
-
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.batch.analyzer.issue.AnalyzerIssue;
-import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.api.batch.fs.FileSystem;
-import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.config.Settings;
-import org.sonar.api.utils.ZipUtils;
-import org.sonar.api.utils.text.JsonWriter;
-
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.Properties;
-
-public final class AnalyzisPublisher {
-
- public static final Logger LOG = LoggerFactory.getLogger(AnalyzisPublisher.class);
- private final Settings settings;
- private final FileSystem fs;
- private final AnalyzerMeasureCache measureCache;
- private final ProjectDefinition def;
- private AnalyzerIssueCache issueCache;
-
- public AnalyzisPublisher(ProjectDefinition def, Settings settings, FileSystem fs, AnalyzerMeasureCache measureCache, AnalyzerIssueCache analyzerIssueCache) {
- this.def = def;
- this.settings = settings;
- this.fs = fs;
- this.measureCache = measureCache;
- this.issueCache = analyzerIssueCache;
- }
-
- public void execute() {
- if (settings.getBoolean("sonar.skipPublish")) {
- LOG.debug("Publishing of results is skipped");
- return;
- }
- File exportDir = prepareExportDir();
-
- exportAnalysisProperties(exportDir);
-
- exportSourceFiles(exportDir);
-
- exportMeasures(exportDir);
-
- exportIssues(exportDir);
-
- createZip(exportDir);
-
- }
-
- private void createZip(File exportDir) {
- File exportZip = new File(fs.workDir(), def.getKey() + "-export.zip");
- try {
- ZipUtils.zipDir(exportDir, exportZip);
- FileUtils.deleteDirectory(exportDir);
- } catch (IOException e) {
- throw unableToExport(e);
- }
- LOG.info("Results packaged in " + exportZip);
- }
-
- private IllegalStateException unableToExport(IOException e) {
- return new IllegalStateException("Unable to export result of analyzis", e);
- }
-
- private void exportIssues(File exportDir) {
- File issuesFile = new File(exportDir, "issues.json");
- FileWriter issueWriter = null;
- try {
- issueWriter = new FileWriter(issuesFile);
- JsonWriter jsonWriter = JsonWriter.of(issueWriter);
- jsonWriter
- .beginObject().name("issues")
- .beginArray();
- for (AnalyzerIssue issue : issueCache.byModule(def.getKey())) {
- jsonWriter.beginObject()
- .prop("repository", issue.ruleKey().repository())
- .prop("rule", issue.ruleKey().rule());
- if (issue.inputFile() != null) {
- jsonWriter.prop("filePath", issue.inputFile().relativePath());
- }
- jsonWriter.prop("message", issue.message())
- .prop("effortToFix", issue.effortToFix())
- .prop("line", issue.line())
- .endObject();
- }
- jsonWriter.endArray()
- .endObject()
- .close();
- } catch (IOException e) {
-
- } finally {
- IOUtils.closeQuietly(issueWriter);
- }
- }
-
- private void exportMeasures(File exportDir) {
- File measuresFile = new File(exportDir, "measures.json");
- FileWriter measureWriter = null;
- try {
- measureWriter = new FileWriter(measuresFile);
- JsonWriter jsonWriter = JsonWriter.of(measureWriter);
- jsonWriter
- .beginObject().name("measures")
- .beginArray();
- for (AnalyzerMeasure<?> measure : measureCache.byModule(def.getKey())) {
- jsonWriter.beginObject()
- .prop("metricKey", measure.metric().key());
- if (measure.inputFile() != null) {
- jsonWriter.prop("filePath", measure.inputFile().relativePath());
- }
- jsonWriter.prop("value", String.valueOf(measure.value()))
- .endObject();
- }
- jsonWriter.endArray()
- .endObject()
- .close();
- } catch (IOException e) {
-
- } finally {
- IOUtils.closeQuietly(measureWriter);
- }
- }
-
- private void exportSourceFiles(File exportDir) {
- File sourceDir = new File(exportDir, "sources");
- for (InputFile inputFile : fs.inputFiles(fs.predicates().all())) {
- File dest = new File(sourceDir, inputFile.relativePath());
- try {
- FileUtils.copyFile(inputFile.file(), dest);
- } catch (IOException e) {
- throw unableToExport(e);
- }
- }
- }
-
- private void exportAnalysisProperties(File exportDir) {
- File propsFile = new File(exportDir, "analysis.properties");
- Properties props = new Properties();
- props.putAll(settings.getProperties());
- FileWriter writer = null;
- try {
- writer = new FileWriter(propsFile);
- props.store(writer, "SonarQube batch");
- } catch (IOException e) {
- throw unableToExport(e);
- } finally {
- IOUtils.closeQuietly(writer);
- }
- }
-
- private File prepareExportDir() {
- File exportDir = new File(fs.workDir(), "export");
- try {
- if (exportDir.exists()) {
- FileUtils.forceDelete(exportDir);
- }
- FileUtils.forceMkdir(exportDir);
- } catch (IOException e) {
- throw unableToExport(e);
- }
- return exportDir;
- }
-}
EnforceIssuesFilter.class,
IgnoreIssuesFilter.class,
- AnalyzisPublisher.class);
+ AnalysisPublisher.class);
}
private void addExtensions() {
package org.sonar.batch.scan2;
import com.google.common.collect.Lists;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.sonar.api.batch.analyzer.AnalyzerContext;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.batch.issue.ignore.scanner.IssueExclusionsLoader;
public final class ModuleScanExecutor {
- public static final Logger LOGGER = LoggerFactory.getLogger(ModuleScanExecutor.class);
-
private final AnalyzersExecutor analyzersExecutor;
private final AnalyzerContext analyzerContext;
private final FileSystemLogger fsLogger;
private final QProfileVerifier profileVerifier;
private final IssueExclusionsLoader issueExclusionsLoader;
- private AnalyzisPublisher analyzisPublisher;
+ private AnalysisPublisher analyzisPublisher;
public ModuleScanExecutor(AnalyzersExecutor analyzersExecutor,
AnalyzerContext analyzerContext,
FileSystemLogger fsLogger, DefaultModuleFileSystem fs, QProfileVerifier profileVerifier,
- IssueExclusionsLoader issueExclusionsLoader, AnalyzisPublisher analyzisPublisher) {
+ IssueExclusionsLoader issueExclusionsLoader, AnalysisPublisher analyzisPublisher) {
this.analyzersExecutor = analyzersExecutor;
this.analyzerContext = analyzerContext;
this.fsLogger = fsLogger;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.analyzer.AnalyzerContext;
-import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.resources.Project;
import org.sonar.batch.scan2.AnalyzerOptimizer;
import java.util.Collection;
+import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
-import static org.hamcrest.Matchers.hasItem;
import static org.mockito.Mockito.mock;
public class BatchExtensionDictionnaryTest {
for (BatchExtension extension : extensions) {
iocContainer.addSingleton(extension);
}
- return new BatchExtensionDictionnary(iocContainer, mock(FileSystem.class), mock(AnalyzerContext.class), mock(AnalyzerOptimizer.class));
+ return new BatchExtensionDictionnary(iocContainer, mock(AnalyzerContext.class), mock(AnalyzerOptimizer.class));
}
@Test
.build();
@Before
- public void prepare() throws Throwable {
+ public void prepare() {
tester.start();
}