diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2016-03-17 17:05:56 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2016-03-18 09:35:05 +0100 |
commit | cfcbe278f7ced12599d898d50f3fe68bfbf95155 (patch) | |
tree | 5b4116ad08a8ba87ffc5bf9f159a431b9609b48f /sonar-batch/src/main/java/org | |
parent | 38ce80934961773a9a35ec0401994b3d726597dc (diff) | |
download | sonarqube-cfcbe278f7ced12599d898d50f3fe68bfbf95155.tar.gz sonarqube-cfcbe278f7ced12599d898d50f3fe68bfbf95155.zip |
Rename batch into scanner
Diffstat (limited to 'sonar-batch/src/main/java/org')
310 files changed, 0 insertions, 26742 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContext.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContext.java deleted file mode 100644 index a4045f18ce0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContext.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch; - -import com.google.common.base.Objects; -import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Maps; -import java.util.Map; -import org.sonar.api.batch.SonarIndex; -import org.sonar.api.measures.FileLinesContext; -import org.sonar.api.measures.Measure; -import org.sonar.api.measures.Metric; -import org.sonar.api.measures.PersistenceMode; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.ResourceUtils; -import org.sonar.api.utils.KeyValueFormat; -import org.sonar.api.utils.KeyValueFormat.Converter; - -public class DefaultFileLinesContext implements FileLinesContext { - - private final SonarIndex index; - private final Resource resource; - - /** - * metric key -> line -> value - */ - private final Map<String, Map<Integer, Object>> map = Maps.newHashMap(); - - public DefaultFileLinesContext(SonarIndex index, Resource resource) { - Preconditions.checkNotNull(index); - Preconditions.checkArgument(ResourceUtils.isFile(resource)); - this.index = index; - this.resource = resource; - } - - @Override - public void setIntValue(String metricKey, int line, int value) { - Preconditions.checkNotNull(metricKey); - Preconditions.checkArgument(line > 0); - - setValue(metricKey, line, value); - } - - @Override - public Integer getIntValue(String metricKey, int line) { - Preconditions.checkNotNull(metricKey); - Preconditions.checkArgument(line > 0); - - Map lines = map.get(metricKey); - if (lines == null) { - // not in memory, so load - lines = loadData(metricKey, KeyValueFormat.newIntegerConverter()); - map.put(metricKey, lines); - } - return (Integer) lines.get(line); - } - - @Override - public void setStringValue(String metricKey, int line, String value) { - Preconditions.checkNotNull(metricKey); - Preconditions.checkArgument(line > 0); - Preconditions.checkNotNull(value); - - setValue(metricKey, line, value); - } - - @Override - public String getStringValue(String metricKey, int line) { - Preconditions.checkNotNull(metricKey); - Preconditions.checkArgument(line > 0); - - Map lines = map.get(metricKey); - if (lines == null) { - // not in memory, so load - lines = loadData(metricKey, KeyValueFormat.newStringConverter()); - map.put(metricKey, lines); - } - return (String) lines.get(line); - } - - private Map<Integer, Object> getOrCreateLines(String metricKey) { - Map<Integer, Object> lines = map.get(metricKey); - if (lines == null) { - lines = Maps.newHashMap(); - map.put(metricKey, lines); - } - return lines; - } - - private void setValue(String metricKey, int line, Object value) { - getOrCreateLines(metricKey).put(line, value); - } - - @Override - public void save() { - for (Map.Entry<String, Map<Integer, Object>> entry : map.entrySet()) { - String metricKey = entry.getKey(); - Map<Integer, Object> lines = entry.getValue(); - if (shouldSave(lines)) { - String data = KeyValueFormat.format(lines); - Measure measure = new Measure(metricKey) - .setPersistenceMode(PersistenceMode.DATABASE) - .setData(data); - index.addMeasure(resource, measure); - entry.setValue(ImmutableMap.copyOf(lines)); - } - } - } - - private Map loadData(String metricKey, Converter converter) { - // FIXME no way to load measure only by key - Measure measure = index.getMeasure(resource, new Metric(metricKey)); - String data = measure != null ? measure.getData() : null; - if (data != null) { - return ImmutableMap.copyOf(KeyValueFormat.parse(data, KeyValueFormat.newIntegerConverter(), converter)); - } - // no such measure - return ImmutableMap.of(); - } - - /** - * Checks that measure was not saved. - * - * @see #loadData(String, Converter) - * @see #save() - */ - private static boolean shouldSave(Map<Integer, Object> lines) { - return !(lines instanceof ImmutableMap); - } - - @Override - public String toString() { - return Objects.toStringHelper(this) - .add("map", map) - .toString(); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContextFactory.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContextFactory.java deleted file mode 100644 index 922f101f085..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultFileLinesContextFactory.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch; - -import org.sonar.api.batch.SonarIndex; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.measures.FileLinesContext; -import org.sonar.api.measures.FileLinesContextFactory; -import org.sonar.api.resources.File; -import org.sonar.api.resources.Resource; - -public class DefaultFileLinesContextFactory implements FileLinesContextFactory { - - private final SonarIndex index; - - public DefaultFileLinesContextFactory(SonarIndex index) { - this.index = index; - } - - @Override - public FileLinesContext createFor(Resource model) { - // Reload resource in case it use deprecated key - Resource resource = index.getResource(model); - return new DefaultFileLinesContext(index, resource); - } - - @Override - public FileLinesContext createFor(InputFile inputFile) { - File sonarFile = File.create(inputFile.relativePath()); - // Reload resource from index - sonarFile = index.getResource(sonarFile); - return new DefaultFileLinesContext(index, sonarFile); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectTree.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectTree.java deleted file mode 100644 index 5b56f27154c..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectTree.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import org.apache.commons.lang.ObjectUtils; -import org.picocontainer.Startable; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.resources.Project; -import org.sonar.batch.scan.ImmutableProjectReactor; - -public class DefaultProjectTree implements Startable { - - private final ProjectConfigurator configurator; - private final ImmutableProjectReactor projectReactor; - - private List<Project> projects; - private Map<ProjectDefinition, Project> projectsByDef; - - public DefaultProjectTree(ImmutableProjectReactor projectReactor, ProjectConfigurator projectConfigurator) { - this.projectReactor = projectReactor; - this.configurator = projectConfigurator; - } - - @Override - public void start() { - doStart(projectReactor.getProjects()); - } - - @Override - public void stop() { - // Nothing to do - } - - void doStart(Collection<ProjectDefinition> definitions) { - projects = Lists.newArrayList(); - projectsByDef = Maps.newHashMap(); - - for (ProjectDefinition def : definitions) { - Project project = configurator.create(def); - projectsByDef.put(def, project); - projects.add(project); - } - - for (Map.Entry<ProjectDefinition, Project> entry : projectsByDef.entrySet()) { - ProjectDefinition def = entry.getKey(); - Project project = entry.getValue(); - for (ProjectDefinition module : def.getSubProjects()) { - projectsByDef.get(module).setParent(project); - } - } - - // Configure - for (Project project : projects) { - configurator.configure(project); - } - } - - public List<Project> getProjects() { - return projects; - } - - public Project getRootProject() { - for (Project project : projects) { - if (project.getParent() == null) { - return project; - } - } - throw new IllegalStateException("Can not find the root project from the list of Maven modules"); - } - - public ProjectDefinition getProjectDefinition(Project project) { - for (Map.Entry<ProjectDefinition, Project> entry : projectsByDef.entrySet()) { - if (ObjectUtils.equals(entry.getValue(), project)) { - return entry.getKey(); - } - } - throw new IllegalStateException("Can not find ProjectDefinition for " + project); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/ProjectConfigurator.java b/sonar-batch/src/main/java/org/sonar/batch/ProjectConfigurator.java deleted file mode 100644 index 997cae744f2..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/ProjectConfigurator.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch; - -import java.util.Date; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.config.Settings; -import org.sonar.api.resources.Project; -import org.sonar.api.utils.SonarException; -import org.sonar.api.utils.System2; - -/** - * Used by views !! - * - */ -@BatchSide -public class ProjectConfigurator { - - private static final Logger LOG = LoggerFactory.getLogger(ProjectConfigurator.class); - private final System2 system2; - private Settings settings; - - public ProjectConfigurator(Settings settings, System2 system2) { - this.settings = settings; - this.system2 = system2; - } - - public Project create(ProjectDefinition definition) { - Project project = new Project(definition.getKey(), definition.getBranch(), definition.getName()); - project.setDescription(StringUtils.defaultString(definition.getDescription())); - return project; - } - - public ProjectConfigurator configure(Project project) { - Date analysisDate = loadAnalysisDate(); - project - .setAnalysisDate(analysisDate) - .setAnalysisVersion(loadAnalysisVersion()) - .setAnalysisType(loadAnalysisType()); - return this; - } - - private Date loadAnalysisDate() { - Date date; - try { - // sonar.projectDate may have been specified as a time - date = settings.getDateTime(CoreProperties.PROJECT_DATE_PROPERTY); - } catch (SonarException e) { - // this is probably just a date - date = settings.getDate(CoreProperties.PROJECT_DATE_PROPERTY); - } - if (date == null) { - date = new Date(system2.now()); - settings.setProperty(CoreProperties.PROJECT_DATE_PROPERTY, date, true); - } - return date; - } - - private Project.AnalysisType loadAnalysisType() { - String value = settings.getString(CoreProperties.DYNAMIC_ANALYSIS_PROPERTY); - if (value == null) { - return Project.AnalysisType.DYNAMIC; - } - - LOG.warn("'sonar.dynamicAnalysis' is deprecated since version 4.3 and should no longer be used."); - if ("true".equals(value)) { - return Project.AnalysisType.DYNAMIC; - } - if ("reuseReports".equals(value)) { - return Project.AnalysisType.REUSE_REPORTS; - } - return Project.AnalysisType.STATIC; - } - - private String loadAnalysisVersion() { - return settings.getString(CoreProperties.PROJECT_VERSION_PROPERTY); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisProperties.java b/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisProperties.java deleted file mode 100644 index e3be61067e6..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisProperties.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.analysis; - -import java.util.Map; -import javax.annotation.Nullable; -import org.sonar.batch.bootstrap.UserProperties; - -/** - * Batch properties that are specific to an analysis (for example - * coming from sonar-project.properties). - */ -public class AnalysisProperties extends UserProperties { - public AnalysisProperties(Map<String, String> properties) { - this(properties, null); - - } - - public AnalysisProperties(Map<String, String> properties, @Nullable String pathToSecretKey) { - super(properties, pathToSecretKey); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisTempFolderProvider.java b/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisTempFolderProvider.java deleted file mode 100644 index 68c2608b864..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisTempFolderProvider.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.analysis; - -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.picocontainer.PicoContainer; -import org.picocontainer.ComponentLifecycle; -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.utils.TempFolder; -import org.sonar.api.utils.internal.DefaultTempFolder; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - -public class AnalysisTempFolderProvider extends ProviderAdapter implements ComponentLifecycle<TempFolder> { - static final String TMP_NAME = ".sonartmp"; - private DefaultTempFolder projectTempFolder; - private boolean started = false; - - public TempFolder provide(ProjectReactor projectReactor) { - if (projectTempFolder == null) { - Path workingDir = projectReactor.getRoot().getWorkDir().toPath(); - Path tempDir = workingDir.normalize().resolve(TMP_NAME); - try { - Files.deleteIfExists(tempDir); - Files.createDirectories(tempDir); - } catch (IOException e) { - throw new IllegalStateException("Unable to create root temp directory " + tempDir, e); - } - - projectTempFolder = new DefaultTempFolder(tempDir.toFile(), true); - } - return projectTempFolder; - } - - @Override - public void start(PicoContainer container) { - started = true; - } - - @Override - public void stop(PicoContainer container) { - if (projectTempFolder != null) { - projectTempFolder.stop(); - } - } - - @Override - public void dispose(PicoContainer container) { - //nothing to do - } - - @Override - public boolean componentHasLifecycle() { - return true; - } - - @Override - public boolean isStarted() { - return started; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisWSLoaderProvider.java b/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisWSLoaderProvider.java deleted file mode 100644 index 54ce6e112a3..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/analysis/AnalysisWSLoaderProvider.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.analysis; - -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.batch.bootstrap.BatchWsClient; -import org.sonar.batch.cache.WSLoader; -import org.sonar.batch.cache.WSLoader.LoadStrategy; -import org.sonar.home.cache.PersistentCache; - -public class AnalysisWSLoaderProvider extends ProviderAdapter { - static final String SONAR_USE_WS_CACHE = "sonar.useWsCache"; - private WSLoader wsLoader; - - public WSLoader provide(AnalysisMode mode, PersistentCache cache, BatchWsClient client, AnalysisProperties props) { - if (wsLoader == null) { - // recreate cache directory if needed for this analysis - cache.reconfigure(); - wsLoader = new WSLoader(getStrategy(mode, props), cache, client); - } - return wsLoader; - } - - private static LoadStrategy getStrategy(AnalysisMode mode, AnalysisProperties props) { - if (mode.isIssues() && "true".equals(props.property(SONAR_USE_WS_CACHE))) { - return LoadStrategy.CACHE_ONLY; - } - - return LoadStrategy.SERVER_ONLY; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/analysis/DefaultAnalysisMode.java b/sonar-batch/src/main/java/org/sonar/batch/analysis/DefaultAnalysisMode.java deleted file mode 100644 index bd27cf81ca0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/analysis/DefaultAnalysisMode.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.analysis; - -import java.util.Map; -import javax.annotation.CheckForNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.batch.bootstrap.AbstractAnalysisMode; -import org.sonar.batch.bootstrap.GlobalProperties; -import org.sonar.batch.mediumtest.FakePluginInstaller; - -/** - * @since 4.0 - */ -public class DefaultAnalysisMode extends AbstractAnalysisMode implements AnalysisMode { - - private static final Logger LOG = LoggerFactory.getLogger(DefaultAnalysisMode.class); - private static final String KEY_SCAN_ALL = "sonar.scanAllFiles"; - - private boolean mediumTestMode; - private boolean notAssociated; - private boolean scanAllFiles; - - public DefaultAnalysisMode(GlobalProperties globalProps, AnalysisProperties props) { - init(globalProps.properties(), props.properties()); - } - - public boolean isMediumTest() { - return mediumTestMode; - } - - public boolean isNotAssociated() { - return notAssociated; - } - - public boolean scanAllFiles() { - return scanAllFiles; - } - - private void init(Map<String, String> globalProps, Map<String, String> analysisProps) { - // make sure analysis is consistent with global properties - boolean globalPreview = isIssues(globalProps); - boolean analysisPreview = isIssues(analysisProps); - - if (!globalPreview && analysisPreview) { - throw new IllegalStateException("Inconsistent properties: global properties doesn't enable issues mode while analysis properties enables it"); - } - - load(globalProps, analysisProps); - } - - private void load(Map<String, String> globalProps, Map<String, String> analysisProps) { - String mode = getPropertyWithFallback(analysisProps, globalProps, CoreProperties.ANALYSIS_MODE); - validate(mode); - issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode); - mediumTestMode = "true".equals(getPropertyWithFallback(analysisProps, globalProps, FakePluginInstaller.MEDIUM_TEST_ENABLED)); - notAssociated = issues && rootProjectKeyMissing(analysisProps); - String scanAllStr = getPropertyWithFallback(analysisProps, globalProps, KEY_SCAN_ALL); - scanAllFiles = !issues || "true".equals(scanAllStr); - } - - public void printMode() { - if (preview) { - LOG.info("Preview mode"); - } else if (issues) { - LOG.info("Issues mode"); - } else { - LOG.info("Publish mode"); - } - if (mediumTestMode) { - LOG.info("Medium test mode"); - } - if (notAssociated) { - LOG.info("Local analysis"); - } - if (!scanAllFiles) { - LOG.info("Scanning only changed files"); - } - } - - @CheckForNull - private static String getPropertyWithFallback(Map<String, String> props1, Map<String, String> props2, String key) { - if (props1.containsKey(key)) { - return props1.get(key); - } - - return props2.get(key); - } - - private static boolean isIssues(Map<String, String> props) { - String mode = props.get(CoreProperties.ANALYSIS_MODE); - - return CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode); - } - - private static boolean rootProjectKeyMissing(Map<String, String> props) { - // ProjectReactorBuilder depends on this class, so it will only create this property later - return !props.containsKey(CoreProperties.PROJECT_KEY_PROPERTY); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/analysis/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/analysis/package-info.java deleted file mode 100644 index be39545404c..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/analysis/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.analysis; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AbstractAnalysisMode.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AbstractAnalysisMode.java deleted file mode 100644 index 2944accbd27..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/AbstractAnalysisMode.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import org.apache.commons.lang.StringUtils; -import org.sonar.api.CoreProperties; - -import java.util.Arrays; - -import org.sonar.api.batch.AnalysisMode; - -public abstract class AbstractAnalysisMode implements AnalysisMode { - private static final String[] VALID_MODES = {CoreProperties.ANALYSIS_MODE_PREVIEW, CoreProperties.ANALYSIS_MODE_PUBLISH, CoreProperties.ANALYSIS_MODE_ISSUES}; - - protected boolean preview; - protected boolean issues; - - protected AbstractAnalysisMode() { - } - - @Override - public boolean isPreview() { - return preview; - } - - @Override - public boolean isIssues() { - return issues; - } - - @Override - public boolean isPublish() { - return !preview && !issues; - } - - protected static void validate(String mode) { - if (StringUtils.isEmpty(mode)) { - return; - } - - if (CoreProperties.ANALYSIS_MODE_INCREMENTAL.equals(mode)) { - throw new IllegalStateException("Invalid analysis mode: " + mode + ". This mode was removed in SonarQube 5.2. Valid modes are: " + Arrays.toString(VALID_MODES)); - } - - if (!Arrays.asList(VALID_MODES).contains(mode)) { - throw new IllegalStateException("Invalid analysis mode: " + mode + ". Valid modes are: " + Arrays.toString(VALID_MODES)); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java deleted file mode 100644 index 0a2ec661d73..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import com.google.common.collect.Lists; -import java.util.Collection; -import java.util.List; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.batch.cpd.CpdComponents; -import org.sonar.batch.issue.tracking.ServerIssueFromWs; -import org.sonar.batch.issue.tracking.TrackedIssue; -import org.sonar.batch.scan.report.ConsoleReport; -import org.sonar.batch.scan.report.HtmlReport; -import org.sonar.batch.scan.report.IssuesReportBuilder; -import org.sonar.batch.scan.report.JSONReport; -import org.sonar.batch.scan.report.RuleNameProvider; -import org.sonar.batch.scan.report.SourceProvider; -import org.sonar.batch.scm.ScmConfiguration; -import org.sonar.batch.scm.ScmSensor; -import org.sonar.batch.source.CodeColorizerSensor; -import org.sonar.batch.source.LinesSensor; -import org.sonar.batch.source.ZeroCoverageSensor; -import org.sonar.batch.task.ListTask; -import org.sonar.batch.task.ScanTask; -import org.sonar.batch.task.Tasks; -import org.sonar.core.component.DefaultResourceTypes; -import org.sonar.core.config.CorePropertyDefinitions; -import org.sonar.core.issue.tracking.Tracker; -import org.sonar.core.platform.SonarQubeVersionProvider; - -public class BatchComponents { - private BatchComponents() { - // only static stuff - } - - public static Collection<Object> all(AnalysisMode analysisMode) { - List<Object> components = Lists.newArrayList( - new SonarQubeVersionProvider(), - DefaultResourceTypes.get(), - - // Tasks - Tasks.class, - ListTask.DEFINITION, - ListTask.class, - ScanTask.DEFINITION, - ScanTask.class); - components.addAll(CorePropertyDefinitions.all()); - if (!analysisMode.isIssues()) { - // SCM - components.add(ScmConfiguration.class); - components.add(ScmSensor.class); - - components.add(LinesSensor.class); - components.add(ZeroCoverageSensor.class); - components.add(CodeColorizerSensor.class); - - // CPD - components.addAll(CpdComponents.all()); - } else { - // Issues tracking - components.add(new Tracker<TrackedIssue, ServerIssueFromWs>()); - - // Issues report - components.add(ConsoleReport.class); - components.add(JSONReport.class); - components.add(HtmlReport.class); - components.add(IssuesReportBuilder.class); - components.add(SourceProvider.class); - components.add(RuleNameProvider.class); - } - return components; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchExtensionDictionnary.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchExtensionDictionnary.java deleted file mode 100644 index cfebad6cbb5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchExtensionDictionnary.java +++ /dev/null @@ -1,264 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import com.google.common.base.Predicates; -import com.google.common.collect.Collections2; -import com.google.common.collect.Lists; -import java.lang.annotation.Annotation; -import java.lang.reflect.Array; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import javax.annotation.Nullable; -import org.apache.commons.lang.ClassUtils; -import org.sonar.api.batch.CheckProject; -import org.sonar.api.batch.DependedUpon; -import org.sonar.api.batch.DependsUpon; -import org.sonar.api.batch.Phase; -import org.sonar.api.batch.postjob.PostJob; -import org.sonar.api.batch.postjob.PostJobContext; -import org.sonar.api.batch.sensor.Sensor; -import org.sonar.api.batch.sensor.SensorContext; -import org.sonar.api.resources.Project; -import org.sonar.api.utils.AnnotationUtils; -import org.sonar.api.utils.dag.DirectAcyclicGraph; -import org.sonar.batch.postjob.PostJobOptimizer; -import org.sonar.batch.postjob.PostJobWrapper; -import org.sonar.batch.sensor.DefaultSensorContext; -import org.sonar.batch.sensor.SensorOptimizer; -import org.sonar.batch.sensor.SensorWrapper; -import org.sonar.core.platform.ComponentContainer; - -/** - * @since 2.6 - */ -public class BatchExtensionDictionnary { - - private final ComponentContainer componentContainer; - private final SensorContext sensorContext; - private final SensorOptimizer sensorOptimizer; - private final PostJobContext postJobContext; - private final PostJobOptimizer postJobOptimizer; - - public BatchExtensionDictionnary(ComponentContainer componentContainer, DefaultSensorContext sensorContext, SensorOptimizer sensorOptimizer, PostJobContext postJobContext, - PostJobOptimizer postJobOptimizer) { - this.componentContainer = componentContainer; - this.sensorContext = sensorContext; - this.sensorOptimizer = sensorOptimizer; - this.postJobContext = postJobContext; - this.postJobOptimizer = postJobOptimizer; - } - - public <T> Collection<T> select(Class<T> type, @Nullable Project project, boolean sort, @Nullable ExtensionMatcher matcher) { - List<T> result = getFilteredExtensions(type, project, matcher); - if (sort) { - return sort(result); - } - return result; - } - - private static Phase.Name evaluatePhase(Object extension) { - Object extensionToEvaluate; - if (extension instanceof SensorWrapper) { - extensionToEvaluate = ((SensorWrapper) extension).wrappedSensor(); - } else { - extensionToEvaluate = extension; - } - Phase phaseAnnotation = AnnotationUtils.getAnnotation(extensionToEvaluate, Phase.class); - if (phaseAnnotation != null) { - return phaseAnnotation.name(); - } - return Phase.Name.DEFAULT; - } - - private <T> List<T> getFilteredExtensions(Class<T> type, @Nullable Project project, @Nullable ExtensionMatcher matcher) { - List<T> result = Lists.newArrayList(); - for (Object extension : getExtensions(type)) { - if (org.sonar.api.batch.Sensor.class.equals(type) && extension instanceof Sensor) { - extension = new SensorWrapper((Sensor) extension, sensorContext, sensorOptimizer); - } - if (shouldKeep(type, extension, project, matcher)) { - result.add((T) extension); - } - } - if (org.sonar.api.batch.Sensor.class.equals(type)) { - // Retrieve new Sensors and wrap then in SensorWrapper - for (Object extension : getExtensions(Sensor.class)) { - extension = new SensorWrapper((Sensor) extension, sensorContext, sensorOptimizer); - if (shouldKeep(type, extension, project, matcher)) { - result.add((T) extension); - } - } - } - if (org.sonar.api.batch.PostJob.class.equals(type)) { - // Retrieve new PostJob and wrap then in PostJobWrapper - for (Object extension : getExtensions(PostJob.class)) { - extension = new PostJobWrapper((PostJob) extension, postJobContext, postJobOptimizer); - if (shouldKeep(type, extension, project, matcher)) { - result.add((T) extension); - } - } - } - return result; - } - - protected List<Object> getExtensions(Class type) { - List<Object> extensions = Lists.newArrayList(); - completeBatchExtensions(componentContainer, extensions, type); - return extensions; - } - - private static void completeBatchExtensions(ComponentContainer container, List<Object> extensions, Class type) { - if (container != null) { - extensions.addAll(container.getComponentsByType(type)); - completeBatchExtensions(container.getParent(), extensions, type); - } - } - - public <T> Collection<T> sort(Collection<T> extensions) { - DirectAcyclicGraph dag = new DirectAcyclicGraph(); - - for (T extension : extensions) { - dag.add(extension); - for (Object dependency : getDependencies(extension)) { - dag.add(extension, dependency); - } - for (Object generates : getDependents(extension)) { - dag.add(generates, extension); - } - completePhaseDependencies(dag, extension); - } - List sortedList = dag.sort(); - - return Collections2.filter(sortedList, Predicates.in(extensions)); - } - - /** - * Extension dependencies - */ - private <T> List<Object> getDependencies(T extension) { - List<Object> result = new ArrayList<>(); - result.addAll(evaluateAnnotatedClasses(extension, DependsUpon.class)); - return result; - } - - /** - * Objects that depend upon this extension. - */ - public <T> List<Object> getDependents(T extension) { - List<Object> result = new ArrayList<>(); - result.addAll(evaluateAnnotatedClasses(extension, DependedUpon.class)); - return result; - } - - private static void completePhaseDependencies(DirectAcyclicGraph dag, Object extension) { - Phase.Name phase = evaluatePhase(extension); - dag.add(extension, phase); - for (Phase.Name name : Phase.Name.values()) { - if (phase.compareTo(name) < 0) { - dag.add(name, extension); - } else if (phase.compareTo(name) > 0) { - dag.add(extension, name); - } - } - } - - protected List<Object> evaluateAnnotatedClasses(Object extension, Class<? extends Annotation> annotation) { - List<Object> results = Lists.newArrayList(); - Class aClass = extension.getClass(); - while (aClass != null) { - evaluateClass(aClass, annotation, results); - - for (Method method : aClass.getDeclaredMethods()) { - if (method.getAnnotation(annotation) != null) { - checkAnnotatedMethod(method); - evaluateMethod(extension, method, results); - } - } - aClass = aClass.getSuperclass(); - } - - return results; - } - - private static void evaluateClass(Class extensionClass, Class annotationClass, List<Object> results) { - Annotation annotation = extensionClass.getAnnotation(annotationClass); - if (annotation != null) { - if (annotation.annotationType().isAssignableFrom(DependsUpon.class)) { - results.addAll(Arrays.asList(((DependsUpon) annotation).value())); - - } else if (annotation.annotationType().isAssignableFrom(DependedUpon.class)) { - results.addAll(Arrays.asList(((DependedUpon) annotation).value())); - } - } - - Class[] interfaces = extensionClass.getInterfaces(); - for (Class anInterface : interfaces) { - evaluateClass(anInterface, annotationClass, results); - } - } - - private void evaluateMethod(Object extension, Method method, List<Object> results) { - try { - Object result = method.invoke(extension); - if (result != null) { - if (result instanceof Class<?>) { - results.addAll(componentContainer.getComponentsByType((Class<?>) result)); - - } else if (result instanceof Collection<?>) { - results.addAll((Collection<?>) result); - - } else if (result.getClass().isArray()) { - for (int i = 0; i < Array.getLength(result); i++) { - results.add(Array.get(result, i)); - } - - } else { - results.add(result); - } - } - } catch (Exception e) { - throw new IllegalStateException("Can not invoke method " + method, e); - } - } - - private static void checkAnnotatedMethod(Method method) { - if (!Modifier.isPublic(method.getModifiers())) { - throw new IllegalStateException("Annotated method must be public:" + method); - } - if (method.getParameterTypes().length > 0) { - throw new IllegalStateException("Annotated method must not have parameters:" + method); - } - } - - private static boolean shouldKeep(Class type, Object extension, @Nullable Project project, @Nullable ExtensionMatcher matcher) { - boolean keep = (ClassUtils.isAssignable(extension.getClass(), type) - || (org.sonar.api.batch.Sensor.class.equals(type) && ClassUtils.isAssignable(extension.getClass(), Sensor.class))) - && (matcher == null || matcher.accept(extension)); - if (keep && project != null && ClassUtils.isAssignable(extension.getClass(), CheckProject.class)) { - keep = ((CheckProject) extension).shouldExecuteOnProject(project); - } - return keep; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginInstaller.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginInstaller.java deleted file mode 100644 index 1c1ee8e1fc4..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginInstaller.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Lists; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang.CharUtils; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.SonarPlugin; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; -import org.sonar.batch.cache.WSLoader; -import org.sonar.batch.cache.WSLoaderResult; -import org.sonar.core.platform.PluginInfo; -import org.sonar.core.platform.RemotePlugin; -import org.sonar.core.platform.RemotePluginFile; -import org.sonar.home.cache.FileCache; -import org.sonarqube.ws.client.GetRequest; -import org.sonarqube.ws.client.WsResponse; - -import static java.lang.String.format; - -/** - * Downloads the plugins installed on server and stores them in a local user cache - * (see {@link FileCacheProvider}). - */ -public class BatchPluginInstaller implements PluginInstaller { - - private static final Logger LOG = Loggers.get(BatchPluginInstaller.class); - private static final String PLUGINS_INDEX_URL = "/deploy/plugins/index.txt"; - - private final WSLoader wsLoader; - private final FileCache fileCache; - private final BatchPluginPredicate pluginPredicate; - private final BatchWsClient wsClient; - - public BatchPluginInstaller(WSLoader wsLoader, BatchWsClient wsClient, FileCache fileCache, BatchPluginPredicate pluginPredicate) { - this.wsLoader = wsLoader; - this.fileCache = fileCache; - this.pluginPredicate = pluginPredicate; - this.wsClient = wsClient; - } - - @Override - public Map<String, PluginInfo> installRemotes() { - return loadPlugins(listRemotePlugins()); - } - - private Map<String, PluginInfo> loadPlugins(List<RemotePlugin> remotePlugins) { - Map<String, PluginInfo> infosByKey = new HashMap<>(); - - Profiler profiler = Profiler.create(LOG).startDebug("Load plugins"); - - for (RemotePlugin remotePlugin : remotePlugins) { - if (pluginPredicate.apply(remotePlugin.getKey())) { - File jarFile = download(remotePlugin); - PluginInfo info = PluginInfo.create(jarFile); - infosByKey.put(info.getKey(), info); - } - } - - profiler.stopDebug(); - return infosByKey; - } - - /** - * Returns empty on purpose. This method is used only by tests. - * @see org.sonar.batch.mediumtest.BatchMediumTester - */ - @Override - public Map<String, SonarPlugin> installLocals() { - return Collections.emptyMap(); - } - - @VisibleForTesting - File download(final RemotePlugin remote) { - try { - final RemotePluginFile file = remote.file(); - return fileCache.get(file.getFilename(), file.getHash(), new FileDownloader(remote.getKey())); - } catch (Exception e) { - throw new IllegalStateException("Fail to download plugin: " + remote.getKey(), e); - } - } - - /** - * Gets information about the plugins installed on server (filename, checksum) - */ - @VisibleForTesting - List<RemotePlugin> listRemotePlugins() { - try { - String pluginIndex = loadPluginIndex(); - String[] rows = StringUtils.split(pluginIndex, CharUtils.LF); - List<RemotePlugin> result = Lists.newArrayList(); - for (String row : rows) { - result.add(RemotePlugin.unmarshal(row)); - } - return result; - - } catch (Exception e) { - throw new IllegalStateException("Fail to load plugin index: " + PLUGINS_INDEX_URL, e); - } - } - - private String loadPluginIndex() { - Profiler profiler = Profiler.create(LOG).startInfo("Load plugins index"); - WSLoaderResult<String> wsResult = wsLoader.loadString(PLUGINS_INDEX_URL); - profiler.stopInfo(wsResult.isFromCache()); - return wsResult.get(); - } - - private class FileDownloader implements FileCache.Downloader { - private String key; - - FileDownloader(String key) { - this.key = key; - } - - @Override - public void download(String filename, File toFile) throws IOException { - String url = format("/deploy/plugins/%s/%s", key, filename); - if (LOG.isDebugEnabled()) { - LOG.debug("Download plugin {} to {}", filename, toFile); - } else { - LOG.info("Download {}", filename); - } - - WsResponse response = wsClient.call(new GetRequest(url)); - try (InputStream stream = response.contentStream()) { - FileUtils.copyInputStreamToFile(stream, toFile); - } - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginJarExploder.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginJarExploder.java deleted file mode 100644 index a91a6b38cbc..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginJarExploder.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import org.apache.commons.io.FileUtils; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.utils.ZipUtils; -import org.sonar.core.platform.ExplodedPlugin; -import org.sonar.core.platform.PluginJarExploder; -import org.sonar.core.platform.PluginInfo; -import org.sonar.home.cache.FileCache; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; - -import static org.sonar.core.util.FileUtils.deleteQuietly; - -@BatchSide -public class BatchPluginJarExploder extends PluginJarExploder { - - private final FileCache fileCache; - - public BatchPluginJarExploder(FileCache fileCache) { - this.fileCache = fileCache; - } - - @Override - public ExplodedPlugin explode(PluginInfo info) { - try { - File dir = unzipFile(info.getNonNullJarFile()); - return explodeFromUnzippedDir(info.getKey(), info.getNonNullJarFile(), dir); - } catch (Exception e) { - throw new IllegalStateException(String.format("Fail to open plugin [%s]: %s", info.getKey(), info.getNonNullJarFile().getAbsolutePath()), e); - } - } - - private File unzipFile(File cachedFile) throws IOException { - String filename = cachedFile.getName(); - File destDir = new File(cachedFile.getParentFile(), filename + "_unzip"); - File lockFile = new File(cachedFile.getParentFile(), filename + "_unzip.lock"); - if (!destDir.exists()) { - FileOutputStream out = new FileOutputStream(lockFile); - try { - java.nio.channels.FileLock lock = out.getChannel().lock(); - try { - // Recheck in case of concurrent processes - if (!destDir.exists()) { - File tempDir = fileCache.createTempDir(); - ZipUtils.unzip(cachedFile, tempDir, newLibFilter()); - FileUtils.moveDirectory(tempDir, destDir); - } - } finally { - lock.release(); - } - } finally { - out.close(); - deleteQuietly(lockFile); - } - } - return destDir; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginPredicate.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginPredicate.java deleted file mode 100644 index ea4380c44da..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginPredicate.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import com.google.common.base.Joiner; -import com.google.common.base.Predicate; -import com.google.common.base.Splitter; -import com.google.common.collect.Lists; - -import java.util.List; -import java.util.Set; - -import javax.annotation.Nonnull; - -import org.apache.commons.lang.StringUtils; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import static com.google.common.collect.Sets.newHashSet; - -/** - * Filters the plugins to be enabled during analysis - */ -@BatchSide -public class BatchPluginPredicate implements Predicate<String> { - - private static final Logger LOG = Loggers.get(BatchPluginPredicate.class); - - private static final String BUILDBREAKER_PLUGIN_KEY = "buildbreaker"; - private static final Joiner COMMA_JOINER = Joiner.on(", "); - - private final Set<String> whites = newHashSet(); - private final Set<String> blacks = newHashSet(); - private final GlobalMode mode; - - public BatchPluginPredicate(Settings settings, GlobalMode mode) { - this.mode = mode; - if (mode.isPreview() || mode.isIssues()) { - // These default values are not supported by Settings because the class CorePlugin - // is not loaded yet. - whites.addAll(propertyValues(settings, - CoreProperties.PREVIEW_INCLUDE_PLUGINS, CoreProperties.PREVIEW_INCLUDE_PLUGINS_DEFAULT_VALUE)); - blacks.addAll(propertyValues(settings, - CoreProperties.PREVIEW_EXCLUDE_PLUGINS, CoreProperties.PREVIEW_EXCLUDE_PLUGINS_DEFAULT_VALUE)); - } - if (!whites.isEmpty()) { - LOG.info("Include plugins: " + COMMA_JOINER.join(whites)); - } - if (!blacks.isEmpty()) { - LOG.info("Exclude plugins: " + COMMA_JOINER.join(blacks)); - } - } - - @Override - public boolean apply(@Nonnull String pluginKey) { - if (BUILDBREAKER_PLUGIN_KEY.equals(pluginKey) && mode.isPreview()) { - LOG.info("Build Breaker plugin is no more supported in preview mode"); - return false; - } - - if (whites.isEmpty()) { - return blacks.isEmpty() || !blacks.contains(pluginKey); - } - return whites.contains(pluginKey); - } - - Set<String> getWhites() { - return whites; - } - - Set<String> getBlacks() { - return blacks; - } - - private static List<String> propertyValues(Settings settings, String key, String defaultValue) { - String s = StringUtils.defaultIfEmpty(settings.getString(key), defaultValue); - return Lists.newArrayList(Splitter.on(",").trimResults().omitEmptyStrings().split(s)); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java deleted file mode 100644 index 4d0d0c46c2d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Maps; -import java.util.Collection; -import java.util.Map; -import org.picocontainer.Startable; -import org.sonar.api.SonarPlugin; -import org.sonar.core.platform.PluginInfo; -import org.sonar.core.platform.PluginLoader; -import org.sonar.core.platform.PluginRepository; - -/** - * Orchestrates the installation and loading of plugins - */ -public class BatchPluginRepository implements PluginRepository, Startable { - private static final Logger LOG = Loggers.get(BatchPluginRepository.class); - - private final PluginInstaller installer; - private final PluginLoader loader; - - private Map<String, SonarPlugin> pluginInstancesByKeys; - private Map<String, PluginInfo> infosByKeys; - - public BatchPluginRepository(PluginInstaller installer, PluginLoader loader) { - this.installer = installer; - this.loader = loader; - } - - @Override - public void start() { - infosByKeys = Maps.newHashMap(installer.installRemotes()); - pluginInstancesByKeys = Maps.newHashMap(loader.load(infosByKeys)); - - // this part is only used by tests - for (Map.Entry<String, SonarPlugin> entry : installer.installLocals().entrySet()) { - String pluginKey = entry.getKey(); - infosByKeys.put(pluginKey, new PluginInfo(pluginKey)); - pluginInstancesByKeys.put(pluginKey, entry.getValue()); - } - - logPlugins(); - } - - private void logPlugins() { - if (infosByKeys.isEmpty()) { - LOG.debug("No plugins loaded"); - } else { - LOG.debug("Plugins:"); - for (PluginInfo p : infosByKeys.values()) { - LOG.debug(" * {} {} ({})", p.getName(), p.getVersion(), p.getKey()); - } - } - } - - @Override - public void stop() { - // close plugin classloaders - loader.unload(pluginInstancesByKeys.values()); - - pluginInstancesByKeys.clear(); - infosByKeys.clear(); - } - - @Override - public Collection<PluginInfo> getPluginInfos() { - return infosByKeys.values(); - } - - @Override - public PluginInfo getPluginInfo(String key) { - PluginInfo info = infosByKeys.get(key); - Preconditions.checkState(info != null, String.format("Plugin [%s] does not exist", key)); - return info; - } - - @Override - public SonarPlugin getPluginInstance(String key) { - SonarPlugin instance = pluginInstancesByKeys.get(key); - Preconditions.checkState(instance != null, String.format("Plugin [%s] does not exist", key)); - return instance; - } - - @Override - public boolean hasPlugin(String key) { - return infosByKeys.containsKey(key); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchWsClient.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchWsClient.java deleted file mode 100644 index d77670a6c86..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchWsClient.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Joiner; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import java.net.HttpURLConnection; -import java.util.ArrayList; -import java.util.List; -import org.sonar.api.CoreProperties; -import org.sonar.api.utils.MessageException; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; -import org.sonarqube.ws.client.HttpException; -import org.sonarqube.ws.client.WsClient; -import org.sonarqube.ws.client.WsConnector; -import org.sonarqube.ws.client.WsRequest; -import org.sonarqube.ws.client.WsResponse; - -import static java.lang.String.format; - -public class BatchWsClient { - - private static final Logger LOG = Loggers.get(BatchWsClient.class); - - private final WsClient target; - private final boolean hasCredentials; - - public BatchWsClient(WsClient target, boolean hasCredentials) { - this.target = target; - this.hasCredentials = hasCredentials; - } - - /** - * @throws IllegalStateException if the request could not be executed due to - * a connectivity problem or timeout. Because networks can - * fail during an exchange, it is possible that the remote server - * accepted the request before the failure - * @throws HttpException if the response code is not in range [200..300) - */ - public WsResponse call(WsRequest request) { - Profiler profiler = Profiler.createIfDebug(LOG).start(); - WsResponse response = target.wsConnector().call(request); - profiler.stopDebug(format("%s %d %s", request.getMethod(), response.code(), response.requestUrl())); - failIfUnauthorized(response); - return response; - } - - public String baseUrl() { - return target.wsConnector().baseUrl(); - } - - @VisibleForTesting - WsConnector wsConnector() { - return target.wsConnector(); - } - - private void failIfUnauthorized(WsResponse response) { - if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) { - if (hasCredentials) { - // credentials are not valid - throw MessageException.of(format("Not authorized. Please check the properties %s and %s.", - CoreProperties.LOGIN, CoreProperties.PASSWORD)); - } - // not authenticated - see https://jira.sonarsource.com/browse/SONAR-4048 - throw MessageException.of(format("Not authorized. Analyzing this project requires to be authenticated. " + - "Please provide the values of the properties %s and %s.", CoreProperties.LOGIN, CoreProperties.PASSWORD)); - - } - if (response.code() == HttpURLConnection.HTTP_FORBIDDEN) { - // SONAR-4397 Details are in response content - throw MessageException.of(tryParseAsJsonError(response.content())); - } - response.failIfNotSuccessful(); - } - - private static String tryParseAsJsonError(String responseContent) { - try { - JsonParser parser = new JsonParser(); - JsonObject obj = parser.parse(responseContent).getAsJsonObject(); - JsonArray errors = obj.getAsJsonArray("errors"); - List<String> errorMessages = new ArrayList<>(); - for (JsonElement e : errors) { - errorMessages.add(e.getAsJsonObject().get("msg").getAsString()); - } - return Joiner.on(", ").join(errorMessages); - } catch (Exception e) { - return responseContent; - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchWsClientProvider.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchWsClientProvider.java deleted file mode 100644 index d33baf821e5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchWsClientProvider.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.BatchSide; -import org.sonar.batch.bootstrapper.EnvironmentInformation; -import org.sonarqube.ws.client.HttpConnector; -import org.sonarqube.ws.client.HttpWsClient; - -import static java.lang.Integer.parseInt; -import static java.lang.String.valueOf; -import static org.apache.commons.lang.StringUtils.defaultIfBlank; - -@BatchSide -public class BatchWsClientProvider extends ProviderAdapter { - - static final int CONNECT_TIMEOUT_MS = 5_000; - static final String READ_TIMEOUT_SEC_PROPERTY = "sonar.ws.timeout"; - static final int DEFAULT_READ_TIMEOUT_SEC = 60; - - private BatchWsClient wsClient; - - public synchronized BatchWsClient provide(final GlobalProperties settings, final EnvironmentInformation env) { - if (wsClient == null) { - String url = defaultIfBlank(settings.property("sonar.host.url"), CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE); - HttpConnector.Builder builder = new HttpConnector.Builder(); - - // TODO proxy - - String timeoutSec = defaultIfBlank(settings.property(READ_TIMEOUT_SEC_PROPERTY), valueOf(DEFAULT_READ_TIMEOUT_SEC)); - String login = defaultIfBlank(settings.property(CoreProperties.LOGIN), null); - builder - .readTimeoutMilliseconds(parseInt(timeoutSec) * 1_000) - .connectTimeoutMilliseconds(CONNECT_TIMEOUT_MS) - .userAgent(env.toString()) - .url(url) - .credentials(login, settings.property(CoreProperties.PASSWORD)); - - wsClient = new BatchWsClient(new HttpWsClient(builder.build()), login != null); - } - return wsClient; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DroppedPropertyChecker.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DroppedPropertyChecker.java deleted file mode 100644 index 72b34d613ea..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DroppedPropertyChecker.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import java.util.Map; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - -import static java.util.Objects.requireNonNull; - -public class DroppedPropertyChecker { - - private static final Logger LOG = Loggers.get(DroppedPropertyChecker.class); - - private final Map<String, String> settings; - private final Map<String, String> properties; - - public DroppedPropertyChecker(Map<String, String> properties, Map<String, String> droppedPropertiesAndMsg) { - this.settings = requireNonNull(properties); - this.properties = requireNonNull(droppedPropertiesAndMsg); - } - - public void checkDroppedProperties() { - for (Map.Entry<String, String> entry : properties.entrySet()) { - if (settings.containsKey(entry.getKey())) { - LOG.warn("Property '{}' is not supported any more. {}", entry.getKey(), entry.getValue()); - } - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionInstaller.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionInstaller.java deleted file mode 100644 index ff610139032..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionInstaller.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import java.util.List; -import javax.annotation.Nullable; -import org.sonar.api.ExtensionProvider; -import org.sonar.api.SonarPlugin; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.core.platform.ComponentContainer; -import org.sonar.core.platform.PluginInfo; -import org.sonar.core.platform.PluginRepository; - -public class ExtensionInstaller { - - private final PluginRepository pluginRepository; - private final AnalysisMode analysisMode; - - public ExtensionInstaller(PluginRepository pluginRepository, AnalysisMode analysisMode) { - this.pluginRepository = pluginRepository; - this.analysisMode = analysisMode; - } - - public ExtensionInstaller install(ComponentContainer container, ExtensionMatcher matcher) { - - // core components - for (Object o : BatchComponents.all(analysisMode)) { - doInstall(container, matcher, null, o); - } - - // plugin extensions - for (PluginInfo pluginInfo : pluginRepository.getPluginInfos()) { - SonarPlugin plugin = pluginRepository.getPluginInstance(pluginInfo.getKey()); - for (Object extension : plugin.getExtensions()) { - doInstall(container, matcher, pluginInfo, extension); - } - } - List<ExtensionProvider> providers = container.getComponentsByType(ExtensionProvider.class); - for (ExtensionProvider provider : providers) { - Object object = provider.provide(); - if (object instanceof Iterable) { - for (Object extension : (Iterable) object) { - doInstall(container, matcher, null, extension); - } - } else { - doInstall(container, matcher, null, object); - } - } - return this; - } - - private static void doInstall(ComponentContainer container, ExtensionMatcher matcher, @Nullable PluginInfo pluginInfo, Object extension) { - if (matcher.accept(extension)) { - container.addExtension(pluginInfo, extension); - } else { - container.declareExtension(pluginInfo, extension); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionMatcher.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionMatcher.java deleted file mode 100644 index d38581f572d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionMatcher.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import org.sonar.api.batch.BatchSide; - -/** - * @since 3.6 - */ -@BatchSide -public interface ExtensionMatcher { - boolean accept(Object extension); -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java deleted file mode 100644 index adf16a40315..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.InstantiationStrategy; -import org.sonar.api.utils.AnnotationUtils; - -public class ExtensionUtils { - - private ExtensionUtils() { - // only static methods - } - - public static boolean isInstantiationStrategy(Object extension, String strategy) { - InstantiationStrategy annotation = AnnotationUtils.getAnnotation(extension, InstantiationStrategy.class); - if (annotation != null) { - return strategy.equals(annotation.value()); - } - return InstantiationStrategy.PER_PROJECT.equals(strategy); - } - - public static boolean isBatchSide(Object extension) { - return AnnotationUtils.getAnnotation(extension, BatchSide.class) != null; - } - - public static boolean isType(Object extension, Class<?> extensionClass) { - Class clazz = extension instanceof Class ? (Class) extension : extension.getClass(); - return extensionClass.isAssignableFrom(clazz); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/FileCacheProvider.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/FileCacheProvider.java deleted file mode 100644 index 9880c7de23b..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/FileCacheProvider.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.config.Settings; -import org.sonar.home.cache.FileCache; -import org.sonar.home.cache.FileCacheBuilder; - -public class FileCacheProvider extends ProviderAdapter { - private FileCache cache; - - public FileCache provide(Settings settings) { - if (cache == null) { - String home = settings.getString("sonar.userHome"); - cache = new FileCacheBuilder(new Slf4jLogger()).setUserHome(home).build(); - } - return cache; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java deleted file mode 100644 index 8ac69fd6258..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import java.util.List; -import java.util.Map; -import org.sonar.api.SonarPlugin; -import org.sonar.api.utils.System2; -import org.sonar.api.utils.UriReader; -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; -import org.sonar.batch.index.CachesManager; -import org.sonar.batch.platform.DefaultServer; -import org.sonar.batch.repository.DefaultGlobalRepositoriesLoader; -import org.sonar.batch.repository.GlobalRepositoriesLoader; -import org.sonar.batch.repository.GlobalRepositoriesProvider; -import org.sonar.batch.task.TaskContainer; -import org.sonar.core.platform.ComponentContainer; -import org.sonar.core.platform.PluginClassloaderFactory; -import org.sonar.core.platform.PluginInfo; -import org.sonar.core.platform.PluginLoader; -import org.sonar.core.platform.PluginRepository; -import org.sonar.core.util.DefaultHttpDownloader; -import org.sonar.core.util.UuidFactoryImpl; - -public class GlobalContainer extends ComponentContainer { - - private final Map<String, String> bootstrapProperties; - private boolean preferCache; - - private GlobalContainer(Map<String, String> bootstrapProperties, boolean preferCache) { - super(); - this.bootstrapProperties = bootstrapProperties; - this.preferCache = preferCache; - } - - public static GlobalContainer create(Map<String, String> bootstrapProperties, List<?> extensions, boolean preferCache) { - GlobalContainer container = new GlobalContainer(bootstrapProperties, preferCache); - container.add(extensions); - return container; - } - - @Override - protected void doBeforeStart() { - GlobalProperties bootstrapProps = new GlobalProperties(bootstrapProperties); - GlobalMode globalMode = new GlobalMode(bootstrapProps); - LoadStrategy strategy = getDataLoadingStrategy(globalMode, preferCache); - StrategyWSLoaderProvider wsLoaderProvider = new StrategyWSLoaderProvider(strategy); - add(wsLoaderProvider); - add(bootstrapProps); - add(globalMode); - addBootstrapComponents(); - } - - private static LoadStrategy getDataLoadingStrategy(GlobalMode mode, boolean preferCache) { - if (!mode.isIssues()) { - return LoadStrategy.SERVER_ONLY; - } - - return preferCache ? LoadStrategy.CACHE_FIRST : LoadStrategy.SERVER_FIRST; - } - - private void addBootstrapComponents() { - add( - // plugins - BatchPluginRepository.class, - PluginLoader.class, - PluginClassloaderFactory.class, - BatchPluginJarExploder.class, - BatchPluginPredicate.class, - ExtensionInstaller.class, - - CachesManager.class, - GlobalSettings.class, - new BatchWsClientProvider(), - DefaultServer.class, - new GlobalTempFolderProvider(), - DefaultHttpDownloader.class, - UriReader.class, - new FileCacheProvider(), - new GlobalPersistentCacheProvider(), - System2.INSTANCE, - new GlobalRepositoriesProvider(), - UuidFactoryImpl.INSTANCE); - addIfMissing(BatchPluginInstaller.class, PluginInstaller.class); - addIfMissing(DefaultGlobalRepositoriesLoader.class, GlobalRepositoriesLoader.class); - } - - @Override - protected void doAfterStart() { - installPlugins(); - } - - private void installPlugins() { - PluginRepository pluginRepository = getComponentByType(PluginRepository.class); - for (PluginInfo pluginInfo : pluginRepository.getPluginInfos()) { - SonarPlugin instance = pluginRepository.getPluginInstance(pluginInfo.getKey()); - addExtension(pluginInfo, instance); - } - } - - public void executeTask(Map<String, String> taskProperties, Object... components) { - new TaskContainer(this, taskProperties, components).execute(); - } - - public void syncProject(String projectKey, boolean force) { - new ProjectSyncContainer(this, projectKey, force).execute(); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalMode.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalMode.java deleted file mode 100644 index 774dac71361..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalMode.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import org.sonar.api.CoreProperties; - -import org.sonar.api.batch.AnalysisMode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class GlobalMode extends AbstractAnalysisMode implements AnalysisMode { - private static final Logger LOG = LoggerFactory.getLogger(GlobalMode.class); - - public GlobalMode(GlobalProperties props) { - String mode = props.property(CoreProperties.ANALYSIS_MODE); - validate(mode); - issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode); - - if (preview) { - LOG.debug("Preview global mode"); - } else if (issues) { - LOG.debug("Issues global mode"); - } else { - LOG.debug("Publish global mode"); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalProperties.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalProperties.java deleted file mode 100644 index 62bc070080e..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalProperties.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import org.sonar.api.CoreProperties; - -import java.util.Map; - -/** - * Immutable batch properties that are not specific to a task (for example - * coming from global configuration file of sonar-runner). - */ -public class GlobalProperties extends UserProperties { - - public GlobalProperties(Map<String, String> properties) { - super(properties, properties.get(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalSettings.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalSettings.java deleted file mode 100644 index 4e5ebdd9c98..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalSettings.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import com.google.common.collect.ImmutableMap; -import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.config.PropertyDefinitions; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.MessageException; -import org.sonar.batch.protocol.input.GlobalRepositories; - -public class GlobalSettings extends Settings { - - private static final Logger LOG = LoggerFactory.getLogger(GlobalSettings.class); - - private static final String JDBC_SPECIFIC_MESSAGE = "It will be ignored. There is no longer any DB connection to the SQ database."; - /** - * A map of dropped properties as key and specific message to display for that property - * (what will happen, what should the user do, ...) as a value - */ - private static final Map<String, String> DROPPED_PROPERTIES = ImmutableMap.of( - "sonar.jdbc.url", JDBC_SPECIFIC_MESSAGE, - "sonar.jdbc.username", JDBC_SPECIFIC_MESSAGE, - "sonar.jdbc.password", JDBC_SPECIFIC_MESSAGE); - - private final GlobalProperties bootstrapProps; - private final GlobalRepositories globalReferentials; - private final GlobalMode mode; - - public GlobalSettings(GlobalProperties bootstrapProps, PropertyDefinitions propertyDefinitions, - GlobalRepositories globalReferentials, GlobalMode mode) { - - super(propertyDefinitions); - this.mode = mode; - getEncryption().setPathToSecretKey(bootstrapProps.property(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); - this.bootstrapProps = bootstrapProps; - this.globalReferentials = globalReferentials; - init(); - new DroppedPropertyChecker(this.getProperties(), DROPPED_PROPERTIES).checkDroppedProperties(); - } - - private void init() { - addProperties(globalReferentials.globalSettings()); - addProperties(bootstrapProps.properties()); - - if (hasKey(CoreProperties.PERMANENT_SERVER_ID)) { - LOG.info("Server id: " + getString(CoreProperties.PERMANENT_SERVER_ID)); - } - } - - @Override - protected void doOnGetProperties(String key) { - if (mode.isIssues() && key.endsWith(".secured") && !key.contains(".license")) { - throw MessageException.of("Access to the secured property '" + key - + "' is not possible in issues mode. The SonarQube plugin which requires this property must be deactivated in issues mode."); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalTempFolderProvider.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalTempFolderProvider.java deleted file mode 100644 index 2ea3bff55a3..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalTempFolderProvider.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import java.io.IOException; -import java.nio.file.DirectoryStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.concurrent.TimeUnit; -import org.apache.commons.lang.StringUtils; -import org.picocontainer.ComponentLifecycle; -import org.picocontainer.PicoContainer; -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.CoreProperties; -import org.sonar.api.utils.System2; -import org.sonar.api.utils.TempFolder; -import org.sonar.api.utils.internal.DefaultTempFolder; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - -import static org.sonar.core.util.FileUtils.deleteQuietly; - -public class GlobalTempFolderProvider extends ProviderAdapter implements ComponentLifecycle<TempFolder> { - private static final Logger LOG = Loggers.get(GlobalTempFolderProvider.class); - private static final long CLEAN_MAX_AGE = TimeUnit.DAYS.toMillis(21); - static final String TMP_NAME_PREFIX = ".sonartmp_"; - private boolean started = false; - - private System2 system; - private DefaultTempFolder tempFolder; - - public GlobalTempFolderProvider() { - this(new System2()); - } - - GlobalTempFolderProvider(System2 system) { - this.system = system; - } - - public TempFolder provide(GlobalProperties bootstrapProps) { - if (tempFolder == null) { - - String workingPathName = StringUtils.defaultIfBlank(bootstrapProps.property(CoreProperties.GLOBAL_WORKING_DIRECTORY), CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE); - Path workingPath = Paths.get(workingPathName); - - if (!workingPath.isAbsolute()) { - Path home = findSonarHome(bootstrapProps); - workingPath = home.resolve(workingPath).normalize(); - } - - try { - cleanTempFolders(workingPath); - } catch (IOException e) { - LOG.error(String.format("failed to clean global working directory: %s", workingPath), e); - } - Path tempDir = createTempFolder(workingPath); - tempFolder = new DefaultTempFolder(tempDir.toFile(), true); - } - return tempFolder; - } - - private static Path createTempFolder(Path workingPath) { - try { - Files.createDirectories(workingPath); - } catch (IOException e) { - throw new IllegalStateException("Failed to create working path: " + workingPath, e); - } - - try { - return Files.createTempDirectory(workingPath, TMP_NAME_PREFIX); - } catch (IOException e) { - throw new IllegalStateException("Failed to create temporary folder in " + workingPath, e); - } - } - - private Path findSonarHome(GlobalProperties props) { - String home = props.property("sonar.userHome"); - if (home != null) { - return Paths.get(home).toAbsolutePath(); - } - - home = system.envVariable("SONAR_USER_HOME"); - - if (home != null) { - return Paths.get(home).toAbsolutePath(); - } - - home = system.property("user.home"); - return Paths.get(home, ".sonar").toAbsolutePath(); - } - - private static void cleanTempFolders(Path path) throws IOException { - if (Files.exists(path)) { - try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, new CleanFilter())) { - for (Path p : stream) { - deleteQuietly(p.toFile()); - } - } - } - } - - private static class CleanFilter implements DirectoryStream.Filter<Path> { - @Override - public boolean accept(Path path) throws IOException { - if (!Files.isDirectory(path)) { - return false; - } - - if (!path.getFileName().toString().startsWith(TMP_NAME_PREFIX)) { - return false; - } - - long threshold = System.currentTimeMillis() - CLEAN_MAX_AGE; - - // we could also check the timestamp in the name, instead - BasicFileAttributes attrs; - - try { - attrs = Files.readAttributes(path, BasicFileAttributes.class); - } catch (IOException ioe) { - LOG.error(String.format("Couldn't read file attributes for %s : ", path), ioe); - return false; - } - - long creationTime = attrs.creationTime().toMillis(); - return creationTime < threshold; - } - } - - @Override - public void start(PicoContainer container) { - started = true; - } - - @Override - public void stop(PicoContainer container) { - if (tempFolder != null) { - tempFolder.stop(); - } - } - - @Override - public void dispose(PicoContainer container) { - //nothing to do - } - - @Override - public boolean componentHasLifecycle() { - return true; - } - - @Override - public boolean isStarted() { - return started; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/MetricProvider.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/MetricProvider.java deleted file mode 100644 index 1735ab4a79b..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/MetricProvider.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import com.google.common.collect.Lists; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.ExtensionProvider; -import org.sonar.api.batch.InstantiationStrategy; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Metric; -import org.sonar.api.measures.Metrics; - -import java.util.List; - -@BatchSide -@InstantiationStrategy(InstantiationStrategy.PER_BATCH) -public class MetricProvider extends ExtensionProvider { - - private Metrics[] factories; - - public MetricProvider(Metrics[] factories) { - this.factories = factories; - } - - public MetricProvider() { - this.factories = new Metrics[0]; - } - - @Override - public List<Metric> provide() { - List<Metric> metrics = Lists.newArrayList(CoreMetrics.getMetrics()); - for (Metrics factory : factories) { - metrics.addAll(factory.getMetrics()); - } - return metrics; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PluginInstaller.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PluginInstaller.java deleted file mode 100644 index 64f923bd9a5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PluginInstaller.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import java.util.Map; -import org.sonar.api.SonarPlugin; -import org.sonar.api.batch.BatchSide; -import org.sonar.core.platform.PluginInfo; - -@BatchSide -public interface PluginInstaller { - - /** - * Gets the list of plugins installed on server and downloads them if not - * already in local cache. - * @return information about all installed plugins, grouped by key - */ - Map<String, PluginInfo> installRemotes(); - - /** - * Used only by tests. - * @see org.sonar.batch.mediumtest.BatchMediumTester - */ - Map<String, SonarPlugin> installLocals(); -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Slf4jLogger.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Slf4jLogger.java deleted file mode 100644 index 270bc10fe0d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Slf4jLogger.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Slf4jLogger implements org.sonar.home.cache.Logger { - - private static final Logger LOG = LoggerFactory.getLogger(Slf4jLogger.class); - - @Override - public void debug(String msg) { - LOG.debug(msg); - } - - @Override - public void info(String msg) { - LOG.info(msg); - } - - @Override - public void warn(String msg) { - LOG.warn(msg); - } - - @Override - public void error(String msg, Throwable t) { - LOG.error(msg, t); - } - - @Override - public void error(String msg) { - LOG.error(msg); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/UserProperties.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/UserProperties.java deleted file mode 100644 index d16f8e72d27..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/UserProperties.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrap; - -import com.google.common.collect.Maps; -import org.sonar.api.config.Encryption; - -import javax.annotation.Nullable; - -import java.util.Map; - -/** - * Properties that are coming from bootstrapper. - */ -public abstract class UserProperties { - - private final Map<String, String> properties; - private final Encryption encryption; - - public UserProperties(Map<String, String> properties, @Nullable String pathToSecretKey) { - encryption = new Encryption(pathToSecretKey); - Map<String, String> decryptedProps = Maps.newHashMap(); - for (Map.Entry<String, String> entry : properties.entrySet()) { - String value = entry.getValue(); - if (value != null && encryption.isEncrypted(value)) { - try { - value = encryption.decrypt(value); - } catch (Exception e) { - throw new IllegalStateException("Fail to decrypt the property " + entry.getKey() + ". Please check your secret key.", e); - } - } - decryptedProps.put(entry.getKey(), value); - } - this.properties = Maps.newHashMap(decryptedProps); - } - - public Map<String, String> properties() { - return properties; - } - - public String property(String key) { - return properties.get(key); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/package-info.java deleted file mode 100644 index 0efbab080ec..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.bootstrap; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java deleted file mode 100644 index d5d3da13087..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java +++ /dev/null @@ -1,274 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrapper; - -import org.sonar.api.utils.MessageException; - -import com.google.common.base.Throwables; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; - -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import org.picocontainer.annotations.Nullable; -import org.sonar.batch.bootstrap.GlobalContainer; - -/** - * Entry point for sonar-runner 2.1. - * - * @since 2.14 - */ -public final class Batch { - - private boolean started = false; - private LoggingConfiguration loggingConfig; - private List<Object> components; - private Map<String, String> bootstrapProperties = Maps.newHashMap(); - private GlobalContainer bootstrapContainer; - - private Batch(Builder builder) { - components = Lists.newArrayList(); - components.addAll(builder.components); - if (builder.environment != null) { - components.add(builder.environment); - } - if (builder.bootstrapProperties != null) { - bootstrapProperties.putAll(builder.bootstrapProperties); - } - if (builder.isEnableLoggingConfiguration()) { - loggingConfig = new LoggingConfiguration(builder.environment).setProperties(bootstrapProperties); - - if (builder.logOutput != null) { - loggingConfig.setLogOutput(builder.logOutput); - } - } - } - - public LoggingConfiguration getLoggingConfiguration() { - return loggingConfig; - } - - /** - * @deprecated since 4.4 use {@link #start()}, {@link #executeTask(Map)} and then {@link #stop()} - */ - @Deprecated - public synchronized Batch execute() { - configureLogging(); - start(); - boolean threw = true; - try { - executeTask(bootstrapProperties); - threw = false; - } finally { - doStop(threw); - } - - return this; - } - - /** - * @since 4.4 - */ - public synchronized Batch start() { - return start(false); - } - - public synchronized Batch start(boolean preferCache) { - if (started) { - throw new IllegalStateException("Batch is already started"); - } - - configureLogging(); - try { - bootstrapContainer = GlobalContainer.create(bootstrapProperties, components, preferCache); - bootstrapContainer.startComponents(); - } catch (RuntimeException e) { - throw handleException(e); - } - this.started = true; - - return this; - } - - /** - * @since 4.4 - */ - public Batch executeTask(Map<String, String> analysisProperties, Object... components) { - checkStarted(); - configureTaskLogging(analysisProperties); - try { - bootstrapContainer.executeTask(analysisProperties, components); - } catch (RuntimeException e) { - throw handleException(e); - } - return this; - } - - /** - * @since 5.2 - */ - public Batch executeTask(Map<String, String> analysisProperties, IssueListener issueListener) { - checkStarted(); - configureTaskLogging(analysisProperties); - try { - bootstrapContainer.executeTask(analysisProperties, components, issueListener); - } catch (RuntimeException e) { - throw handleException(e); - } - return this; - } - - private void checkStarted() { - if (!started) { - throw new IllegalStateException("Batch is not started. Unable to execute task."); - } - } - - private RuntimeException handleException(RuntimeException t) { - if (loggingConfig.isVerbose()) { - return t; - } - - for (Throwable y : Throwables.getCausalChain(t)) { - if (y instanceof MessageException) { - return (MessageException) y; - } - } - - return t; - } - - /** - * @since 5.2 - */ - public Batch syncProject(String projectKey) { - checkStarted(); - bootstrapContainer.syncProject(projectKey, true); - return this; - } - - /** - * @since 4.4 - */ - public synchronized void stop() { - doStop(false); - } - - private void doStop(boolean swallowException) { - checkStarted(); - configureLogging(); - try { - bootstrapContainer.stopComponents(swallowException); - } catch (RuntimeException e) { - throw handleException(e); - } - this.started = false; - } - - private void configureLogging() { - if (loggingConfig != null) { - loggingConfig.setProperties(bootstrapProperties); - LoggingConfigurator.apply(loggingConfig); - } - } - - private void configureTaskLogging(Map<String, String> taskProperties) { - if (loggingConfig != null) { - loggingConfig.setProperties(taskProperties, bootstrapProperties); - LoggingConfigurator.apply(loggingConfig); - } - } - - public static Builder builder() { - return new Builder(); - } - - public static final class Builder { - private Map<String, String> bootstrapProperties; - private EnvironmentInformation environment; - private List<Object> components = Lists.newArrayList(); - private boolean enableLoggingConfiguration = true; - private LogOutput logOutput; - - private Builder() { - } - - public Builder setEnvironment(EnvironmentInformation env) { - this.environment = env; - return this; - } - - public Builder setComponents(List<Object> l) { - this.components = l; - return this; - } - - public Builder setLogOutput(@Nullable LogOutput logOutput) { - this.logOutput = logOutput; - return this; - } - - /** - * @deprecated since 3.7 use {@link #setBootstrapProperties(Map)} - */ - @Deprecated - public Builder setGlobalProperties(Map<String, String> globalProperties) { - this.bootstrapProperties = globalProperties; - return this; - } - - public Builder setBootstrapProperties(Map<String, String> bootstrapProperties) { - this.bootstrapProperties = bootstrapProperties; - return this; - } - - public Builder addComponents(Object... components) { - Collections.addAll(this.components, components); - return this; - } - - public Builder addComponent(Object component) { - this.components.add(component); - return this; - } - - public boolean isEnableLoggingConfiguration() { - return enableLoggingConfiguration; - } - - /** - * Logback is configured by default. It can be disabled, but n this case the batch bootstrapper must provide its - * own implementation of SLF4J. - */ - public Builder setEnableLoggingConfiguration(boolean b) { - this.enableLoggingConfiguration = b; - return this; - } - - public Batch build() { - if (components == null) { - throw new IllegalStateException("Batch components are not set"); - } - return new Batch(this); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/EnvironmentInformation.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/EnvironmentInformation.java deleted file mode 100644 index a04da9df40a..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/EnvironmentInformation.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrapper; - -import org.sonar.api.batch.BatchSide; - -/** - * Describes execution environment. - * - * @since 2.6 - */ -@BatchSide -public class EnvironmentInformation { - - private String key; - private String version; - - public EnvironmentInformation(String key, String version) { - this.key = key; - this.version = version; - } - - /** - * @return unique key of environment, for example - "maven", "ant" - */ - public String getKey() { - return key; - } - - /** - * @return version of environment, for example Maven can have "2.2.1" or "3.0.2", - * but there is no guarantees about format - it's just a string. - */ - public String getVersion() { - return version; - } - - @Override - public String toString() { - return String.format("%s/%s", key, version); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/IssueListener.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/IssueListener.java deleted file mode 100644 index bf3f399142a..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/IssueListener.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrapper; - -public interface IssueListener { - void handle(Issue issue); - - class Issue { - /** @since 5.3 */ - private Integer startLine; - /** @since 5.3 */ - private Integer startLineOffset; - /** @since 5.3 */ - private Integer endLine; - /** @since 5.3 */ - private Integer endLineOffset; - - private String key; - private String componentKey; - private String message; - private String ruleKey; - private String ruleName; - private String status; - private String resolution; - private boolean isNew; - private String assigneeLogin; - private String assigneeName; - private String severity; - - public String getSeverity() { - return severity; - } - - public void setSeverity(String severity) { - this.severity = severity; - } - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } - - public String getComponentKey() { - return componentKey; - } - - public void setComponentKey(String componentKey) { - this.componentKey = componentKey; - } - - public Integer getStartLine() { - return startLine; - } - - public void setStartLine(Integer startLine) { - this.startLine = startLine; - } - - public Integer getStartLineOffset() { - return startLineOffset; - } - - public void setStartLineOffset(Integer startLineOffset) { - this.startLineOffset = startLineOffset; - } - - public Integer getEndLine() { - return endLine; - } - - public void setEndLine(Integer endLine) { - this.endLine = endLine; - } - - public Integer getEndLineOffset() { - return endLineOffset; - } - - public void setEndLineOffset(Integer endLineOffset) { - this.endLineOffset = endLineOffset; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public String getRuleKey() { - return ruleKey; - } - - public void setRuleKey(String ruleKey) { - this.ruleKey = ruleKey; - } - - public String getRuleName() { - return ruleName; - } - - public void setRuleName(String ruleName) { - this.ruleName = ruleName; - } - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - - public String getResolution() { - return resolution; - } - - public void setResolution(String resolution) { - this.resolution = resolution; - } - - public boolean isNew() { - return isNew; - } - - public void setNew(boolean isNew) { - this.isNew = isNew; - } - - public String getAssigneeLogin() { - return assigneeLogin; - } - - public void setAssigneeLogin(String assigneeLogin) { - this.assigneeLogin = assigneeLogin; - } - - public String getAssigneeName() { - return assigneeName; - } - - public void setAssigneeName(String assigneeName) { - this.assigneeName = assigneeName; - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LogCallbackAppender.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LogCallbackAppender.java deleted file mode 100644 index 183cd378711..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LogCallbackAppender.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrapper; - -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.UnsynchronizedAppenderBase; - -public class LogCallbackAppender extends UnsynchronizedAppenderBase<ILoggingEvent> { - protected LogOutput target; - - public LogCallbackAppender(LogOutput target) { - setTarget(target); - } - - public void setTarget(LogOutput target) { - this.target = target; - } - - @Override - protected void append(ILoggingEvent event) { - target.log(event.getFormattedMessage(), translate(event.getLevel())); - } - - private static LogOutput.Level translate(Level level) { - switch (level.toInt()) { - case Level.ERROR_INT: - return LogOutput.Level.ERROR; - case Level.WARN_INT: - return LogOutput.Level.WARN; - case Level.INFO_INT: - return LogOutput.Level.INFO; - case Level.TRACE_INT: - return LogOutput.Level.TRACE; - case Level.DEBUG_INT: - default: - return LogOutput.Level.DEBUG; - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LogOutput.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LogOutput.java deleted file mode 100644 index b69c405d931..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LogOutput.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrapper; - -/** - * Allow to redirect batch logs to a custom output. By defaults logs are written to System.out - * @since 5.2 - */ -public interface LogOutput { - - void log(String formattedMessage, Level level); - - enum Level { - ERROR, WARN, INFO, DEBUG, TRACE; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LoggingConfiguration.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LoggingConfiguration.java deleted file mode 100644 index 41a741bbd08..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LoggingConfiguration.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrapper; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Maps; - -import java.util.Map; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - -import org.apache.commons.lang.StringUtils; - -/** - * @since 2.14 - */ -public final class LoggingConfiguration { - - public static final String PROPERTY_ROOT_LOGGER_LEVEL = "ROOT_LOGGER_LEVEL"; - public static final String PROPERTY_SQL_LOGGER_LEVEL = "SQL_LOGGER_LEVEL"; - - public static final String PROPERTY_FORMAT = "FORMAT"; - - public static final String LEVEL_ROOT_VERBOSE = "DEBUG"; - public static final String LEVEL_ROOT_DEFAULT = "INFO"; - - @VisibleForTesting - static final String FORMAT_DEFAULT = "%d{HH:mm:ss.SSS} %-5level - %msg%n"; - @VisibleForTesting - static final String FORMAT_MAVEN = "[%level] [%d{HH:mm:ss.SSS}] %msg%n"; - - private Map<String, String> substitutionVariables = Maps.newHashMap(); - private LogOutput logOutput = null; - private boolean verbose; - - public LoggingConfiguration() { - this(null); - } - - public LoggingConfiguration(@Nullable EnvironmentInformation environment) { - setVerbose(false); - if (environment != null && "maven".equalsIgnoreCase(environment.getKey())) { - setFormat(FORMAT_MAVEN); - } else { - setFormat(FORMAT_DEFAULT); - } - } - - public LoggingConfiguration setProperties(Map<String, String> properties) { - setShowSql(properties, null); - setVerbose(properties, null); - return this; - } - - public LoggingConfiguration setProperties(Map<String, String> properties, @Nullable Map<String, String> fallback) { - setShowSql(properties, fallback); - setVerbose(properties, fallback); - return this; - } - - public LoggingConfiguration setLogOutput(@Nullable LogOutput listener) { - this.logOutput = listener; - return this; - } - - public LoggingConfiguration setVerbose(boolean verbose) { - return setRootLevel(verbose ? LEVEL_ROOT_VERBOSE : LEVEL_ROOT_DEFAULT); - } - - public boolean isVerbose() { - return verbose; - } - - public LoggingConfiguration setVerbose(Map<String, String> props, @Nullable Map<String, String> fallback) { - String logLevel = getFallback("sonar.log.level", props, fallback); - String deprecatedProfilingLevel = getFallback("sonar.log.profilingLevel", props, fallback); - verbose = "true".equals(getFallback("sonar.verbose", props, fallback)) || - "DEBUG".equals(logLevel) || "TRACE".equals(logLevel) || - "BASIC".equals(deprecatedProfilingLevel) || "FULL".equals(deprecatedProfilingLevel); - - return setVerbose(verbose); - } - - @CheckForNull - private static String getFallback(String key, Map<String, String> properties, @Nullable Map<String, String> fallback) { - if (properties.containsKey(key)) { - return properties.get(key); - } - - if (fallback != null) { - return fallback.get(key); - } - - return null; - } - - public LoggingConfiguration setRootLevel(String level) { - return addSubstitutionVariable(PROPERTY_ROOT_LOGGER_LEVEL, level); - } - - public LoggingConfiguration setShowSql(boolean showSql) { - return addSubstitutionVariable(PROPERTY_SQL_LOGGER_LEVEL, showSql ? "TRACE" : "WARN"); - } - - public LoggingConfiguration setShowSql(Map<String, String> properties, @Nullable Map<String, String> fallback) { - String logLevel = getFallback("sonar.log.level", properties, fallback); - String deprecatedProfilingLevel = getFallback("sonar.log.profilingLevel", properties, fallback); - boolean sql = "TRACE".equals(logLevel) || "FULL".equals(deprecatedProfilingLevel); - - return setShowSql(sql); - } - - @VisibleForTesting - LoggingConfiguration setFormat(String format) { - return addSubstitutionVariable(PROPERTY_FORMAT, StringUtils.defaultIfBlank(format, FORMAT_DEFAULT)); - } - - public LoggingConfiguration addSubstitutionVariable(String key, String value) { - substitutionVariables.put(key, value); - return this; - } - - @VisibleForTesting - String getSubstitutionVariable(String key) { - return substitutionVariables.get(key); - } - - Map<String, String> getSubstitutionVariables() { - return substitutionVariables; - } - - LogOutput getLogOutput() { - return logOutput; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LoggingConfigurator.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LoggingConfigurator.java deleted file mode 100644 index f7f45b1c6fe..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/LoggingConfigurator.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.bootstrapper; - -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.Logger; -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.Appender; -import java.io.File; -import org.apache.commons.lang.StringUtils; -import org.slf4j.LoggerFactory; -import org.sonar.core.config.Logback; - -public class LoggingConfigurator { - private static final String CUSTOM_APPENDER_NAME = "custom_stream"; - - private LoggingConfigurator() { - } - - public static void apply(LoggingConfiguration conf, File logbackFile) { - Logback.configure(logbackFile, conf.getSubstitutionVariables()); - - if (conf.getLogOutput() != null) { - setCustomRootAppender(conf); - } - } - - public static void apply(LoggingConfiguration conf) { - apply(conf, "/org/sonar/batch/bootstrapper/logback.xml"); - } - - public static void apply(LoggingConfiguration conf, String classloaderPath) { - Logback.configure(classloaderPath, conf.getSubstitutionVariables()); - - // if not set, keep default behavior (configured to stdout through the file in classpath) - if (conf.getLogOutput() != null) { - setCustomRootAppender(conf); - } - } - - private static void setCustomRootAppender(LoggingConfiguration conf) { - Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); - String level = StringUtils.defaultIfBlank(conf.getSubstitutionVariables().get(LoggingConfiguration.PROPERTY_ROOT_LOGGER_LEVEL), LoggingConfiguration.LEVEL_ROOT_DEFAULT); - - if (logger.getAppender(CUSTOM_APPENDER_NAME) == null) { - logger.detachAndStopAllAppenders(); - logger.addAppender(createAppender(conf.getLogOutput())); - } - logger.setLevel(Level.toLevel(level)); - } - - private static Appender<ILoggingEvent> createAppender(LogOutput target) { - LogCallbackAppender appender = new LogCallbackAppender(target); - appender.setName(CUSTOM_APPENDER_NAME); - appender.start(); - - return appender; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/package-info.java deleted file mode 100644 index 645b8d7dfe2..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/package-info.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -/** - * This package is a part of bootstrap process, so we should take care about backward compatibility. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.bootstrapper; - -import javax.annotation.ParametersAreNonnullByDefault; - diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/DefaultProjectCacheStatus.java b/sonar-batch/src/main/java/org/sonar/batch/cache/DefaultProjectCacheStatus.java deleted file mode 100644 index 45baaecacc8..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/DefaultProjectCacheStatus.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cache; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Date; -import org.sonar.home.cache.PersistentCache; - -import static org.sonar.core.util.FileUtils.deleteQuietly; - -public class DefaultProjectCacheStatus implements ProjectCacheStatus { - private static final String STATUS_FILENAME = "cache-sync-status"; - private PersistentCache cache; - - public DefaultProjectCacheStatus(PersistentCache cache) { - this.cache = cache; - } - - @Override - public void save() { - Date now = new Date(); - - try { - try (ObjectOutputStream objOutput = new ObjectOutputStream(new FileOutputStream(getStatusFilePath().toFile()))) { - objOutput.writeObject(now); - } - } catch (IOException e) { - throw new IllegalStateException("Failed to write cache sync status", e); - } - } - - @Override - public void delete() { - cache.clear(); - deleteQuietly(getStatusFilePath().toFile()); - } - - @Override - public Date getSyncStatus() { - Path p = getStatusFilePath(); - try { - if (!Files.isRegularFile(p)) { - return null; - } - try (ObjectInputStream objInput = new ObjectInputStream(new FileInputStream(p.toFile()))) { - return (Date) objInput.readObject(); - } - } catch (IOException | ClassNotFoundException e) { - deleteQuietly(p.toFile()); - throw new IllegalStateException("Failed to read cache sync status", e); - } - } - - private Path getStatusFilePath() { - return cache.getDirectory().resolve(STATUS_FILENAME); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/GlobalPersistentCacheProvider.java b/sonar-batch/src/main/java/org/sonar/batch/cache/GlobalPersistentCacheProvider.java deleted file mode 100644 index 6b8677e2ada..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/GlobalPersistentCacheProvider.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cache; - -import org.apache.commons.lang.StringUtils; -import org.sonar.batch.bootstrap.Slf4jLogger; -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); - cache = builder.build(); - } - - return cache; - } - - private static String getServerUrl(GlobalProperties props) { - return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/"); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/NonAssociatedCacheSynchronizer.java b/sonar-batch/src/main/java/org/sonar/batch/cache/NonAssociatedCacheSynchronizer.java deleted file mode 100644 index 9287c268634..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/NonAssociatedCacheSynchronizer.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cache; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; -import org.sonar.batch.repository.QualityProfileLoader; -import org.sonar.batch.rule.ActiveRulesLoader; -import org.sonar.batch.rule.RulesLoader; -import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile; - -public class NonAssociatedCacheSynchronizer { - private static final Logger LOG = LoggerFactory.getLogger(NonAssociatedCacheSynchronizer.class); - - private final ProjectCacheStatus cacheStatus; - private final QualityProfileLoader qualityProfileLoader; - private final ActiveRulesLoader activeRulesLoader; - private final RulesLoader rulesLoader; - - public NonAssociatedCacheSynchronizer(RulesLoader rulesLoader, QualityProfileLoader qualityProfileLoader, ActiveRulesLoader activeRulesLoader, ProjectCacheStatus cacheStatus) { - this.rulesLoader = rulesLoader; - this.qualityProfileLoader = qualityProfileLoader; - this.activeRulesLoader = activeRulesLoader; - this.cacheStatus = cacheStatus; - } - - public void execute(boolean force) { - Date lastSync = cacheStatus.getSyncStatus(); - - if (lastSync != null) { - if (!force) { - LOG.info("Found cache [{}]", lastSync); - return; - } else { - LOG.info("-- Found cache [{}], synchronizing data..", lastSync); - } - } else { - LOG.info("-- Cache not found, synchronizing data.."); - } - - loadData(); - cacheStatus.save(); - LOG.info("-- Succesfully synchronized cache"); - } - - private static Collection<String> getKeys(Collection<QualityProfile> qProfiles) { - List<String> list = new ArrayList<>(qProfiles.size()); - for (QualityProfile qp : qProfiles) { - list.add(qp.getKey()); - } - - return list; - } - - private void loadData() { - Profiler profiler = Profiler.create(Loggers.get(ProjectCacheSynchronizer.class)); - - profiler.startInfo("Load rules"); - rulesLoader.load(null); - profiler.stopInfo(); - - profiler.startInfo("Load default quality profiles"); - Collection<QualityProfile> qProfiles = qualityProfileLoader.loadDefault(null, null); - profiler.stopInfo(); - - profiler.startInfo("Load default active rules"); - Collection<String> keys = getKeys(qProfiles); - for (String k : keys) { - activeRulesLoader.load(k, null); - } - profiler.stopInfo(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectCacheStatus.java b/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectCacheStatus.java deleted file mode 100644 index ef5fd9754a1..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectCacheStatus.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cache; - -import java.util.Date; - -public interface ProjectCacheStatus { - void save(); - - void delete(); - - Date getSyncStatus(); -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectCacheSynchronizer.java b/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectCacheSynchronizer.java deleted file mode 100644 index 9272d279aad..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectCacheSynchronizer.java +++ /dev/null @@ -1,185 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cache; - -import com.google.common.base.Function; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collection; -import java.util.Date; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; -import org.sonar.batch.protocol.input.BatchInput.ServerIssue; -import org.sonar.batch.repository.ProjectRepositories; -import org.sonar.batch.repository.ProjectRepositoriesLoader; -import org.sonar.batch.repository.QualityProfileLoader; -import org.sonar.batch.repository.ServerIssuesLoader; -import org.sonar.batch.repository.user.UserRepositoryLoader; -import org.sonar.batch.rule.ActiveRulesLoader; -import org.sonar.batch.rule.RulesLoader; -import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile; - -public class ProjectCacheSynchronizer { - private static final Logger LOG = LoggerFactory.getLogger(ProjectCacheSynchronizer.class); - - private final ServerIssuesLoader issuesLoader; - private final UserRepositoryLoader userRepository; - private final ProjectCacheStatus cacheStatus; - private final QualityProfileLoader qualityProfileLoader; - private final ProjectRepositoriesLoader projectRepositoriesLoader; - private final ActiveRulesLoader activeRulesLoader; - private final RulesLoader rulesLoader; - - public ProjectCacheSynchronizer(RulesLoader rulesLoader, QualityProfileLoader qualityProfileLoader, ProjectRepositoriesLoader projectSettingsLoader, - ActiveRulesLoader activeRulesLoader, ServerIssuesLoader issuesLoader, - UserRepositoryLoader userRepository, ProjectCacheStatus cacheStatus) { - this.rulesLoader = rulesLoader; - this.qualityProfileLoader = qualityProfileLoader; - this.projectRepositoriesLoader = projectSettingsLoader; - this.activeRulesLoader = activeRulesLoader; - this.issuesLoader = issuesLoader; - this.userRepository = userRepository; - this.cacheStatus = cacheStatus; - } - - private static boolean isToday(Date d) { - Calendar c1 = Calendar.getInstance(); - Calendar c2 = Calendar.getInstance(); - c2.setTime(d); - - return c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR) && - c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR); - } - - private static boolean shouldUpdate(Date lastUpdate) { - return !isToday(lastUpdate); - } - - public void load(String projectKey, boolean force) { - Date lastSync = cacheStatus.getSyncStatus(); - boolean failOnError = true; - - if (lastSync != null) { - if (force) { - LOG.info("-- Found project [{}] cache [{}], synchronizing data (forced)..", projectKey, lastSync); - } else if (shouldUpdate(lastSync)) { - LOG.info("-- Found project [{}] cache [{}], synchronizing data..", projectKey, lastSync); - failOnError = false; - } else { - LOG.info("Found project [{}] cache [{}]", projectKey, lastSync); - return; - } - } else { - LOG.info("-- Cache for project [{}] not found, synchronizing data..", projectKey); - } - - try { - loadData(projectKey); - } catch (Exception e) { - if (failOnError) { - throw e; - } - - LOG.warn("-- Cache update for project [{}] failed, continuing from cache..", projectKey, e); - return; - } - - saveStatus(); - } - - private void saveStatus() { - cacheStatus.save(); - LOG.info("-- Successfully synchronized project cache"); - } - - private void loadData(String projectKey) { - Profiler profiler = Profiler.create(Loggers.get(ProjectCacheSynchronizer.class)); - - profiler.startInfo("Load rules"); - rulesLoader.load(null); - profiler.stopInfo(); - - profiler.startInfo("Load project settings"); - ProjectRepositories projectRepo = projectRepositoriesLoader.load(projectKey, true, null); - - if (!projectRepo.exists()) { - LOG.debug("Project doesn't exist in the server"); - } else if (projectRepo.lastAnalysisDate() == null) { - LOG.debug("No previous analysis found"); - } - profiler.stopInfo(); - - profiler.startInfo("Load project quality profiles"); - Collection<QualityProfile> qProfiles; - if (projectRepo.exists()) { - qProfiles = qualityProfileLoader.load(projectKey, null, null); - } else { - qProfiles = qualityProfileLoader.loadDefault(null, null); - } - profiler.stopInfo(); - - profiler.startInfo("Load project active rules"); - Collection<String> keys = getKeys(qProfiles); - for (String k : keys) { - activeRulesLoader.load(k, null); - } - profiler.stopInfo(); - - if (projectRepo.lastAnalysisDate() != null) { - profiler.startInfo("Load server issues"); - UserLoginAccumulator consumer = new UserLoginAccumulator(); - issuesLoader.load(projectKey, consumer); - profiler.stopInfo(); - - profiler.startInfo("Load user information"); - for (String login : consumer.loginSet) { - userRepository.load(login, null); - } - profiler.stopInfo("Load user information"); - } - } - - private static Collection<String> getKeys(Collection<QualityProfile> qProfiles) { - List<String> list = new ArrayList<>(qProfiles.size()); - for (QualityProfile qp : qProfiles) { - list.add(qp.getKey()); - } - - return list; - } - - private static class UserLoginAccumulator implements Function<ServerIssue, Void> { - Set<String> loginSet = new HashSet<>(); - - @Override - public Void apply(ServerIssue input) { - if (!StringUtils.isEmpty(input.getAssigneeLogin())) { - loginSet.add(input.getAssigneeLogin()); - } - return null; - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectKeySupplier.java b/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectKeySupplier.java deleted file mode 100644 index abbd8a69485..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectKeySupplier.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.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; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectPersistentCacheProvider.java b/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectPersistentCacheProvider.java deleted file mode 100644 index 2594a006543..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectPersistentCacheProvider.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.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 static String getServerUrl(GlobalProperties props) { - return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/"); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectSyncContainer.java b/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectSyncContainer.java deleted file mode 100644 index 816091786b3..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/ProjectSyncContainer.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cache; - -import java.util.HashMap; -import java.util.Map; -import javax.annotation.Nullable; -import org.sonar.api.CoreProperties; -import org.sonar.batch.analysis.AnalysisProperties; -import org.sonar.batch.analysis.DefaultAnalysisMode; -import org.sonar.batch.bootstrap.GlobalProperties; -import org.sonar.batch.cache.WSLoader.LoadStrategy; -import org.sonar.batch.repository.DefaultProjectRepositoriesLoader; -import org.sonar.batch.repository.DefaultQualityProfileLoader; -import org.sonar.batch.repository.DefaultServerIssuesLoader; -import org.sonar.batch.repository.ProjectRepositoriesLoader; -import org.sonar.batch.repository.QualityProfileLoader; -import org.sonar.batch.repository.ServerIssuesLoader; -import org.sonar.batch.repository.user.UserRepositoryLoader; -import org.sonar.batch.rule.ActiveRulesLoader; -import org.sonar.batch.rule.DefaultActiveRulesLoader; -import org.sonar.batch.rule.DefaultRulesLoader; -import org.sonar.batch.rule.RulesLoader; -import org.sonar.core.platform.ComponentContainer; - -public class ProjectSyncContainer extends ComponentContainer { - private final boolean force; - private final String projectKey; - - public ProjectSyncContainer(ComponentContainer globalContainer, @Nullable String projectKey, boolean force) { - super(globalContainer); - this.projectKey = projectKey; - this.force = force; - } - - @Override - public void doBeforeStart() { - addComponents(); - } - - @Override - public void doAfterStart() { - if (projectKey != null) { - getComponentByType(ProjectCacheSynchronizer.class).load(projectKey, force); - } else { - getComponentByType(NonAssociatedCacheSynchronizer.class).execute(force); - } - } - - 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 ProjectPersistentCacheProvider(), - createIssuesAnalysisMode(projectKey)); - - addIfMissing(DefaultProjectCacheStatus.class, ProjectCacheStatus.class); - addIfMissing(DefaultProjectRepositoriesLoader.class, ProjectRepositoriesLoader.class); - addIfMissing(DefaultServerIssuesLoader.class, ServerIssuesLoader.class); - addIfMissing(DefaultQualityProfileLoader.class, QualityProfileLoader.class); - addIfMissing(DefaultRulesLoader.class, RulesLoader.class); - addIfMissing(DefaultActiveRulesLoader.class, ActiveRulesLoader.class); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/StrategyWSLoaderProvider.java b/sonar-batch/src/main/java/org/sonar/batch/cache/StrategyWSLoaderProvider.java deleted file mode 100644 index d89de5ba448..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/StrategyWSLoaderProvider.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cache; - -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.batch.bootstrap.BatchWsClient; -import org.sonar.batch.cache.WSLoader.LoadStrategy; -import org.sonar.home.cache.PersistentCache; - -public class StrategyWSLoaderProvider extends ProviderAdapter { - private final LoadStrategy strategy; - private WSLoader wsLoader; - - public StrategyWSLoaderProvider(LoadStrategy strategy) { - this.strategy = strategy; - } - - public WSLoader provide(PersistentCache cache, BatchWsClient client) { - if (wsLoader == null) { - wsLoader = new WSLoader(strategy, cache, client); - } - return wsLoader; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/WSLoader.java b/sonar-batch/src/main/java/org/sonar/batch/cache/WSLoader.java deleted file mode 100644 index 328480e68bf..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/WSLoader.java +++ /dev/null @@ -1,240 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cache; - -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.nio.charset.StandardCharsets; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import org.apache.commons.io.IOUtils; -import org.sonar.api.utils.MessageException; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.batch.bootstrap.BatchWsClient; -import org.sonar.home.cache.PersistentCache; -import org.sonarqube.ws.client.GetRequest; -import org.sonarqube.ws.client.HttpException; - -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; - -public class WSLoader { - private static final Logger LOG = Loggers.get(WSLoader.class); - private static final String FAIL_MSG = "Server is not accessible and data is not cached"; - - public enum ServerStatus { - UNKNOWN, ACCESSIBLE, NOT_ACCESSIBLE - } - - public enum LoadStrategy { - SERVER_FIRST, CACHE_FIRST, SERVER_ONLY, CACHE_ONLY - } - - private final LoadStrategy defautLoadStrategy; - private final BatchWsClient wsClient; - private final PersistentCache cache; - private ServerStatus serverStatus; - - private DataLoader<String> stringServerLoader = new DataLoader<String>() { - @Override - public String load(String id) throws IOException { - GetRequest getRequest = new GetRequest(id); - try (Reader reader = wsClient.call(getRequest).contentReader()) { - String str = IOUtils.toString(reader); - 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> stringCacheLoader = new DataLoader<String>() { - @Override - public String load(String id) throws IOException { - return cache.getString(id); - } - }; - - private DataLoader<InputStream> streamServerLoader = new DataLoader<InputStream>() { - @Override - public InputStream load(String id) throws IOException { - GetRequest getRequest = new GetRequest(id); - try (InputStream is = wsClient.call(getRequest).contentStream()) { - try { - cache.put(id, is); - } catch (IOException e) { - throw new IllegalStateException("Error saving to WS cache", e); - } - } - return cache.getStream(id); - } - }; - - private DataLoader<InputStream> streamCacheLoader = new DataLoader<InputStream>() { - @Override - public InputStream load(String id) throws IOException { - return cache.getStream(id); - } - }; - - private static class NotAvailableException extends Exception { - private static final long serialVersionUID = 1L; - - public NotAvailableException(String message) { - super(message); - } - - public NotAvailableException(Throwable cause) { - super(cause); - } - } - - public WSLoader(LoadStrategy strategy, PersistentCache cache, BatchWsClient wsClient) { - this.defautLoadStrategy = strategy; - this.serverStatus = UNKNOWN; - this.cache = cache; - this.wsClient = wsClient; - } - - @Nonnull - public WSLoaderResult<InputStream> loadStream(String id) { - return load(id, defautLoadStrategy, streamServerLoader, streamCacheLoader); - } - - @Nonnull - public WSLoaderResult<String> loadString(String id) { - return loadString(id, defautLoadStrategy); - } - - @Nonnull - public WSLoaderResult<String> loadString(String id, WSLoader.LoadStrategy strategy) { - return load(id, strategy, stringServerLoader, stringCacheLoader); - } - - @Nonnull - private <T> WSLoaderResult<T> load(String id, WSLoader.LoadStrategy strategy, DataLoader<T> serverLoader, DataLoader<T> cacheLoader) { - switch (strategy) { - case CACHE_FIRST: - return loadFromCacheFirst(id, cacheLoader, serverLoader); - case CACHE_ONLY: - return loadFromCacheFirst(id, cacheLoader, null); - case SERVER_FIRST: - return loadFromServerFirst(id, serverLoader, cacheLoader); - case SERVER_ONLY: - default: - return loadFromServerFirst(id, serverLoader, null); - } - } - - public LoadStrategy getDefaultStrategy() { - return this.defautLoadStrategy; - } - - private void switchToOffline() { - LOG.debug("server not available - switching to offline mode"); - serverStatus = NOT_ACCESSIBLE; - } - - private void switchToOnline() { - serverStatus = ACCESSIBLE; - } - - private boolean isOffline() { - return serverStatus == NOT_ACCESSIBLE; - } - - @Nonnull - private <T> WSLoaderResult<T> loadFromCacheFirst(String id, DataLoader<T> cacheLoader, @Nullable DataLoader<T> serverLoader) { - try { - return loadFromCache(id, cacheLoader); - } catch (NotAvailableException cacheNotAvailable) { - if (serverLoader != null) { - try { - return loadFromServer(id, serverLoader); - } catch (NotAvailableException serverNotAvailable) { - throw new IllegalStateException(FAIL_MSG, serverNotAvailable.getCause()); - } - } - throw new IllegalStateException("Data is not cached", cacheNotAvailable.getCause()); - } - } - - @Nonnull - private <T> WSLoaderResult<T> loadFromServerFirst(String id, DataLoader<T> serverLoader, @Nullable DataLoader<T> cacheLoader) { - try { - return loadFromServer(id, serverLoader); - } catch (NotAvailableException serverNotAvailable) { - if (cacheLoader != null) { - try { - return loadFromCache(id, cacheLoader); - } catch (NotAvailableException cacheNotAvailable) { - throw new IllegalStateException(FAIL_MSG, serverNotAvailable.getCause()); - } - } - throw new IllegalStateException("Server is not available: " + wsClient.baseUrl(), serverNotAvailable.getCause()); - } - } - - interface DataLoader<T> { - T load(String id) throws IOException; - } - - private <T> WSLoaderResult<T> loadFromCache(String id, DataLoader<T> loader) throws NotAvailableException { - T result; - - try { - 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<>(result, true); - } - - private <T> WSLoaderResult<T> loadFromServer(String id, DataLoader<T> loader) throws NotAvailableException { - if (isOffline()) { - throw new NotAvailableException("Server not available"); - } - try { - T t = loader.load(id); - switchToOnline(); - return new WSLoaderResult<>(t, false); - } catch (HttpException | MessageException e) { - // fail fast if it could connect but there was a application-level error - throw e; - } catch (IllegalStateException e) { - switchToOffline(); - throw new NotAvailableException(e); - } catch (Exception e) { - // fail fast - throw new IllegalStateException(e); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/WSLoaderResult.java b/sonar-batch/src/main/java/org/sonar/batch/cache/WSLoaderResult.java deleted file mode 100644 index 29db0bcb3a7..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/WSLoaderResult.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cache; - -import javax.annotation.Nonnull; - -public class WSLoaderResult<T> { - private T result; - private boolean fromCache; - - public WSLoaderResult(T result, boolean fromCache) { - this.result = result; - this.fromCache = fromCache; - } - - @Nonnull - public T get() { - return result; - } - - public boolean isFromCache() { - return fromCache; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cache/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/cache/package-info.java deleted file mode 100644 index 3f72a2d38b6..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cache/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.cache; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdComponents.java b/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdComponents.java deleted file mode 100644 index 30f4863783d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdComponents.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cpd; - -import com.google.common.collect.ImmutableList; -import java.util.List; - -public final class CpdComponents { - - private CpdComponents() { - } - - public static List<Class<? extends Object>> all() { - return ImmutableList.of( - CpdSensor.class, - CpdMappings.class, - JavaCpdBlockIndexer.class, - DefaultCpdBlockIndexer.class); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdExecutor.java deleted file mode 100644 index 38cee8c127c..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdExecutor.java +++ /dev/null @@ -1,218 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cpd; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.batch.cpd.index.SonarCpdBlockIndex; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReport.Duplicate; -import org.sonar.batch.protocol.output.BatchReport.Duplication; -import org.sonar.batch.report.ReportPublisher; -import org.sonar.batch.util.ProgressReport; -import org.sonar.duplications.block.Block; -import org.sonar.duplications.detector.suffixtree.SuffixTreeCloneDetectionAlgorithm; -import org.sonar.duplications.index.CloneGroup; -import org.sonar.duplications.index.ClonePart; -import org.sonar.duplications.index.PackedMemoryCloneIndex.ResourceBlocks; - -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import static com.google.common.collect.FluentIterable.from; - -/** - * Runs on the root module, at the end of the project analysis. - * It executes copy paste detection involving all files of all modules, which were indexed during sensors execution for each module - * by {@link CpdSensor). The sensor is responsible for handling exclusions and block sizes. - */ -public class CpdExecutor { - private static final Logger LOG = Loggers.get(CpdExecutor.class); - // timeout for the computation of duplicates in a file (seconds) - private static final int TIMEOUT = 5 * 60; - static final int MAX_CLONE_GROUP_PER_FILE = 100; - static final int MAX_CLONE_PART_PER_GROUP = 100; - - private final SonarCpdBlockIndex index; - private final ReportPublisher publisher; - private final BatchComponentCache batchComponentCache; - private final Settings settings; - private final ExecutorService executorService; - private final ProgressReport progressReport; - private int count; - private int total; - - public CpdExecutor(Settings settings, SonarCpdBlockIndex index, ReportPublisher publisher, BatchComponentCache batchComponentCache) { - this.settings = settings; - this.index = index; - this.publisher = publisher; - this.batchComponentCache = batchComponentCache; - this.executorService = Executors.newSingleThreadExecutor(); - this.progressReport = new ProgressReport("CPD computation", TimeUnit.SECONDS.toMillis(10)); - } - - public void execute() { - total = index.noResources(); - progressReport.start(String.format("Calculating CPD for %d files", total)); - try { - Iterator<ResourceBlocks> it = index.iterator(); - - while (it.hasNext()) { - ResourceBlocks resourceBlocks = it.next(); - runCpdAnalysis(resourceBlocks.resourceId(), resourceBlocks.blocks()); - count++; - } - progressReport.stop("CPD calculation finished"); - } catch (Exception e) { - progressReport.stop(""); - throw e; - } - } - - private void runCpdAnalysis(String resource, final Collection<Block> fileBlocks) { - LOG.debug("Detection of duplications for {}", resource); - - BatchComponent component = batchComponentCache.get(resource); - if (component == null) { - LOG.error("Resource not found in component cache: {}. Skipping CPD computation for it", resource); - return; - } - - InputFile inputFile = (InputFile) component.inputComponent(); - progressReport.message(String.format("%d/%d - current file: %s", count, total, inputFile.absolutePath())); - - List<CloneGroup> duplications; - Future<List<CloneGroup>> futureResult = null; - try { - futureResult = executorService.submit(new Callable<List<CloneGroup>>() { - @Override - public List<CloneGroup> call() throws Exception { - return SuffixTreeCloneDetectionAlgorithm.detect(index, fileBlocks); - } - }); - duplications = futureResult.get(TIMEOUT, TimeUnit.SECONDS); - } catch (TimeoutException e) { - LOG.warn("Timeout during detection of duplications for " + inputFile.absolutePath()); - if (futureResult != null) { - futureResult.cancel(true); - } - return; - } catch (Exception e) { - throw new IllegalStateException("Fail during detection of duplication for " + inputFile.absolutePath(), e); - } - - List<CloneGroup> filtered; - if (!"java".equalsIgnoreCase(inputFile.language())) { - Predicate<CloneGroup> minimumTokensPredicate = DuplicationPredicates.numberOfUnitsNotLessThan(getMinimumTokens(inputFile.language())); - filtered = from(duplications).filter(minimumTokensPredicate).toList(); - } else { - filtered = duplications; - } - - saveDuplications(component, filtered); - } - - @VisibleForTesting - /** - * Not applicable to Java, as the {@link BlockChunker} that it uses does not record start and end units of each block. - * Also, it uses statements instead of tokens. - * @param languageKey - * @return - */ - int getMinimumTokens(String languageKey) { - int minimumTokens = settings.getInt("sonar.cpd." + languageKey + ".minimumTokens"); - if (minimumTokens == 0) { - minimumTokens = 100; - } - - return minimumTokens; - } - - @VisibleForTesting - final void saveDuplications(final BatchComponent component, List<CloneGroup> duplications) { - if (duplications.size() > MAX_CLONE_GROUP_PER_FILE) { - LOG.warn("Too many duplication groups on file " + component.inputComponent() + ". Keep only the first " + MAX_CLONE_GROUP_PER_FILE + - " groups."); - } - Iterable<org.sonar.batch.protocol.output.BatchReport.Duplication> reportDuplications = from(duplications) - .limit(MAX_CLONE_GROUP_PER_FILE) - .transform( - new Function<CloneGroup, BatchReport.Duplication>() { - private final BatchReport.Duplication.Builder dupBuilder = BatchReport.Duplication.newBuilder(); - private final BatchReport.Duplicate.Builder blockBuilder = BatchReport.Duplicate.newBuilder(); - - @Override - public BatchReport.Duplication apply(CloneGroup input) { - return toReportDuplication(component, dupBuilder, blockBuilder, input); - } - - }); - publisher.getWriter().writeComponentDuplications(component.batchId(), reportDuplications); - } - - private Duplication toReportDuplication(BatchComponent component, Duplication.Builder dupBuilder, Duplicate.Builder blockBuilder, CloneGroup input) { - dupBuilder.clear(); - ClonePart originBlock = input.getOriginPart(); - blockBuilder.clear(); - dupBuilder.setOriginPosition(BatchReport.TextRange.newBuilder() - .setStartLine(originBlock.getStartLine()) - .setEndLine(originBlock.getEndLine()) - .build()); - int clonePartCount = 0; - for (ClonePart duplicate : input.getCloneParts()) { - if (!duplicate.equals(originBlock)) { - clonePartCount++; - if (clonePartCount > MAX_CLONE_PART_PER_GROUP) { - LOG.warn("Too many duplication references on file " + component.inputComponent() + " for block at line " + - originBlock.getStartLine() + ". Keep only the first " - + MAX_CLONE_PART_PER_GROUP + " references."); - break; - } - blockBuilder.clear(); - String componentKey = duplicate.getResourceId(); - if (!component.key().equals(componentKey)) { - BatchComponent sameProjectComponent = batchComponentCache.get(componentKey); - blockBuilder.setOtherFileRef(sameProjectComponent.batchId()); - } - dupBuilder.addDuplicate(blockBuilder - .setRange(BatchReport.TextRange.newBuilder() - .setStartLine(duplicate.getStartLine()) - .setEndLine(duplicate.getEndLine()) - .build()) - .build()); - } - } - return dupBuilder.build(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdIndexer.java deleted file mode 100644 index 9ebf80c88a7..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdIndexer.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cpd; - -import org.slf4j.Logger; -import org.sonar.api.batch.BatchSide; - -@BatchSide -public abstract class CpdIndexer { - - abstract boolean isLanguageSupported(String language); - - abstract void index(String language); - - protected void logExclusions(String[] exclusions, Logger logger) { - if (exclusions.length > 0) { - StringBuilder message = new StringBuilder("Copy-paste detection exclusions:"); - for (String exclusion : exclusions) { - message.append("\n "); - message.append(exclusion); - } - - logger.info(message.toString()); - } - } - - @Override - public String toString() { - return getClass().getSimpleName(); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdMappings.java b/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdMappings.java deleted file mode 100644 index 9a930d62fb5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdMappings.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cpd; - -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.CpdMapping; - -import javax.annotation.CheckForNull; - -@BatchSide -public class CpdMappings { - - private final CpdMapping[] mappings; - - public CpdMappings(CpdMapping[] mappings) { - this.mappings = mappings; - } - - public CpdMappings() { - this(new CpdMapping[0]); - } - - @CheckForNull - public CpdMapping getMapping(String language) { - if (mappings != null) { - for (CpdMapping cpdMapping : mappings) { - if (cpdMapping.getLanguage().getKey().equals(language)) { - return cpdMapping; - } - } - } - return null; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdSensor.java b/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdSensor.java deleted file mode 100644 index 9ea1bac9254..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cpd/CpdSensor.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cpd; - -import com.google.common.annotations.VisibleForTesting; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.Phase; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.sensor.Sensor; -import org.sonar.api.batch.sensor.SensorContext; -import org.sonar.api.batch.sensor.SensorDescriptor; -import org.sonar.api.config.Settings; - -@Phase(name = Phase.Name.POST) -public class CpdSensor implements Sensor { - - private static final Logger LOG = LoggerFactory.getLogger(CpdSensor.class); - - private CpdIndexer sonarEngine; - private CpdIndexer sonarBridgeEngine; - private Settings settings; - private FileSystem fs; - - public CpdSensor(JavaCpdBlockIndexer sonarEngine, DefaultCpdBlockIndexer sonarBridgeEngine, Settings settings, FileSystem fs) { - this.sonarEngine = sonarEngine; - this.sonarBridgeEngine = sonarBridgeEngine; - this.settings = settings; - this.fs = fs; - } - - @Override - public void describe(SensorDescriptor descriptor) { - descriptor.name("CPD Sensor"); - } - - @VisibleForTesting - CpdIndexer getEngine(String language) { - if (sonarEngine.isLanguageSupported(language)) { - return sonarEngine; - } - return sonarBridgeEngine; - } - - @VisibleForTesting - boolean isSkipped(String language) { - String key = "sonar.cpd." + language + ".skip"; - if (settings.hasKey(key)) { - return settings.getBoolean(key); - } - return settings.getBoolean(CoreProperties.CPD_SKIP_PROPERTY); - } - - @Override - public void execute(SensorContext context) { - if (settings.hasKey(CoreProperties.CPD_SKIP_PROPERTY)) { - LOG.warn("\"sonar.cpd.skip\" property is deprecated and will be removed. Please set \"sonar.cpd.exclusions=**\" instead to disable duplication mechanism."); - } - - for (String language : fs.languages()) { - if (settings.hasKey("sonar.cpd." + language + ".skip")) { - LOG - .warn("\"sonar.cpd." + language + ".skip\" property is deprecated and will be removed. Please set \"sonar.cpd.exclusions=**\" instead to disable duplication mechanism."); - } - - if (isSkipped(language)) { - LOG.info("Detection of duplicated code is skipped for {}", language); - continue; - } - - CpdIndexer engine = getEngine(language); - if (!engine.isLanguageSupported(language)) { - LOG.debug("Detection of duplicated code is not supported for {}", language); - continue; - } - LOG.info("{} is used for {}", engine, language); - engine.index(language); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cpd/DefaultCpdBlockIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/cpd/DefaultCpdBlockIndexer.java deleted file mode 100644 index 33b2f269595..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cpd/DefaultCpdBlockIndexer.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cpd; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Lists; -import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.CpdMapping; -import org.sonar.api.batch.fs.FilePredicates; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.config.Settings; -import org.sonar.batch.cpd.index.SonarCpdBlockIndex; -import org.sonar.duplications.block.Block; -import org.sonar.duplications.internal.pmd.TokenizerBridge; - -public class DefaultCpdBlockIndexer extends CpdIndexer { - - private static final Logger LOG = LoggerFactory.getLogger(DefaultCpdBlockIndexer.class); - - private final CpdMappings mappings; - private final FileSystem fs; - private final Settings settings; - private final SonarCpdBlockIndex index; - - public DefaultCpdBlockIndexer(CpdMappings mappings, FileSystem fs, Settings settings, SonarCpdBlockIndex index) { - this.mappings = mappings; - this.fs = fs; - this.settings = settings; - this.index = index; - } - - @Override - public boolean isLanguageSupported(String language) { - return true; - } - - @Override - public void index(String languageKey) { - CpdMapping mapping = mappings.getMapping(languageKey); - if (mapping == null) { - LOG.debug("No CpdMapping for language " + languageKey); - return; - } - - String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS); - logExclusions(cpdExclusions, LOG); - FilePredicates p = fs.predicates(); - List<InputFile> sourceFiles = Lists.newArrayList(fs.inputFiles(p.and( - p.hasType(InputFile.Type.MAIN), - p.hasLanguage(languageKey), - p.doesNotMatchPathPatterns(cpdExclusions)))); - if (sourceFiles.isEmpty()) { - return; - } - - // Create index - populateIndex(languageKey, sourceFiles, mapping); - } - - private void populateIndex(String languageKey, List<InputFile> sourceFiles, CpdMapping mapping) { - TokenizerBridge bridge = new TokenizerBridge(mapping.getTokenizer(), fs.encoding().name(), getBlockSize(languageKey)); - for (InputFile inputFile : sourceFiles) { - if (!index.isIndexed(inputFile)) { - LOG.debug("Populating index from {}", inputFile); - String resourceEffectiveKey = ((DefaultInputFile) inputFile).key(); - List<Block> blocks = bridge.chunk(resourceEffectiveKey, inputFile.file()); - index.insert(inputFile, blocks); - } - } - } - - @VisibleForTesting - int getBlockSize(String languageKey) { - int blockSize = settings.getInt("sonar.cpd." + languageKey + ".minimumLines"); - if (blockSize == 0) { - blockSize = getDefaultBlockSize(languageKey); - } - return blockSize; - } - - @VisibleForTesting - public static int getDefaultBlockSize(String languageKey) { - if ("cobol".equals(languageKey)) { - return 30; - } else if ("abap".equals(languageKey)) { - return 20; - } else { - return 10; - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cpd/DuplicationPredicates.java b/sonar-batch/src/main/java/org/sonar/batch/cpd/DuplicationPredicates.java deleted file mode 100644 index e016ae6f970..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cpd/DuplicationPredicates.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cpd; - -import com.google.common.base.Predicate; -import org.sonar.duplications.index.CloneGroup; - -import javax.annotation.Nullable; - -public final class DuplicationPredicates { - - private DuplicationPredicates() { - } - - public static Predicate<CloneGroup> numberOfUnitsNotLessThan(int min) { - return new NumberOfUnitsNotLessThan(min); - } - - private static class NumberOfUnitsNotLessThan implements Predicate<CloneGroup> { - private final int min; - - public NumberOfUnitsNotLessThan(int min) { - this.min = min; - } - - @Override - public boolean apply(@Nullable CloneGroup input) { - return input != null && input.getLengthInUnits() >= min; - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cpd/JavaCpdBlockIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/cpd/JavaCpdBlockIndexer.java deleted file mode 100644 index a2dde817c74..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cpd/JavaCpdBlockIndexer.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cpd; - -import com.google.common.collect.Lists; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; -import java.util.List; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.fs.FilePredicates; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.config.Settings; -import org.sonar.batch.cpd.index.SonarCpdBlockIndex; -import org.sonar.duplications.block.Block; -import org.sonar.duplications.block.BlockChunker; -import org.sonar.duplications.java.JavaStatementBuilder; -import org.sonar.duplications.java.JavaTokenProducer; -import org.sonar.duplications.statement.Statement; -import org.sonar.duplications.statement.StatementChunker; -import org.sonar.duplications.token.TokenChunker; - -public class JavaCpdBlockIndexer extends CpdIndexer { - - private static final Logger LOG = LoggerFactory.getLogger(JavaCpdBlockIndexer.class); - - private static final int BLOCK_SIZE = 10; - - private final FileSystem fs; - private final Settings settings; - private final SonarCpdBlockIndex index; - - public JavaCpdBlockIndexer(FileSystem fs, Settings settings, SonarCpdBlockIndex index) { - this.fs = fs; - this.settings = settings; - this.index = index; - } - - @Override - public boolean isLanguageSupported(String language) { - return "java".equals(language); - } - - @Override - public void index(String languageKey) { - String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS); - logExclusions(cpdExclusions, LOG); - FilePredicates p = fs.predicates(); - List<InputFile> sourceFiles = Lists.newArrayList(fs.inputFiles(p.and( - p.hasType(InputFile.Type.MAIN), - p.hasLanguage(languageKey), - p.doesNotMatchPathPatterns(cpdExclusions)))); - if (sourceFiles.isEmpty()) { - return; - } - createIndex(sourceFiles); - } - - private void createIndex(Iterable<InputFile> sourceFiles) { - TokenChunker tokenChunker = JavaTokenProducer.build(); - StatementChunker statementChunker = JavaStatementBuilder.build(); - BlockChunker blockChunker = new BlockChunker(BLOCK_SIZE); - - for (InputFile inputFile : sourceFiles) { - LOG.debug("Populating index from {}", inputFile); - String resourceEffectiveKey = ((DefaultInputFile) inputFile).key(); - - List<Statement> statements; - - try(Reader reader = new InputStreamReader(new FileInputStream(inputFile.file()), fs.encoding())) { - statements = statementChunker.chunk(tokenChunker.chunk(reader)); - } catch (FileNotFoundException e) { - throw new IllegalStateException("Cannot find file " + inputFile.file(), e); - } catch (IOException e ) { - throw new IllegalStateException("Exception hnadling file: " + inputFile.file(), e); - } - - List<Block> blocks = blockChunker.chunk(resourceEffectiveKey, statements); - index.insert(inputFile, blocks); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cpd/index/SonarCpdBlockIndex.java b/sonar-batch/src/main/java/org/sonar/batch/cpd/index/SonarCpdBlockIndex.java deleted file mode 100644 index 48ee611ffaf..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cpd/index/SonarCpdBlockIndex.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.cpd.index; - -import com.google.common.base.Function; -import com.google.common.collect.Iterables; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.config.Settings; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.report.ReportPublisher; -import org.sonar.duplications.block.Block; -import org.sonar.duplications.block.ByteArray; -import org.sonar.duplications.index.AbstractCloneIndex; -import org.sonar.duplications.index.CloneIndex; -import org.sonar.duplications.index.PackedMemoryCloneIndex; -import org.sonar.duplications.index.PackedMemoryCloneIndex.ResourceBlocks; - -public class SonarCpdBlockIndex extends AbstractCloneIndex { - - private final CloneIndex mem = new PackedMemoryCloneIndex(); - private final ReportPublisher publisher; - private final BatchComponentCache batchComponentCache; - private final Settings settings; - // Files already tokenized - private final Set<InputFile> indexedFiles = new HashSet<>(); - - public SonarCpdBlockIndex(ReportPublisher publisher, BatchComponentCache batchComponentCache, Settings settings) { - this.publisher = publisher; - this.batchComponentCache = batchComponentCache; - this.settings = settings; - } - - public void insert(InputFile inputFile, Collection<Block> blocks) { - if (isCrossProjectDuplicationEnabled(settings)) { - int id = batchComponentCache.get(inputFile).batchId(); - final BatchReport.CpdTextBlock.Builder builder = BatchReport.CpdTextBlock.newBuilder(); - publisher.getWriter().writeCpdTextBlocks(id, Iterables.transform(blocks, new Function<Block, BatchReport.CpdTextBlock>() { - @Override - public BatchReport.CpdTextBlock apply(Block input) { - builder.clear(); - builder.setStartLine(input.getStartLine()); - builder.setEndLine(input.getEndLine()); - builder.setStartTokenIndex(input.getStartUnit()); - builder.setEndTokenIndex(input.getEndUnit()); - builder.setHash(input.getBlockHash().toHexString()); - return builder.build(); - } - })); - } - for (Block block : blocks) { - mem.insert(block); - } - indexedFiles.add(inputFile); - } - - public boolean isIndexed(InputFile inputFile) { - return indexedFiles.contains(inputFile); - } - - public static boolean isCrossProjectDuplicationEnabled(Settings settings) { - return settings.getBoolean(CoreProperties.CPD_CROSS_PROJECT) - // No cross project duplication for branches - && StringUtils.isBlank(settings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY)); - } - - public Collection<Block> getByInputFile(String resourceKey) { - return mem.getByResourceId(resourceKey); - } - - @Override - public Collection<Block> getBySequenceHash(ByteArray hash) { - return mem.getBySequenceHash(hash); - } - - @Override - public Collection<Block> getByResourceId(String resourceId) { - throw new UnsupportedOperationException(); - } - - @Override - public void insert(Block block) { - throw new UnsupportedOperationException(); - } - - @Override - public Iterator<ResourceBlocks> iterator() { - return mem.iterator(); - } - - @Override - public int noResources() { - return mem.noResources(); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/cpd/index/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/cpd/index/package-info.java deleted file mode 100644 index 86431b3a529..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cpd/index/package-info.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.cpd.index; - -import javax.annotation.ParametersAreNonnullByDefault; - diff --git a/sonar-batch/src/main/java/org/sonar/batch/cpd/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/cpd/package-info.java deleted file mode 100644 index c4205b6d38e..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/cpd/package-info.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.cpd; - -import javax.annotation.ParametersAreNonnullByDefault; - diff --git a/sonar-batch/src/main/java/org/sonar/batch/deprecated/DeprecatedSensorContext.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/DeprecatedSensorContext.java deleted file mode 100644 index 33a56f3e5ee..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/deprecated/DeprecatedSensorContext.java +++ /dev/null @@ -1,221 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.deprecated; - -import java.io.Serializable; -import java.util.Collection; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.SonarIndex; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputDir; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputModule; -import org.sonar.api.batch.fs.InputPath; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.api.config.Settings; -import org.sonar.api.design.Dependency; -import org.sonar.api.measures.Measure; -import org.sonar.api.measures.MeasuresFilter; -import org.sonar.api.measures.Metric; -import org.sonar.api.resources.Directory; -import org.sonar.api.resources.File; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.Resource; -import org.sonar.api.utils.SonarException; -import org.sonar.batch.sensor.DefaultSensorContext; -import org.sonar.batch.sensor.coverage.CoverageExclusions; - -public class DeprecatedSensorContext extends DefaultSensorContext implements SensorContext { - - private static final Logger LOG = LoggerFactory.getLogger(DeprecatedSensorContext.class); - - private final SonarIndex index; - private final Project project; - private final CoverageExclusions coverageFilter; - - public DeprecatedSensorContext(InputModule module, SonarIndex index, Project project, Settings settings, FileSystem fs, ActiveRules activeRules, - AnalysisMode analysisMode, CoverageExclusions coverageFilter, - SensorStorage sensorStorage) { - super(module, settings, fs, activeRules, analysisMode, sensorStorage); - this.index = index; - this.project = project; - this.coverageFilter = coverageFilter; - - } - - public Project getProject() { - return project; - } - - @Override - public boolean index(Resource resource) { - // SONAR-5006 - logWarning(); - return true; - } - - @Override - public boolean index(Resource resource, Resource parentReference) { - // SONAR-5006 - logWarning(); - return true; - } - - private static void logWarning() { - if (LOG.isDebugEnabled()) { - LOG.debug("Plugins are no more responsible for indexing physical resources like directories and files. This is now handled by the platform.", new SonarException( - "Plugin should not index physical resources")); - } - } - - @Override - public boolean isExcluded(Resource reference) { - return index.isExcluded(reference); - } - - @Override - public boolean isIndexed(Resource reference, boolean acceptExcluded) { - return index.isIndexed(reference, acceptExcluded); - } - - @Override - public Resource getParent(Resource reference) { - return index.getParent(reference); - } - - @Override - public Collection<Resource> getChildren(Resource reference) { - return index.getChildren(reference); - } - - @Override - public <G extends Serializable> Measure<G> getMeasure(Metric<G> metric) { - return index.getMeasure(project, metric); - } - - @Override - public <M> M getMeasures(MeasuresFilter<M> filter) { - return index.getMeasures(project, filter); - } - - @Override - public Measure saveMeasure(Measure measure) { - return index.addMeasure(project, measure); - } - - @Override - public Measure saveMeasure(Metric metric, Double value) { - return index.addMeasure(project, new Measure(metric, value)); - } - - @Override - public <G extends Serializable> Measure<G> getMeasure(Resource resource, Metric<G> metric) { - return index.getMeasure(resource, metric); - } - - @Override - public String saveResource(Resource resource) { - Resource persistedResource = index.addResource(resource); - if (persistedResource != null) { - return persistedResource.getEffectiveKey(); - } - return null; - } - - public boolean saveResource(Resource resource, Resource parentReference) { - return index.index(resource, parentReference); - } - - @Override - public Resource getResource(Resource resource) { - return index.getResource(resource); - } - - @Override - public <M> M getMeasures(Resource resource, MeasuresFilter<M> filter) { - return index.getMeasures(resource, filter); - } - - @Override - public Measure saveMeasure(Resource resource, Metric metric, Double value) { - Measure<?> measure = new Measure(metric, value); - coverageFilter.validate(measure, resource.getPath()); - return saveMeasure(resource, measure); - } - - @Override - public Measure saveMeasure(Resource resource, Measure measure) { - Resource resourceOrProject = resourceOrProject(resource); - - if (coverageFilter.accept(resourceOrProject, measure)) { - return index.addMeasure(resourceOrProject, measure); - } else { - return measure; - } - } - - @Override - public Dependency saveDependency(Dependency dependency) { - return null; - } - - @Override - public void saveSource(Resource reference, String source) { - // useless since 4.2. - } - - private Resource resourceOrProject(Resource resource) { - if (resource == null) { - return project; - } - Resource indexedResource = getResource(resource); - return indexedResource != null ? indexedResource : resource; - } - - @Override - public Measure saveMeasure(InputFile inputFile, Metric metric, Double value) { - Measure<?> measure = new Measure(metric, value); - coverageFilter.validate(measure, inputFile); - return saveMeasure(getResource(inputFile), measure); - } - - @Override - public Measure saveMeasure(InputFile inputFile, Measure measure) { - coverageFilter.validate(measure, inputFile); - return saveMeasure(getResource(inputFile), measure); - } - - @Override - public Resource getResource(InputPath inputPath) { - Resource r; - if (inputPath instanceof InputDir) { - r = Directory.create(((InputDir) inputPath).relativePath()); - } else if (inputPath instanceof InputFile) { - r = File.create(((InputFile) inputPath).relativePath()); - } else { - throw new IllegalArgumentException("Unknow input path type: " + inputPath); - } - return getResource(r); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/deprecated/InputFileComponent.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/InputFileComponent.java deleted file mode 100644 index e8dcc72cb32..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/deprecated/InputFileComponent.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.deprecated; - -import org.sonar.api.batch.fs.InputFile.Type; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.component.Component; -import org.sonar.api.resources.Qualifiers; - -public class InputFileComponent implements Component { - - private final DefaultInputFile inputFile; - - public InputFileComponent(DefaultInputFile inputFile) { - this.inputFile = inputFile; - } - - @Override - public String key() { - return inputFile.key(); - } - - @Override - public String path() { - return inputFile.relativePath(); - } - - @Override - public String name() { - return inputFile.file().getName(); - } - - @Override - public String longName() { - return inputFile.relativePath(); - } - - @Override - public String qualifier() { - return inputFile.type() == Type.MAIN ? Qualifiers.FILE : Qualifiers.UNIT_TEST_FILE; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/deprecated/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/package-info.java deleted file mode 100644 index 9ec5890816a..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/deprecated/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.deprecated; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/deprecated/perspectives/BatchPerspectives.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/perspectives/BatchPerspectives.java deleted file mode 100644 index ebc8465b0d3..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/deprecated/perspectives/BatchPerspectives.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.deprecated.perspectives; - -import com.google.common.collect.Maps; -import java.util.Map; -import javax.annotation.CheckForNull; -import org.sonar.api.batch.SonarIndex; -import org.sonar.api.batch.fs.InputPath; -import org.sonar.api.component.Perspective; -import org.sonar.api.component.ResourcePerspectives; -import org.sonar.api.resources.Resource; -import org.sonar.batch.index.BatchComponentCache; - -public class BatchPerspectives implements ResourcePerspectives { - - private final Map<Class<?>, PerspectiveBuilder<?>> builders = Maps.newHashMap(); - private final SonarIndex resourceIndex; - private final BatchComponentCache componentCache; - - public BatchPerspectives(PerspectiveBuilder[] builders, SonarIndex resourceIndex, BatchComponentCache componentCache) { - this.resourceIndex = resourceIndex; - this.componentCache = componentCache; - for (PerspectiveBuilder builder : builders) { - this.builders.put(builder.getPerspectiveClass(), builder); - } - } - - @Override - @CheckForNull - public <P extends Perspective> P as(Class<P> perspectiveClass, Resource resource) { - Resource indexedResource = resource; - if (resource.getEffectiveKey() == null) { - indexedResource = resourceIndex.getResource(resource); - } - if (indexedResource != null) { - PerspectiveBuilder<P> builder = builderFor(perspectiveClass); - return builder.loadPerspective(perspectiveClass, componentCache.get(indexedResource)); - } - return null; - } - - @Override - public <P extends Perspective> P as(Class<P> perspectiveClass, InputPath inputPath) { - PerspectiveBuilder<P> builder = builderFor(perspectiveClass); - return builder.loadPerspective(perspectiveClass, componentCache.get(inputPath)); - } - - private <T extends Perspective> PerspectiveBuilder<T> builderFor(Class<T> clazz) { - PerspectiveBuilder<T> builder = (PerspectiveBuilder<T>) builders.get(clazz); - if (builder == null) { - throw new PerspectiveNotFoundException("Perspective class is not registered: " + clazz); - } - return builder; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/deprecated/perspectives/PerspectiveBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/perspectives/PerspectiveBuilder.java deleted file mode 100644 index 0f38dc2ed41..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/deprecated/perspectives/PerspectiveBuilder.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.deprecated.perspectives; - -import javax.annotation.CheckForNull; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.component.Perspective; -import org.sonar.batch.index.BatchComponent; - -@BatchSide -public abstract class PerspectiveBuilder<T extends Perspective> { - - private final Class<T> perspectiveClass; - - protected PerspectiveBuilder(Class<T> perspectiveClass) { - this.perspectiveClass = perspectiveClass; - } - - public Class<T> getPerspectiveClass() { - return perspectiveClass; - } - - @CheckForNull - public abstract T loadPerspective(Class<T> perspectiveClass, BatchComponent component); -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/deprecated/perspectives/PerspectiveNotFoundException.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/perspectives/PerspectiveNotFoundException.java deleted file mode 100644 index 76dcaf532eb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/deprecated/perspectives/PerspectiveNotFoundException.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.deprecated.perspectives; - -public class PerspectiveNotFoundException extends RuntimeException { - public PerspectiveNotFoundException(String message) { - super(message); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/deprecated/perspectives/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/deprecated/perspectives/package-info.java deleted file mode 100644 index 1496e020dd6..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/deprecated/perspectives/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.deprecated.perspectives; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/events/BatchEvent.java b/sonar-batch/src/main/java/org/sonar/batch/events/BatchEvent.java deleted file mode 100644 index f168bb4cd7e..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/events/BatchEvent.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.events; - -import org.sonar.api.batch.events.EventHandler; - -/** - * Root of all Sonar Batch events. - * - * @param <H> handler type - */ -public abstract class BatchEvent<H extends EventHandler> { - - protected BatchEvent() { - } - - /** - * Do not call directly - should be called only by {@link EventBus}. - * Typically should be implemented as following: <code>handler.onEvent(this)</code> - */ - protected abstract void dispatch(H handler); - - /** - * Returns class of associated handler. Used by {@link EventBus} to dispatch events to the correct handlers. - */ - protected abstract Class getType(); - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/events/BatchStepEvent.java b/sonar-batch/src/main/java/org/sonar/batch/events/BatchStepEvent.java deleted file mode 100644 index cfa628edbf0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/events/BatchStepEvent.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.events; - -import org.sonar.batch.phases.AbstractPhaseEvent; - -/** - * Generic event for some steps of project scan. - * @since 3.7 - * - */ -public class BatchStepEvent extends AbstractPhaseEvent<BatchStepHandler> - implements BatchStepHandler.BatchStepEvent { - - private String stepName; - - public BatchStepEvent(String stepName, boolean start) { - super(start); - this.stepName = stepName; - } - - @Override - public String stepName() { - return stepName; - } - - @Override - protected void dispatch(BatchStepHandler handler) { - handler.onBatchStep(this); - } - - @Override - protected Class getType() { - return BatchStepHandler.class; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/events/BatchStepHandler.java b/sonar-batch/src/main/java/org/sonar/batch/events/BatchStepHandler.java deleted file mode 100644 index 9d2cc5e54ba..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/events/BatchStepHandler.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.events; - -import org.sonar.api.batch.events.EventHandler; - -/** - * @since 3.7 - */ -public interface BatchStepHandler extends EventHandler { - - /** - * This interface is not intended to be implemented by clients. - */ - interface BatchStepEvent { - - String stepName(); - - boolean isStart(); - - boolean isEnd(); - - } - - /** - * Called before and after execution of each final step of project analysis - */ - void onBatchStep(BatchStepEvent event); - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/events/EventBus.java b/sonar-batch/src/main/java/org/sonar/batch/events/EventBus.java deleted file mode 100644 index 4b1629f1444..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/events/EventBus.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.events; - -import com.google.common.collect.Lists; -import org.sonar.api.batch.events.EventHandler; - -import java.util.List; - -/** - * Dispatches {@link BatchEvent}s. Eases decoupling by allowing objects to interact without having direct dependencies upon one another, and - * without requiring event sources to deal with maintaining handler lists. - */ -public class EventBus { - - private EventHandler[] registeredHandlers; - - public EventBus(EventHandler[] handlers) { - this.registeredHandlers = handlers; - } - - /** - * Fires the given event. - */ - public void fireEvent(BatchEvent event) { - doFireEvent(event); - } - - private void doFireEvent(BatchEvent event) { - List<EventHandler> handlers = getDispatchList(event.getType()); - for (EventHandler handler : handlers) { - event.dispatch(handler); - } - } - - private List<EventHandler> getDispatchList(Class<? extends EventHandler> handlerType) { - List<EventHandler> result = Lists.newArrayList(); - for (EventHandler handler : registeredHandlers) { - if (handlerType.isAssignableFrom(handler.getClass())) { - result.add(handler); - } - } - return result; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/events/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/events/package-info.java deleted file mode 100644 index 922fe861be7..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/events/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.events; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/BatchComponent.java b/sonar-batch/src/main/java/org/sonar/batch/index/BatchComponent.java deleted file mode 100644 index 9690c833b1e..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/index/BatchComponent.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.index; - -import java.util.ArrayList; -import java.util.Collection; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.sonar.api.batch.fs.InputComponent; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.ResourceUtils; - -public class BatchComponent { - - private final int batchId; - private final Resource r; - private final BatchComponent parent; - private final Collection<BatchComponent> children = new ArrayList<>(); - private InputComponent inputComponent; - - public BatchComponent(int batchId, Resource r, @Nullable BatchComponent parent) { - this.batchId = batchId; - this.r = r; - this.parent = parent; - if (parent != null) { - parent.children.add(this); - } - } - - public String key() { - return r.getEffectiveKey(); - } - - public int batchId() { - return batchId; - } - - public Resource resource() { - return r; - } - - @CheckForNull - public BatchComponent parent() { - return parent; - } - - public Collection<BatchComponent> children() { - return children; - } - - public boolean isFile() { - return this.inputComponent.isFile(); - } - - public BatchComponent setInputComponent(InputComponent inputComponent) { - this.inputComponent = inputComponent; - return this; - } - - public InputComponent inputComponent() { - return inputComponent; - } - - public boolean isProjectOrModule() { - return ResourceUtils.isProject(r); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/BatchComponentCache.java b/sonar-batch/src/main/java/org/sonar/batch/index/BatchComponentCache.java deleted file mode 100644 index 68c29f69c95..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/index/BatchComponentCache.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.index; - -import com.google.common.base.Preconditions; -import com.google.common.base.Strings; -import com.google.common.collect.Maps; -import java.util.Collection; -import java.util.Map; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.fs.InputComponent; -import org.sonar.api.resources.Resource; - -@BatchSide -public class BatchComponentCache { - // components by key - private final Map<String, BatchComponent> components = Maps.newLinkedHashMap(); - - private BatchComponent root; - - @CheckForNull - public BatchComponent get(String componentKey) { - return components.get(componentKey); - } - - public BatchComponent get(Resource resource) { - return components.get(resource.getEffectiveKey()); - } - - public BatchComponent get(InputComponent inputComponent) { - return components.get(inputComponent.key()); - } - - public BatchComponent add(Resource resource, @Nullable Resource parentResource) { - String componentKey = resource.getEffectiveKey(); - Preconditions.checkState(!Strings.isNullOrEmpty(componentKey), "Missing resource effective key"); - BatchComponent parent = parentResource != null ? get(parentResource.getEffectiveKey()) : null; - BatchComponent batchComponent = new BatchComponent(components.size() + 1, resource, parent); - components.put(componentKey, batchComponent); - if (parent == null) { - root = batchComponent; - } - return batchComponent; - } - - public Collection<BatchComponent> all() { - return components.values(); - } - - public BatchComponent getRoot() { - return root; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/Bucket.java b/sonar-batch/src/main/java/org/sonar/batch/index/Bucket.java deleted file mode 100644 index 5fde268c097..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/index/Bucket.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.index; - -import com.google.common.collect.Lists; -import org.sonar.api.resources.Resource; - -import javax.annotation.Nullable; - -import java.util.Collections; -import java.util.List; - -public final class Bucket { - - private Resource resource; - - private Bucket parent; - private List<Bucket> children; - - public Bucket(Resource resource) { - this.resource = resource; - } - - public Resource getResource() { - return resource; - } - - public Bucket setParent(@Nullable Bucket parent) { - this.parent = parent; - if (parent != null) { - parent.addChild(this); - } - return this; - } - - private Bucket addChild(Bucket child) { - if (children == null) { - children = Lists.newArrayList(); - } - children.add(child); - return this; - } - - private void removeChild(Bucket child) { - if (children != null) { - children.remove(child); - } - } - - public List<Bucket> getChildren() { - return children == null ? Collections.<Bucket>emptyList() : children; - } - - public Bucket getParent() { - return parent; - } - - public void clear() { - children = null; - if (parent != null) { - parent.removeChild(this); - parent = null; - } - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - Bucket that = (Bucket) o; - return resource.equals(that.resource); - } - - @Override - public int hashCode() { - return resource.hashCode(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/Cache.java b/sonar-batch/src/main/java/org/sonar/batch/index/Cache.java deleted file mode 100644 index ad194c3aad0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/index/Cache.java +++ /dev/null @@ -1,505 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.index; - -import com.google.common.collect.Sets; -import com.persistit.Exchange; -import com.persistit.Key; -import com.persistit.KeyFilter; -import com.persistit.exception.PersistitException; -import org.apache.commons.lang.builder.ToStringBuilder; - -import javax.annotation.CheckForNull; - -import java.util.Iterator; -import java.util.NoSuchElementException; -import java.util.Set; - -/** - * <p> - * This cache is not thread-safe, due to direct usage of {@link com.persistit.Exchange} - * </p> - */ -public class Cache<V> { - - private final String name; - private final Exchange exchange; - - Cache(String name, Exchange exchange) { - this.name = name; - this.exchange = exchange; - } - - public Cache<V> put(Object key, V value) { - resetKey(key); - return doPut(value); - } - - public Cache<V> put(Object firstKey, Object secondKey, V value) { - resetKey(firstKey, secondKey); - return doPut(value); - } - - public Cache<V> put(Object firstKey, Object secondKey, Object thirdKey, V value) { - resetKey(firstKey, secondKey, thirdKey); - return doPut(value); - } - - public Cache<V> put(Object[] key, V value) { - resetKey(key); - return doPut(value); - } - - private Cache<V> doPut(V value) { - try { - exchange.getValue().put(value); - exchange.store(); - return this; - } catch (Exception e) { - throw new IllegalStateException("Fail to put element in the cache " + name, e); - } - } - - /** - * Returns the value object associated with keys, or null if not found. - */ - public V get(Object key) { - resetKey(key); - return doGet(); - } - - /** - * Returns the value object associated with keys, or null if not found. - */ - @CheckForNull - public V get(Object firstKey, Object secondKey) { - resetKey(firstKey, secondKey); - return doGet(); - } - - /** - * Returns the value object associated with keys, or null if not found. - */ - @CheckForNull - public V get(Object firstKey, Object secondKey, Object thirdKey) { - resetKey(firstKey, secondKey, thirdKey); - return doGet(); - } - - /** - * Returns the value object associated with keys, or null if not found. - */ - @CheckForNull - public V get(Object[] key) { - resetKey(key); - return doGet(); - } - - @SuppressWarnings("unchecked") - @CheckForNull - private V doGet() { - try { - exchange.fetch(); - if (!exchange.getValue().isDefined()) { - return null; - } - return (V) exchange.getValue().get(); - } catch (Exception e) { - // TODO add parameters to message - throw new IllegalStateException("Fail to get element from cache " + name, e); - } - } - - public boolean containsKey(Object key) { - resetKey(key); - return doContainsKey(); - } - - public boolean containsKey(Object firstKey, Object secondKey) { - resetKey(firstKey, secondKey); - return doContainsKey(); - } - - public boolean containsKey(Object firstKey, Object secondKey, Object thirdKey) { - resetKey(firstKey, secondKey, thirdKey); - return doContainsKey(); - } - - public boolean containsKey(Object[] key) { - resetKey(key); - return doContainsKey(); - } - - private boolean doContainsKey() { - try { - exchange.fetch(); - return exchange.isValueDefined(); - } catch (Exception e) { - // TODO add parameters to message - throw new IllegalStateException("Fail to check if element is in cache " + name, e); - } - } - - public boolean remove(Object key) { - resetKey(key); - return doRemove(); - } - - public boolean remove(Object firstKey, Object secondKey) { - resetKey(firstKey, secondKey); - return doRemove(); - } - - public boolean remove(Object firstKey, Object secondKey, Object thirdKey) { - resetKey(firstKey, secondKey, thirdKey); - return doRemove(); - } - - public boolean remove(Object[] key) { - resetKey(key); - return doRemove(); - } - - private boolean doRemove() { - try { - return exchange.remove(); - } catch (Exception e) { - // TODO add parameters to message - throw new IllegalStateException("Fail to get element from cache " + name, e); - } - } - - /** - * Removes everything in the specified group. - * - * @param group The group name. - */ - public Cache<V> clear(Object key) { - resetKey(key); - return doClear(); - } - - public Cache<V> clear(Object firstKey, Object secondKey) { - resetKey(firstKey, secondKey); - return doClear(); - } - - public Cache<V> clear(Object firstKey, Object secondKey, Object thirdKey) { - resetKey(firstKey, secondKey, thirdKey); - return doClear(); - } - - public Cache<V> clear(Object[] key) { - resetKey(key); - return doClear(); - } - - private Cache<V> doClear() { - try { - Key to = new Key(exchange.getKey()); - to.append(Key.AFTER); - exchange.removeKeyRange(exchange.getKey(), to); - return this; - } catch (Exception e) { - throw new IllegalStateException("Fail to clear values from cache " + name, e); - } - } - - /** - * Clears the default as well as all group caches. - */ - public void clear() { - try { - exchange.clear(); - exchange.removeAll(); - } catch (Exception e) { - throw new IllegalStateException("Fail to clear cache", e); - } - } - - /** - * Returns the set of cache keys associated with this group. - * TODO implement a lazy-loading equivalent with Iterator/Iterable - * - * @param group The group. - * @return The set of cache keys for this group. - */ - @SuppressWarnings("rawtypes") - public Set keySet(Object key) { - try { - Set<Object> keys = Sets.newLinkedHashSet(); - exchange.clear(); - Exchange iteratorExchange = new Exchange(exchange); - iteratorExchange.append(key); - iteratorExchange.append(Key.BEFORE); - while (iteratorExchange.next(false)) { - keys.add(iteratorExchange.getKey().indexTo(-1).decode()); - } - return keys; - } catch (Exception e) { - throw new IllegalStateException("Fail to get keys from cache " + name, e); - } - } - - @SuppressWarnings("rawtypes") - public Set keySet(Object firstKey, Object secondKey) { - try { - Set<Object> keys = Sets.newLinkedHashSet(); - exchange.clear(); - Exchange iteratorExchange = new Exchange(exchange); - iteratorExchange.append(firstKey); - iteratorExchange.append(secondKey); - iteratorExchange.append(Key.BEFORE); - while (iteratorExchange.next(false)) { - keys.add(iteratorExchange.getKey().indexTo(-1).decode()); - } - return keys; - } catch (Exception e) { - throw new IllegalStateException("Fail to get keys from cache " + name, e); - } - } - - /** - * Returns the set of keys associated with this cache. - * - * @return The set containing the keys for this cache. - */ - public Set<Object> keySet() { - try { - Set<Object> keys = Sets.newLinkedHashSet(); - exchange.clear(); - Exchange iteratorExchange = new Exchange(exchange); - iteratorExchange.append(Key.BEFORE); - while (iteratorExchange.next(false)) { - keys.add(iteratorExchange.getKey().indexTo(-1).decode()); - } - return keys; - } catch (Exception e) { - throw new IllegalStateException("Fail to get keys from cache " + name, e); - } - } - - /** - * Lazy-loading values for given keys - */ - public Iterable<V> values(Object firstKey, Object secondKey) { - return new ValueIterable<>(exchange, firstKey, secondKey); - } - - /** - * Lazy-loading values for a given key - */ - public Iterable<V> values(Object firstKey) { - return new ValueIterable<>(exchange, firstKey); - } - - /** - * Lazy-loading values - */ - public Iterable<V> values() { - return new ValueIterable<>(exchange); - } - - public Iterable<Entry<V>> entries() { - return new EntryIterable<>(exchange); - } - - public Iterable<Entry<V>> entries(Object firstKey) { - return new EntryIterable<>(exchange, firstKey); - } - - private void resetKey(Object key) { - exchange.clear(); - exchange.append(key); - } - - private void resetKey(Object first, Object second) { - exchange.clear(); - exchange.append(first).append(second); - } - - private void resetKey(Object first, Object second, Object third) { - exchange.clear(); - exchange.append(first).append(second).append(third); - } - - private void resetKey(Object[] keys) { - exchange.clear(); - for (Object o : keys) { - exchange.append(o); - } - } - - // - // LAZY ITERATORS AND ITERABLES - // - - private static class ValueIterable<T> implements Iterable<T> { - private final Exchange originExchange; - private final Object[] keys; - - private ValueIterable(Exchange originExchange, Object... keys) { - this.originExchange = originExchange; - this.keys = keys; - } - - @Override - public Iterator<T> iterator() { - originExchange.clear(); - KeyFilter filter = new KeyFilter(); - for (Object key : keys) { - originExchange.append(key); - filter = filter.append(KeyFilter.simpleTerm(key)); - } - originExchange.append(Key.BEFORE); - Exchange iteratorExchange = new Exchange(originExchange); - return new ValueIterator<>(iteratorExchange, filter); - } - } - - private static class ValueIterator<T> implements Iterator<T> { - private final Exchange exchange; - private final KeyFilter keyFilter; - - private ValueIterator(Exchange exchange, KeyFilter keyFilter) { - this.exchange = exchange; - this.keyFilter = keyFilter; - } - - @Override - public boolean hasNext() { - try { - return exchange.hasNext(keyFilter); - } catch (PersistitException e) { - throw new IllegalStateException(e); - } - } - - @SuppressWarnings("unchecked") - @Override - public T next() { - try { - exchange.next(keyFilter); - } catch (PersistitException e) { - throw new IllegalStateException(e); - } - if (exchange.getValue().isDefined()) { - return (T) exchange.getValue().get(); - } - throw new NoSuchElementException(); - } - - @Override - public void remove() { - throw new UnsupportedOperationException("Removing an item is not supported"); - } - } - - private static class EntryIterable<T> implements Iterable<Entry<T>> { - private final Exchange originExchange; - private final Object[] keys; - - private EntryIterable(Exchange originExchange, Object... keys) { - this.originExchange = originExchange; - this.keys = keys; - } - - @Override - public Iterator<Entry<T>> iterator() { - originExchange.clear(); - KeyFilter filter = new KeyFilter(); - for (Object key : keys) { - originExchange.append(key); - filter = filter.append(KeyFilter.simpleTerm(key)); - } - originExchange.append(Key.BEFORE); - Exchange iteratorExchange = new Exchange(originExchange); - return new EntryIterator<>(iteratorExchange, filter); - } - } - - private static class EntryIterator<T> implements Iterator<Entry<T>> { - private final Exchange exchange; - private final KeyFilter keyFilter; - - private EntryIterator(Exchange exchange, KeyFilter keyFilter) { - this.exchange = exchange; - this.keyFilter = keyFilter; - } - - @Override - public boolean hasNext() { - try { - return exchange.hasNext(keyFilter); - } catch (PersistitException e) { - throw new IllegalStateException(e); - } - } - - @SuppressWarnings("unchecked") - @Override - public Entry<T> next() { - try { - exchange.next(keyFilter); - } catch (PersistitException e) { - throw new IllegalStateException(e); - } - if (exchange.getValue().isDefined()) { - T value = (T) exchange.getValue().get(); - Key key = exchange.getKey(); - Object[] array = new Object[key.getDepth()]; - for (int i = 0; i < key.getDepth(); i++) { - array[i] = key.indexTo(i - key.getDepth()).decode(); - } - return new Entry<>(array, value); - } - throw new NoSuchElementException(); - } - - @Override - public void remove() { - throw new UnsupportedOperationException("Removing an item is not supported"); - } - } - - public static class Entry<V> { - private final Object[] key; - private final V value; - - Entry(Object[] key, V value) { - this.key = key; - this.value = value; - } - - public Object[] key() { - return key; - } - - public V value() { - return value; - } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/Caches.java b/sonar-batch/src/main/java/org/sonar/batch/index/Caches.java deleted file mode 100644 index ddddb271dfd..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/index/Caches.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.index; - -import com.google.common.collect.Maps; - -import java.util.Map; -import java.util.Map.Entry; - -import com.google.common.base.Preconditions; -import com.persistit.Exchange; -import com.persistit.Value; -import com.persistit.encoding.CoderManager; -import com.persistit.Persistit; -import com.persistit.encoding.ValueCoder; -import com.persistit.exception.PersistitException; -import com.persistit.Volume; -import org.picocontainer.Startable; -import org.sonar.api.batch.BatchSide; - -@BatchSide -public class Caches implements Startable { - private final Map<String, Exchange> cacheMap = Maps.newHashMap(); - private Persistit persistit; - private Volume volume; - - public Caches(CachesManager caches) { - persistit = caches.persistit(); - doStart(); - } - - @Override - public void start() { - // done in constructor - } - - private void doStart() { - try { - persistit.flush(); - volume = persistit.createTemporaryVolume(); - } catch (Exception e) { - throw new IllegalStateException("Fail to create a cache volume", e); - } - } - - public void registerValueCoder(Class<?> clazz, ValueCoder coder) { - CoderManager cm = persistit.getCoderManager(); - cm.registerValueCoder(clazz, coder); - } - - public <V> Cache<V> createCache(String cacheName) { - Preconditions.checkState(volume != null && volume.isOpened(), "Caches are not initialized"); - Preconditions.checkState(!cacheMap.containsKey(cacheName), "Cache is already created: " + cacheName); - try { - Exchange exchange = persistit.getExchange(volume, cacheName, true); - exchange.setMaximumValueSize(Value.MAXIMUM_SIZE); - Cache<V> cache = new Cache<>(cacheName, exchange); - cacheMap.put(cacheName, exchange); - return cache; - } catch (Exception e) { - throw new IllegalStateException("Fail to create cache: " + cacheName, e); - } - } - - @Override - public void stop() { - for (Entry<String, Exchange> e : cacheMap.entrySet()) { - persistit.releaseExchange(e.getValue()); - } - - cacheMap.clear(); - - if (volume != null) { - try { - volume.close(); - volume.delete(); - } catch (PersistitException e) { - throw new IllegalStateException("Fail to close caches", e); - } - volume = null; - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/CachesManager.java b/sonar-batch/src/main/java/org/sonar/batch/index/CachesManager.java deleted file mode 100644 index 010375a84eb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/index/CachesManager.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.index; - -import com.persistit.Persistit; -import com.persistit.exception.PersistitException; -import com.persistit.logging.Slf4jAdapter; -import java.io.File; -import java.util.Properties; -import org.picocontainer.Startable; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.utils.TempFolder; - -import static org.sonar.core.util.FileUtils.deleteQuietly; - -/** - * Factory of caches - * - * @since 3.6 - */ -@BatchSide -public class CachesManager implements Startable { - private File tempDir; - private Persistit persistit; - private final TempFolder tempFolder; - - public CachesManager(TempFolder tempFolder) { - this.tempFolder = tempFolder; - initPersistit(); - } - - private void initPersistit() { - try { - tempDir = tempFolder.newDir("caches"); - persistit = new Persistit(); - persistit.setPersistitLogger(new Slf4jAdapter(LoggerFactory.getLogger("PERSISTIT"))); - Properties props = new Properties(); - props.setProperty("datapath", tempDir.getAbsolutePath()); - props.setProperty("logpath", "${datapath}/log"); - props.setProperty("logfile", "${logpath}/persistit_${timestamp}.log"); - props.setProperty("buffer.count.8192", "10"); - props.setProperty("journalpath", "${datapath}/journal"); - props.setProperty("tmpvoldir", "${datapath}"); - props.setProperty("volume.1", "${datapath}/persistit,create,pageSize:8192,initialPages:10,extensionPages:100,maximumPages:25000"); - props.setProperty("jmx", "false"); - persistit.setProperties(props); - persistit.initialize(); - - } catch (Exception e) { - throw new IllegalStateException("Fail to start caches", e); - } - } - - @Override - public void start() { - // already started in constructor - } - - @Override - public void stop() { - if (persistit != null) { - try { - persistit.close(false); - persistit = null; - } catch (PersistitException e) { - throw new IllegalStateException("Fail to close caches", e); - } - } - deleteQuietly(tempDir); - tempDir = null; - } - - File tempDir() { - return tempDir; - } - - Persistit persistit() { - return persistit; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java deleted file mode 100644 index d5f1a561729..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ /dev/null @@ -1,353 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.index; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang.ObjectUtils; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.SonarIndex; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.fs.internal.DefaultInputModule; -import org.sonar.api.design.Dependency; -import org.sonar.api.measures.Measure; -import org.sonar.api.measures.MeasuresFilter; -import org.sonar.api.measures.MeasuresFilters; -import org.sonar.api.resources.Directory; -import org.sonar.api.resources.File; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.Qualifiers; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.ResourceUtils; -import org.sonar.api.scan.filesystem.PathResolver; -import org.sonar.batch.DefaultProjectTree; -import org.sonar.batch.scan.measure.MeasureCache; -import org.sonar.batch.sensor.DefaultSensorStorage; -import org.sonar.core.component.ComponentKeys; - -public class DefaultIndex extends SonarIndex { - - private static final Logger LOG = LoggerFactory.getLogger(DefaultIndex.class); - - private final BatchComponentCache componentCache; - private final MeasureCache measureCache; - private final PathResolver pathResolver; - private final DefaultProjectTree projectTree; - // caches - private DefaultSensorStorage sensorStorage; - private Project currentProject; - private Map<Resource, Bucket> buckets = Maps.newLinkedHashMap(); - - public DefaultIndex(BatchComponentCache componentCache, DefaultProjectTree projectTree, MeasureCache measureCache, PathResolver pathResolver) { - this.componentCache = componentCache; - this.projectTree = projectTree; - this.measureCache = measureCache; - this.pathResolver = pathResolver; - } - - public void start() { - Project rootProject = projectTree.getRootProject(); - if (StringUtils.isNotBlank(rootProject.getKey())) { - doStart(rootProject); - } - } - - void doStart(Project rootProject) { - Bucket bucket = new Bucket(rootProject); - addBucket(rootProject, bucket); - BatchComponent component = componentCache.add(rootProject, null); - component.setInputComponent(new DefaultInputModule(rootProject.getEffectiveKey())); - currentProject = rootProject; - - for (Project module : rootProject.getModules()) { - addModule(rootProject, module); - } - } - - private void addBucket(Resource resource, Bucket bucket) { - buckets.put(resource, bucket); - } - - private void addModule(Project parent, Project module) { - ProjectDefinition parentDefinition = projectTree.getProjectDefinition(parent); - java.io.File parentBaseDir = parentDefinition.getBaseDir(); - ProjectDefinition moduleDefinition = projectTree.getProjectDefinition(module); - java.io.File moduleBaseDir = moduleDefinition.getBaseDir(); - module.setPath(new PathResolver().relativePath(parentBaseDir, moduleBaseDir)); - addResource(module); - for (Project submodule : module.getModules()) { - addModule(module, submodule); - } - } - - @Override - public Project getProject() { - return currentProject; - } - - public void setCurrentProject(Project project, DefaultSensorStorage sensorStorage) { - this.currentProject = project; - - // the following components depend on the current module, so they need to be reloaded. - this.sensorStorage = sensorStorage; - } - - /** - * Keep only project stuff - */ - public void clear() { - Iterator<Map.Entry<Resource, Bucket>> it = buckets.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry<Resource, Bucket> entry = it.next(); - Resource resource = entry.getKey(); - if (!ResourceUtils.isSet(resource)) { - entry.getValue().clear(); - it.remove(); - } - - } - } - - @CheckForNull - @Override - public Measure getMeasure(Resource resource, org.sonar.api.batch.measure.Metric<?> metric) { - return getMeasures(resource, MeasuresFilters.metric(metric)); - } - - @CheckForNull - @Override - public <M> M getMeasures(Resource resource, MeasuresFilter<M> filter) { - // Reload resource so that effective key is populated - Resource indexedResource = getResource(resource); - if (indexedResource == null) { - return null; - } - Collection<Measure> unfiltered = new ArrayList<>(); - if (filter instanceof MeasuresFilters.MetricFilter) { - // optimization - Measure byMetric = measureCache.byMetric(indexedResource, ((MeasuresFilters.MetricFilter<M>) filter).filterOnMetricKey()); - if (byMetric != null) { - unfiltered.add(byMetric); - } - } else { - for (Measure measure : measureCache.byResource(indexedResource)) { - unfiltered.add(measure); - } - } - return filter.filter(unfiltered); - } - - @Override - public Measure addMeasure(Resource resource, Measure measure) { - Bucket bucket = getBucket(resource); - if (bucket != null) { - return sensorStorage.saveMeasure(resource, measure); - } - return measure; - } - - @Override - public Dependency addDependency(Dependency dependency) { - return dependency; - } - - @Override - public Set<Resource> getResources() { - return buckets.keySet(); - } - - @Override - public String getSource(Resource reference) { - Resource resource = getResource(reference); - if (resource instanceof File) { - File file = (File) resource; - Project module = currentProject; - ProjectDefinition def = projectTree.getProjectDefinition(module); - try { - return FileUtils.readFileToString(new java.io.File(def.getBaseDir(), file.getPath())); - } catch (IOException e) { - throw new IllegalStateException("Unable to read file content " + reference, e); - } - } - return null; - } - - /** - * Does nothing if the resource is already registered. - */ - @Override - public Resource addResource(Resource resource) { - Bucket bucket = doIndex(resource); - return bucket != null ? bucket.getResource() : null; - } - - @Override - @CheckForNull - public <R extends Resource> R getResource(@Nullable R reference) { - Bucket bucket = getBucket(reference); - if (bucket != null) { - return (R) bucket.getResource(); - } - return null; - } - - @Override - public List<Resource> getChildren(Resource resource) { - List<Resource> children = Lists.newLinkedList(); - Bucket bucket = getBucket(resource); - if (bucket != null) { - for (Bucket childBucket : bucket.getChildren()) { - children.add(childBucket.getResource()); - } - } - return children; - } - - @Override - public Resource getParent(Resource resource) { - Bucket bucket = getBucket(resource); - if (bucket != null && bucket.getParent() != null) { - return bucket.getParent().getResource(); - } - return null; - } - - @Override - public boolean index(Resource resource) { - Bucket bucket = doIndex(resource); - return bucket != null; - } - - private Bucket doIndex(Resource resource) { - if (resource.getParent() != null) { - doIndex(resource.getParent()); - } - return doIndex(resource, resource.getParent()); - } - - @Override - public boolean index(Resource resource, Resource parentReference) { - Bucket bucket = doIndex(resource, parentReference); - return bucket != null; - } - - private Bucket doIndex(Resource resource, @Nullable Resource parentReference) { - Bucket bucket = getBucket(resource); - if (bucket != null) { - return bucket; - } - - if (StringUtils.isBlank(resource.getKey())) { - LOG.warn("Unable to index a resource without key " + resource); - return null; - } - - Resource parent = (Resource) ObjectUtils.defaultIfNull(parentReference, currentProject); - - Bucket parentBucket = getBucket(parent); - if (parentBucket == null && parent != null) { - LOG.warn("Resource ignored, parent is not indexed: " + resource); - return null; - } - - if (ResourceUtils.isProject(resource) || /* For technical projects */ResourceUtils.isRootProject(resource)) { - resource.setEffectiveKey(resource.getKey()); - } else { - resource.setEffectiveKey(ComponentKeys.createEffectiveKey(currentProject, resource)); - } - bucket = new Bucket(resource).setParent(parentBucket); - addBucket(resource, bucket); - - Resource parentResource = parentBucket != null ? parentBucket.getResource() : null; - BatchComponent component = componentCache.add(resource, parentResource); - if (ResourceUtils.isProject(resource)) { - component.setInputComponent(new DefaultInputModule(resource.getEffectiveKey())); - } - - return bucket; - } - - @Override - public boolean isExcluded(@Nullable Resource reference) { - return false; - } - - @Override - public boolean isIndexed(@Nullable Resource reference, boolean acceptExcluded) { - return getBucket(reference) != null; - } - - private Bucket getBucket(@Nullable Resource reference) { - if (reference == null) { - return null; - } - if (StringUtils.isNotBlank(reference.getKey())) { - return buckets.get(reference); - } - String relativePathFromSourceDir = null; - boolean isTest = false; - boolean isDir = false; - if (reference instanceof File) { - File referenceFile = (File) reference; - isTest = Qualifiers.UNIT_TEST_FILE.equals(referenceFile.getQualifier()); - relativePathFromSourceDir = referenceFile.relativePathFromSourceDir(); - } else if (reference instanceof Directory) { - isDir = true; - Directory referenceDir = (Directory) reference; - relativePathFromSourceDir = referenceDir.relativePathFromSourceDir(); - if (Directory.ROOT.equals(relativePathFromSourceDir)) { - relativePathFromSourceDir = ""; - } - } - if (relativePathFromSourceDir != null) { - // Resolve using deprecated key - List<String> dirs; - ProjectDefinition projectDef = projectTree.getProjectDefinition(getProject()); - if (isTest) { - dirs = projectDef.getTestDirs(); - } else { - dirs = projectDef.getSourceDirs(); - } - for (String src : dirs) { - java.io.File dirOrFile = pathResolver.relativeFile(projectDef.getBaseDir(), src); - java.io.File abs = new java.io.File(dirOrFile, relativePathFromSourceDir); - Bucket b = getBucket(isDir ? Directory.fromIOFile(abs, getProject()) : File.fromIOFile(abs, getProject())); - if (b != null) { - return b; - } - } - - } - return null; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/index/package-info.java deleted file mode 100644 index 6fc80086043..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/index/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.index; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultFilterableIssue.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultFilterableIssue.java deleted file mode 100644 index a7f28e5bfa4..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultFilterableIssue.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import java.util.Date; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import org.sonar.api.resources.Project; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.scan.issue.filter.FilterableIssue; -import org.sonar.batch.protocol.output.BatchReport.Issue; - -public class DefaultFilterableIssue implements FilterableIssue { - private final Issue rawIssue; - private final Project project; - private final String componentKey; - - public DefaultFilterableIssue(Project project, Issue rawIssue, String componentKey) { - this.project = project; - this.rawIssue = rawIssue; - this.componentKey = componentKey; - - } - - @Override - public String componentKey() { - return componentKey; - } - - @Override - public RuleKey ruleKey() { - return RuleKey.of(rawIssue.getRuleRepository(), rawIssue.getRuleKey()); - } - - @Override - public String severity() { - return rawIssue.getSeverity().name(); - } - - @Override - public String message() { - return rawIssue.getMsg(); - } - - @Override - public Integer line() { - return rawIssue.hasLine() ? rawIssue.getLine() : null; - } - - @Override - public Double gap() { - return rawIssue.hasGap() ? rawIssue.getGap() : null; - } - - @Override - public Double effortToFix() { - return gap(); - } - - @Override - public Date creationDate() { - return project.getAnalysisDate(); - } - - @Override - public String projectKey() { - return project.getEffectiveKey(); - } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java deleted file mode 100644 index 291fb7bb2ee..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import java.util.Collections; -import java.util.List; -import org.sonar.api.batch.sensor.SensorContext; -import org.sonar.api.batch.sensor.issue.internal.DefaultIssue; -import org.sonar.api.issue.Issuable; -import org.sonar.api.issue.Issue; -import org.sonar.batch.index.BatchComponent; - -/** - * @since 3.6 - */ -public class DefaultIssuable implements Issuable { - - private final BatchComponent component; - private final SensorContext sensorContext; - - DefaultIssuable(BatchComponent component, SensorContext sensorContext) { - this.component = component; - this.sensorContext = sensorContext; - } - - @Override - public IssueBuilder newIssueBuilder() { - DefaultIssue newIssue = (DefaultIssue) sensorContext.newIssue(); - return new DeprecatedIssueBuilderWrapper(component.inputComponent(), newIssue); - } - - @Override - public boolean addIssue(Issue issue) { - ((DeprecatedIssueWrapper) issue).wrapped().save(); - return true; - } - - @Override - public List<Issue> resolvedIssues() { - return Collections.emptyList(); - } - - @Override - public List<Issue> issues() { - return Collections.emptyList(); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssueCallback.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssueCallback.java deleted file mode 100644 index e021cc02884..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssueCallback.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import org.sonar.batch.issue.tracking.TrackedIssue; - -import org.apache.commons.lang.StringUtils; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.batch.rule.Rule; -import org.sonar.api.batch.rule.Rules; -import org.sonar.batch.protocol.input.BatchInput.User; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import org.sonar.batch.repository.user.UserRepositoryLoader; -import org.sonar.batch.bootstrapper.IssueListener; - -public class DefaultIssueCallback implements IssueCallback { - private final IssueCache issues; - private final IssueListener listener; - private final UserRepositoryLoader userRepository; - private final Rules rules; - - private Set<String> userLoginNames = new HashSet<>(); - private Map<String, String> userMap = new HashMap<>(); - private Set<RuleKey> ruleKeys = new HashSet<>(); - - public DefaultIssueCallback(IssueCache issues, IssueListener listener, UserRepositoryLoader userRepository, Rules rules) { - this.issues = issues; - this.listener = listener; - this.userRepository = userRepository; - this.rules = rules; - } - - /** - * If no listener exists, this constructor will be used by pico. - */ - public DefaultIssueCallback(IssueCache issues, UserRepositoryLoader userRepository, Rules rules) { - this(issues, null, userRepository, rules); - } - - @Override - public void execute() { - if (listener == null) { - return; - } - - for (TrackedIssue issue : issues.all()) { - collectInfo(issue); - } - - getUsers(); - - for (TrackedIssue issue : issues.all()) { - IssueListener.Issue newIssue = new IssueListener.Issue(); - newIssue.setAssigneeLogin(issue.assignee()); - newIssue.setAssigneeName(getAssigneeName(issue.assignee())); - newIssue.setComponentKey(issue.componentKey()); - newIssue.setKey(issue.key()); - newIssue.setMessage(issue.getMessage()); - newIssue.setNew(issue.isNew()); - newIssue.setResolution(issue.resolution()); - newIssue.setRuleKey(issue.getRuleKey().toString()); - newIssue.setRuleName(getRuleName(issue.getRuleKey())); - newIssue.setSeverity(issue.severity()); - newIssue.setStatus(issue.status()); - newIssue.setStartLine(issue.startLine()); - newIssue.setStartLineOffset(issue.startLineOffset()); - newIssue.setEndLine(issue.endLine()); - newIssue.setEndLineOffset(issue.endLineOffset()); - - listener.handle(newIssue); - } - } - - private void collectInfo(TrackedIssue issue) { - if (!StringUtils.isEmpty(issue.assignee())) { - userLoginNames.add(issue.assignee()); - } - if (issue.getRuleKey() != null) { - ruleKeys.add(issue.getRuleKey()); - } - } - - private String getAssigneeName(String login) { - return userMap.get(login); - } - - private void getUsers() { - for (String loginName : userLoginNames) { - User user = userRepository.load(loginName); - if (user != null) { - userMap.put(user.getLogin(), user.getName()); - } - } - } - - private String getRuleName(RuleKey ruleKey) { - Rule rule = rules.find(ruleKey); - return rule != null ? rule.name() : null; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssueFilterChain.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssueFilterChain.java deleted file mode 100644 index 2d9b86ad4a8..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssueFilterChain.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import com.google.common.collect.ImmutableList; -import org.sonar.api.scan.issue.filter.IssueFilter; - -import java.util.List; - -import org.sonar.api.scan.issue.filter.FilterableIssue; -import org.sonar.api.scan.issue.filter.IssueFilterChain; - -public class DefaultIssueFilterChain implements IssueFilterChain { - private final List<IssueFilter> filters; - - public DefaultIssueFilterChain(IssueFilter... filters) { - this.filters = ImmutableList.copyOf(filters); - } - - public DefaultIssueFilterChain() { - this.filters = ImmutableList.of(); - } - - private DefaultIssueFilterChain(List<IssueFilter> filters) { - this.filters = filters; - } - - @Override - public boolean accept(FilterableIssue issue) { - if (filters.isEmpty()) { - return true; - } else { - return filters.get(0).accept(issue, new DefaultIssueFilterChain(filters.subList(1, filters.size()))); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultProjectIssues.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultProjectIssues.java deleted file mode 100644 index be81a1a2a53..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultProjectIssues.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; -import org.sonar.api.issue.Issue; -import org.sonar.api.issue.ProjectIssues; -import org.sonar.batch.issue.tracking.TrackedIssue; - -import javax.annotation.Nullable; - -/** - * Expose list of issues for the current project - * @since 4.0 - */ -public class DefaultProjectIssues implements ProjectIssues { - - private final IssueCache cache; - - public DefaultProjectIssues(IssueCache cache) { - this.cache = cache; - } - - @Override - public Iterable<Issue> issues() { - return Iterables.transform( - Iterables.filter(cache.all(), new ResolvedPredicate(false)), - new IssueTransformer()); - } - - @Override - public Iterable<Issue> resolvedIssues() { - return Iterables.transform( - Iterables.filter(cache.all(), new ResolvedPredicate(true)), - new IssueTransformer()); - } - - private static class ResolvedPredicate implements Predicate<TrackedIssue> { - private final boolean resolved; - - private ResolvedPredicate(boolean resolved) { - this.resolved = resolved; - } - - @Override - public boolean apply(@Nullable TrackedIssue issue) { - if (issue != null) { - return resolved ? (issue.resolution() != null) : (issue.resolution() == null); - } - return false; - } - } - - private static class IssueTransformer implements Function<TrackedIssue, Issue> { - @Override - public Issue apply(TrackedIssue issue) { - return new TrackedIssueAdapter(issue); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueAdapterForFilter.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueAdapterForFilter.java deleted file mode 100644 index f6df0fdbd39..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueAdapterForFilter.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.Map; -import org.sonar.api.issue.Issue; -import org.sonar.api.issue.IssueComment; -import org.sonar.api.resources.Project; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.Duration; - -/** - * @deprecated since 5.3 - */ -@Deprecated -class DeprecatedIssueAdapterForFilter implements Issue { - private final Project project; - private final org.sonar.batch.protocol.output.BatchReport.Issue rawIssue; - private final String componentKey; - - DeprecatedIssueAdapterForFilter(Project project, org.sonar.batch.protocol.output.BatchReport.Issue rawIssue, String componentKey) { - this.project = project; - this.rawIssue = rawIssue; - this.componentKey = componentKey; - } - - @Override - public String key() { - throw unsupported(); - } - - @Override - public String componentKey() { - return componentKey; - } - - @Override - public RuleKey ruleKey() { - return RuleKey.of(rawIssue.getRuleRepository(), rawIssue.getRuleKey()); - } - - @Override - public String language() { - throw unsupported(); - } - - @Override - public String severity() { - return rawIssue.getSeverity().name(); - } - - @Override - public String message() { - return rawIssue.getMsg(); - } - - @Override - public Integer line() { - return rawIssue.hasLine() ? rawIssue.getLine() : null; - } - - @Override - @Deprecated - public Double effortToFix() { - return gap(); - } - - @Override - public Double gap() { - return rawIssue.hasGap() ? rawIssue.getGap() : null; - } - - @Override - public String status() { - return Issue.STATUS_OPEN; - } - - @Override - public String resolution() { - return null; - } - - @Override - public String reporter() { - throw unsupported(); - } - - @Override - public String assignee() { - return null; - } - - @Override - public Date creationDate() { - return project.getAnalysisDate(); - } - - @Override - public Date updateDate() { - return null; - } - - @Override - public Date closeDate() { - return null; - } - - @Override - public String attribute(String key) { - return attributes().get(key); - } - - @Override - public Map<String, String> attributes() { - return Collections.emptyMap(); - } - - @Override - public String authorLogin() { - throw unsupported(); - } - - @Override - public String actionPlanKey() { - throw unsupported(); - } - - @Override - public List<IssueComment> comments() { - throw unsupported(); - } - - @Override - public boolean isNew() { - throw unsupported(); - } - - @Deprecated - @Override - public Duration debt() { - return effort(); - } - - @Override - public Duration effort() { - throw unsupported(); - } - - @Override - public String projectKey() { - return project.getEffectiveKey(); - } - - @Override - public String projectUuid() { - throw unsupported(); - } - - @Override - public String componentUuid() { - throw unsupported(); - } - - @Override - public Collection<String> tags() { - throw unsupported(); - } - - private static UnsupportedOperationException unsupported() { - return new UnsupportedOperationException("Not available for issues filters"); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueBuilderWrapper.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueBuilderWrapper.java deleted file mode 100644 index 0ebef7b6c73..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueBuilderWrapper.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import com.google.common.base.Preconditions; -import javax.annotation.Nullable; -import org.sonar.api.batch.fs.InputComponent; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.rule.Severity; -import org.sonar.api.batch.sensor.issue.NewIssueLocation; -import org.sonar.api.batch.sensor.issue.internal.DefaultIssue; -import org.sonar.api.batch.sensor.issue.internal.DefaultIssueLocation; -import org.sonar.api.issue.Issuable; -import org.sonar.api.issue.Issuable.IssueBuilder; -import org.sonar.api.issue.Issue; -import org.sonar.api.rule.RuleKey; - -public class DeprecatedIssueBuilderWrapper implements Issuable.IssueBuilder { - - private final DefaultIssue newIssue; - private final InputComponent primaryComponent; - private TextRange primaryRange = null; - private String primaryMessage = null; - - public DeprecatedIssueBuilderWrapper(InputComponent primaryComponent, DefaultIssue newIssue) { - this.primaryComponent = primaryComponent; - this.newIssue = newIssue; - } - - @Override - public IssueBuilder ruleKey(RuleKey ruleKey) { - newIssue.forRule(ruleKey); - return this; - } - - @Override - public IssueBuilder line(@Nullable Integer line) { - Preconditions.checkState(newIssue.primaryLocation() == null, "Do not use line() and at() for the same issue"); - if (primaryComponent.isFile()) { - if (line != null) { - this.primaryRange = ((InputFile) primaryComponent).selectLine(line.intValue()); - } - return this; - } else { - throw new IllegalArgumentException("Unable to set line for issues on project or directory"); - } - } - - @Override - public IssueBuilder message(String message) { - Preconditions.checkState(newIssue.primaryLocation() == null, "Do not use message() and at() for the same issue"); - this.primaryMessage = message; - return this; - } - - @Override - public NewIssueLocation newLocation() { - return new DefaultIssueLocation(); - } - - @Override - public IssueBuilder at(NewIssueLocation primaryLocation) { - Preconditions.checkState(primaryMessage == null && primaryRange == null, "Do not use message() or line() and at() for the same issue"); - newIssue.at(primaryLocation); - return this; - } - - @Override - public IssueBuilder addLocation(NewIssueLocation secondaryLocation) { - newIssue.addLocation(secondaryLocation); - return this; - } - - @Override - public IssueBuilder addFlow(Iterable<NewIssueLocation> flowLocations) { - newIssue.addFlow(flowLocations); - return this; - } - - @Override - public IssueBuilder severity(String severity) { - newIssue.overrideSeverity(Severity.valueOf(severity)); - return this; - } - - @Override - public IssueBuilder reporter(String reporter) { - throw new UnsupportedOperationException("Not supported during sensor phase"); - } - - @Override - public IssueBuilder effortToFix(Double d) { - newIssue.effortToFix(d); - return this; - } - - @Override - public IssueBuilder attribute(String key, String value) { - throw new UnsupportedOperationException("Not supported during sensor phase"); - } - - @Override - public Issue build() { - if (newIssue.primaryLocation() == null) { - NewIssueLocation newLocation = newIssue.newLocation().on(primaryComponent); - if (primaryMessage != null) { - newLocation.message(primaryMessage); - } - if (primaryComponent.isFile() && primaryRange != null) { - newLocation.at(primaryRange); - } - newIssue.at(newLocation); - } - return new DeprecatedIssueWrapper(newIssue); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueFilterChain.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueFilterChain.java deleted file mode 100644 index 83994cfb49d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueFilterChain.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import com.google.common.collect.ImmutableList; -import org.sonar.api.issue.Issue; -import org.sonar.api.issue.batch.IssueFilter; -import org.sonar.api.issue.batch.IssueFilterChain; - -import java.util.List; - -/** - * @deprecated since 5.3 - */ -@Deprecated -public class DeprecatedIssueFilterChain implements IssueFilterChain { - - private final List<IssueFilter> filters; - - public DeprecatedIssueFilterChain(IssueFilter... filters) { - this.filters = ImmutableList.copyOf(filters); - } - - public DeprecatedIssueFilterChain() { - this.filters = ImmutableList.of(); - } - - private DeprecatedIssueFilterChain(List<IssueFilter> filters) { - this.filters = filters; - } - - @Override - public boolean accept(Issue issue) { - if (filters.isEmpty()) { - return true; - } else { - return filters.get(0).accept(issue, new DeprecatedIssueFilterChain(filters.subList(1, filters.size()))); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueWrapper.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueWrapper.java deleted file mode 100644 index 61e215cc931..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueWrapper.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.Map; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.rule.Severity; -import org.sonar.api.batch.sensor.issue.internal.DefaultIssue; -import org.sonar.api.issue.Issue; -import org.sonar.api.issue.IssueComment; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.Duration; - -public class DeprecatedIssueWrapper implements Issue { - - private final DefaultIssue newIssue; - - public DeprecatedIssueWrapper(DefaultIssue newIssue) { - this.newIssue = newIssue; - } - - public DefaultIssue wrapped() { - return newIssue; - } - - @Override - public String key() { - return null; - } - - @Override - public String componentKey() { - return null; - } - - @Override - public RuleKey ruleKey() { - return newIssue.ruleKey(); - } - - @Override - public String language() { - return null; - } - - @Override - public String severity() { - Severity overridenSeverity = newIssue.overriddenSeverity(); - return overridenSeverity != null ? overridenSeverity.name() : null; - } - - @Override - public String message() { - return newIssue.primaryLocation().message(); - } - - @Override - public Integer line() { - TextRange textRange = newIssue.primaryLocation().textRange(); - return textRange != null ? textRange.start().line() : null; - } - - /** - * @deprecated since 5.5, replaced by {@link #gap()} - */ - @Override - @Deprecated - public Double effortToFix() { - return gap(); - } - - @Override - public Double gap() { - return newIssue.effortToFix(); - } - - @Override - public String status() { - return null; - } - - @Override - public String resolution() { - return null; - } - - @Override - public String reporter() { - return null; - } - - @Override - public String assignee() { - return null; - } - - @Override - public Date creationDate() { - return null; - } - - @Override - public Date updateDate() { - return null; - } - - @Override - public Date closeDate() { - return null; - } - - @Override - public String attribute(String key) { - return null; - } - - @Override - public Map<String, String> attributes() { - return Collections.emptyMap(); - } - - @Override - public String authorLogin() { - return null; - } - - @Override - public String actionPlanKey() { - return null; - } - - @Override - public List<IssueComment> comments() { - return Collections.emptyList(); - } - - @Override - public boolean isNew() { - return false; - } - - @Override - public Duration debt() { - return null; - } - - @Override - public Duration effort() { - return null; - } - - @Override - public String projectKey() { - return null; - } - - @Override - public String projectUuid() { - return null; - } - - @Override - public String componentUuid() { - return null; - } - - @Override - public Collection<String> tags() { - return Collections.emptyList(); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/IssuableFactory.java b/sonar-batch/src/main/java/org/sonar/batch/issue/IssuableFactory.java deleted file mode 100644 index a2c7afc9c59..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/IssuableFactory.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import org.sonar.api.batch.sensor.SensorContext; -import org.sonar.api.issue.Issuable; -import org.sonar.batch.deprecated.perspectives.PerspectiveBuilder; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.sensor.DefaultSensorContext; - -/** - * Create the perspective {@link Issuable} on components. - * @since 3.6 - */ -public class IssuableFactory extends PerspectiveBuilder<Issuable> { - - private final SensorContext sensorContext; - - public IssuableFactory(DefaultSensorContext sensorContext) { - super(Issuable.class); - this.sensorContext = sensorContext; - } - - @Override - public Issuable loadPerspective(Class<Issuable> perspectiveClass, BatchComponent component) { - return new DefaultIssuable(component, sensorContext); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/IssueCache.java b/sonar-batch/src/main/java/org/sonar/batch/issue/IssueCache.java deleted file mode 100644 index ac1bc6826a5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/IssueCache.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import org.sonar.batch.issue.tracking.TrackedIssue; - -import org.sonar.api.batch.BatchSide; -import org.sonar.batch.index.Cache; -import org.sonar.batch.index.Caches; - -import java.util.Collection; - -/** - * Shared issues among all project modules - */ -@BatchSide -public class IssueCache { - - // component key -> issue key -> issue - private final Cache<TrackedIssue> cache; - - public IssueCache(Caches caches) { - cache = caches.createCache("issues"); - } - - public Iterable<TrackedIssue> byComponent(String componentKey) { - return cache.values(componentKey); - } - - public Iterable<TrackedIssue> all() { - return cache.values(); - } - - public Collection<Object> componentKeys() { - return cache.keySet(); - } - - public IssueCache put(TrackedIssue issue) { - cache.put(issue.componentKey(), issue.key(), issue); - return this; - } - - public void clear(String componentKey) { - cache.clear(componentKey); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/IssueCallback.java b/sonar-batch/src/main/java/org/sonar/batch/issue/IssueCallback.java deleted file mode 100644 index b27b7887045..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/IssueCallback.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -public interface IssueCallback { - void execute(); -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/IssueFilters.java b/sonar-batch/src/main/java/org/sonar/batch/issue/IssueFilters.java deleted file mode 100644 index b84352da6df..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/IssueFilters.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import org.sonar.api.scan.issue.filter.FilterableIssue; - -import org.sonar.api.scan.issue.filter.IssueFilterChain; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.issue.Issue; -import org.sonar.api.scan.issue.filter.IssueFilter; -import org.sonar.api.resources.Project; -import org.sonar.batch.protocol.output.BatchReport; - -@BatchSide -public class IssueFilters { - private final IssueFilter[] filters; - private final org.sonar.api.issue.batch.IssueFilter[] deprecatedFilters; - private final Project project; - - public IssueFilters(Project project, IssueFilter[] exclusionFilters, org.sonar.api.issue.batch.IssueFilter[] filters) { - this.project = project; - this.filters = exclusionFilters; - this.deprecatedFilters = filters; - } - - public IssueFilters(Project project, IssueFilter[] filters) { - this(project, filters, new org.sonar.api.issue.batch.IssueFilter[0]); - } - - public IssueFilters(Project project, org.sonar.api.issue.batch.IssueFilter[] deprecatedFilters) { - this(project, new IssueFilter[0], deprecatedFilters); - } - - public IssueFilters(Project project) { - this(project, new IssueFilter[0], new org.sonar.api.issue.batch.IssueFilter[0]); - } - - public boolean accept(String componentKey, BatchReport.Issue rawIssue) { - IssueFilterChain filterChain = new DefaultIssueFilterChain(filters); - FilterableIssue fIssue = new DefaultFilterableIssue(project, rawIssue, componentKey); - if (filterChain.accept(fIssue)) { - return acceptDeprecated(componentKey, rawIssue); - } - - return false; - } - - public boolean acceptDeprecated(String componentKey, BatchReport.Issue rawIssue) { - Issue issue = new DeprecatedIssueAdapterForFilter(project, rawIssue, componentKey); - return new DeprecatedIssueFilterChain(deprecatedFilters).accept(issue); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/IssueTransformer.java b/sonar-batch/src/main/java/org/sonar/batch/issue/IssueTransformer.java deleted file mode 100644 index b606854be93..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/IssueTransformer.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import com.google.common.base.Preconditions; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import javax.annotation.Nullable; -import org.sonar.api.issue.Issue; -import org.sonar.api.rule.RuleKey; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.issue.tracking.SourceHashHolder; -import org.sonar.batch.issue.tracking.TrackedIssue; -import org.sonar.batch.protocol.input.BatchInput.ServerIssue; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReport.TextRange; -import org.sonar.core.component.ComponentKeys; -import org.sonar.core.util.Uuids; - -public class IssueTransformer { - private IssueTransformer() { - // static only - } - - public static TrackedIssue toTrackedIssue(ServerIssue serverIssue) { - TrackedIssue issue = new TrackedIssue(); - issue.setKey(serverIssue.getKey()); - issue.setStatus(serverIssue.getStatus()); - issue.setResolution(serverIssue.hasResolution() ? serverIssue.getResolution() : null); - issue.setMessage(serverIssue.hasMsg() ? serverIssue.getMsg() : null); - issue.setStartLine(serverIssue.hasLine() ? serverIssue.getLine() : null); - issue.setEndLine(serverIssue.hasLine() ? serverIssue.getLine() : null); - issue.setSeverity(serverIssue.getSeverity().name()); - issue.setAssignee(serverIssue.hasAssigneeLogin() ? serverIssue.getAssigneeLogin() : null); - issue.setComponentKey(ComponentKeys.createEffectiveKey(serverIssue.getModuleKey(), serverIssue.hasPath() ? serverIssue.getPath() : null)); - issue.setCreationDate(new Date(serverIssue.getCreationDate())); - issue.setRuleKey(RuleKey.of(serverIssue.getRuleRepository(), serverIssue.getRuleKey())); - issue.setNew(false); - return issue; - } - - public static void close(TrackedIssue issue) { - issue.setStatus(Issue.STATUS_CLOSED); - issue.setStartLine(null); - issue.setEndLine(null); - issue.setResolution(Issue.RESOLUTION_FIXED); - } - - public static void resolveRemove(TrackedIssue issue) { - issue.setStatus(Issue.STATUS_CLOSED); - issue.setStartLine(null); - issue.setEndLine(null); - issue.setResolution(Issue.RESOLUTION_REMOVED); - } - - public static Collection<TrackedIssue> toTrackedIssue(BatchComponent component, Collection<BatchReport.Issue> rawIssues, @Nullable SourceHashHolder hashes) { - List<TrackedIssue> issues = new ArrayList<>(rawIssues.size()); - - for (BatchReport.Issue issue : rawIssues) { - issues.add(toTrackedIssue(component, issue, hashes)); - } - - return issues; - } - - public static TrackedIssue toTrackedIssue(BatchComponent component, BatchReport.Issue rawIssue, @Nullable SourceHashHolder hashes) { - RuleKey ruleKey = RuleKey.of(rawIssue.getRuleRepository(), rawIssue.getRuleKey()); - - Preconditions.checkNotNull(component.key(), "Component key must be set"); - Preconditions.checkNotNull(ruleKey, "Rule key must be set"); - - TrackedIssue issue = new TrackedIssue(hashes != null ? hashes.getHashedSource() : null); - - issue.setKey(Uuids.createFast()); - issue.setComponentKey(component.key()); - issue.setRuleKey(ruleKey); - issue.setGap(rawIssue.hasGap() ? rawIssue.getGap() : null); - issue.setSeverity(rawIssue.getSeverity().name()); - issue.setMessage(rawIssue.hasMsg() ? rawIssue.getMsg() : null); - issue.setResolution(null); - issue.setStatus(Issue.STATUS_OPEN); - issue.setNew(true); - - if (rawIssue.hasTextRange()) { - TextRange r = rawIssue.getTextRange(); - - issue.setStartLine(r.hasStartLine() ? rawIssue.getTextRange().getStartLine() : null); - issue.setStartLineOffset(r.hasStartOffset() ? r.getStartOffset() : null); - issue.setEndLine(r.hasEndLine() ? r.getEndLine() : issue.startLine()); - issue.setEndLineOffset(r.hasEndOffset() ? r.getEndOffset() : null); - } - - return issue; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java deleted file mode 100644 index fb798fd563b..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import com.google.common.base.Strings; -import org.sonar.api.batch.fs.InputComponent; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.rule.ActiveRule; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.rule.Rule; -import org.sonar.api.batch.rule.Rules; -import org.sonar.api.batch.sensor.issue.Issue; -import org.sonar.api.batch.sensor.issue.Issue.Flow; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.MessageException; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.Constants.Severity; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReport.IssueLocation; -import org.sonar.batch.protocol.output.BatchReport.IssueLocation.Builder; -import org.sonar.batch.report.ReportPublisher; - -/** - * Initialize the issues raised during scan. - */ -public class ModuleIssues { - - private final ActiveRules activeRules; - private final Rules rules; - private final IssueFilters filters; - private final ReportPublisher reportPublisher; - private final BatchComponentCache componentCache; - private final BatchReport.Issue.Builder builder = BatchReport.Issue.newBuilder(); - private final Builder locationBuilder = IssueLocation.newBuilder(); - private final org.sonar.batch.protocol.output.BatchReport.TextRange.Builder textRangeBuilder = org.sonar.batch.protocol.output.BatchReport.TextRange.newBuilder(); - private final BatchReport.Flow.Builder flowBuilder = BatchReport.Flow.newBuilder(); - - public ModuleIssues(ActiveRules activeRules, Rules rules, IssueFilters filters, ReportPublisher reportPublisher, BatchComponentCache componentCache) { - this.activeRules = activeRules; - this.rules = rules; - this.filters = filters; - this.reportPublisher = reportPublisher; - this.componentCache = componentCache; - } - - public boolean initAndAddIssue(Issue issue) { - InputComponent inputComponent = issue.primaryLocation().inputComponent(); - BatchComponent component = componentCache.get(inputComponent); - - Rule rule = validateRule(issue); - ActiveRule activeRule = activeRules.find(issue.ruleKey()); - if (activeRule == null) { - // rule does not exist or is not enabled -> ignore the issue - return false; - } - - String primaryMessage = Strings.isNullOrEmpty(issue.primaryLocation().message()) ? rule.name() : issue.primaryLocation().message(); - org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity(); - Severity severity = overriddenSeverity != null ? Severity.valueOf(overriddenSeverity.name()) : Severity.valueOf(activeRule.severity()); - - builder.clear(); - locationBuilder.clear(); - // non-null fields - builder.setSeverity(severity); - builder.setRuleRepository(issue.ruleKey().repository()); - builder.setRuleKey(issue.ruleKey().rule()); - builder.setMsg(primaryMessage); - locationBuilder.setMsg(primaryMessage); - - locationBuilder.setComponentRef(component.batchId()); - TextRange primaryTextRange = issue.primaryLocation().textRange(); - if (primaryTextRange != null) { - builder.setLine(primaryTextRange.start().line()); - builder.setTextRange(toProtobufTextRange(primaryTextRange)); - } - Double gap = issue.gap(); - if (gap != null) { - builder.setGap(gap); - } - applyFlows(issue); - BatchReport.Issue rawIssue = builder.build(); - - if (filters.accept(inputComponent.key(), rawIssue)) { - write(component, rawIssue); - return true; - } - return false; - } - - private void applyFlows(Issue issue) { - for (Flow flow : issue.flows()) { - if (!flow.locations().isEmpty()) { - flowBuilder.clear(); - for (org.sonar.api.batch.sensor.issue.IssueLocation location : flow.locations()) { - locationBuilder.clear(); - locationBuilder.setComponentRef(componentCache.get(location.inputComponent()).batchId()); - String message = location.message(); - if (message != null) { - locationBuilder.setMsg(message); - } - TextRange textRange = location.textRange(); - if (textRange != null) { - locationBuilder.setTextRange(toProtobufTextRange(textRange)); - } - flowBuilder.addLocation(locationBuilder.build()); - } - builder.addFlow(flowBuilder.build()); - } - } - } - - private org.sonar.batch.protocol.output.BatchReport.TextRange toProtobufTextRange(TextRange primaryTextRange) { - textRangeBuilder.clear(); - textRangeBuilder.setStartLine(primaryTextRange.start().line()); - textRangeBuilder.setStartOffset(primaryTextRange.start().lineOffset()); - textRangeBuilder.setEndLine(primaryTextRange.end().line()); - textRangeBuilder.setEndOffset(primaryTextRange.end().lineOffset()); - return textRangeBuilder.build(); - } - - private Rule validateRule(Issue issue) { - RuleKey ruleKey = issue.ruleKey(); - Rule rule = rules.find(ruleKey); - if (rule == null) { - throw MessageException.of(String.format("The rule '%s' does not exist.", ruleKey)); - } - if (Strings.isNullOrEmpty(rule.name()) && Strings.isNullOrEmpty(issue.primaryLocation().message())) { - throw MessageException.of(String.format("The rule '%s' has no name and the related issue has no message.", ruleKey)); - } - return rule; - } - - public void write(BatchComponent component, BatchReport.Issue rawIssue) { - reportPublisher.getWriter().appendComponentIssue(component.batchId(), rawIssue); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/TrackedIssueAdapter.java b/sonar-batch/src/main/java/org/sonar/batch/issue/TrackedIssueAdapter.java deleted file mode 100644 index 19027847d9d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/TrackedIssueAdapter.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.sonar.api.issue.Issue; -import org.sonar.api.issue.IssueComment; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.Duration; -import org.sonar.batch.issue.tracking.TrackedIssue; - -public class TrackedIssueAdapter implements Issue { - private TrackedIssue issue; - - public TrackedIssueAdapter(TrackedIssue issue) { - this.issue = issue; - } - - @Override - public String key() { - return issue.key(); - } - - @Override - public String componentKey() { - return issue.componentKey(); - } - - @Override - public RuleKey ruleKey() { - return issue.getRuleKey(); - } - - @Override - public String severity() { - return issue.severity(); - } - - @Override - public String message() { - return issue.getMessage(); - } - - @Override - public Integer line() { - return issue.startLine(); - } - - /** - * @deprecated since 5.5, replaced by {@link #gap()} - */ - @Override - @Deprecated - public Double effortToFix() { - return gap(); - } - - @Override - public Double gap() { - return issue.gap(); - } - - @Override - public String status() { - return issue.status(); - } - - @Override - public String resolution() { - return issue.resolution(); - } - - @Override - public String reporter() { - return issue.reporter(); - } - - @Override - public String assignee() { - return issue.assignee(); - } - - @Override - public boolean isNew() { - return issue.isNew(); - } - - @Override - public Map<String, String> attributes() { - return new HashMap<>(); - } - - @Override - public Date creationDate() { - return issue.getCreationDate(); - } - - @Override - public String language() { - return null; - } - - @Override - public Date updateDate() { - return null; - } - - @Override - public Date closeDate() { - return null; - } - - @Override - public String attribute(String key) { - return attributes().get(key); - } - - @Override - public String authorLogin() { - return null; - } - - @Override - public String actionPlanKey() { - return null; - } - - @Override - public List<IssueComment> comments() { - return new ArrayList<>(); - } - - /** - * @deprecated since 5.5, replaced by {@link #effort()} - */ - @Override - @Deprecated - public Duration debt() { - return null; - } - - @Override - public Duration effort() { - return null; - } - - @Override - public String projectKey() { - return null; - } - - @Override - public String projectUuid() { - return null; - } - - @Override - public String componentUuid() { - return null; - } - - @Override - public Collection<String> tags() { - return new ArrayList<>(); - } - - @Override - public boolean equals(Object o) { - if (o == null || !(o instanceof Issue)) { - return false; - } - Issue that = (Issue) o; - return !(issue.key() != null ? !issue.key().equals(that.key()) : (that.key() != null)); - } - - @Override - public int hashCode() { - return issue.key() != null ? issue.key().hashCode() : 0; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/EnforceIssuesFilter.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/EnforceIssuesFilter.java deleted file mode 100644 index de4920a1f06..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/EnforceIssuesFilter.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.ignore; - -import org.sonar.api.scan.issue.filter.FilterableIssue; - -import org.sonar.batch.issue.ignore.pattern.IssueInclusionPatternInitializer; -import org.sonar.batch.issue.ignore.pattern.IssuePattern; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.scan.issue.filter.IssueFilter; -import org.sonar.api.scan.issue.filter.IssueFilterChain; - -public class EnforceIssuesFilter implements IssueFilter { - - private IssueInclusionPatternInitializer patternInitializer; - - private static final Logger LOG = LoggerFactory.getLogger(EnforceIssuesFilter.class); - - public EnforceIssuesFilter(IssueInclusionPatternInitializer patternInitializer) { - this.patternInitializer = patternInitializer; - } - - @Override - public boolean accept(FilterableIssue issue, IssueFilterChain chain) { - boolean atLeastOneRuleMatched = false; - boolean atLeastOnePatternFullyMatched = false; - IssuePattern matchingPattern = null; - - for (IssuePattern pattern : patternInitializer.getMulticriteriaPatterns()) { - if (pattern.getRulePattern().match(issue.ruleKey().toString())) { - atLeastOneRuleMatched = true; - String pathForComponent = patternInitializer.getPathForComponent(issue.componentKey()); - if (pathForComponent != null && pattern.getResourcePattern().match(pathForComponent)) { - atLeastOnePatternFullyMatched = true; - matchingPattern = pattern; - } - } - } - - if (atLeastOneRuleMatched) { - if (atLeastOnePatternFullyMatched) { - LOG.debug("Issue {} enforced by pattern {}", issue, matchingPattern); - } - return atLeastOnePatternFullyMatched; - } else { - return chain.accept(issue); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/IgnoreIssuesFilter.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/IgnoreIssuesFilter.java deleted file mode 100644 index 99a716148c6..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/IgnoreIssuesFilter.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.ignore; - -import org.sonar.api.scan.issue.filter.FilterableIssue; - -import org.sonar.batch.issue.ignore.pattern.IssueExclusionPatternInitializer; -import org.sonar.batch.issue.ignore.pattern.IssuePattern; -import org.sonar.batch.issue.ignore.pattern.PatternMatcher; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.scan.issue.filter.IssueFilter; -import org.sonar.api.scan.issue.filter.IssueFilterChain; - -public class IgnoreIssuesFilter implements IssueFilter { - - private PatternMatcher patternMatcher; - - private static final Logger LOG = LoggerFactory.getLogger(IgnoreIssuesFilter.class); - - public IgnoreIssuesFilter(IssueExclusionPatternInitializer patternInitializer) { - this.patternMatcher = patternInitializer.getPatternMatcher(); - } - - @Override - public boolean accept(FilterableIssue issue, IssueFilterChain chain) { - if (hasMatchFor(issue)) { - return false; - } else { - return chain.accept(issue); - } - } - - private boolean hasMatchFor(FilterableIssue issue) { - IssuePattern pattern = patternMatcher.getMatchingPattern(issue); - if (pattern != null) { - logExclusion(issue, pattern); - return true; - } - return false; - } - - private static void logExclusion(FilterableIssue issue, IssuePattern pattern) { - LOG.debug("Issue {} ignored by exclusion pattern {}", issue, pattern); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/package-info.java deleted file mode 100644 index ead2a62c636..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.issue.ignore; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/AbstractPatternInitializer.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/AbstractPatternInitializer.java deleted file mode 100644 index a3e58803a71..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/AbstractPatternInitializer.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.ignore.pattern; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Lists; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.config.Settings; - -import java.util.List; - -import static com.google.common.base.Objects.firstNonNull; - -@BatchSide -public abstract class AbstractPatternInitializer { - - private Settings settings; - - private List<IssuePattern> multicriteriaPatterns; - - protected AbstractPatternInitializer(Settings settings) { - this.settings = settings; - initPatterns(); - } - - protected Settings getSettings() { - return settings; - } - - public List<IssuePattern> getMulticriteriaPatterns() { - return multicriteriaPatterns; - } - - public boolean hasConfiguredPatterns() { - return hasMulticriteriaPatterns(); - } - - public boolean hasMulticriteriaPatterns() { - return !multicriteriaPatterns.isEmpty(); - } - - public abstract void initializePatternsForPath(String relativePath, String componentKey); - - @VisibleForTesting - protected final void initPatterns() { - // Patterns Multicriteria - multicriteriaPatterns = Lists.newArrayList(); - String patternConf = StringUtils.defaultIfBlank(settings.getString(getMulticriteriaConfigurationKey()), ""); - for (String id : StringUtils.split(patternConf, ',')) { - String propPrefix = getMulticriteriaConfigurationKey() + "." + id + "."; - String resourceKeyPattern = settings.getString(propPrefix + "resourceKey"); - String ruleKeyPattern = settings.getString(propPrefix + "ruleKey"); - String lineRange = "*"; - String[] fields = new String[] {resourceKeyPattern, ruleKeyPattern, lineRange}; - PatternDecoder.checkRegularLineConstraints(StringUtils.join(fields, ","), fields); - IssuePattern pattern = new IssuePattern(firstNonNull(resourceKeyPattern, "*"), firstNonNull(ruleKeyPattern, "*")); - PatternDecoder.decodeRangeOfLines(pattern, firstNonNull(lineRange, "*")); - multicriteriaPatterns.add(pattern); - } - } - - protected abstract String getMulticriteriaConfigurationKey(); -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/IssueExclusionPatternInitializer.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/IssueExclusionPatternInitializer.java deleted file mode 100644 index 74d80f95186..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/IssueExclusionPatternInitializer.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.ignore.pattern; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Lists; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.config.Settings; -import org.sonar.core.config.IssueExclusionProperties; - -import java.util.List; - -import static com.google.common.base.Strings.nullToEmpty; - -public class IssueExclusionPatternInitializer extends AbstractPatternInitializer { - - private List<IssuePattern> blockPatterns; - private List<IssuePattern> allFilePatterns; - private PatternMatcher patternMatcher; - - public IssueExclusionPatternInitializer(Settings settings) { - super(settings); - patternMatcher = new PatternMatcher(); - loadFileContentPatterns(); - } - - @Override - protected String getMulticriteriaConfigurationKey() { - return "sonar.issue.ignore" + ".multicriteria"; - } - - public PatternMatcher getPatternMatcher() { - return patternMatcher; - } - - @Override - public void initializePatternsForPath(String relativePath, String componentKey) { - for (IssuePattern pattern : getMulticriteriaPatterns()) { - if (pattern.matchResource(relativePath)) { - getPatternMatcher().addPatternForComponent(componentKey, pattern); - } - } - } - - @Override - public boolean hasConfiguredPatterns() { - return hasFileContentPattern() || hasMulticriteriaPatterns(); - } - - @VisibleForTesting - protected final void loadFileContentPatterns() { - // Patterns Block - blockPatterns = Lists.newArrayList(); - String patternConf = StringUtils.defaultIfBlank(getSettings().getString(IssueExclusionProperties.PATTERNS_BLOCK_KEY), ""); - for (String id : StringUtils.split(patternConf, ',')) { - String propPrefix = IssueExclusionProperties.PATTERNS_BLOCK_KEY + "." + id + "."; - String beginBlockRegexp = getSettings().getString(propPrefix + IssueExclusionProperties.BEGIN_BLOCK_REGEXP); - String endBlockRegexp = getSettings().getString(propPrefix + IssueExclusionProperties.END_BLOCK_REGEXP); - String[] fields = new String[]{beginBlockRegexp, endBlockRegexp}; - PatternDecoder.checkDoubleRegexpLineConstraints(StringUtils.join(fields, ","), fields); - IssuePattern pattern = new IssuePattern().setBeginBlockRegexp(nullToEmpty(beginBlockRegexp)).setEndBlockRegexp(nullToEmpty(endBlockRegexp)); - blockPatterns.add(pattern); - } - - // Patterns All File - allFilePatterns = Lists.newArrayList(); - patternConf = StringUtils.defaultIfBlank(getSettings().getString(IssueExclusionProperties.PATTERNS_ALLFILE_KEY), ""); - for (String id : StringUtils.split(patternConf, ',')) { - String propPrefix = IssueExclusionProperties.PATTERNS_ALLFILE_KEY + "." + id + "."; - String allFileRegexp = getSettings().getString(propPrefix + IssueExclusionProperties.FILE_REGEXP); - PatternDecoder.checkWholeFileRegexp(allFileRegexp); - IssuePattern pattern = new IssuePattern().setAllFileRegexp(nullToEmpty(allFileRegexp)); - allFilePatterns.add(pattern); - } - } - - public List<IssuePattern> getBlockPatterns() { - return blockPatterns; - } - - public List<IssuePattern> getAllFilePatterns() { - return allFilePatterns; - } - - public boolean hasFileContentPattern() { - return !(blockPatterns.isEmpty() && allFilePatterns.isEmpty()); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/IssueInclusionPatternInitializer.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/IssueInclusionPatternInitializer.java deleted file mode 100644 index 9fec33bb952..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/IssueInclusionPatternInitializer.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.ignore.pattern; - -import com.google.common.collect.Maps; -import org.sonar.api.config.Settings; - -import java.util.Map; - -public class IssueInclusionPatternInitializer extends AbstractPatternInitializer { - - private Map<String, String> pathForComponent; - - public IssueInclusionPatternInitializer(Settings settings) { - super(settings); - pathForComponent = Maps.newHashMap(); - } - - @Override - protected String getMulticriteriaConfigurationKey() { - return "sonar.issue.enforce" + ".multicriteria"; - } - - @Override - public void initializePatternsForPath(String relativePath, String componentKey) { - pathForComponent.put(componentKey, relativePath); - } - - public String getPathForComponent(String componentKey) { - return pathForComponent.get(componentKey); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/IssuePattern.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/IssuePattern.java deleted file mode 100644 index 4c4cc0b4715..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/IssuePattern.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.ignore.pattern; - -import org.sonar.api.scan.issue.filter.FilterableIssue; - -import com.google.common.collect.Sets; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.WildcardPattern; - -import java.util.Set; - -public class IssuePattern { - - private WildcardPattern resourcePattern; - private WildcardPattern rulePattern; - private Set<Integer> lines = Sets.newLinkedHashSet(); - private Set<LineRange> lineRanges = Sets.newLinkedHashSet(); - private String beginBlockRegexp; - private String endBlockRegexp; - private String allFileRegexp; - private boolean checkLines = true; - - public IssuePattern() { - } - - public IssuePattern(String resourcePattern, String rulePattern) { - this.resourcePattern = WildcardPattern.create(resourcePattern); - this.rulePattern = WildcardPattern.create(rulePattern); - } - - public IssuePattern(String resourcePattern, String rulePattern, Set<LineRange> lineRanges) { - this(resourcePattern, rulePattern); - this.lineRanges = lineRanges; - } - - public WildcardPattern getResourcePattern() { - return resourcePattern; - } - - public WildcardPattern getRulePattern() { - return rulePattern; - } - - public String getBeginBlockRegexp() { - return beginBlockRegexp; - } - - public String getEndBlockRegexp() { - return endBlockRegexp; - } - - public String getAllFileRegexp() { - return allFileRegexp; - } - - IssuePattern addLineRange(int fromLineId, int toLineId) { - lineRanges.add(new LineRange(fromLineId, toLineId)); - return this; - } - - IssuePattern addLine(int lineId) { - lines.add(lineId); - return this; - } - - boolean isCheckLines() { - return checkLines; - } - - IssuePattern setCheckLines(boolean b) { - this.checkLines = b; - return this; - } - - IssuePattern setBeginBlockRegexp(String beginBlockRegexp) { - this.beginBlockRegexp = beginBlockRegexp; - return this; - } - - IssuePattern setEndBlockRegexp(String endBlockRegexp) { - this.endBlockRegexp = endBlockRegexp; - return this; - } - - IssuePattern setAllFileRegexp(String allFileRegexp) { - this.allFileRegexp = allFileRegexp; - return this; - } - - Set<Integer> getAllLines() { - Set<Integer> allLines = Sets.newLinkedHashSet(lines); - for (LineRange lineRange : lineRanges) { - allLines.addAll(lineRange.toLines()); - } - return allLines; - } - - public boolean match(FilterableIssue issue) { - boolean match = matchResource(issue.componentKey()) - && matchRule(issue.ruleKey()); - if (checkLines) { - Integer line = issue.line(); - if (line == null) { - match = false; - } else { - match = match && matchLine(line); - } - } - return match; - } - - boolean matchLine(int lineId) { - if (lines.contains(lineId)) { - return true; - } - - for (LineRange range : lineRanges) { - if (range.in(lineId)) { - return true; - } - } - - return false; - } - - boolean matchRule(RuleKey rule) { - if (rule == null) { - return false; - } - - String key = new StringBuilder().append(rule.repository()).append(':').append(rule.rule()).toString(); - return rulePattern.match(key); - } - - boolean matchResource(String resource) { - return resource != null && resourcePattern.match(resource); - } - - public IssuePattern forResource(String resource) { - return new IssuePattern(resource, rulePattern.toString(), lineRanges).setCheckLines(isCheckLines()); - } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/LineRange.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/LineRange.java deleted file mode 100644 index 72d43f5f216..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/LineRange.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.ignore.pattern; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Sets; - -import java.util.Set; - -public class LineRange { - private int from; - private int to; - - public LineRange(int from, int to) { - Preconditions.checkArgument(from <= to, "Line range is not valid: %s must be greater than %s", from, to); - - this.from = from; - this.to = to; - } - - public boolean in(int lineId) { - return from <= lineId && lineId <= to; - } - - public Set<Integer> toLines() { - Set<Integer> lines = Sets.newLinkedHashSet(); - for (int index = from; index <= to; index++) { - lines.add(index); - } - return lines; - } - - @Override - public String toString() { - return "[" + from + "-" + to + "]"; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + from; - result = prime * result + to; - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - if (fieldsDiffer((LineRange) obj)) { - return false; - } - return true; - } - - private boolean fieldsDiffer(LineRange other) { - if (from != other.from) { - return true; - } - if (to != other.to) { - return true; - } - return false; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/PatternDecoder.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/PatternDecoder.java deleted file mode 100644 index 11dc501cac4..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/PatternDecoder.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.ignore.pattern; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Lists; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.utils.SonarException; - -import java.util.List; - -public class PatternDecoder { - - private static final int THREE_FIELDS_PER_LINE = 3; - private static final String LINE_RANGE_REGEXP = "\\[((\\d+|\\d+-\\d+),?)*\\]"; - private static final String CONFIG_FORMAT_ERROR_PREFIX = "Exclusions > Issues : Invalid format. "; - - public List<IssuePattern> decode(String patternsList) { - List<IssuePattern> patterns = Lists.newLinkedList(); - String[] patternsLines = StringUtils.split(patternsList, "\n"); - for (String patternLine : patternsLines) { - IssuePattern pattern = decodeLine(patternLine.trim()); - if (pattern != null) { - patterns.add(pattern); - } - } - return patterns; - } - - /** - * Main method that decodes a line which defines a pattern - */ - public IssuePattern decodeLine(String line) { - if (isBlankOrComment(line)) { - return null; - } - - String[] fields = StringUtils.splitPreserveAllTokens(line, ';'); - if (fields.length > THREE_FIELDS_PER_LINE) { - throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The following line has more than 3 fields separated by comma: " + line); - } - - IssuePattern pattern; - if (fields.length == THREE_FIELDS_PER_LINE) { - checkRegularLineConstraints(line, fields); - pattern = new IssuePattern(StringUtils.trim(fields[0]), StringUtils.trim(fields[1])); - decodeRangeOfLines(pattern, fields[2]); - } else if (fields.length == 2) { - checkDoubleRegexpLineConstraints(line, fields); - pattern = new IssuePattern().setBeginBlockRegexp(fields[0]).setEndBlockRegexp(fields[1]); - } else { - checkWholeFileRegexp(fields[0]); - pattern = new IssuePattern().setAllFileRegexp(fields[0]); - } - - return pattern; - } - - static void checkRegularLineConstraints(String line, String[] fields) { - if (!isResource(fields[0])) { - throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The first field does not define a resource pattern: " + line); - } - if (!isRule(fields[1])) { - throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The second field does not define a rule pattern: " + line); - } - if (!isLinesRange(fields[2])) { - throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The third field does not define a range of lines: " + line); - } - } - - static void checkDoubleRegexpLineConstraints(String line, String[] fields) { - if (!isRegexp(fields[0])) { - throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The first field does not define a regular expression: " + line); - } - // As per configuration help, missing second field means: from start regexp to EOF - } - - static void checkWholeFileRegexp(String regexp) { - if (!isRegexp(regexp)) { - throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The field does not define a regular expression: " + regexp); - } - } - - public static void decodeRangeOfLines(IssuePattern pattern, String field) { - if (StringUtils.equals(field, "*")) { - pattern.setCheckLines(false); - } else { - pattern.setCheckLines(true); - String s = StringUtils.substringBetween(StringUtils.trim(field), "[", "]"); - String[] parts = StringUtils.split(s, ','); - for (String part : parts) { - if (StringUtils.contains(part, '-')) { - String[] range = StringUtils.split(part, '-'); - pattern.addLineRange(Integer.valueOf(range[0]), Integer.valueOf(range[1])); - } else { - pattern.addLine(Integer.valueOf(part)); - } - } - } - } - - @VisibleForTesting - static boolean isLinesRange(String field) { - return StringUtils.equals(field, "*") || java.util.regex.Pattern.matches(LINE_RANGE_REGEXP, field); - } - - @VisibleForTesting - static boolean isBlankOrComment(String line) { - return StringUtils.isBlank(line) ^ StringUtils.startsWith(line, "#"); - } - - @VisibleForTesting - static boolean isResource(String field) { - return StringUtils.isNotBlank(field); - } - - @VisibleForTesting - static boolean isRule(String field) { - return StringUtils.isNotBlank(field); - } - - @VisibleForTesting - static boolean isRegexp(String field) { - return StringUtils.isNotBlank(field); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/PatternMatcher.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/PatternMatcher.java deleted file mode 100644 index 80b6f716cff..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/PatternMatcher.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.ignore.pattern; - -import org.sonar.api.scan.issue.filter.FilterableIssue; - -import com.google.common.collect.LinkedHashMultimap; -import com.google.common.collect.Multimap; - -import java.util.Collection; -import java.util.Iterator; -import java.util.Set; - -public class PatternMatcher { - - private Multimap<String, IssuePattern> patternByComponent = LinkedHashMultimap.create(); - - public IssuePattern getMatchingPattern(FilterableIssue issue) { - IssuePattern matchingPattern = null; - Iterator<IssuePattern> patternIterator = getPatternsForComponent(issue.componentKey()).iterator(); - while(matchingPattern == null && patternIterator.hasNext()) { - IssuePattern nextPattern = patternIterator.next(); - if (nextPattern.match(issue)) { - matchingPattern = nextPattern; - } - } - return matchingPattern; - } - - public Collection<IssuePattern> getPatternsForComponent(String componentKey) { - return patternByComponent.get(componentKey); - } - - public void addPatternForComponent(String component, IssuePattern pattern) { - patternByComponent.put(component, pattern.forResource(component)); - } - - public void addPatternToExcludeResource(String resource) { - addPatternForComponent(resource, new IssuePattern(resource, "*").setCheckLines(false)); - } - - public void addPatternToExcludeLines(String resource, Set<LineRange> lineRanges) { - addPatternForComponent(resource, new IssuePattern(resource, "*", lineRanges).setCheckLines(true)); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/package-info.java deleted file mode 100644 index ebbc7b8371e..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/pattern/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.issue.ignore.pattern; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsLoader.java deleted file mode 100644 index 8622dc618eb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsLoader.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.ignore.scanner; - -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.resources.Project; -import org.sonar.batch.issue.ignore.pattern.IssueExclusionPatternInitializer; -import org.sonar.batch.issue.ignore.pattern.IssueInclusionPatternInitializer; - -import java.nio.charset.Charset; - -public final class IssueExclusionsLoader { - - private final IssueExclusionsRegexpScanner regexpScanner; - private final IssueExclusionPatternInitializer exclusionPatternInitializer; - private final IssueInclusionPatternInitializer inclusionPatternInitializer; - private final FileSystem fileSystem; - - public IssueExclusionsLoader(IssueExclusionsRegexpScanner regexpScanner, IssueExclusionPatternInitializer exclusionPatternInitializer, - IssueInclusionPatternInitializer inclusionPatternInitializer, - FileSystem fileSystem) { - this.regexpScanner = regexpScanner; - this.exclusionPatternInitializer = exclusionPatternInitializer; - this.inclusionPatternInitializer = inclusionPatternInitializer; - this.fileSystem = fileSystem; - } - - public boolean shouldExecuteOnProject(Project project) { - return inclusionPatternInitializer.hasConfiguredPatterns() - || exclusionPatternInitializer.hasConfiguredPatterns(); - } - - /** - * {@inheritDoc} - */ - public void execute() { - Charset sourcesEncoding = fileSystem.encoding(); - - for (InputFile inputFile : fileSystem.inputFiles(fileSystem.predicates().all())) { - try { - String componentEffectiveKey = ((DefaultInputFile) inputFile).key(); - if (componentEffectiveKey != null) { - String path = inputFile.relativePath(); - inclusionPatternInitializer.initializePatternsForPath(path, componentEffectiveKey); - exclusionPatternInitializer.initializePatternsForPath(path, componentEffectiveKey); - if (exclusionPatternInitializer.hasFileContentPattern()) { - regexpScanner.scan(componentEffectiveKey, inputFile.file(), sourcesEncoding); - } - } - } catch (Exception e) { - throw new IllegalStateException("Unable to read the source file : '" + inputFile.absolutePath() + "' with the charset : '" - + sourcesEncoding.name() + "'.", e); - } - } - } - - @Override - public String toString() { - return "Issues Exclusions - Source Scanner"; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsRegexpScanner.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsRegexpScanner.java deleted file mode 100644 index de465ad4dc5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/scanner/IssueExclusionsRegexpScanner.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.ignore.scanner; - -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchSide; -import org.sonar.batch.issue.ignore.pattern.IssueExclusionPatternInitializer; -import org.sonar.batch.issue.ignore.pattern.IssuePattern; -import org.sonar.batch.issue.ignore.pattern.LineRange; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; -import java.util.List; -import java.util.Set; - -@BatchSide -public class IssueExclusionsRegexpScanner { - - private static final Logger LOG = LoggerFactory.getLogger(IssueExclusionsRegexpScanner.class); - - private IssueExclusionPatternInitializer exclusionPatternInitializer; - private List<java.util.regex.Pattern> allFilePatterns; - private List<DoubleRegexpMatcher> blockMatchers; - - // fields to be reset at every new scan - private DoubleRegexpMatcher currentMatcher; - private int fileLength; - private List<LineExclusion> lineExclusions; - private LineExclusion currentLineExclusion; - - public IssueExclusionsRegexpScanner(IssueExclusionPatternInitializer patternsInitializer) { - this.exclusionPatternInitializer = patternsInitializer; - - lineExclusions = Lists.newArrayList(); - allFilePatterns = Lists.newArrayList(); - blockMatchers = Lists.newArrayList(); - - for (IssuePattern pattern : patternsInitializer.getAllFilePatterns()) { - allFilePatterns.add(java.util.regex.Pattern.compile(pattern.getAllFileRegexp())); - } - for (IssuePattern pattern : patternsInitializer.getBlockPatterns()) { - blockMatchers.add(new DoubleRegexpMatcher( - java.util.regex.Pattern.compile(pattern.getBeginBlockRegexp()), - java.util.regex.Pattern.compile(pattern.getEndBlockRegexp()))); - } - - init(); - } - - private void init() { - currentMatcher = null; - fileLength = 0; - lineExclusions.clear(); - currentLineExclusion = null; - } - - public void scan(String resource, File file, Charset sourcesEncoding) throws IOException { - LOG.debug("Scanning {}", resource); - init(); - - List<String> lines = FileUtils.readLines(file, sourcesEncoding.name()); - int lineIndex = 0; - for (String line : lines) { - lineIndex++; - if (line.trim().length() == 0) { - continue; - } - - // first check the single regexp patterns that can be used to totally exclude a file - for (java.util.regex.Pattern pattern : allFilePatterns) { - if (pattern.matcher(line).find()) { - exclusionPatternInitializer.getPatternMatcher().addPatternToExcludeResource(resource); - // nothing more to do on this file - LOG.debug("- Exclusion pattern '{}': every violation in this file will be ignored.", pattern); - return; - } - } - - // then check the double regexps if we're still here - checkDoubleRegexps(line, lineIndex); - } - - if (currentMatcher != null && !currentMatcher.hasSecondPattern()) { - // this will happen when there is a start block regexp but no end block regexp - endExclusion(lineIndex + 1); - } - - // now create the new line-based pattern for this file if there are exclusions - fileLength = lineIndex; - if (!lineExclusions.isEmpty()) { - Set<LineRange> lineRanges = convertLineExclusionsToLineRanges(); - LOG.debug("- Line exclusions found: {}", lineRanges); - exclusionPatternInitializer.getPatternMatcher().addPatternToExcludeLines(resource, lineRanges); - } - } - - private Set<LineRange> convertLineExclusionsToLineRanges() { - Set<LineRange> lineRanges = Sets.newHashSet(); - for (LineExclusion lineExclusion : lineExclusions) { - lineRanges.add(lineExclusion.toLineRange()); - } - return lineRanges; - } - - private void checkDoubleRegexps(String line, int lineIndex) { - if (currentMatcher == null) { - for (DoubleRegexpMatcher matcher : blockMatchers) { - if (matcher.matchesFirstPattern(line)) { - startExclusion(lineIndex); - currentMatcher = matcher; - break; - } - } - } else { - if (currentMatcher.matchesSecondPattern(line)) { - endExclusion(lineIndex); - currentMatcher = null; - } - } - } - - private void startExclusion(int lineIndex) { - currentLineExclusion = new LineExclusion(lineIndex); - lineExclusions.add(currentLineExclusion); - } - - private void endExclusion(int lineIndex) { - currentLineExclusion.setEnd(lineIndex); - currentLineExclusion = null; - } - - private class LineExclusion { - - private int start; - private int end; - - LineExclusion(int start) { - this.start = start; - this.end = -1; - } - - void setEnd(int end) { - this.end = end; - } - - public LineRange toLineRange() { - return new LineRange(start, end == -1 ? fileLength : end); - } - - } - - private static class DoubleRegexpMatcher { - - private java.util.regex.Pattern firstPattern; - private java.util.regex.Pattern secondPattern; - - DoubleRegexpMatcher(java.util.regex.Pattern firstPattern, java.util.regex.Pattern secondPattern) { - this.firstPattern = firstPattern; - this.secondPattern = secondPattern; - } - - boolean matchesFirstPattern(String line) { - return firstPattern.matcher(line).find(); - } - - boolean matchesSecondPattern(String line) { - return hasSecondPattern() && secondPattern.matcher(line).find(); - } - - boolean hasSecondPattern() { - return StringUtils.isNotEmpty(secondPattern.toString()); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/scanner/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/scanner/package-info.java deleted file mode 100644 index 3beb574f061..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/ignore/scanner/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.issue.ignore.scanner; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/issue/package-info.java deleted file mode 100644 index 75db5554d7f..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.issue; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/DefaultServerLineHashesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/DefaultServerLineHashesLoader.java deleted file mode 100644 index 954997466fe..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/DefaultServerLineHashesLoader.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.tracking; - -import org.sonar.batch.cache.WSLoader.LoadStrategy; - -import org.sonar.batch.cache.WSLoaderResult; -import org.sonar.batch.cache.WSLoader; -import org.apache.commons.lang.mutable.MutableBoolean; - -import javax.annotation.Nullable; - -import org.sonar.batch.util.BatchUtils; -import com.google.common.base.Splitter; -import com.google.common.collect.Iterators; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; - -public class DefaultServerLineHashesLoader implements ServerLineHashesLoader { - - private final WSLoader wsLoader; - - public DefaultServerLineHashesLoader(WSLoader wsLoader) { - this.wsLoader = wsLoader; - } - - @Override - public String[] getLineHashes(String fileKey, @Nullable MutableBoolean fromCache) { - String hashesFromWs = loadHashesFromWs(fileKey, fromCache); - return Iterators.toArray(Splitter.on('\n').split(hashesFromWs).iterator(), String.class); - } - - private String loadHashesFromWs(String fileKey, @Nullable MutableBoolean fromCache) { - Profiler profiler = Profiler.createIfDebug(Loggers.get(getClass())) - .addContext("file", fileKey) - .startDebug("Load line hashes"); - WSLoaderResult<String> result = wsLoader.loadString("/api/sources/hash?key=" + BatchUtils.encodeForUrl(fileKey), LoadStrategy.CACHE_FIRST); - try { - if (fromCache != null) { - fromCache.setValue(result.isFromCache()); - } - return result.get(); - } finally { - if (result.isFromCache()) { - profiler.stopDebug("Load line hashes (done from cache)"); - } else { - profiler.stopDebug(); - } - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/FileHashes.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/FileHashes.java deleted file mode 100644 index 3284b5a681b..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/FileHashes.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.tracking; - -import org.sonar.api.batch.fs.internal.FileMetadata; -import org.sonar.api.batch.fs.internal.FileMetadata.LineHashConsumer; - -import com.google.common.collect.LinkedHashMultimap; -import com.google.common.collect.Multimap; -import org.apache.commons.codec.binary.Hex; -import org.apache.commons.lang.ObjectUtils; -import org.sonar.api.batch.fs.internal.DefaultInputFile; - -import javax.annotation.Nullable; - -import java.util.Collection; - -/** - * Wraps a {@link Sequence} to assign hash codes to elements. - */ -public final class FileHashes { - - private final String[] hashes; - private final Multimap<String, Integer> linesByHash; - - private FileHashes(String[] hashes, Multimap<String, Integer> linesByHash) { - this.hashes = hashes; - this.linesByHash = linesByHash; - } - - public static FileHashes create(String[] hashes) { - int size = hashes.length; - Multimap<String, Integer> linesByHash = LinkedHashMultimap.create(); - for (int i = 0; i < size; i++) { - // indices in array are shifted one line before - linesByHash.put(hashes[i], i + 1); - } - return new FileHashes(hashes, linesByHash); - } - - public static FileHashes create(DefaultInputFile f) { - final byte[][] hashes = new byte[f.lines()][]; - FileMetadata.computeLineHashesForIssueTracking(f, new LineHashConsumer() { - - @Override - public void consume(int lineIdx, @Nullable byte[] hash) { - hashes[lineIdx - 1] = hash; - } - }); - - int size = hashes.length; - Multimap<String, Integer> linesByHash = LinkedHashMultimap.create(); - String[] hexHashes = new String[size]; - for (int i = 0; i < size; i++) { - String hash = hashes[i] != null ? Hex.encodeHexString(hashes[i]) : ""; - hexHashes[i] = hash; - // indices in array are shifted one line before - linesByHash.put(hash, i + 1); - } - return new FileHashes(hexHashes, linesByHash); - } - - public int length() { - return hashes.length; - } - - public Collection<Integer> getLinesForHash(String hash) { - return linesByHash.get(hash); - } - - public String[] hashes() { - return hashes; - } - - public String getHash(int line) { - // indices in array are shifted one line before - return (String) ObjectUtils.defaultIfNull(hashes[line - 1], ""); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueTrackingInput.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueTrackingInput.java deleted file mode 100644 index 031396b54d7..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueTrackingInput.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.tracking; - -import org.sonar.core.issue.tracking.Trackable; -import org.sonar.core.issue.tracking.BlockHashSequence; -import org.sonar.core.issue.tracking.LineHashSequence; - -import java.util.Collection; -import java.util.List; - -import org.sonar.core.issue.tracking.Input; - -public class IssueTrackingInput<T extends Trackable> implements Input<T> { - - private final Collection<T> issues; - private final LineHashSequence lineHashes; - private final BlockHashSequence blockHashes; - - public IssueTrackingInput(Collection<T> issues, List<String> hashes) { - this.issues = issues; - this.lineHashes = new LineHashSequence(hashes); - this.blockHashes = BlockHashSequence.create(lineHashes); - } - - @Override - public LineHashSequence getLineHashSequence() { - return lineHashes; - } - - @Override - public BlockHashSequence getBlockHashSequence() { - return blockHashes; - } - - @Override - public Collection<T> getIssues() { - return issues; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueTransition.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueTransition.java deleted file mode 100644 index fef29bfcb39..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/IssueTransition.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.tracking; - -import org.sonar.batch.util.ProgressReport; -import org.sonar.batch.issue.IssueTransformer; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.resources.Project; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.issue.IssueCache; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReportReader; -import org.sonar.batch.report.ReportPublisher; -import org.sonar.core.util.CloseableIterator; - -import javax.annotation.Nullable; - -import java.util.ArrayList; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; -import java.util.concurrent.TimeUnit; - -@BatchSide -public class IssueTransition { - private final IssueCache issueCache; - private final BatchComponentCache componentCache; - private final ReportPublisher reportPublisher; - private final Date analysisDate; - @Nullable - private final LocalIssueTracking localIssueTracking; - - public IssueTransition(BatchComponentCache componentCache, IssueCache issueCache, ReportPublisher reportPublisher, - @Nullable LocalIssueTracking localIssueTracking) { - this.componentCache = componentCache; - this.issueCache = issueCache; - this.reportPublisher = reportPublisher; - this.localIssueTracking = localIssueTracking; - this.analysisDate = ((Project) componentCache.getRoot().resource()).getAnalysisDate(); - } - - public IssueTransition(BatchComponentCache componentCache, IssueCache issueCache, ReportPublisher reportPublisher) { - this(componentCache, issueCache, reportPublisher, null); - } - - public void execute() { - if (localIssueTracking != null) { - localIssueTracking.init(); - } - - BatchReportReader reader = new BatchReportReader(reportPublisher.getReportDir()); - int nbComponents = componentCache.all().size(); - - if (nbComponents == 0) { - return; - } - - ProgressReport progressReport = new ProgressReport("issue-tracking-report", TimeUnit.SECONDS.toMillis(10)); - progressReport.start("Performing issue tracking"); - int count = 0; - - try { - for (BatchComponent component : componentCache.all()) { - trackIssues(reader, component); - count++; - progressReport.message(count + "/" + nbComponents + " components tracked"); - } - } finally { - progressReport.stop(count + "/" + nbComponents + " components tracked"); - } - } - - public void trackIssues(BatchReportReader reader, BatchComponent component) { - // raw issues = all the issues created by rule engines during this module scan and not excluded by filters - List<BatchReport.Issue> rawIssues = new LinkedList<>(); - try (CloseableIterator<BatchReport.Issue> it = reader.readComponentIssues(component.batchId())) { - while (it.hasNext()) { - rawIssues.add(it.next()); - } - } catch (Exception e) { - throw new IllegalStateException("Can't read issues for " + component.key(), e); - } - - List<TrackedIssue> trackedIssues; - if (localIssueTracking != null) { - trackedIssues = localIssueTracking.trackIssues(component, rawIssues, analysisDate); - } else { - trackedIssues = doTransition(rawIssues, component); - } - - for (TrackedIssue issue : trackedIssues) { - issueCache.put(issue); - } - } - - private static List<TrackedIssue> doTransition(List<BatchReport.Issue> rawIssues, BatchComponent component) { - List<TrackedIssue> issues = new ArrayList<>(rawIssues.size()); - - for (BatchReport.Issue issue : rawIssues) { - issues.add(IssueTransformer.toTrackedIssue(component, issue, null)); - } - - return issues; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/LocalIssueTracking.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/LocalIssueTracking.java deleted file mode 100644 index 5a6c739b956..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/LocalIssueTracking.java +++ /dev/null @@ -1,266 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.tracking; - -import org.sonar.core.issue.tracking.Tracking; -import org.sonar.core.issue.tracking.Input; -import org.sonar.core.issue.tracking.Tracker; -import org.sonar.batch.issue.IssueTransformer; -import org.sonar.api.batch.fs.InputFile.Status; -import org.sonar.batch.analysis.DefaultAnalysisMode; -import com.google.common.annotations.VisibleForTesting; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.rule.ActiveRule; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.issue.Issue; -import org.sonar.api.resources.ResourceUtils; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.repository.ProjectRepositories; - -@BatchSide -public class LocalIssueTracking { - private final Tracker<TrackedIssue, ServerIssueFromWs> tracker; - private final ServerLineHashesLoader lastLineHashes; - private final ActiveRules activeRules; - private final ServerIssueRepository serverIssueRepository; - private final DefaultAnalysisMode mode; - - private boolean hasServerAnalysis; - - public LocalIssueTracking(Tracker<TrackedIssue, ServerIssueFromWs> tracker, ServerLineHashesLoader lastLineHashes, - ActiveRules activeRules, ServerIssueRepository serverIssueRepository, ProjectRepositories projectRepositories, DefaultAnalysisMode mode) { - this.tracker = tracker; - this.lastLineHashes = lastLineHashes; - this.serverIssueRepository = serverIssueRepository; - this.mode = mode; - this.activeRules = activeRules; - this.hasServerAnalysis = projectRepositories.lastAnalysisDate() != null; - } - - public void init() { - if (hasServerAnalysis) { - serverIssueRepository.load(); - } - } - - public List<TrackedIssue> trackIssues(BatchComponent component, Collection<BatchReport.Issue> reportIssues, Date analysisDate) { - List<TrackedIssue> trackedIssues = new LinkedList<>(); - if (hasServerAnalysis) { - // all the issues that are not closed in db before starting this module scan, including manual issues - Collection<ServerIssueFromWs> serverIssues = loadServerIssues(component); - - if (shouldCopyServerIssues(component)) { - // raw issues should be empty, we just need to deal with server issues (SONAR-6931) - copyServerIssues(serverIssues, trackedIssues); - } else { - - SourceHashHolder sourceHashHolder = loadSourceHashes(component); - Collection<TrackedIssue> rIssues = IssueTransformer.toTrackedIssue(component, reportIssues, sourceHashHolder); - - Input<ServerIssueFromWs> baseIssues = createBaseInput(serverIssues, sourceHashHolder); - Input<TrackedIssue> rawIssues = createRawInput(rIssues, sourceHashHolder); - - Tracking<TrackedIssue, ServerIssueFromWs> track = tracker.track(rawIssues, baseIssues); - - addUnmatchedFromServer(track.getUnmatchedBases(), sourceHashHolder, trackedIssues); - addUnmatchedFromServer(track.getOpenManualIssuesByLine().values(), sourceHashHolder, trackedIssues); - mergeMatched(track, trackedIssues, rIssues); - addUnmatchedFromReport(track.getUnmatchedRaws(), trackedIssues, analysisDate); - } - } - - if (hasServerAnalysis && ResourceUtils.isRootProject(component.resource())) { - // issues that relate to deleted components - addIssuesOnDeletedComponents(trackedIssues); - } - - return trackedIssues; - } - - private static Input<ServerIssueFromWs> createBaseInput(Collection<ServerIssueFromWs> serverIssues, @Nullable SourceHashHolder sourceHashHolder) { - List<String> refHashes; - - if (sourceHashHolder != null && sourceHashHolder.getHashedReference() != null) { - refHashes = Arrays.asList(sourceHashHolder.getHashedReference().hashes()); - } else { - refHashes = new ArrayList<>(0); - } - - return new IssueTrackingInput<>(serverIssues, refHashes); - } - - private static Input<TrackedIssue> createRawInput(Collection<TrackedIssue> rIssues, @Nullable SourceHashHolder sourceHashHolder) { - List<String> baseHashes; - if (sourceHashHolder != null && sourceHashHolder.getHashedSource() != null) { - baseHashes = Arrays.asList(sourceHashHolder.getHashedSource().hashes()); - } else { - baseHashes = new ArrayList<>(0); - } - - return new IssueTrackingInput<>(rIssues, baseHashes); - } - - private boolean shouldCopyServerIssues(BatchComponent component) { - if (!mode.scanAllFiles() && component.isFile()) { - DefaultInputFile inputFile = (DefaultInputFile) component.inputComponent(); - if (inputFile.status() == Status.SAME) { - return true; - } - } - return false; - } - - private void copyServerIssues(Collection<ServerIssueFromWs> serverIssues, List<TrackedIssue> trackedIssues) { - for (ServerIssueFromWs serverIssue : serverIssues) { - org.sonar.batch.protocol.input.BatchInput.ServerIssue unmatchedPreviousIssue = serverIssue.getDto(); - TrackedIssue unmatched = IssueTransformer.toTrackedIssue(unmatchedPreviousIssue); - - ActiveRule activeRule = activeRules.find(unmatched.getRuleKey()); - unmatched.setNew(false); - - if (activeRule == null) { - // rule removed - IssueTransformer.resolveRemove(unmatched); - } - - trackedIssues.add(unmatched); - } - } - - @CheckForNull - private SourceHashHolder loadSourceHashes(BatchComponent component) { - SourceHashHolder sourceHashHolder = null; - if (component.isFile()) { - DefaultInputFile file = (DefaultInputFile) component.inputComponent(); - if (file == null) { - throw new IllegalStateException("Resource " + component.resource() + " was not found in InputPath cache"); - } - sourceHashHolder = new SourceHashHolder(file, lastLineHashes); - } - return sourceHashHolder; - } - - private Collection<ServerIssueFromWs> loadServerIssues(BatchComponent component) { - Collection<ServerIssueFromWs> serverIssues = new ArrayList<>(); - for (org.sonar.batch.protocol.input.BatchInput.ServerIssue previousIssue : serverIssueRepository.byComponent(component)) { - serverIssues.add(new ServerIssueFromWs(previousIssue)); - } - return serverIssues; - } - - @VisibleForTesting - protected void mergeMatched(Tracking<TrackedIssue, ServerIssueFromWs> result, Collection<TrackedIssue> mergeTo, Collection<TrackedIssue> rawIssues) { - for (Map.Entry<TrackedIssue, ServerIssueFromWs> e : result.getMatchedRaws().entrySet()) { - org.sonar.batch.protocol.input.BatchInput.ServerIssue dto = e.getValue().getDto(); - TrackedIssue tracked = e.getKey(); - - // invariant fields - tracked.setKey(dto.getKey()); - - // non-persisted fields - tracked.setNew(false); - - // fields to update with old values - tracked.setResolution(dto.hasResolution() ? dto.getResolution() : null); - tracked.setStatus(dto.getStatus()); - tracked.setAssignee(dto.hasAssigneeLogin() ? dto.getAssigneeLogin() : null); - tracked.setCreationDate(new Date(dto.getCreationDate())); - - if (dto.getManualSeverity()) { - // Severity overriden by user - tracked.setSeverity(dto.getSeverity().name()); - } - mergeTo.add(tracked); - } - } - - private void addUnmatchedFromServer(Iterable<ServerIssueFromWs> unmatchedIssues, SourceHashHolder sourceHashHolder, Collection<TrackedIssue> mergeTo) { - for (ServerIssueFromWs unmatchedIssue : unmatchedIssues) { - org.sonar.batch.protocol.input.BatchInput.ServerIssue unmatchedPreviousIssue = unmatchedIssue.getDto(); - TrackedIssue unmatched = IssueTransformer.toTrackedIssue(unmatchedPreviousIssue); - if (unmatchedIssue.getRuleKey().isManual() && !Issue.STATUS_CLOSED.equals(unmatchedPreviousIssue.getStatus())) { - relocateManualIssue(unmatched, unmatchedIssue, sourceHashHolder); - } - updateUnmatchedIssue(unmatched, false /* manual issues can be kept open */); - mergeTo.add(unmatched); - } - } - - private static void addUnmatchedFromReport(Iterable<TrackedIssue> rawIssues, Collection<TrackedIssue> trackedIssues, Date analysisDate) { - for (TrackedIssue rawIssue : rawIssues) { - rawIssue.setCreationDate(analysisDate); - trackedIssues.add(rawIssue); - } - } - - private void addIssuesOnDeletedComponents(Collection<TrackedIssue> issues) { - for (org.sonar.batch.protocol.input.BatchInput.ServerIssue previous : serverIssueRepository.issuesOnMissingComponents()) { - TrackedIssue dead = IssueTransformer.toTrackedIssue(previous); - updateUnmatchedIssue(dead, true); - issues.add(dead); - } - } - - private void updateUnmatchedIssue(TrackedIssue issue, boolean forceEndOfLife) { - ActiveRule activeRule = activeRules.find(issue.getRuleKey()); - issue.setNew(false); - - boolean manualIssue = issue.getRuleKey().isManual(); - boolean isRemovedRule = activeRule == null; - - if (isRemovedRule) { - IssueTransformer.resolveRemove(issue); - } else if (forceEndOfLife || !manualIssue) { - IssueTransformer.close(issue); - } - } - - private static void relocateManualIssue(TrackedIssue newIssue, ServerIssueFromWs oldIssue, SourceHashHolder sourceHashHolder) { - Integer previousLine = oldIssue.getLine(); - if (previousLine == null) { - return; - } - - Collection<Integer> newLinesWithSameHash = sourceHashHolder.getNewLinesMatching(previousLine); - if (newLinesWithSameHash.isEmpty()) { - if (previousLine > sourceHashHolder.getHashedSource().length()) { - IssueTransformer.resolveRemove(newIssue); - } - } else if (newLinesWithSameHash.size() == 1) { - Integer newLine = newLinesWithSameHash.iterator().next(); - newIssue.setStartLine(newLine); - newIssue.setEndLine(newLine); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/RollingFileHashes.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/RollingFileHashes.java deleted file mode 100644 index aaf2ba7bdf5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/RollingFileHashes.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.tracking; - -/** - * Compute hashes of block around each line - */ -public class RollingFileHashes { - - final int[] rollingHashes; - - private RollingFileHashes(int[] hashes) { - this.rollingHashes = hashes; - } - - public static RollingFileHashes create(FileHashes hashes, int halfBlockSize) { - int size = hashes.length(); - int[] rollingHashes = new int[size]; - - RollingHashCalculator hashCalulator = new RollingHashCalculator(halfBlockSize * 2 + 1); - for (int i = 1; i <= Math.min(size, halfBlockSize + 1); i++) { - hashCalulator.add(hashes.getHash(i).hashCode()); - } - for (int i = 1; i <= size; i++) { - rollingHashes[i - 1] = hashCalulator.getHash(); - if (i - halfBlockSize > 0) { - hashCalulator.remove(hashes.getHash(i - halfBlockSize).hashCode()); - } - if (i + 1 + halfBlockSize <= size) { - hashCalulator.add(hashes.getHash(i + 1 + halfBlockSize).hashCode()); - } else { - hashCalulator.add(0); - } - } - - return new RollingFileHashes(rollingHashes); - } - - public int getHash(int line) { - return rollingHashes[line - 1]; - } - - private static class RollingHashCalculator { - - private static final int PRIME_BASE = 31; - - private final int power; - private int hash; - - public RollingHashCalculator(int size) { - int pow = 1; - for (int i = 0; i < size - 1; i++) { - pow = pow * PRIME_BASE; - } - this.power = pow; - } - - public void add(int value) { - hash = hash * PRIME_BASE + value; - } - - public void remove(int value) { - hash = hash - power * value; - } - - public int getHash() { - return hash; - } - - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerIssueFromWs.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerIssueFromWs.java deleted file mode 100644 index bbc68cb1455..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerIssueFromWs.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.tracking; - -import javax.annotation.CheckForNull; - -import org.sonar.core.issue.tracking.Trackable; -import org.sonar.api.rule.RuleKey; - -public class ServerIssueFromWs implements Trackable { - - private org.sonar.batch.protocol.input.BatchInput.ServerIssue dto; - - public ServerIssueFromWs(org.sonar.batch.protocol.input.BatchInput.ServerIssue dto) { - this.dto = dto; - } - - public org.sonar.batch.protocol.input.BatchInput.ServerIssue getDto() { - return dto; - } - - public String key() { - return dto.getKey(); - } - - @Override - public RuleKey getRuleKey() { - return RuleKey.of(dto.getRuleRepository(), dto.getRuleKey()); - } - - @Override - @CheckForNull - public String getLineHash() { - return dto.hasChecksum() ? dto.getChecksum() : null; - } - - @Override - @CheckForNull - public Integer getLine() { - return dto.hasLine() ? dto.getLine() : null; - } - - @Override - public String getMessage() { - return dto.hasMsg() ? dto.getMsg() : ""; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerIssueRepository.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerIssueRepository.java deleted file mode 100644 index eb864b04c61..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerIssueRepository.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.tracking; - -import com.google.common.base.Function; -import javax.annotation.Nullable; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.InstantiationStrategy; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.index.Cache; -import org.sonar.batch.index.Caches; -import org.sonar.batch.protocol.input.BatchInput.ServerIssue; -import org.sonar.batch.repository.ServerIssuesLoader; -import org.sonar.batch.scan.ImmutableProjectReactor; -import org.sonar.core.component.ComponentKeys; - -@InstantiationStrategy(InstantiationStrategy.PER_BATCH) -@BatchSide -public class ServerIssueRepository { - - private static final Logger LOG = Loggers.get(ServerIssueRepository.class); - private static final String LOG_MSG = "Load server issues"; - - private final Caches caches; - private Cache<ServerIssue> issuesCache; - private final ServerIssuesLoader previousIssuesLoader; - private final ImmutableProjectReactor reactor; - private final BatchComponentCache resourceCache; - - public ServerIssueRepository(Caches caches, ServerIssuesLoader previousIssuesLoader, ImmutableProjectReactor reactor, BatchComponentCache resourceCache) { - this.caches = caches; - this.previousIssuesLoader = previousIssuesLoader; - this.reactor = reactor; - this.resourceCache = resourceCache; - } - - public void load() { - Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - this.issuesCache = caches.createCache("previousIssues"); - caches.registerValueCoder(ServerIssue.class, new ServerIssueValueCoder()); - boolean fromCache = previousIssuesLoader.load(reactor.getRoot().getKeyWithBranch(), new SaveIssueConsumer()); - profiler.stopInfo(fromCache); - } - - public Iterable<ServerIssue> byComponent(BatchComponent component) { - return issuesCache.values(component.batchId()); - } - - private class SaveIssueConsumer implements Function<ServerIssue, Void> { - - @Override - public Void apply(@Nullable ServerIssue issue) { - if (issue == null) { - return null; - } - String componentKey = ComponentKeys.createEffectiveKey(issue.getModuleKey(), issue.hasPath() ? issue.getPath() : null); - BatchComponent r = resourceCache.get(componentKey); - if (r == null) { - // Deleted resource - issuesCache.put(0, issue.getKey(), issue); - } else { - issuesCache.put(r.batchId(), issue.getKey(), issue); - } - return null; - } - } - - public Iterable<ServerIssue> issuesOnMissingComponents() { - return issuesCache.values(0); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerIssueValueCoder.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerIssueValueCoder.java deleted file mode 100644 index c3c6f26b451..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerIssueValueCoder.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.tracking; - -import com.persistit.Value; -import com.persistit.encoding.CoderContext; -import com.persistit.encoding.ValueCoder; -import org.sonar.batch.protocol.input.BatchInput.ServerIssue; - -import java.io.IOException; - -public class ServerIssueValueCoder implements ValueCoder { - - @Override - public void put(Value value, Object object, CoderContext context) { - ServerIssue issue = (ServerIssue) object; - value.putByteArray(issue.toByteArray()); - } - - @Override - public Object get(Value value, Class<?> clazz, CoderContext context) { - try { - return ServerIssue.parseFrom(value.getByteArray()); - } catch (IOException e) { - throw new IllegalStateException("Unable to read issue from cache", e); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerLineHashesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerLineHashesLoader.java deleted file mode 100644 index fb0b32d0d2b..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/ServerLineHashesLoader.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.tracking; - -import org.apache.commons.lang.mutable.MutableBoolean; - -import javax.annotation.Nullable; - -import org.sonar.api.batch.BatchSide; - -@BatchSide -public interface ServerLineHashesLoader { - - String[] getLineHashes(String fileKey, @Nullable MutableBoolean fromCache); -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/SourceHashHolder.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/SourceHashHolder.java deleted file mode 100644 index 583a92f0b9d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/SourceHashHolder.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.tracking; - -import com.google.common.collect.ImmutableSet; -import org.sonar.api.batch.fs.InputFile.Status; -import org.sonar.api.batch.fs.internal.DefaultInputFile; - -import javax.annotation.CheckForNull; - -import java.util.Collection; - -public class SourceHashHolder { - - private final ServerLineHashesLoader lastSnapshots; - - private FileHashes hashedReference; - private FileHashes hashedSource; - private DefaultInputFile inputFile; - - public SourceHashHolder(DefaultInputFile inputFile, ServerLineHashesLoader lastSnapshots) { - this.inputFile = inputFile; - this.lastSnapshots = lastSnapshots; - } - - private void initHashes() { - if (hashedSource == null) { - hashedSource = FileHashes.create(inputFile); - Status status = inputFile.status(); - if (status == Status.ADDED) { - hashedReference = null; - } else if (status == Status.SAME) { - hashedReference = hashedSource; - } else { - String[] lineHashes = lastSnapshots.getLineHashes(inputFile.key(), null); - hashedReference = lineHashes != null ? FileHashes.create(lineHashes) : null; - } - } - } - - @CheckForNull - public FileHashes getHashedReference() { - initHashes(); - return hashedReference; - } - - public FileHashes getHashedSource() { - initHashes(); - return hashedSource; - } - - public Collection<Integer> getNewLinesMatching(Integer originLine) { - FileHashes reference = getHashedReference(); - if (reference == null) { - return ImmutableSet.of(); - } else { - return getHashedSource().getLinesForHash(reference.getHash(originLine)); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/TrackedIssue.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/TrackedIssue.java deleted file mode 100644 index ea303bf6a4f..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/TrackedIssue.java +++ /dev/null @@ -1,262 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.issue.tracking; - -import com.google.common.base.Preconditions; -import java.io.Serializable; -import java.util.Date; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import org.sonar.api.rule.RuleKey; -import org.sonar.core.issue.tracking.Trackable; - -public class TrackedIssue implements Trackable, Serializable { - private static final long serialVersionUID = -1755017079070964287L; - - private RuleKey ruleKey; - private String key; - private String severity; - private Integer startLine; - private Integer startLineOffset; - private Integer endLine; - private Integer endLineOffset; - private Double gap; - private boolean isNew; - private Date creationDate; - private String resolution; - private String status; - private String assignee; - private String reporter; - private String componentKey; - private String message; - - private transient FileHashes hashes; - - public TrackedIssue() { - hashes = null; - } - - public TrackedIssue(@Nullable FileHashes hashes) { - this.hashes = hashes; - } - - @Override - @CheckForNull - public String getLineHash() { - if (getLine() == null || hashes == null) { - return null; - } - - int line = getLine(); - Preconditions.checkState(line <= hashes.length(), "Invalid line number for issue %s. File has only %s line(s)", this, hashes.length()); - - return hashes.getHash(line); - } - - @Override - public String getMessage() { - return message; - } - - public TrackedIssue setMessage(String message) { - this.message = message; - return this; - } - - public String componentKey() { - return componentKey; - } - - public TrackedIssue setComponentKey(String componentKey) { - this.componentKey = componentKey; - return this; - } - - public String key() { - return key; - } - - public Integer startLine() { - return startLine; - } - - @Override - public Integer getLine() { - return startLine; - } - - public TrackedIssue setStartLine(Integer startLine) { - this.startLine = startLine; - return this; - } - - public Integer startLineOffset() { - return startLineOffset; - } - - public TrackedIssue setStartLineOffset(Integer startLineOffset) { - this.startLineOffset = startLineOffset; - return this; - } - - public Integer endLine() { - return endLine; - } - - public TrackedIssue setEndLine(Integer endLine) { - this.endLine = endLine; - return this; - } - - public Integer endLineOffset() { - return endLineOffset; - } - - public TrackedIssue setEndLineOffset(Integer endLineOffset) { - this.endLineOffset = endLineOffset; - return this; - } - - public TrackedIssue setKey(String key) { - this.key = key; - return this; - } - - public String assignee() { - return assignee; - } - - public TrackedIssue setAssignee(String assignee) { - this.assignee = assignee; - return this; - } - - public String reporter() { - return reporter; - } - - public TrackedIssue setReporter(String reporter) { - this.reporter = reporter; - return this; - } - - public String resolution() { - return resolution; - } - - public TrackedIssue setResolution(String resolution) { - this.resolution = resolution; - return this; - } - - public String status() { - return status; - } - - public TrackedIssue setStatus(String status) { - this.status = status; - return this; - } - - @Override - public RuleKey getRuleKey() { - return ruleKey; - } - - public String severity() { - return severity; - } - - public Double gap() { - return gap; - } - - public Date getCreationDate() { - return creationDate; - } - - public boolean isNew() { - return isNew; - } - - public TrackedIssue setNew(boolean isNew) { - this.isNew = isNew; - return this; - } - - public Date creationDate() { - return creationDate; - } - - public TrackedIssue setCreationDate(Date creationDate) { - this.creationDate = creationDate; - return this; - } - - public TrackedIssue setRuleKey(RuleKey ruleKey) { - this.ruleKey = ruleKey; - return this; - } - - public TrackedIssue setSeverity(String severity) { - this.severity = severity; - return this; - } - - public TrackedIssue setGap(Double gap) { - this.gap = gap; - return this; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((key == null) ? 0 : key.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - TrackedIssue other = (TrackedIssue) obj; - if (key == null) { - if (other.key != null) { - return false; - } - } else if (!key.equals(other.key)) { - return false; - } - return true; - } - - @Override - public String toString() { - return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/package-info.java deleted file mode 100644 index 148dc77f0f4..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/tracking/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.issue.tracking; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/FakePluginInstaller.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/FakePluginInstaller.java deleted file mode 100644 index 71205c7b08d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/FakePluginInstaller.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.mediumtest; - -import java.io.File; -import java.util.HashMap; -import java.util.Map; -import org.sonar.api.SonarPlugin; -import org.sonar.batch.bootstrap.PluginInstaller; -import org.sonar.core.platform.PluginInfo; - -public class FakePluginInstaller implements PluginInstaller { - public static final String MEDIUM_TEST_ENABLED = "sonar.mediumTest.enabled"; - - private final Map<String, PluginInfo> infosByKeys = new HashMap<>(); - private final Map<String, SonarPlugin> instancesByKeys = new HashMap<>(); - - public FakePluginInstaller add(String pluginKey, File jarFile) { - infosByKeys.put(pluginKey, PluginInfo.create(jarFile)); - return this; - } - - public FakePluginInstaller add(String pluginKey, SonarPlugin instance) { - instancesByKeys.put(pluginKey, instance); - return this; - } - - @Override - public Map<String, PluginInfo> installRemotes() { - return infosByKeys; - } - - @Override - public Map<String, SonarPlugin> installLocals() { - return instancesByKeys; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/ScanTaskObserver.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/ScanTaskObserver.java deleted file mode 100644 index 05b9f338eef..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/ScanTaskObserver.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.mediumtest; - -import org.sonar.api.batch.BatchSide; -import org.sonar.api.ExtensionPoint; -import org.sonar.batch.scan.ProjectScanContainer; - -@BatchSide -@ExtensionPoint -public interface ScanTaskObserver { - - void scanTaskCompleted(ProjectScanContainer container); - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/ScanTaskObservers.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/ScanTaskObservers.java deleted file mode 100644 index 90e092cad77..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/ScanTaskObservers.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.mediumtest; - -import org.sonar.batch.scan.ProjectScanContainer; - -public class ScanTaskObservers { - - private ScanTaskObserver[] observers; - private ProjectScanContainer projectScanContainer; - - public ScanTaskObservers(ProjectScanContainer projectScanContainer, ScanTaskObserver... observers) { - this.projectScanContainer = projectScanContainer; - this.observers = observers; - } - - public ScanTaskObservers(ProjectScanContainer projectScanContainer) { - this(projectScanContainer, new ScanTaskObserver[0]); - } - - public void notifyEndOfScanTask() { - for (ScanTaskObserver scanTaskObserver : observers) { - scanTaskObserver.scanTaskCompleted(projectScanContainer); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/TaskResult.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/TaskResult.java deleted file mode 100644 index 04d2b8843e7..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/TaskResult.java +++ /dev/null @@ -1,292 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.mediumtest; - -import com.google.common.collect.Iterators; -import com.google.common.collect.Lists; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.apache.commons.io.FileUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.fs.InputComponent; -import org.sonar.api.batch.fs.InputDir; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextPointer; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.sensor.highlighting.TypeOfText; -import org.sonar.batch.issue.IssueCache; -import org.sonar.batch.issue.tracking.TrackedIssue; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReport.Component; -import org.sonar.batch.protocol.output.BatchReport.Metadata; -import org.sonar.batch.protocol.output.BatchReport.Symbol; -import org.sonar.batch.protocol.output.BatchReportReader; -import org.sonar.batch.report.BatchReportUtils; -import org.sonar.batch.report.ReportPublisher; -import org.sonar.batch.scan.ProjectScanContainer; -import org.sonar.batch.scan.filesystem.InputPathCache; -import org.sonar.core.util.CloseableIterator; - -public class TaskResult implements org.sonar.batch.mediumtest.ScanTaskObserver { - - private static final Logger LOG = LoggerFactory.getLogger(TaskResult.class); - - private List<TrackedIssue> issues = new ArrayList<>(); - private Map<String, InputFile> inputFiles = new HashMap<>(); - private Map<String, Component> reportComponents = new HashMap<>(); - private Map<String, InputDir> inputDirs = new HashMap<>(); - private BatchReportReader reader; - - @Override - public void scanTaskCompleted(ProjectScanContainer container) { - LOG.info("Store analysis results in memory for later assertions in medium test"); - for (TrackedIssue issue : container.getComponentByType(IssueCache.class).all()) { - issues.add(issue); - } - - ReportPublisher reportPublisher = container.getComponentByType(ReportPublisher.class); - reader = new BatchReportReader(reportPublisher.getReportDir()); - if (!container.getComponentByType(AnalysisMode.class).isIssues()) { - Metadata readMetadata = getReportReader().readMetadata(); - int rootComponentRef = readMetadata.getRootComponentRef(); - storeReportComponents(rootComponentRef, null, readMetadata.hasBranch() ? readMetadata.getBranch() : null); - } - - storeFs(container); - - } - - private void storeReportComponents(int componentRef, String parentModuleKey, @Nullable String branch) { - Component component = getReportReader().readComponent(componentRef); - if (component.hasKey()) { - reportComponents.put(component.getKey() + (branch != null ? ":" + branch : ""), component); - } else { - reportComponents.put(parentModuleKey + (branch != null ? ":" + branch : "") + ":" + component.getPath(), component); - } - for (int childId : component.getChildRefList()) { - storeReportComponents(childId, component.hasKey() ? component.getKey() : parentModuleKey, branch); - } - - } - - public BatchReportReader getReportReader() { - return reader; - } - - private void storeFs(ProjectScanContainer container) { - InputPathCache inputFileCache = container.getComponentByType(InputPathCache.class); - for (InputFile inputPath : inputFileCache.allFiles()) { - inputFiles.put(inputPath.relativePath(), inputPath); - } - for (InputDir inputPath : inputFileCache.allDirs()) { - inputDirs.put(inputPath.relativePath(), inputPath); - } - } - - public List<TrackedIssue> trackedIssues() { - return issues; - } - - public Component getReportComponent(String key) { - return reportComponents.get(key); - } - - public List<BatchReport.Issue> issuesFor(InputComponent inputComponent) { - int ref = reportComponents.get(inputComponent.key()).getRef(); - return issuesFor(ref); - } - - public List<BatchReport.Issue> issuesFor(Component reportComponent) { - int ref = reportComponent.getRef(); - return issuesFor(ref); - } - - private List<BatchReport.Issue> issuesFor(int ref) { - List<BatchReport.Issue> result = Lists.newArrayList(); - try (CloseableIterator<BatchReport.Issue> it = reader.readComponentIssues(ref)) { - while (it.hasNext()) { - result.add(it.next()); - } - } - return result; - } - - public Collection<InputFile> inputFiles() { - return inputFiles.values(); - } - - @CheckForNull - public InputFile inputFile(String relativePath) { - return inputFiles.get(relativePath); - } - - public Collection<InputDir> inputDirs() { - return inputDirs.values(); - } - - @CheckForNull - public InputDir inputDir(String relativePath) { - return inputDirs.get(relativePath); - } - - public Map<String, List<BatchReport.Measure>> allMeasures() { - Map<String, List<BatchReport.Measure>> result = new HashMap<>(); - for (Map.Entry<String, Component> component : reportComponents.entrySet()) { - List<BatchReport.Measure> measures = new ArrayList<>(); - try (CloseableIterator<BatchReport.Measure> it = reader.readComponentMeasures(component.getValue().getRef())) { - Iterators.addAll(measures, it); - } - result.put(component.getKey(), measures); - } - return result; - } - - /** - * Get highlighting types at a given position in an inputfile - * @param lineOffset 0-based offset in file - */ - public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) { - int ref = reportComponents.get(((DefaultInputFile) file).key()).getRef(); - if (!reader.hasSyntaxHighlighting(ref)) { - return Collections.emptyList(); - } - TextPointer pointer = file.newPointer(line, lineOffset); - List<TypeOfText> result = new ArrayList<>(); - try (CloseableIterator<BatchReport.SyntaxHighlighting> it = reader.readComponentSyntaxHighlighting(ref)) { - while (it.hasNext()) { - BatchReport.SyntaxHighlighting rule = it.next(); - TextRange ruleRange = toRange(file, rule.getRange()); - if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) { - result.add(BatchReportUtils.toBatchType(rule.getType())); - } - } - } catch (Exception e) { - throw new IllegalStateException("Can't read syntax highlighting for " + file.absolutePath(), e); - } - return result; - } - - private static TextRange toRange(InputFile file, BatchReport.TextRange reportRange) { - return file.newRange(file.newPointer(reportRange.getStartLine(), reportRange.getStartOffset()), file.newPointer(reportRange.getEndLine(), reportRange.getEndOffset())); - } - - /** - * Get list of all start positions of a symbol in an inputfile - * @param symbolStartLine 0-based start offset for the symbol in file - * @param symbolStartLineOffset 0-based end offset for the symbol in file - */ - @CheckForNull - public List<BatchReport.TextRange> symbolReferencesFor(InputFile file, int symbolStartLine, int symbolStartLineOffset) { - int ref = reportComponents.get(((DefaultInputFile) file).key()).getRef(); - try (CloseableIterator<Symbol> symbols = getReportReader().readComponentSymbols(ref)) { - while (symbols.hasNext()) { - Symbol symbol = symbols.next(); - if (symbol.getDeclaration().getStartLine() == symbolStartLine && symbol.getDeclaration().getStartOffset() == symbolStartLineOffset) { - return symbol.getReferenceList(); - } - } - } - return Collections.emptyList(); - } - - public List<BatchReport.Duplication> duplicationsFor(InputFile file) { - List<BatchReport.Duplication> result = new ArrayList<>(); - int ref = reportComponents.get(((DefaultInputFile) file).key()).getRef(); - try (CloseableIterator<BatchReport.Duplication> it = getReportReader().readComponentDuplications(ref)) { - while (it.hasNext()) { - result.add(it.next()); - } - } catch (Exception e) { - throw new IllegalStateException(e); - } - return result; - } - - public List<BatchReport.CpdTextBlock> duplicationBlocksFor(InputFile file) { - List<BatchReport.CpdTextBlock> result = new ArrayList<>(); - int ref = reportComponents.get(((DefaultInputFile) file).key()).getRef(); - try (CloseableIterator<BatchReport.CpdTextBlock> it = getReportReader().readCpdTextBlocks(ref)) { - while (it.hasNext()) { - result.add(it.next()); - } - } catch (Exception e) { - throw new IllegalStateException(e); - } - return result; - } - - @CheckForNull - public BatchReport.Coverage coverageFor(InputFile file, int line) { - int ref = reportComponents.get(((DefaultInputFile) file).key()).getRef(); - try (CloseableIterator<BatchReport.Coverage> it = getReportReader().readComponentCoverage(ref)) { - while (it.hasNext()) { - BatchReport.Coverage coverage = it.next(); - if (coverage.getLine() == line) { - return coverage; - } - } - } catch (Exception e) { - throw new IllegalStateException(e); - } - return null; - } - - public BatchReport.Test testExecutionFor(InputFile testFile, String testName) { - int ref = reportComponents.get(((DefaultInputFile) testFile).key()).getRef(); - try (InputStream inputStream = FileUtils.openInputStream(getReportReader().readTests(ref))) { - BatchReport.Test test = BatchReport.Test.PARSER.parseDelimitedFrom(inputStream); - while (test != null) { - if (test.getName().equals(testName)) { - return test; - } - test = BatchReport.Test.PARSER.parseDelimitedFrom(inputStream); - } - } catch (Exception e) { - throw new IllegalStateException(e); - } - return null; - } - - public BatchReport.CoverageDetail coveragePerTestFor(InputFile testFile, String testName) { - int ref = reportComponents.get(((DefaultInputFile) testFile).key()).getRef(); - try (InputStream inputStream = FileUtils.openInputStream(getReportReader().readCoverageDetails(ref))) { - BatchReport.CoverageDetail details = BatchReport.CoverageDetail.PARSER.parseDelimitedFrom(inputStream); - while (details != null) { - if (details.getTestName().equals(testName)) { - return details; - } - details = BatchReport.CoverageDetail.PARSER.parseDelimitedFrom(inputStream); - } - } catch (Exception e) { - throw new IllegalStateException(e); - } - return null; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/package-info.java deleted file mode 100644 index 2cc10ab66ff..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.mediumtest; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/package-info.java deleted file mode 100644 index 408dd9f93a6..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/AbstractPhaseEvent.java b/sonar-batch/src/main/java/org/sonar/batch/phases/AbstractPhaseEvent.java deleted file mode 100644 index df8bc690534..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/AbstractPhaseEvent.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.sonar.api.batch.events.EventHandler; -import org.sonar.batch.events.BatchEvent; - -public abstract class AbstractPhaseEvent<H extends EventHandler> extends BatchEvent<H> { - - private final boolean start; - - public AbstractPhaseEvent(boolean start) { - this.start = start; - } - - public final boolean isStart() { - return start; - } - - public final boolean isEnd() { - return !start; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/AbstractPhaseExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/AbstractPhaseExecutor.java deleted file mode 100644 index c388b75f1f3..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/AbstractPhaseExecutor.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.sonar.api.batch.SensorContext; -import org.sonar.api.resources.Project; -import org.sonar.batch.events.BatchStepEvent; -import org.sonar.batch.events.EventBus; -import org.sonar.batch.index.DefaultIndex; -import org.sonar.batch.issue.ignore.scanner.IssueExclusionsLoader; -import org.sonar.batch.rule.QProfileVerifier; -import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; -import org.sonar.batch.scan.filesystem.FileSystemLogger; - -public abstract class AbstractPhaseExecutor { - - private final EventBus eventBus; - private final PostJobsExecutor postJobsExecutor; - private final InitializersExecutor initializersExecutor; - private final SensorsExecutor sensorsExecutor; - private final SensorContext sensorContext; - private final DefaultIndex index; - private final ProjectInitializer pi; - private final FileSystemLogger fsLogger; - private final DefaultModuleFileSystem fs; - private final QProfileVerifier profileVerifier; - private final IssueExclusionsLoader issueExclusionsLoader; - - public AbstractPhaseExecutor(InitializersExecutor initializersExecutor, PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor, - SensorContext sensorContext, DefaultIndex index, - EventBus eventBus, ProjectInitializer pi, - FileSystemLogger fsLogger, DefaultModuleFileSystem fs, QProfileVerifier profileVerifier, - IssueExclusionsLoader issueExclusionsLoader) { - this.postJobsExecutor = postJobsExecutor; - this.initializersExecutor = initializersExecutor; - this.sensorsExecutor = sensorsExecutor; - this.sensorContext = sensorContext; - this.index = index; - this.eventBus = eventBus; - this.pi = pi; - this.fsLogger = fsLogger; - this.fs = fs; - this.profileVerifier = profileVerifier; - this.issueExclusionsLoader = issueExclusionsLoader; - } - - /** - * Executed on each module - */ - public final void execute(Project module) { - pi.execute(module); - - eventBus.fireEvent(new ProjectAnalysisEvent(module, true)); - - executeInitializersPhase(); - - // Index and lock the filesystem - indexFs(); - - // Log detected languages and their profiles after FS is indexed and languages detected - profileVerifier.execute(); - - // Initialize issue exclusions - initIssueExclusions(); - - sensorsExecutor.execute(sensorContext); - - if (module.isRoot()) { - executeOnRoot(); - postJobsExecutor.execute(sensorContext); - } - cleanMemory(); - eventBus.fireEvent(new ProjectAnalysisEvent(module, false)); - } - - protected abstract void executeOnRoot(); - - private void initIssueExclusions() { - String stepName = "Init issue exclusions"; - eventBus.fireEvent(new BatchStepEvent(stepName, true)); - issueExclusionsLoader.execute(); - eventBus.fireEvent(new BatchStepEvent(stepName, false)); - } - - private void indexFs() { - String stepName = "Index filesystem"; - eventBus.fireEvent(new BatchStepEvent(stepName, true)); - fs.index(); - eventBus.fireEvent(new BatchStepEvent(stepName, false)); - } - - private void executeInitializersPhase() { - initializersExecutor.execute(); - fsLogger.log(); - } - - private void cleanMemory() { - String cleanMemory = "Clean memory"; - eventBus.fireEvent(new BatchStepEvent(cleanMemory, true)); - index.clear(); - eventBus.fireEvent(new BatchStepEvent(cleanMemory, false)); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/InitializerExecutionEvent.java b/sonar-batch/src/main/java/org/sonar/batch/phases/InitializerExecutionEvent.java deleted file mode 100644 index 9a8bdc92706..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/InitializerExecutionEvent.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.sonar.api.batch.Initializer; -import org.sonar.api.batch.events.InitializerExecutionHandler; - -class InitializerExecutionEvent extends AbstractPhaseEvent<InitializerExecutionHandler> - implements org.sonar.api.batch.events.InitializerExecutionHandler.InitializerExecutionEvent { - - private final Initializer initializer; - - InitializerExecutionEvent(Initializer initializer, boolean start) { - super(start); - this.initializer = initializer; - } - - @Override - public Initializer getInitializer() { - return initializer; - } - - @Override - public void dispatch(InitializerExecutionHandler handler) { - handler.onInitializerExecution(this); - } - - @Override - public Class getType() { - return InitializerExecutionHandler.class; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java deleted file mode 100644 index 591969ea1b0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import com.google.common.collect.Lists; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.batch.Initializer; -import org.sonar.api.resources.Project; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; -import org.sonar.batch.bootstrap.BatchExtensionDictionnary; -import org.sonar.batch.events.EventBus; - -import java.util.Collection; - -public class InitializersExecutor { - - private static final Logger LOG = Loggers.get(SensorsExecutor.class); - - private Project project; - private BatchExtensionDictionnary selector; - private EventBus eventBus; - - public InitializersExecutor(BatchExtensionDictionnary selector, Project project, EventBus eventBus) { - this.selector = selector; - this.project = project; - this.eventBus = eventBus; - } - - public void execute() { - Collection<Initializer> initializers = selector.select(Initializer.class, project, true, null); - eventBus.fireEvent(new InitializersPhaseEvent(Lists.newArrayList(initializers), true)); - if (LOG.isDebugEnabled()) { - LOG.debug("Initializers : {}", StringUtils.join(initializers, " -> ")); - } - - for (Initializer initializer : initializers) { - eventBus.fireEvent(new InitializerExecutionEvent(initializer, true)); - - Profiler profiler = Profiler.create(LOG).startInfo("Initializer " + initializer); - initializer.execute(project); - profiler.stopInfo(); - eventBus.fireEvent(new InitializerExecutionEvent(initializer, false)); - } - - eventBus.fireEvent(new InitializersPhaseEvent(Lists.newArrayList(initializers), false)); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersPhaseEvent.java b/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersPhaseEvent.java deleted file mode 100644 index 5e109625da6..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersPhaseEvent.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.sonar.api.batch.Initializer; -import org.sonar.api.batch.events.InitializersPhaseHandler; - -import java.util.List; - -class InitializersPhaseEvent extends AbstractPhaseEvent<InitializersPhaseHandler> - implements org.sonar.api.batch.events.InitializersPhaseHandler.InitializersPhaseEvent { - - private final List<Initializer> initializers; - - InitializersPhaseEvent(List<Initializer> initializers, boolean start) { - super(start); - this.initializers = initializers; - } - - @Override - public List<Initializer> getInitializers() { - return initializers; - } - - @Override - protected void dispatch(InitializersPhaseHandler handler) { - handler.onInitializersPhase(this); - } - - @Override - protected Class getType() { - return InitializersPhaseHandler.class; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/IssuesPhaseExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/IssuesPhaseExecutor.java deleted file mode 100644 index ba9a155962e..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/IssuesPhaseExecutor.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.SensorContext; -import org.sonar.batch.events.BatchStepEvent; -import org.sonar.batch.events.EventBus; -import org.sonar.batch.index.DefaultIndex; -import org.sonar.batch.issue.IssueCallback; -import org.sonar.batch.issue.ignore.scanner.IssueExclusionsLoader; -import org.sonar.batch.issue.tracking.IssueTransition; -import org.sonar.batch.rule.QProfileVerifier; -import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; -import org.sonar.batch.scan.filesystem.FileSystemLogger; -import org.sonar.batch.scan.report.IssuesReports; - -public final class IssuesPhaseExecutor extends AbstractPhaseExecutor { - - private static final Logger LOG = LoggerFactory.getLogger(IssuesPhaseExecutor.class); - - private final EventBus eventBus; - private final IssuesReports issuesReport; - private final IssueTransition localIssueTracking; - private final IssueCallback issueCallback; - - public IssuesPhaseExecutor(InitializersExecutor initializersExecutor, PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor, SensorContext sensorContext, - DefaultIndex index, EventBus eventBus, ProjectInitializer pi, FileSystemLogger fsLogger, IssuesReports jsonReport, DefaultModuleFileSystem fs, QProfileVerifier profileVerifier, - IssueExclusionsLoader issueExclusionsLoader, IssueTransition localIssueTracking, IssueCallback issueCallback) { - super(initializersExecutor, postJobsExecutor, sensorsExecutor, sensorContext, index, eventBus, pi, fsLogger, fs, profileVerifier, issueExclusionsLoader); - this.eventBus = eventBus; - this.issuesReport = jsonReport; - this.localIssueTracking = localIssueTracking; - this.issueCallback = issueCallback; - } - - @Override - protected void executeOnRoot() { - localIssueTracking(); - issuesCallback(); - issuesReport(); - LOG.info("ANALYSIS SUCCESSFUL"); - } - - private void localIssueTracking() { - String stepName = "Local Issue Tracking"; - eventBus.fireEvent(new BatchStepEvent(stepName, true)); - localIssueTracking.execute(); - eventBus.fireEvent(new BatchStepEvent(stepName, false)); - } - - private void issuesCallback() { - String stepName = "Issues Callback"; - eventBus.fireEvent(new BatchStepEvent(stepName, true)); - issueCallback.execute(); - eventBus.fireEvent(new BatchStepEvent(stepName, false)); - } - - private void issuesReport() { - String stepName = "Issues Reports"; - eventBus.fireEvent(new BatchStepEvent(stepName, true)); - issuesReport.execute(); - eventBus.fireEvent(new BatchStepEvent(stepName, false)); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/PhasesTimeProfiler.java b/sonar-batch/src/main/java/org/sonar/batch/phases/PhasesTimeProfiler.java deleted file mode 100644 index ba8c7c498d0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/PhasesTimeProfiler.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - - -import org.sonar.batch.util.BatchUtils; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.batch.events.SensorExecutionHandler; -import org.sonar.api.batch.events.SensorsPhaseHandler; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; - -public class PhasesTimeProfiler implements SensorExecutionHandler, SensorsPhaseHandler { - - private static final Logger LOG = Loggers.get(PhasesTimeProfiler.class); - - private Profiler profiler = Profiler.create(LOG); - - @Override - public void onSensorsPhase(SensorsPhaseEvent event) { - if (event.isStart()) { - LOG.debug("Sensors : {}", StringUtils.join(event.getSensors(), " -> ")); - } - } - - @Override - public void onSensorExecution(SensorExecutionEvent event) { - if (event.isStart()) { - profiler.startInfo("Sensor " + BatchUtils.describe(event.getSensor())); - } else { - profiler.stopInfo(); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobExecutionEvent.java b/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobExecutionEvent.java deleted file mode 100644 index b7cd92a217b..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobExecutionEvent.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.sonar.api.batch.PostJob; -import org.sonar.api.batch.events.PostJobExecutionHandler; - -class PostJobExecutionEvent extends AbstractPhaseEvent<PostJobExecutionHandler> - implements org.sonar.api.batch.events.PostJobExecutionHandler.PostJobExecutionEvent { - - private final PostJob postJob; - - PostJobExecutionEvent(PostJob postJob, boolean start) { - super(start); - this.postJob = postJob; - } - - @Override - public PostJob getPostJob() { - return postJob; - } - - @Override - public void dispatch(PostJobExecutionHandler handler) { - handler.onPostJobExecution(this); - } - - @Override - public Class getType() { - return PostJobExecutionHandler.class; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobPhaseEvent.java b/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobPhaseEvent.java deleted file mode 100644 index 860d7cc938d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobPhaseEvent.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.sonar.api.batch.PostJob; -import org.sonar.api.batch.events.PostJobsPhaseHandler; - -import java.util.List; - -class PostJobPhaseEvent extends AbstractPhaseEvent<PostJobsPhaseHandler> - implements org.sonar.api.batch.events.PostJobsPhaseHandler.PostJobsPhaseEvent { - - private final List<PostJob> postJobs; - - PostJobPhaseEvent(List<PostJob> postJobs, boolean start) { - super(start); - this.postJobs = postJobs; - } - - @Override - public List<PostJob> getPostJobs() { - return postJobs; - } - - @Override - protected void dispatch(PostJobsPhaseHandler handler) { - handler.onPostJobsPhase(this); - } - - @Override - protected Class getType() { - return PostJobsPhaseHandler.class; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java deleted file mode 100644 index 277b45b8d3c..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.sonar.batch.util.BatchUtils; - -import com.google.common.collect.Lists; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.PostJob; -import org.sonar.api.batch.SensorContext; -import org.sonar.api.resources.Project; -import org.sonar.batch.bootstrap.BatchExtensionDictionnary; -import org.sonar.batch.events.EventBus; - -import java.util.Collection; - -@BatchSide -public class PostJobsExecutor { - private static final Logger LOG = LoggerFactory.getLogger(PostJobsExecutor.class); - - private final BatchExtensionDictionnary selector; - private final Project project; - private final EventBus eventBus; - - public PostJobsExecutor(BatchExtensionDictionnary selector, Project project, EventBus eventBus) { - this.selector = selector; - this.project = project; - this.eventBus = eventBus; - } - - public void execute(SensorContext context) { - Collection<PostJob> postJobs = selector.select(PostJob.class, project, true, null); - - eventBus.fireEvent(new PostJobPhaseEvent(Lists.newArrayList(postJobs), true)); - execute(context, postJobs); - eventBus.fireEvent(new PostJobPhaseEvent(Lists.newArrayList(postJobs), false)); - } - - private void execute(SensorContext context, Collection<PostJob> postJobs) { - logPostJobs(postJobs); - - for (PostJob postJob : postJobs) { - LOG.info("Executing post-job {}", BatchUtils.describe(postJob)); - eventBus.fireEvent(new PostJobExecutionEvent(postJob, true)); - postJob.executeOn(project, context); - eventBus.fireEvent(new PostJobExecutionEvent(postJob, false)); - } - } - - private static void logPostJobs(Collection<PostJob> postJobs) { - if (LOG.isDebugEnabled()) { - LOG.debug("Post-jobs : {}", StringUtils.join(postJobs, " -> ")); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/ProjectAnalysisEvent.java b/sonar-batch/src/main/java/org/sonar/batch/phases/ProjectAnalysisEvent.java deleted file mode 100644 index 428774acd82..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/ProjectAnalysisEvent.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.sonar.api.batch.events.ProjectAnalysisHandler; -import org.sonar.api.resources.Project; - -class ProjectAnalysisEvent extends AbstractPhaseEvent<ProjectAnalysisHandler> - implements org.sonar.api.batch.events.ProjectAnalysisHandler.ProjectAnalysisEvent { - - private final Project project; - - ProjectAnalysisEvent(Project project, boolean start) { - super(start); - this.project = project; - } - - @Override - public Project getProject() { - return project; - } - - @Override - protected void dispatch(ProjectAnalysisHandler handler) { - handler.onProjectAnalysis(this); - } - - @Override - protected Class getType() { - return ProjectAnalysisHandler.class; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/ProjectInitializer.java b/sonar-batch/src/main/java/org/sonar/batch/phases/ProjectInitializer.java deleted file mode 100644 index 8dac11520e5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/ProjectInitializer.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.sonar.api.batch.BatchSide; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.CoreProperties; -import org.sonar.api.config.Settings; -import org.sonar.api.resources.Language; -import org.sonar.api.resources.Languages; -import org.sonar.api.resources.Project; -import org.sonar.api.utils.MessageException; - -/** - * Should be dropped when org.sonar.api.resources.Project is fully refactored. - */ -@BatchSide -public class ProjectInitializer { - - private Languages languages; - private Settings settings; - - public ProjectInitializer(Settings settings, Languages languages) { - this.settings = settings; - this.languages = languages; - } - - public void execute(Project project) { - if (project.getLanguage() == null) { - initDeprecatedLanguage(project); - } - } - - private void initDeprecatedLanguage(Project project) { - String languageKey = settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY); - if (StringUtils.isNotBlank(languageKey)) { - Language language = languages.get(languageKey); - if (language == null) { - throw MessageException.of("Language with key '" + languageKey + "' not found"); - } - project.setLanguage(language); - } else { - project.setLanguage(Project.NONE_LANGUAGE); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/PublishPhaseExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/PublishPhaseExecutor.java deleted file mode 100644 index 33073a110b3..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/PublishPhaseExecutor.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.sonar.api.batch.SensorContext; -import org.sonar.batch.cpd.CpdExecutor; -import org.sonar.batch.events.BatchStepEvent; -import org.sonar.batch.events.EventBus; -import org.sonar.batch.index.DefaultIndex; -import org.sonar.batch.issue.ignore.scanner.IssueExclusionsLoader; -import org.sonar.batch.report.ReportPublisher; -import org.sonar.batch.rule.QProfileVerifier; -import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; -import org.sonar.batch.scan.filesystem.FileSystemLogger; - -public final class PublishPhaseExecutor extends AbstractPhaseExecutor { - - private final EventBus eventBus; - private final ReportPublisher reportPublisher; - private final CpdExecutor cpdExecutor; - - public PublishPhaseExecutor(InitializersExecutor initializersExecutor, PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor, SensorContext sensorContext, - DefaultIndex index, EventBus eventBus, ReportPublisher reportPublisher, ProjectInitializer pi, FileSystemLogger fsLogger, DefaultModuleFileSystem fs, - QProfileVerifier profileVerifier, IssueExclusionsLoader issueExclusionsLoader, CpdExecutor cpdExecutor) { - super(initializersExecutor, postJobsExecutor, sensorsExecutor, sensorContext, index, eventBus, pi, fsLogger, fs, profileVerifier, issueExclusionsLoader); - this.eventBus = eventBus; - this.reportPublisher = reportPublisher; - this.cpdExecutor = cpdExecutor; - } - - @Override - protected void executeOnRoot() { - computeDuplications(); - publishReportJob(); - } - - private void computeDuplications() { - String stepName = "Computing duplications"; - eventBus.fireEvent(new BatchStepEvent(stepName, true)); - cpdExecutor.execute(); - eventBus.fireEvent(new BatchStepEvent(stepName, false)); - } - - private void publishReportJob() { - String stepName = "Publish report"; - eventBus.fireEvent(new BatchStepEvent(stepName, true)); - this.reportPublisher.execute(); - eventBus.fireEvent(new BatchStepEvent(stepName, false)); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/SensorExecutionEvent.java b/sonar-batch/src/main/java/org/sonar/batch/phases/SensorExecutionEvent.java deleted file mode 100644 index 22cbd25aadb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/SensorExecutionEvent.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.sonar.api.batch.Sensor; -import org.sonar.api.batch.events.SensorExecutionHandler; - -class SensorExecutionEvent extends AbstractPhaseEvent<SensorExecutionHandler> - implements org.sonar.api.batch.events.SensorExecutionHandler.SensorExecutionEvent { - - private final Sensor sensor; - - SensorExecutionEvent(Sensor sensor, boolean start) { - super(start); - this.sensor = sensor; - } - - @Override - public Sensor getSensor() { - return sensor; - } - - @Override - public void dispatch(SensorExecutionHandler handler) { - handler.onSensorExecution(this); - } - - @Override - public Class getType() { - return SensorExecutionHandler.class; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/SensorsExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/SensorsExecutor.java deleted file mode 100644 index 3af1cf2b6ee..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/SensorsExecutor.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import com.google.common.collect.Lists; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.Sensor; -import org.sonar.api.batch.SensorContext; -import org.sonar.api.resources.Project; -import org.sonar.batch.bootstrap.BatchExtensionDictionnary; -import org.sonar.batch.events.EventBus; - -import java.util.Collection; - -@BatchSide -public class SensorsExecutor { - - private EventBus eventBus; - private Project module; - private BatchExtensionDictionnary selector; - - public SensorsExecutor(BatchExtensionDictionnary selector, Project project, EventBus eventBus) { - this.selector = selector; - this.eventBus = eventBus; - this.module = project; - } - - public void execute(SensorContext context) { - Collection<Sensor> sensors = selector.select(Sensor.class, module, true, null); - eventBus.fireEvent(new SensorsPhaseEvent(Lists.newArrayList(sensors), true)); - - for (Sensor sensor : sensors) { - executeSensor(context, sensor); - } - - eventBus.fireEvent(new SensorsPhaseEvent(Lists.newArrayList(sensors), false)); - } - - private void executeSensor(SensorContext context, Sensor sensor) { - eventBus.fireEvent(new SensorExecutionEvent(sensor, true)); - sensor.analyse(module, context); - eventBus.fireEvent(new SensorExecutionEvent(sensor, false)); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/SensorsPhaseEvent.java b/sonar-batch/src/main/java/org/sonar/batch/phases/SensorsPhaseEvent.java deleted file mode 100644 index a1f658efa1e..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/SensorsPhaseEvent.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.phases; - -import org.sonar.api.batch.Sensor; -import org.sonar.api.batch.events.SensorsPhaseHandler; - -import java.util.List; - -class SensorsPhaseEvent extends AbstractPhaseEvent<SensorsPhaseHandler> - implements org.sonar.api.batch.events.SensorsPhaseHandler.SensorsPhaseEvent { - - private final List<Sensor> sensors; - - SensorsPhaseEvent(List<Sensor> sensors, boolean start) { - super(start); - this.sensors = sensors; - } - - @Override - public List<Sensor> getSensors() { - return sensors; - } - - @Override - protected void dispatch(SensorsPhaseHandler handler) { - handler.onSensorsPhase(this); - } - - @Override - protected Class getType() { - return SensorsPhaseHandler.class; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/phases/package-info.java deleted file mode 100644 index f4cd97fca3c..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.phases; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/platform/DefaultServer.java b/sonar-batch/src/main/java/org/sonar/batch/platform/DefaultServer.java deleted file mode 100644 index 3136f4a3112..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/platform/DefaultServer.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.platform; - -import java.io.File; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import javax.annotation.CheckForNull; -import org.apache.commons.lang.StringUtils; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.config.Settings; -import org.sonar.api.platform.Server; -import org.sonar.batch.bootstrap.GlobalProperties; - -@BatchSide -public class DefaultServer extends Server { - - private Settings settings; - private GlobalProperties props; - - public DefaultServer(Settings settings, GlobalProperties props) { - this.settings = settings; - this.props = props; - } - - @Override - public String getId() { - return settings.getString(CoreProperties.SERVER_ID); - } - - @Override - public String getVersion() { - return settings.getString(CoreProperties.SERVER_VERSION); - } - - @Override - public Date getStartedAt() { - String dateString = settings.getString(CoreProperties.SERVER_STARTTIME); - if (dateString != null) { - try { - return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(dateString); - - } catch (ParseException e) { - LoggerFactory.getLogger(getClass()).error("The property " + CoreProperties.SERVER_STARTTIME + " is badly formatted.", e); - } - } - return null; - } - - @Override - public File getRootDir() { - return null; - } - - @Override - @CheckForNull - public File getDeployDir() { - return null; - } - - @Override - public String getContextPath() { - return ""; - } - - @Override - public String getPublicRootUrl() { - return null; - } - - @Override - public boolean isDev() { - return false; - } - - @Override - public boolean isSecured() { - return false; - } - - @Override - public String getURL() { - return StringUtils.removeEnd(StringUtils.defaultIfBlank(props.property("sonar.host.url"), "http://localhost:9000"), "/"); - } - - @Override - public String getPermanentServerId() { - return settings.getString(CoreProperties.PERMANENT_SERVER_ID); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/platform/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/platform/package-info.java deleted file mode 100644 index cb2793702b0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/platform/package-info.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.platform; - -import javax.annotation.ParametersAreNonnullByDefault; - diff --git a/sonar-batch/src/main/java/org/sonar/batch/postjob/DefaultPostJobContext.java b/sonar-batch/src/main/java/org/sonar/batch/postjob/DefaultPostJobContext.java deleted file mode 100644 index 02b1582b999..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/postjob/DefaultPostJobContext.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.postjob; - -import org.sonar.batch.issue.tracking.TrackedIssue; - -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; - -import javax.annotation.Nullable; - -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.fs.InputComponent; -import org.sonar.api.batch.postjob.PostJobContext; -import org.sonar.api.batch.postjob.issue.Issue; -import org.sonar.api.batch.rule.Severity; -import org.sonar.api.config.Settings; -import org.sonar.api.rule.RuleKey; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.issue.IssueCache; - -public class DefaultPostJobContext implements PostJobContext { - - private final Settings settings; - private final AnalysisMode analysisMode; - private final IssueCache cache; - private final BatchComponentCache resourceCache; - - public DefaultPostJobContext(Settings settings, AnalysisMode analysisMode, IssueCache cache, BatchComponentCache resourceCache) { - this.settings = settings; - this.analysisMode = analysisMode; - this.cache = cache; - this.resourceCache = resourceCache; - } - - @Override - public Settings settings() { - return settings; - } - - @Override - public AnalysisMode analysisMode() { - return analysisMode; - } - - @Override - public Iterable<Issue> issues() { - return Iterables.transform(Iterables.filter(cache.all(), new ResolvedPredicate(false)), new IssueTransformer()); - } - - @Override - public Iterable<Issue> resolvedIssues() { - return Iterables.transform(Iterables.filter(cache.all(), new ResolvedPredicate(true)), new IssueTransformer()); - } - - private class DefaultIssueWrapper implements Issue { - - private final TrackedIssue wrapped; - - public DefaultIssueWrapper(TrackedIssue wrapped) { - this.wrapped = wrapped; - } - - @Override - public String key() { - return wrapped.key(); - } - - @Override - public RuleKey ruleKey() { - return wrapped.getRuleKey(); - } - - @Override - public String componentKey() { - return wrapped.componentKey(); - } - - @Override - public InputComponent inputComponent() { - BatchComponent component = resourceCache.get(wrapped.componentKey()); - return component != null ? component.inputComponent() : null; - } - - @Override - public Integer line() { - return wrapped.startLine(); - } - - @Override - public Double effortToFix() { - return wrapped.gap(); - } - - @Override - public String message() { - return wrapped.getMessage(); - } - - @Override - public Severity severity() { - String severity = wrapped.severity(); - return severity != null ? Severity.valueOf(severity) : null; - } - - @Override - public boolean isNew() { - return wrapped.isNew(); - } - } - - private class IssueTransformer implements Function<TrackedIssue, Issue> { - @Override - public Issue apply(TrackedIssue input) { - return new DefaultIssueWrapper(input); - } - } - - private static class ResolvedPredicate implements Predicate<TrackedIssue> { - private final boolean resolved; - - private ResolvedPredicate(boolean resolved) { - this.resolved = resolved; - } - - @Override - public boolean apply(@Nullable TrackedIssue issue) { - if (issue != null) { - return resolved ? issue.resolution() != null : issue.resolution() == null; - } - return false; - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/postjob/PostJobOptimizer.java b/sonar-batch/src/main/java/org/sonar/batch/postjob/PostJobOptimizer.java deleted file mode 100644 index b27e89e87f8..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/postjob/PostJobOptimizer.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.postjob; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.postjob.internal.DefaultPostJobDescriptor; -import org.sonar.api.config.Settings; - -@BatchSide -public class PostJobOptimizer { - - private static final Logger LOG = LoggerFactory.getLogger(PostJobOptimizer.class); - - private final Settings settings; - private final AnalysisMode analysisMode; - - public PostJobOptimizer(Settings settings, AnalysisMode analysisMode) { - this.settings = settings; - this.analysisMode = analysisMode; - } - - /** - * Decide if the given PostJob should be executed. - */ - public boolean shouldExecute(DefaultPostJobDescriptor descriptor) { - if (!settingsCondition(descriptor)) { - LOG.debug("'{}' skipped because one of the required properties is missing", descriptor.name()); - return false; - } - if (descriptor.isDisabledInIssues() && analysisMode.isIssues()) { - LOG.debug("'{}' skipped in issues mode", descriptor.name()); - return false; - } - return true; - } - - private boolean settingsCondition(DefaultPostJobDescriptor descriptor) { - if (!descriptor.properties().isEmpty()) { - for (String propertyKey : descriptor.properties()) { - if (!settings.hasKey(propertyKey)) { - return false; - } - } - } - return true; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/postjob/PostJobWrapper.java b/sonar-batch/src/main/java/org/sonar/batch/postjob/PostJobWrapper.java deleted file mode 100644 index c5ba661637c..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/postjob/PostJobWrapper.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.postjob; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.CheckProject; -import org.sonar.api.batch.postjob.PostJob; -import org.sonar.api.batch.postjob.PostJobContext; -import org.sonar.api.batch.postjob.internal.DefaultPostJobDescriptor; -import org.sonar.api.resources.Project; - -public class PostJobWrapper implements org.sonar.api.batch.PostJob, CheckProject { - - private static final Logger LOG = LoggerFactory.getLogger(PostJobWrapper.class); - - private PostJob wrappedPostJob; - private PostJobContext adaptor; - private DefaultPostJobDescriptor descriptor; - private PostJobOptimizer optimizer; - - public PostJobWrapper(PostJob newPostJob, PostJobContext adaptor, PostJobOptimizer optimizer) { - this.wrappedPostJob = newPostJob; - this.optimizer = optimizer; - this.descriptor = new DefaultPostJobDescriptor(); - newPostJob.describe(descriptor); - this.adaptor = adaptor; - } - - public PostJob wrappedPostJob() { - return wrappedPostJob; - } - - @Override - public boolean shouldExecuteOnProject(Project project) { - return optimizer.shouldExecute(descriptor); - } - - @Override - public void executeOn(Project project, org.sonar.api.batch.SensorContext context) { - wrappedPostJob.execute(adaptor); - } - - @Override - public String toString() { - return descriptor.name() + (LOG.isDebugEnabled() ? " (wrapped)" : ""); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/postjob/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/postjob/package-info.java deleted file mode 100644 index 0e8cb6ac72d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/postjob/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.batch.postjob; diff --git a/sonar-batch/src/main/java/org/sonar/batch/profiling/AbstractTimeProfiling.java b/sonar-batch/src/main/java/org/sonar/batch/profiling/AbstractTimeProfiling.java deleted file mode 100644 index 6e204596616..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/profiling/AbstractTimeProfiling.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.profiling; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import javax.annotation.Nullable; -import org.sonar.api.utils.System2; -import org.sonar.api.utils.TimeUtils; - -public abstract class AbstractTimeProfiling { - - private final long startTime; - - private long totalTime; - - private System2 system; - - public AbstractTimeProfiling(System2 system) { - this.system = system; - this.startTime = system.now(); - } - - protected System2 system() { - return system; - } - - public long startTime() { - return startTime; - } - - public void stop() { - this.totalTime = system.now() - startTime; - } - - public long totalTime() { - return totalTime; - } - - public String totalTimeAsString() { - return TimeUtils.formatDuration(totalTime); - } - - public void setTotalTime(long totalTime) { - this.totalTime = totalTime; - } - - protected void add(AbstractTimeProfiling other) { - this.setTotalTime(this.totalTime() + other.totalTime()); - } - - static <G extends AbstractTimeProfiling> Map<Object, G> sortByDescendingTotalTime(Map<?, G> unsorted) { - List<Map.Entry<?, G>> entries = - new ArrayList<Map.Entry<?, G>>(unsorted.entrySet()); - Collections.sort(entries, new Comparator<Map.Entry<?, G>>() { - @Override - public int compare(Map.Entry<?, G> o1, Map.Entry<?, G> o2) { - return Long.valueOf(o2.getValue().totalTime()).compareTo(o1.getValue().totalTime()); - } - }); - Map<Object, G> sortedMap = new LinkedHashMap<>(); - for (Map.Entry<?, G> entry : entries) { - sortedMap.put(entry.getKey(), entry.getValue()); - } - return sortedMap; - } - - static <G extends AbstractTimeProfiling> List<G> truncate(Collection<G> sortedList) { - int maxSize = 10; - List<G> result = new ArrayList<>(maxSize); - int i = 0; - for (G item : sortedList) { - if (i >= maxSize || item.totalTime() == 0) { - return result; - } - i++; - result.add(item); - } - return result; - } - - protected void println(String msg) { - PhasesSumUpTimeProfiler.println(msg); - } - - protected void println(String text, @Nullable Double percent, AbstractTimeProfiling phaseProfiling) { - PhasesSumUpTimeProfiler.println(text, percent, phaseProfiling); - } - - protected void println(String text, AbstractTimeProfiling phaseProfiling) { - println(text, null, phaseProfiling); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/profiling/ItemProfiling.java b/sonar-batch/src/main/java/org/sonar/batch/profiling/ItemProfiling.java deleted file mode 100644 index abbb11ccbcb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/profiling/ItemProfiling.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.profiling; - -import org.sonar.api.utils.System2; - -public class ItemProfiling extends AbstractTimeProfiling { - - private final String itemName; - - public ItemProfiling(System2 system, String itemName) { - super(system); - this.itemName = itemName; - } - - public String itemName() { - return itemName; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/profiling/ModuleProfiling.java b/sonar-batch/src/main/java/org/sonar/batch/profiling/ModuleProfiling.java deleted file mode 100644 index 7c04dcff55e..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/profiling/ModuleProfiling.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.profiling; - -import com.google.common.collect.Maps; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; -import javax.annotation.Nullable; -import org.sonar.api.resources.Project; -import org.sonar.api.utils.System2; - -public class ModuleProfiling extends AbstractTimeProfiling { - - private Map<Phase, PhaseProfiling> profilingPerPhase = new HashMap<>(); - private Map<String, ItemProfiling> profilingPerBatchStep = new LinkedHashMap<>(); - private final Project module; - - public ModuleProfiling(@Nullable Project module, System2 system) { - super(system); - this.module = module; - } - - public String moduleName() { - if (module != null) { - return module.getName(); - } - return null; - } - - public PhaseProfiling getProfilingPerPhase(Phase phase) { - return profilingPerPhase.get(phase); - } - - public ItemProfiling getProfilingPerBatchStep(String stepName) { - return profilingPerBatchStep.get(stepName); - } - - public void addPhaseProfiling(Phase phase) { - profilingPerPhase.put(phase, PhaseProfiling.create(system(), phase)); - } - - public void addBatchStepProfiling(String stepName) { - profilingPerBatchStep.put(stepName, new ItemProfiling(system(), stepName)); - } - - public void dump(Properties props) { - double percent = this.totalTime() / 100.0; - Map<Object, AbstractTimeProfiling> categories = Maps.newLinkedHashMap(); - categories.putAll(profilingPerPhase); - categories.putAll(profilingPerBatchStep); - - for (Map.Entry<Object, AbstractTimeProfiling> batchStep : categories.entrySet()) { - props.setProperty(batchStep.getKey().toString(), Long.toString(batchStep.getValue().totalTime())); - } - - for (Map.Entry<Object, AbstractTimeProfiling> batchStep : sortByDescendingTotalTime(categories).entrySet()) { - println(" * " + batchStep.getKey() + " execution time: ", percent, batchStep.getValue()); - } - // Breakdown per phase - for (Phase phase : Phase.values()) { - if (profilingPerPhase.containsKey(phase) && getProfilingPerPhase(phase).hasItems()) { - println(""); - println(" * " + phase + " execution time breakdown: ", getProfilingPerPhase(phase)); - getProfilingPerPhase(phase).dump(props); - } - } - } - - public void merge(ModuleProfiling other) { - super.add(other); - for (Entry<Phase, PhaseProfiling> entry : other.profilingPerPhase.entrySet()) { - if (!this.profilingPerPhase.containsKey(entry.getKey())) { - this.addPhaseProfiling(entry.getKey()); - } - this.getProfilingPerPhase(entry.getKey()).merge(entry.getValue()); - } - for (Map.Entry<String, ItemProfiling> entry : other.profilingPerBatchStep.entrySet()) { - if (!this.profilingPerBatchStep.containsKey(entry.getKey())) { - profilingPerBatchStep.put(entry.getKey(), new ItemProfiling(system(), entry.getKey())); - } - this.getProfilingPerBatchStep(entry.getKey()).add(entry.getValue()); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/profiling/Phase.java b/sonar-batch/src/main/java/org/sonar/batch/profiling/Phase.java deleted file mode 100644 index f00496f9644..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/profiling/Phase.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.profiling; - -public enum Phase { - - INIT("Initializers"), SENSOR("Sensors"), DECORATOR("Decorators"), PERSISTER("Persisters"), POSTJOB("Post-Jobs"); - - private final String label; - - private Phase(String label) { - this.label = label; - } - - @Override - public String toString() { - return label; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/profiling/PhaseProfiling.java b/sonar-batch/src/main/java/org/sonar/batch/profiling/PhaseProfiling.java deleted file mode 100644 index 5b13f1d4ee1..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/profiling/PhaseProfiling.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.profiling; - -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; -import org.sonar.api.utils.System2; - -public class PhaseProfiling extends AbstractTimeProfiling { - - private final Phase phase; - - private Map<String, ItemProfiling> profilingPerItem = new HashMap<>(); - - PhaseProfiling(System2 system, Phase phase) { - super(system); - this.phase = phase; - } - - public static PhaseProfiling create(System2 system, Phase phase) { - return new PhaseProfiling(system, phase); - } - - public Phase phase() { - return phase; - } - - public boolean hasItems() { - return !profilingPerItem.isEmpty(); - } - - public ItemProfiling getProfilingPerItem(Object item) { - String stringOrSimpleName = toStringOrSimpleName(item); - return profilingPerItem.get(stringOrSimpleName); - } - - public void newItemProfiling(Object item) { - String stringOrSimpleName = toStringOrSimpleName(item); - profilingPerItem.put(stringOrSimpleName, new ItemProfiling(system(), stringOrSimpleName)); - } - - public void newItemProfiling(String itemName) { - profilingPerItem.put(itemName, new ItemProfiling(system(), itemName)); - } - - public void merge(PhaseProfiling other) { - super.add(other); - for (Entry<String, ItemProfiling> entry : other.profilingPerItem.entrySet()) { - if (!this.profilingPerItem.containsKey(entry.getKey())) { - newItemProfiling(entry.getKey()); - } - this.getProfilingPerItem(entry.getKey()).add(entry.getValue()); - } - } - - public void dump(Properties props) { - double percent = this.totalTime() / 100.0; - for (ItemProfiling itemProfiling : profilingPerItem.values()) { - props.setProperty(itemProfiling.itemName(), Long.toString(itemProfiling.totalTime())); - } - for (ItemProfiling itemProfiling : truncate(sortByDescendingTotalTime(profilingPerItem).values())) { - println(" o " + itemProfiling.itemName() + ": ", percent, itemProfiling); - } - } - - /** - * Try to use toString if it is not the default {@link Object#toString()}. Else use {@link Class#getSimpleName()} - * @param o - * @return - */ - private static String toStringOrSimpleName(Object o) { - String toString = o.toString(); - if (toString == null || toString.startsWith(o.getClass().getName())) { - return o.getClass().getSimpleName(); - } - return toString; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/profiling/PhasesSumUpTimeProfiler.java b/sonar-batch/src/main/java/org/sonar/batch/profiling/PhasesSumUpTimeProfiler.java deleted file mode 100644 index f03b84caaac..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/profiling/PhasesSumUpTimeProfiler.java +++ /dev/null @@ -1,278 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.profiling; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Lists; -import java.io.File; -import java.io.FileOutputStream; -import java.util.HashMap; -import java.util.IdentityHashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.Decorator; -import org.sonar.api.batch.events.DecoratorExecutionHandler; -import org.sonar.api.batch.events.DecoratorsPhaseHandler; -import org.sonar.api.batch.events.InitializerExecutionHandler; -import org.sonar.api.batch.events.InitializersPhaseHandler; -import org.sonar.api.batch.events.PostJobExecutionHandler; -import org.sonar.api.batch.events.PostJobsPhaseHandler; -import org.sonar.api.batch.events.ProjectAnalysisHandler; -import org.sonar.api.batch.events.SensorExecutionHandler; -import org.sonar.api.batch.events.SensorsPhaseHandler; -import org.sonar.api.resources.Project; -import org.sonar.api.utils.System2; -import org.sonar.api.utils.TimeUtils; -import org.sonar.batch.bootstrap.GlobalProperties; -import org.sonar.batch.events.BatchStepHandler; -import org.sonar.batch.util.BatchUtils; - -import static org.sonar.batch.profiling.AbstractTimeProfiling.sortByDescendingTotalTime; -import static org.sonar.batch.profiling.AbstractTimeProfiling.truncate; - -public class PhasesSumUpTimeProfiler implements ProjectAnalysisHandler, SensorExecutionHandler, DecoratorExecutionHandler, PostJobExecutionHandler, DecoratorsPhaseHandler, - SensorsPhaseHandler, PostJobsPhaseHandler, InitializersPhaseHandler, InitializerExecutionHandler, BatchStepHandler { - - static final Logger LOG = LoggerFactory.getLogger(PhasesSumUpTimeProfiler.class); - private static final int TEXT_RIGHT_PAD = 60; - private static final int TIME_LEFT_PAD = 10; - - @VisibleForTesting - ModuleProfiling currentModuleProfiling; - - @VisibleForTesting - ModuleProfiling totalProfiling; - - private Map<Project, ModuleProfiling> modulesProfilings = new HashMap<>(); - private DecoratorsProfiler decoratorsProfiler; - - private final System2 system; - private final File out; - - public PhasesSumUpTimeProfiler(System2 system, GlobalProperties bootstrapProps) { - String workingDirPath = StringUtils.defaultIfBlank(bootstrapProps.property(CoreProperties.WORKING_DIRECTORY), CoreProperties.WORKING_DIRECTORY_DEFAULT_VALUE); - File workingDir = new File(workingDirPath).getAbsoluteFile(); - this.out = new File(workingDir, "profiling"); - this.out.mkdirs(); - this.totalProfiling = new ModuleProfiling(null, system); - this.system = system; - } - - static void println(String msg) { - LOG.info(msg); - } - - static void println(String text, @Nullable Double percent, AbstractTimeProfiling phaseProfiling) { - StringBuilder sb = new StringBuilder(); - sb.append(StringUtils.rightPad(text, TEXT_RIGHT_PAD)).append(StringUtils.leftPad(phaseProfiling.totalTimeAsString(), TIME_LEFT_PAD)); - if (percent != null) { - sb.append(" (").append((int) (phaseProfiling.totalTime() / percent)).append("%)"); - } - println(sb.toString()); - } - - @Override - public void onProjectAnalysis(ProjectAnalysisEvent event) { - Project module = event.getProject(); - if (event.isStart()) { - decoratorsProfiler = new DecoratorsProfiler(); - currentModuleProfiling = new ModuleProfiling(module, system); - } else { - currentModuleProfiling.stop(); - modulesProfilings.put(module, currentModuleProfiling); - long moduleTotalTime = currentModuleProfiling.totalTime(); - println(""); - println(" -------- Profiling of module " + module.getName() + ": " + TimeUtils.formatDuration(moduleTotalTime) + " --------"); - println(""); - Properties props = new Properties(); - currentModuleProfiling.dump(props); - println(""); - println(" -------- End of profiling of module " + module.getName() + " --------"); - println(""); - String fileName = module.getKey() + "-profiler.properties"; - dumpToFile(props, BatchUtils.cleanKeyForFilename(fileName)); - totalProfiling.merge(currentModuleProfiling); - if (module.isRoot() && !module.getModules().isEmpty()) { - dumpTotalExecutionSummary(); - } - } - } - - private void dumpTotalExecutionSummary() { - totalProfiling.stop(); - long totalTime = totalProfiling.totalTime(); - println(""); - println(" ======== Profiling of total execution: " + TimeUtils.formatDuration(totalTime) + " ========"); - println(""); - println(" * Module execution time breakdown: "); - double percent = totalTime / 100.0; - for (ModuleProfiling modulesProfiling : truncate(sortByDescendingTotalTime(modulesProfilings).values())) { - println(" o " + modulesProfiling.moduleName() + " execution time: ", percent, modulesProfiling); - } - println(""); - Properties props = new Properties(); - totalProfiling.dump(props); - println(""); - println(" ======== End of profiling of total execution ========"); - println(""); - String fileName = "total-execution-profiler.properties"; - dumpToFile(props, fileName); - } - - private void dumpToFile(Properties props, String fileName) { - File file = new File(out, fileName); - try (FileOutputStream fos = new FileOutputStream(file)) { - props.store(fos, "SonarQube"); - println("Profiling data stored in " + file.getAbsolutePath()); - } catch (Exception e) { - throw new IllegalStateException("Unable to store profiler output: " + file, e); - } - } - - @Override - public void onSensorsPhase(SensorsPhaseEvent event) { - if (event.isStart()) { - currentModuleProfiling.addPhaseProfiling(Phase.SENSOR); - } else { - currentModuleProfiling.getProfilingPerPhase(Phase.SENSOR).stop(); - } - } - - @Override - public void onSensorExecution(SensorExecutionEvent event) { - PhaseProfiling profiling = currentModuleProfiling.getProfilingPerPhase(Phase.SENSOR); - if (event.isStart()) { - profiling.newItemProfiling(event.getSensor()); - } else { - profiling.getProfilingPerItem(event.getSensor()).stop(); - } - } - - @Override - public void onDecoratorExecution(DecoratorExecutionEvent event) { - PhaseProfiling profiling = currentModuleProfiling.getProfilingPerPhase(Phase.DECORATOR); - if (event.isStart()) { - if (profiling.getProfilingPerItem(event.getDecorator()) == null) { - profiling.newItemProfiling(event.getDecorator()); - } - decoratorsProfiler.start(event.getDecorator()); - } else { - decoratorsProfiler.stop(); - } - } - - @Override - public void onDecoratorsPhase(DecoratorsPhaseEvent event) { - if (event.isStart()) { - currentModuleProfiling.addPhaseProfiling(Phase.DECORATOR); - } else { - for (Decorator decorator : decoratorsProfiler.getDurations().keySet()) { - currentModuleProfiling.getProfilingPerPhase(Phase.DECORATOR) - .getProfilingPerItem(decorator).setTotalTime(decoratorsProfiler.getDurations().get(decorator)); - } - currentModuleProfiling.getProfilingPerPhase(Phase.DECORATOR).stop(); - } - } - - @Override - public void onPostJobsPhase(PostJobsPhaseEvent event) { - if (event.isStart()) { - currentModuleProfiling.addPhaseProfiling(Phase.POSTJOB); - } else { - currentModuleProfiling.getProfilingPerPhase(Phase.POSTJOB).stop(); - } - } - - @Override - public void onPostJobExecution(PostJobExecutionEvent event) { - PhaseProfiling profiling = currentModuleProfiling.getProfilingPerPhase(Phase.POSTJOB); - if (event.isStart()) { - profiling.newItemProfiling(event.getPostJob()); - } else { - profiling.getProfilingPerItem(event.getPostJob()).stop(); - } - } - - @Override - public void onInitializersPhase(InitializersPhaseEvent event) { - if (event.isStart()) { - currentModuleProfiling.addPhaseProfiling(Phase.INIT); - } else { - currentModuleProfiling.getProfilingPerPhase(Phase.INIT).stop(); - } - } - - @Override - public void onInitializerExecution(InitializerExecutionEvent event) { - PhaseProfiling profiling = currentModuleProfiling.getProfilingPerPhase(Phase.INIT); - if (event.isStart()) { - profiling.newItemProfiling(event.getInitializer()); - } else { - profiling.getProfilingPerItem(event.getInitializer()).stop(); - } - } - - @Override - public void onBatchStep(BatchStepEvent event) { - if (event.isStart()) { - currentModuleProfiling.addBatchStepProfiling(event.stepName()); - } else { - currentModuleProfiling.getProfilingPerBatchStep(event.stepName()).stop(); - } - } - - class DecoratorsProfiler { - private List<Decorator> decorators = Lists.newArrayList(); - private Map<Decorator, Long> durations = new IdentityHashMap<>(); - private long startTime; - private Decorator currentDecorator; - - DecoratorsProfiler() { - } - - void start(Decorator decorator) { - this.startTime = system.now(); - this.currentDecorator = decorator; - } - - void stop() { - final Long cumulatedDuration; - if (durations.containsKey(currentDecorator)) { - cumulatedDuration = durations.get(currentDecorator); - } else { - decorators.add(currentDecorator); - cumulatedDuration = 0L; - } - durations.put(currentDecorator, cumulatedDuration + (system.now() - startTime)); - } - - public Map<Decorator, Long> getDurations() { - return durations; - } - - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/profiling/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/profiling/package-info.java deleted file mode 100644 index 8f57dd4e4c7..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/profiling/package-info.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.profiling; - -import javax.annotation.ParametersAreNonnullByDefault; - diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/ActiveRulesPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/ActiveRulesPublisher.java deleted file mode 100644 index f7d059c3d83..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/report/ActiveRulesPublisher.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.report; - -import com.google.common.base.Function; -import com.google.common.collect.FluentIterable; -import java.util.Map; -import javax.annotation.Nonnull; -import org.sonar.api.batch.rule.ActiveRule; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.batch.protocol.Constants; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReportWriter; - -public class ActiveRulesPublisher implements ReportPublisherStep { - - private final ActiveRules activeRules; - - public ActiveRulesPublisher(ActiveRules activeRules) { - this.activeRules = activeRules; - } - - @Override - public void publish(BatchReportWriter writer) { - Iterable<BatchReport.ActiveRule> activeRuleMessages = FluentIterable.from(activeRules.findAll()).transform(new ToMessage()); - writer.writeActiveRules(activeRuleMessages); - } - - private static class ToMessage implements Function<ActiveRule, BatchReport.ActiveRule> { - private final BatchReport.ActiveRule.Builder builder = BatchReport.ActiveRule.newBuilder(); - - @Override - public BatchReport.ActiveRule apply(@Nonnull ActiveRule input) { - builder.clear(); - builder.setRuleRepository(input.ruleKey().repository()); - builder.setRuleKey(input.ruleKey().rule()); - builder.setSeverity(Constants.Severity.valueOf(input.severity())); - for (Map.Entry<String, String> entry : input.params().entrySet()) { - builder.addParamBuilder().setKey(entry.getKey()).setValue(entry.getValue()).build(); - - } - return builder.build(); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/AnalysisContextReportPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/AnalysisContextReportPublisher.java deleted file mode 100644 index a15cb2b2ed6..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/report/AnalysisContextReportPublisher.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.report; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.StandardOpenOption; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import java.util.TreeSet; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.utils.System2; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.batch.bootstrap.BatchPluginRepository; -import org.sonar.batch.protocol.output.BatchReportWriter; -import org.sonar.batch.repository.ProjectRepositories; -import org.sonar.core.platform.PluginInfo; - -@BatchSide -public class AnalysisContextReportPublisher { - - private static final Logger LOG = Loggers.get(AnalysisContextReportPublisher.class); - - private static final String ENV_PROP_PREFIX = "env."; - private static final String SONAR_PROP_PREFIX = "sonar."; - private final BatchPluginRepository pluginRepo; - private final AnalysisMode mode; - private final System2 system; - private final ProjectRepositories projectRepos; - - private BatchReportWriter writer; - - public AnalysisContextReportPublisher(AnalysisMode mode, BatchPluginRepository pluginRepo, System2 system, ProjectRepositories projectRepos) { - this.mode = mode; - this.pluginRepo = pluginRepo; - this.system = system; - this.projectRepos = projectRepos; - } - - public void init(BatchReportWriter writer) { - if (mode.isIssues()) { - return; - } - this.writer = writer; - File analysisLog = writer.getFileStructure().analysisLog(); - try (BufferedWriter fileWriter = Files.newBufferedWriter(analysisLog.toPath(), StandardCharsets.UTF_8)) { - if (LOG.isDebugEnabled()) { - writeEnvVariables(fileWriter); - writeSystemProps(fileWriter); - } - writePlugins(fileWriter); - } catch (IOException e) { - throw new IllegalStateException("Unable to write analysis log", e); - } - } - - private void writePlugins(BufferedWriter fileWriter) throws IOException { - fileWriter.write("SonarQube plugins:\n"); - for (PluginInfo p : pluginRepo.getPluginInfos()) { - fileWriter.append(String.format(" - %s %s (%s)", p.getName(), p.getVersion(), p.getKey())).append('\n'); - } - } - - private void writeSystemProps(BufferedWriter fileWriter) throws IOException { - fileWriter.write("System properties:\n"); - Properties sysProps = system.properties(); - for (String prop : new TreeSet<>(sysProps.stringPropertyNames())) { - if (prop.startsWith(SONAR_PROP_PREFIX)) { - continue; - } - fileWriter.append(String.format(" - %s=%s", prop, sysProps.getProperty(prop))).append('\n'); - } - } - - private void writeEnvVariables(BufferedWriter fileWriter) throws IOException { - fileWriter.append("Environment variables:\n"); - Map<String, String> envVariables = system.envVariables(); - for (String env : new TreeSet<>(envVariables.keySet())) { - fileWriter.append(String.format(" - %s=%s", env, envVariables.get(env))).append('\n'); - } - } - - public void dumpSettings(ProjectDefinition moduleDefinition) { - if (mode.isIssues()) { - return; - } - - File analysisLog = writer.getFileStructure().analysisLog(); - try (BufferedWriter fileWriter = Files.newBufferedWriter(analysisLog.toPath(), StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.APPEND)) { - Map<String, String> moduleSpecificProps = collectModuleSpecificProps(moduleDefinition); - fileWriter.append(String.format("Settings for module: %s", moduleDefinition.getKey())).append('\n'); - for (String prop : new TreeSet<>(moduleSpecificProps.keySet())) { - if (isSystemProp(prop) || isEnvVariable(prop) || !isSqProp(prop)) { - continue; - } - fileWriter.append(String.format(" - %s=%s", prop, sensitive(prop) ? "******" : moduleSpecificProps.get(prop))).append('\n'); - } - } catch (IOException e) { - throw new IllegalStateException("Unable to write analysis log", e); - } - } - - /** - * Only keep props that are not in parent - */ - private Map<String, String> collectModuleSpecificProps(ProjectDefinition moduleDefinition) { - Map<String, String> moduleSpecificProps = new HashMap<>(); - if (projectRepos.moduleExists(moduleDefinition.getKeyWithBranch())) { - moduleSpecificProps.putAll(projectRepos.settings(moduleDefinition.getKeyWithBranch())); - } - ProjectDefinition parent = moduleDefinition.getParent(); - if (parent == null) { - moduleSpecificProps.putAll(moduleDefinition.properties()); - } else { - Map<String, String> parentProps = parent.properties(); - for (Map.Entry<String, String> entry : moduleDefinition.properties().entrySet()) { - if (!parentProps.containsKey(entry.getKey()) || !parentProps.get(entry.getKey()).equals(entry.getValue())) { - moduleSpecificProps.put(entry.getKey(), entry.getValue()); - } - } - } - return moduleSpecificProps; - } - - private static boolean isSqProp(String propKey) { - return propKey.startsWith(SONAR_PROP_PREFIX); - } - - private boolean isSystemProp(String propKey) { - return system.properties().containsKey(propKey) && !propKey.startsWith(SONAR_PROP_PREFIX); - } - - private boolean isEnvVariable(String propKey) { - return propKey.startsWith(ENV_PROP_PREFIX) && system.envVariables().containsKey(propKey.substring(ENV_PROP_PREFIX.length())); - } - - private static boolean sensitive(String key) { - return key.contains(".password") || key.contains(".secured"); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/BatchReportUtils.java b/sonar-batch/src/main/java/org/sonar/batch/report/BatchReportUtils.java deleted file mode 100644 index 9ba54f164ab..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/report/BatchReportUtils.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.report; - -import org.sonar.api.batch.sensor.highlighting.TypeOfText; -import org.sonar.batch.protocol.Constants.HighlightingType; - -public class BatchReportUtils { - - private BatchReportUtils() { - } - - public static HighlightingType toProtocolType(TypeOfText textType) { - switch (textType) { - case ANNOTATION: - return HighlightingType.ANNOTATION; - case COMMENT: - return HighlightingType.COMMENT; - case CONSTANT: - return HighlightingType.CONSTANT; - case CPP_DOC: - return HighlightingType.CPP_DOC; - case KEYWORD: - return HighlightingType.KEYWORD; - case KEYWORD_LIGHT: - return HighlightingType.KEYWORD_LIGHT; - case PREPROCESS_DIRECTIVE: - return HighlightingType.PREPROCESS_DIRECTIVE; - case STRING: - return HighlightingType.HIGHLIGHTING_STRING; - case STRUCTURED_COMMENT: - return HighlightingType.STRUCTURED_COMMENT; - default: - throw new IllegalArgumentException("Unknow highlighting type: " + textType); - } - } - - public static TypeOfText toBatchType(HighlightingType type) { - switch (type) { - case ANNOTATION: - return TypeOfText.ANNOTATION; - case COMMENT: - return TypeOfText.COMMENT; - case CONSTANT: - return TypeOfText.CONSTANT; - case CPP_DOC: - return TypeOfText.CPP_DOC; - case HIGHLIGHTING_STRING: - return TypeOfText.STRING; - case KEYWORD: - return TypeOfText.KEYWORD; - case KEYWORD_LIGHT: - return TypeOfText.KEYWORD_LIGHT; - case PREPROCESS_DIRECTIVE: - return TypeOfText.PREPROCESS_DIRECTIVE; - case STRUCTURED_COMMENT: - return TypeOfText.STRUCTURED_COMMENT; - default: - throw new IllegalArgumentException(type + " is not a valid type"); - } - } - - public static String toCssClass(HighlightingType type) { - return toBatchType(type).cssClass(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java deleted file mode 100644 index e576c3f8393..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.report; - -import javax.annotation.CheckForNull; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.resources.Language; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.ResourceUtils; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.Constants; -import org.sonar.batch.protocol.Constants.ComponentLinkType; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReport.ComponentLink; -import org.sonar.batch.protocol.output.BatchReportWriter; -import org.sonar.batch.scan.ImmutableProjectReactor; - -/** - * Adds components and analysis metadata to output report - */ -public class ComponentsPublisher implements ReportPublisherStep { - - private final BatchComponentCache resourceCache; - private final ImmutableProjectReactor reactor; - - public ComponentsPublisher(ImmutableProjectReactor reactor, BatchComponentCache resourceCache) { - this.reactor = reactor; - this.resourceCache = resourceCache; - } - - @Override - public void publish(BatchReportWriter writer) { - BatchComponent rootProject = resourceCache.get(reactor.getRoot().getKeyWithBranch()); - recursiveWriteComponent(rootProject, writer); - } - - private void recursiveWriteComponent(BatchComponent batchComponent, BatchReportWriter writer) { - Resource r = batchComponent.resource(); - BatchReport.Component.Builder builder = BatchReport.Component.newBuilder(); - - // non-null fields - builder.setRef(batchComponent.batchId()); - builder.setType(getType(r)); - - // Don't set key on directories and files to save space since it can be deduced from path - if (batchComponent.isProjectOrModule()) { - // Here we want key without branch - ProjectDefinition def = reactor.getProjectDefinition(batchComponent.key()); - builder.setKey(def.getKey()); - } - - // protocol buffers does not accept null values - - if (batchComponent.isFile()) { - builder.setIsTest(ResourceUtils.isUnitTestFile(r)); - builder.setLines(((InputFile) batchComponent.inputComponent()).lines()); - } - String name = getName(r); - if (name != null) { - builder.setName(name); - } - String description = getDescription(r); - if (description != null) { - builder.setDescription(description); - } - String path = r.getPath(); - if (path != null) { - builder.setPath(path); - } - String lang = getLanguageKey(r); - if (lang != null) { - builder.setLanguage(lang); - } - for (BatchComponent child : batchComponent.children()) { - builder.addChildRef(child.batchId()); - } - writeLinks(batchComponent, builder); - writeVersion(batchComponent, builder); - writer.writeComponent(builder.build()); - - for (BatchComponent child : batchComponent.children()) { - recursiveWriteComponent(child, writer); - } - } - - private void writeVersion(BatchComponent c, BatchReport.Component.Builder builder) { - if (c.isProjectOrModule()) { - ProjectDefinition def = reactor.getProjectDefinition(c.key()); - String version = getVersion(def); - builder.setVersion(version); - } - } - - private static String getVersion(ProjectDefinition def) { - String version = def.getVersion(); - return StringUtils.isNotBlank(version) ? version : getVersion(def.getParent()); - } - - private void writeLinks(BatchComponent c, BatchReport.Component.Builder builder) { - if (c.isProjectOrModule()) { - ProjectDefinition def = reactor.getProjectDefinition(c.key()); - ComponentLink.Builder linkBuilder = ComponentLink.newBuilder(); - - writeProjectLink(builder, def, linkBuilder, CoreProperties.LINKS_HOME_PAGE, ComponentLinkType.HOME); - writeProjectLink(builder, def, linkBuilder, CoreProperties.LINKS_CI, ComponentLinkType.CI); - writeProjectLink(builder, def, linkBuilder, CoreProperties.LINKS_ISSUE_TRACKER, ComponentLinkType.ISSUE); - writeProjectLink(builder, def, linkBuilder, CoreProperties.LINKS_SOURCES, ComponentLinkType.SCM); - writeProjectLink(builder, def, linkBuilder, CoreProperties.LINKS_SOURCES_DEV, ComponentLinkType.SCM_DEV); - } - } - - private static void writeProjectLink(BatchReport.Component.Builder componentBuilder, ProjectDefinition def, ComponentLink.Builder linkBuilder, String linkProp, - ComponentLinkType linkType) { - String link = def.properties().get(linkProp); - if (StringUtils.isNotBlank(link)) { - linkBuilder.setType(linkType); - linkBuilder.setHref(link); - componentBuilder.addLink(linkBuilder.build()); - linkBuilder.clear(); - } - } - - @CheckForNull - private static String getLanguageKey(Resource r) { - Language language = r.getLanguage(); - return ResourceUtils.isFile(r) && language != null ? language.getKey() : null; - } - - @CheckForNull - private static String getName(Resource r) { - // Don't return name for directories and files since it can be guessed from the path - return (ResourceUtils.isFile(r) || ResourceUtils.isDirectory(r)) ? null : r.getName(); - } - - @CheckForNull - private static String getDescription(Resource r) { - // Only for projets and modules - return ResourceUtils.isProject(r) ? r.getDescription() : null; - } - - private Constants.ComponentType getType(Resource r) { - if (ResourceUtils.isFile(r)) { - return Constants.ComponentType.FILE; - } else if (ResourceUtils.isDirectory(r)) { - return Constants.ComponentType.DIRECTORY; - } else if (ResourceUtils.isModuleProject(r)) { - return Constants.ComponentType.MODULE; - } else if (ResourceUtils.isRootProject(r)) { - return Constants.ComponentType.PROJECT; - } - throw new IllegalArgumentException("Unknown resource type: " + r); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/CoveragePublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/CoveragePublisher.java deleted file mode 100644 index 921c37f5a83..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/report/CoveragePublisher.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.report; - -import com.google.common.base.Function; -import com.google.common.collect.Iterables; -import java.util.LinkedHashMap; -import java.util.Map; -import javax.annotation.Nonnull; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; -import org.sonar.api.utils.KeyValueFormat; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.output.BatchReport.Coverage; -import org.sonar.batch.protocol.output.BatchReport.Coverage.Builder; -import org.sonar.batch.protocol.output.BatchReportWriter; -import org.sonar.batch.scan.measure.MeasureCache; - -public class CoveragePublisher implements ReportPublisherStep { - - private final BatchComponentCache resourceCache; - private final MeasureCache measureCache; - - public CoveragePublisher(BatchComponentCache resourceCache, MeasureCache measureCache) { - this.resourceCache = resourceCache; - this.measureCache = measureCache; - } - - @Override - public void publish(BatchReportWriter writer) { - for (final BatchComponent resource : resourceCache.all()) { - if (!resource.isFile()) { - continue; - } - Map<Integer, Coverage.Builder> coveragePerLine = new LinkedHashMap<>(); - - int lineCount = ((InputFile) resource.inputComponent()).lines(); - applyLineMeasure(resource.key(), lineCount, CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY, coveragePerLine, - new MeasureOperation() { - @Override - public void apply(String value, Coverage.Builder builder) { - builder.setUtHits(Integer.parseInt(value) > 0); - } - }); - applyLineMeasure(resource.key(), lineCount, CoreMetrics.CONDITIONS_BY_LINE_KEY, coveragePerLine, - new MeasureOperation() { - @Override - public void apply(String value, Coverage.Builder builder) { - builder.setConditions(Integer.parseInt(value)); - } - }); - applyLineMeasure(resource.key(), lineCount, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY, coveragePerLine, - new MeasureOperation() { - @Override - public void apply(String value, Coverage.Builder builder) { - builder.setUtCoveredConditions(Integer.parseInt(value)); - } - }); - applyLineMeasure(resource.key(), lineCount, CoreMetrics.IT_COVERAGE_LINE_HITS_DATA_KEY, coveragePerLine, - new MeasureOperation() { - @Override - public void apply(String value, Coverage.Builder builder) { - builder.setItHits(Integer.parseInt(value) > 0); - } - }); - applyLineMeasure(resource.key(), lineCount, CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE_KEY, coveragePerLine, - new MeasureOperation() { - @Override - public void apply(String value, Coverage.Builder builder) { - builder.setItCoveredConditions(Integer.parseInt(value)); - } - }); - applyLineMeasure(resource.key(), lineCount, CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE_KEY, coveragePerLine, - new MeasureOperation() { - @Override - public void apply(String value, Coverage.Builder builder) { - builder.setOverallCoveredConditions(Integer.parseInt(value)); - } - }); - writer.writeComponentCoverage(resource.batchId(), Iterables.transform(coveragePerLine.values(), BuildCoverage.INSTANCE)); - } - } - - void applyLineMeasure(String inputFileKey, int lineCount, String metricKey, Map<Integer, Coverage.Builder> coveragePerLine, MeasureOperation op) { - Measure measure = measureCache.byMetric(inputFileKey, metricKey); - if (measure != null) { - Map<Integer, String> lineMeasures = KeyValueFormat.parseIntString((String) measure.value()); - for (Map.Entry<Integer, String> lineMeasure : lineMeasures.entrySet()) { - int lineIdx = lineMeasure.getKey(); - if (lineIdx <= lineCount) { - String value = lineMeasure.getValue(); - if (StringUtils.isNotEmpty(value)) { - Coverage.Builder coverageBuilder = coveragePerLine.get(lineIdx); - if (coverageBuilder == null) { - coverageBuilder = Coverage.newBuilder(); - coverageBuilder.setLine(lineIdx); - coveragePerLine.put(lineIdx, coverageBuilder); - } - op.apply(value, coverageBuilder); - } - } - } - } - } - - interface MeasureOperation { - void apply(String value, Coverage.Builder builder); - } - - private enum BuildCoverage implements Function<Coverage.Builder, Coverage> { - INSTANCE; - - @Override - public Coverage apply(@Nonnull Builder input) { - return input.build(); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java deleted file mode 100644 index e19b590610f..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.report; - -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import java.io.Serializable; -import java.util.Set; -import javax.annotation.Nonnull; -import org.sonar.api.batch.measure.Metric; -import org.sonar.api.measures.Measure; -import org.sonar.api.measures.Metric.ValueType; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.Constants.MeasureValueType; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReportWriter; -import org.sonar.batch.scan.measure.MeasureCache; -import org.sonar.core.metric.BatchMetrics; - -import static com.google.common.collect.Iterables.filter; -import static com.google.common.collect.Iterables.transform; -import static com.google.common.collect.Sets.newHashSet; - -public class MeasuresPublisher implements ReportPublisherStep { - - private static final class MeasureToReportMeasure implements Function<Measure, BatchReport.Measure> { - private final BatchComponent resource; - private final BatchReport.Measure.Builder builder = BatchReport.Measure.newBuilder(); - - private MeasureToReportMeasure(BatchComponent resource) { - this.resource = resource; - } - - @Override - public BatchReport.Measure apply(@Nonnull Measure input) { - validateMeasure(input, resource.key()); - return toReportMeasure(builder, input); - } - - private static void validateMeasure(Measure measure, String componentKey) { - if (measure.getValue() == null && measure.getData() == null) { - throw new IllegalArgumentException(String.format("Measure on metric '%s' and component '%s' has no value, but it's not allowed", measure.getMetricKey(), componentKey)); - } - } - - private BatchReport.Measure toReportMeasure(BatchReport.Measure.Builder builder, Measure measure) { - builder.clear(); - - builder.setValueType(getMeasureValueType(measure.getMetric().getType())); - setValueAccordingToType(builder, measure); - // Because some numeric measures also have a data (like maintainability rating) - String data = measure.getData(); - if (data != null) { - builder.setStringValue(data); - } - builder.setMetricKey(measure.getMetricKey()); - return builder.build(); - } - - private void setValueAccordingToType(BatchReport.Measure.Builder builder, Measure measure) { - Serializable value = measure.value(); - switch (builder.getValueType()) { - case BOOLEAN: - builder.setBooleanValue((Boolean) value); - break; - case DOUBLE: - builder.setDoubleValue(((Number) value).doubleValue()); - break; - case INT: - builder.setIntValue(((Number) value).intValue()); - break; - case LONG: - builder.setLongValue(((Number) value).longValue()); - break; - case STRING: - builder.setStringValue((String) value); - break; - default: - throw new IllegalStateException("Unknown value type: " + builder.getValueType()); - } - } - - private MeasureValueType getMeasureValueType(ValueType type) { - switch (type) { - case INT: - case RATING: - return MeasureValueType.INT; - case FLOAT: - case PERCENT: - return MeasureValueType.DOUBLE; - case BOOL: - return MeasureValueType.BOOLEAN; - case STRING: - case DATA: - case LEVEL: - case DISTRIB: - return MeasureValueType.STRING; - case WORK_DUR: - case MILLISEC: - return MeasureValueType.LONG; - default: - throw new IllegalStateException("Unknown value type: " + type); - } - } - - } - - private static final class IsMetricAllowed implements Predicate<Measure> { - private final Set<String> allowedMetricKeys; - - private IsMetricAllowed(Set<String> allowedMetricKeys) { - this.allowedMetricKeys = allowedMetricKeys; - } - - @Override - public boolean apply(Measure input) { - return allowedMetricKeys.contains(input.getMetricKey()); - } - } - - private static final class MetricToKey implements Function<Metric, String> { - @Override - public String apply(Metric input) { - return input.key(); - } - } - - private final BatchComponentCache resourceCache; - private final MeasureCache measureCache; - private final BatchMetrics batchMetrics; - - public MeasuresPublisher(BatchComponentCache resourceCache, MeasureCache measureCache, BatchMetrics batchMetrics) { - this.resourceCache = resourceCache; - this.measureCache = measureCache; - this.batchMetrics = batchMetrics; - } - - @Override - public void publish(BatchReportWriter writer) { - final Set<String> allowedMetricKeys = newHashSet(transform(batchMetrics.getMetrics(), new MetricToKey())); - for (final BatchComponent resource : resourceCache.all()) { - Iterable<Measure> batchMeasures = measureCache.byResource(resource.resource()); - Iterable<org.sonar.batch.protocol.output.BatchReport.Measure> reportMeasures = transform( - filter(batchMeasures, new IsMetricAllowed(allowedMetricKeys)), - new MeasureToReportMeasure(resource)); - writer.writeComponentMeasures(resource.batchId(), reportMeasures); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/MetadataPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/MetadataPublisher.java deleted file mode 100644 index eac42e0ad7d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/report/MetadataPublisher.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.report; - -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.config.Settings; -import org.sonar.api.resources.Project; -import org.sonar.batch.cpd.index.SonarCpdBlockIndex; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReportWriter; -import org.sonar.batch.scan.ImmutableProjectReactor; - -public class MetadataPublisher implements ReportPublisherStep { - - private final BatchComponentCache componentCache; - private final ImmutableProjectReactor reactor; - private final Settings settings; - - public MetadataPublisher(BatchComponentCache componentCache, ImmutableProjectReactor reactor, Settings settings) { - this.componentCache = componentCache; - this.reactor = reactor; - this.settings = settings; - } - - @Override - public void publish(BatchReportWriter writer) { - ProjectDefinition root = reactor.getRoot(); - BatchComponent rootProject = componentCache.getRoot(); - BatchReport.Metadata.Builder builder = BatchReport.Metadata.newBuilder() - .setAnalysisDate(((Project) rootProject.resource()).getAnalysisDate().getTime()) - // Here we want key without branch - .setProjectKey(root.getKey()) - .setCrossProjectDuplicationActivated(SonarCpdBlockIndex.isCrossProjectDuplicationEnabled(settings)) - .setRootComponentRef(rootProject.batchId()); - String branch = root.properties().get(CoreProperties.PROJECT_BRANCH_PROPERTY); - if (branch != null) { - builder.setBranch(branch); - } - writer.writeMetadata(builder.build()); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java deleted file mode 100644 index 4654892fb57..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java +++ /dev/null @@ -1,242 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.report; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Throwables; -import com.google.common.io.Files; -import com.squareup.okhttp.HttpUrl; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.Writer; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.LinkedHashMap; -import java.util.Map; -import javax.annotation.Nullable; -import org.apache.commons.io.FileUtils; -import org.picocontainer.Startable; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.MessageException; -import org.sonar.api.utils.TempFolder; -import org.sonar.api.utils.ZipUtils; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.batch.analysis.DefaultAnalysisMode; -import org.sonar.batch.bootstrap.BatchWsClient; -import org.sonar.batch.protocol.output.BatchReportWriter; -import org.sonar.batch.scan.ImmutableProjectReactor; -import org.sonarqube.ws.MediaTypes; -import org.sonarqube.ws.WsCe; -import org.sonarqube.ws.client.PostRequest; -import org.sonarqube.ws.client.WsResponse; - -import static org.apache.commons.lang.StringUtils.trimToEmpty; -import static org.sonar.core.util.FileUtils.deleteQuietly; - -@BatchSide -public class ReportPublisher implements Startable { - - private static final Logger LOG = Loggers.get(ReportPublisher.class); - - public static final String KEEP_REPORT_PROP_KEY = "sonar.batch.keepReport"; - public static final String VERBOSE_KEY = "sonar.verbose"; - public static final String METADATA_DUMP_FILENAME = "report-task.txt"; - - private final Settings settings; - private final BatchWsClient wsClient; - private final AnalysisContextReportPublisher contextPublisher; - private final ImmutableProjectReactor projectReactor; - private final DefaultAnalysisMode analysisMode; - private final TempFolder temp; - private final ReportPublisherStep[] publishers; - - private File reportDir; - private BatchReportWriter writer; - - public ReportPublisher(Settings settings, BatchWsClient wsClient, AnalysisContextReportPublisher contextPublisher, - ImmutableProjectReactor projectReactor, DefaultAnalysisMode analysisMode, TempFolder temp, ReportPublisherStep[] publishers) { - this.settings = settings; - this.wsClient = wsClient; - this.contextPublisher = contextPublisher; - this.projectReactor = projectReactor; - this.analysisMode = analysisMode; - this.temp = temp; - this.publishers = publishers; - } - - @Override - public void start() { - reportDir = new File(projectReactor.getRoot().getWorkDir(), "batch-report"); - writer = new BatchReportWriter(reportDir); - contextPublisher.init(writer); - - if (!analysisMode.isIssues() && !analysisMode.isMediumTest()) { - String publicUrl = publicUrl(); - if (HttpUrl.parse(publicUrl) == null) { - throw MessageException.of("Failed to parse public URL set in SonarQube server: " + publicUrl); - } - } - } - - @Override - public void stop() { - if (!settings.getBoolean(KEEP_REPORT_PROP_KEY) && !settings.getBoolean(VERBOSE_KEY)) { - deleteQuietly(reportDir); - } else { - LOG.info("Analysis report generated in " + reportDir); - } - } - - public File getReportDir() { - return reportDir; - } - - public BatchReportWriter getWriter() { - return writer; - } - - public void execute() { - // If this is a issues mode analysis then we should not upload reports - String taskId = null; - if (!analysisMode.isIssues()) { - File report = generateReportFile(); - if (!analysisMode.isMediumTest()) { - taskId = upload(report); - } - } - logSuccess(taskId); - } - - private File generateReportFile() { - try { - long startTime = System.currentTimeMillis(); - for (ReportPublisherStep publisher : publishers) { - publisher.publish(writer); - } - long stopTime = System.currentTimeMillis(); - LOG.info("Analysis report generated in {}ms, dir size={}", stopTime - startTime, FileUtils.byteCountToDisplaySize(FileUtils.sizeOfDirectory(reportDir))); - - startTime = System.currentTimeMillis(); - File reportZip = temp.newFile("batch-report", ".zip"); - ZipUtils.zipDir(reportDir, reportZip); - stopTime = System.currentTimeMillis(); - LOG.info("Analysis reports compressed in {}ms, zip size={}", stopTime - startTime, FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(reportZip))); - return reportZip; - } catch (IOException e) { - throw new IllegalStateException("Unable to prepare analysis report", e); - } - } - - /** - * Uploads the report file to server and returns the generated task id - */ - @VisibleForTesting - String upload(File report) { - LOG.debug("Upload report"); - long startTime = System.currentTimeMillis(); - ProjectDefinition projectDefinition = projectReactor.getRoot(); - PostRequest.Part filePart = new PostRequest.Part(MediaTypes.ZIP, report); - PostRequest post = new PostRequest("api/ce/submit") - .setMediaType(MediaTypes.PROTOBUF) - .setParam("projectKey", projectDefinition.getKey()) - .setParam("projectName", projectDefinition.getName()) - .setParam("projectBranch", projectDefinition.getBranch()) - .setPart("report", filePart); - WsResponse response = wsClient.call(post).failIfNotSuccessful(); - try (InputStream protobuf = response.contentStream()) { - return WsCe.SubmitResponse.parser().parseFrom(protobuf).getTaskId(); - } catch (Exception e) { - throw Throwables.propagate(e); - } finally { - long stopTime = System.currentTimeMillis(); - LOG.info("Analysis report uploaded in " + (stopTime - startTime) + "ms"); - } - } - - @VisibleForTesting - void logSuccess(@Nullable String taskId) { - if (taskId == null) { - LOG.info("ANALYSIS SUCCESSFUL"); - } else { - String publicUrl = publicUrl(); - HttpUrl httpUrl = HttpUrl.parse(publicUrl); - - Map<String, String> metadata = new LinkedHashMap<>(); - String effectiveKey = projectReactor.getRoot().getKeyWithBranch(); - metadata.put("projectKey", effectiveKey); - metadata.put("serverUrl", publicUrl); - - URL dashboardUrl = httpUrl.newBuilder() - .addPathSegment("dashboard").addPathSegment("index").addPathSegment(effectiveKey) - .build() - .url(); - metadata.put("dashboardUrl", dashboardUrl.toExternalForm()); - - URL taskUrl = HttpUrl.parse(publicUrl).newBuilder() - .addPathSegment("api").addPathSegment("ce").addPathSegment("task") - .addQueryParameter("id", taskId) - .build() - .url(); - metadata.put("ceTaskId", taskId); - metadata.put("ceTaskUrl", taskUrl.toExternalForm()); - - LOG.info("ANALYSIS SUCCESSFUL, you can browse {}", dashboardUrl); - LOG.info("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report"); - LOG.info("More about the report processing at {}", taskUrl); - - dumpMetadata(metadata); - } - } - - private void dumpMetadata(Map<String, String> metadata) { - File file = new File(projectReactor.getRoot().getWorkDir(), METADATA_DUMP_FILENAME); - try (Writer output = Files.newWriter(file, StandardCharsets.UTF_8)) { - for (Map.Entry<String, String> entry : metadata.entrySet()) { - output.write(entry.getKey()); - output.write("="); - output.write(entry.getValue()); - output.write("\n"); - } - - LOG.debug("Report metadata written to {}", file); - } catch (IOException e) { - throw new IllegalStateException("Unable to dump " + file, e); - } - } - - /** - * The public URL is optionally configured on server. If not, then the regular URL is returned. - * See https://jira.sonarsource.com/browse/SONAR-4239 - */ - private String publicUrl() { - String baseUrl = trimToEmpty(settings.getString(CoreProperties.SERVER_BASE_URL)); - if (baseUrl.equals(settings.getDefaultValue(CoreProperties.SERVER_BASE_URL))) { - // crap workaround for https://jira.sonarsource.com/browse/SONAR-7109 - // If server base URL was not configured in Sonar server then is is better to take URL configured on batch side - baseUrl = wsClient.baseUrl(); - } - return baseUrl.replaceAll("(/)+$", ""); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisherStep.java b/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisherStep.java deleted file mode 100644 index 873e95a5683..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisherStep.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.report; - -import org.sonar.batch.protocol.output.BatchReportWriter; - -/** - * Adds a sub-part of data to output report - */ -public interface ReportPublisherStep { - - void publish(BatchReportWriter writer); - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/SourcePublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/SourcePublisher.java deleted file mode 100644 index 02fe7449038..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/report/SourcePublisher.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.report; - -import org.apache.commons.io.ByteOrderMark; -import org.apache.commons.io.IOUtils; -import org.apache.commons.io.input.BOMInputStream; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.output.BatchReportWriter; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; - -public class SourcePublisher implements ReportPublisherStep { - - private final BatchComponentCache resourceCache; - - public SourcePublisher(BatchComponentCache resourceCache) { - this.resourceCache = resourceCache; - } - - @Override - public void publish(BatchReportWriter writer) { - for (final BatchComponent resource : resourceCache.all()) { - if (!resource.isFile()) { - continue; - } - - DefaultInputFile inputFile = (DefaultInputFile) resource.inputComponent(); - File iofile = writer.getSourceFile(resource.batchId()); - int line = 0; - try (FileOutputStream output = new FileOutputStream(iofile); BOMInputStream bomIn = new BOMInputStream(new FileInputStream(inputFile.file()), - ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE); - BufferedReader reader = new BufferedReader(new InputStreamReader(bomIn, inputFile.charset()))) { - String lineStr = reader.readLine(); - while (lineStr != null) { - IOUtils.write(lineStr, output, StandardCharsets.UTF_8); - line++; - if (line < inputFile.lines()) { - IOUtils.write("\n", output, StandardCharsets.UTF_8); - } - lineStr = reader.readLine(); - } - } catch (IOException e) { - throw new IllegalStateException("Unable to store file source in the report", e); - } - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/TestExecutionAndCoveragePublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/TestExecutionAndCoveragePublisher.java deleted file mode 100644 index 22513bdc9d9..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/report/TestExecutionAndCoveragePublisher.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.report; - -import com.google.common.base.Function; -import com.google.common.collect.Iterables; -import java.util.HashSet; -import java.util.Set; -import javax.annotation.Nonnull; -import org.sonar.api.batch.fs.InputFile.Type; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.test.CoverageBlock; -import org.sonar.api.test.MutableTestCase; -import org.sonar.api.test.MutableTestPlan; -import org.sonar.api.test.TestCase; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.Constants.TestStatus; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReport.CoverageDetail; -import org.sonar.batch.protocol.output.BatchReport.Test; -import org.sonar.batch.protocol.output.BatchReportWriter; -import org.sonar.batch.test.DefaultTestable; -import org.sonar.batch.test.TestPlanBuilder; - -public class TestExecutionAndCoveragePublisher implements ReportPublisherStep { - - private static final class TestConverter implements Function<MutableTestCase, BatchReport.Test> { - private final Set<String> testNamesWithCoverage; - private BatchReport.Test.Builder builder = BatchReport.Test.newBuilder(); - - private TestConverter(Set<String> testNamesWithCoverage) { - this.testNamesWithCoverage = testNamesWithCoverage; - } - - @Override - public Test apply(@Nonnull MutableTestCase testCase) { - builder.clear(); - builder.setName(testCase.name()); - if (testCase.doesCover()) { - testNamesWithCoverage.add(testCase.name()); - } - Long durationInMs = testCase.durationInMs(); - if (durationInMs != null) { - builder.setDurationInMs(durationInMs); - } - String msg = testCase.message(); - if (msg != null) { - builder.setMsg(msg); - } - String stack = testCase.stackTrace(); - if (stack != null) { - builder.setStacktrace(stack); - } - TestCase.Status status = testCase.status(); - if (status != null) { - builder.setStatus(TestStatus.valueOf(status.name())); - } - return builder.build(); - } - } - - private final class TestCoverageConverter implements Function<String, CoverageDetail> { - private final MutableTestPlan testPlan; - private BatchReport.CoverageDetail.Builder builder = BatchReport.CoverageDetail.newBuilder(); - private BatchReport.CoverageDetail.CoveredFile.Builder coveredBuilder = BatchReport.CoverageDetail.CoveredFile.newBuilder(); - - private TestCoverageConverter(MutableTestPlan testPlan) { - this.testPlan = testPlan; - } - - @Override - public CoverageDetail apply(@Nonnull String testName) { - // Take first test with provided name - MutableTestCase testCase = testPlan.testCasesByName(testName).iterator().next(); - builder.clear(); - builder.setTestName(testName); - for (CoverageBlock block : testCase.coverageBlocks()) { - coveredBuilder.clear(); - coveredBuilder.setFileRef(componentCache.get(((DefaultTestable) block.testable()).inputFile().key()).batchId()); - for (int line : block.lines()) { - coveredBuilder.addCoveredLine(line); - } - builder.addCoveredFile(coveredBuilder.build()); - } - return builder.build(); - } - } - - private final BatchComponentCache componentCache; - private final TestPlanBuilder testPlanBuilder; - - public TestExecutionAndCoveragePublisher(BatchComponentCache resourceCache, TestPlanBuilder testPlanBuilder) { - this.componentCache = resourceCache; - this.testPlanBuilder = testPlanBuilder; - } - - @Override - public void publish(BatchReportWriter writer) { - for (final BatchComponent component : componentCache.all()) { - if (!component.isFile()) { - continue; - } - - DefaultInputFile inputFile = (DefaultInputFile) component.inputComponent(); - if (inputFile.type() != Type.TEST) { - continue; - } - - final MutableTestPlan testPlan = testPlanBuilder.loadPerspective(MutableTestPlan.class, component); - if (testPlan == null || Iterables.isEmpty(testPlan.testCases())) { - continue; - } - - final Set<String> testNamesWithCoverage = new HashSet<>(); - - writer.writeTests(component.batchId(), Iterables.transform(testPlan.testCases(), new TestConverter(testNamesWithCoverage))); - - writer.writeCoverageDetails(component.batchId(), Iterables.transform(testNamesWithCoverage, new TestCoverageConverter(testPlan))); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/report/package-info.java deleted file mode 100644 index f4b3d8e51a9..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/report/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.report; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultGlobalRepositoriesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultGlobalRepositoriesLoader.java deleted file mode 100644 index 630b49a0bba..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultGlobalRepositoriesLoader.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -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.sonar.batch.protocol.input.GlobalRepositories; - -public class DefaultGlobalRepositoriesLoader implements GlobalRepositoriesLoader { - - private static final String BATCH_GLOBAL_URL = "/batch/global"; - - private final WSLoader wsLoader; - - public DefaultGlobalRepositoriesLoader(WSLoader wsLoader) { - this.wsLoader = wsLoader; - } - - @Override - public GlobalRepositories load(@Nullable MutableBoolean fromCache) { - WSLoaderResult<String> result = wsLoader.loadString(BATCH_GLOBAL_URL); - if (fromCache != null) { - fromCache.setValue(result.isFromCache()); - } - return GlobalRepositories.fromJson(result.get()); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java deleted file mode 100644 index 0e8a0904b57..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -import com.google.common.base.Throwables; - -import com.google.common.collect.HashBasedTable; -import com.google.common.collect.Table; - -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.util.Date; -import java.util.Map; - -import javax.annotation.Nullable; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.mutable.MutableBoolean; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.utils.MessageException; -import org.sonar.batch.cache.WSLoader; -import org.sonar.batch.cache.WSLoaderResult; -import org.sonar.batch.util.BatchUtils; -import org.sonarqube.ws.WsBatch.WsProjectResponse; -import org.sonarqube.ws.WsBatch.WsProjectResponse.FileDataByPath; -import org.sonarqube.ws.WsBatch.WsProjectResponse.Settings; -import org.sonarqube.ws.client.HttpException; - -public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoader { - private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectRepositoriesLoader.class); - private static final String BATCH_PROJECT_URL = "/batch/project.protobuf"; - private final WSLoader loader; - - public DefaultProjectRepositoriesLoader(WSLoader loader) { - this.loader = loader; - } - - @Override - public ProjectRepositories load(String projectKey, boolean issuesMode, @Nullable MutableBoolean fromCache) { - try { - WSLoaderResult<InputStream> result = loader.loadStream(getUrl(projectKey, issuesMode)); - if (fromCache != null) { - fromCache.setValue(result.isFromCache()); - } - return processStream(result.get(), projectKey); - } catch (RuntimeException e) { - if (shouldThrow(e)) { - throw e; - } - - LOG.debug("Project repository not available - continuing without it", e); - return new ProjectRepositories(); - } - } - - private static String getUrl(String projectKey, boolean issuesMode) { - StringBuilder builder = new StringBuilder(); - - builder.append(BATCH_PROJECT_URL) - .append("?key=").append(BatchUtils.encodeForUrl(projectKey)); - if (issuesMode) { - builder.append("&issues_mode=true"); - } - return builder.toString(); - } - - private static boolean shouldThrow(Exception e) { - for (Throwable t : Throwables.getCausalChain(e)) { - if (t instanceof HttpException) { - HttpException http = (HttpException) t; - return http.code() != HttpURLConnection.HTTP_NOT_FOUND; - } - if (t instanceof MessageException) { - return true; - } - } - - return false; - } - - private static ProjectRepositories processStream(InputStream is, String projectKey) { - try { - WsProjectResponse response = WsProjectResponse.parseFrom(is); - - Table<String, String, FileData> fileDataTable = HashBasedTable.create(); - Table<String, String, String> settings = HashBasedTable.create(); - - Map<String, Settings> settingsByModule = response.getSettingsByModule(); - for (Map.Entry<String, Settings> e1 : settingsByModule.entrySet()) { - for (Map.Entry<String, String> e2 : e1.getValue().getSettings().entrySet()) { - settings.put(e1.getKey(), e2.getKey(), e2.getValue()); - } - } - - Map<String, FileDataByPath> fileDataByModuleAndPath = response.getFileDataByModuleAndPath(); - for (Map.Entry<String, FileDataByPath> e1 : fileDataByModuleAndPath.entrySet()) { - for (Map.Entry<String, org.sonarqube.ws.WsBatch.WsProjectResponse.FileData> e2 : e1.getValue().getFileDataByPath().entrySet()) { - FileData fd = new FileData(e2.getValue().getHash(), e2.getValue().getRevision()); - fileDataTable.put(e1.getKey(), e2.getKey(), fd); - } - } - - return new ProjectRepositories(settings, fileDataTable, new Date(response.getLastAnalysisDate())); - } catch (IOException e) { - throw new IllegalStateException("Couldn't load project repository for " + projectKey, e); - } finally { - IOUtils.closeQuietly(is); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java deleted file mode 100644 index 524e6f6856d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -import org.sonar.api.utils.MessageException; - -import org.sonarqube.ws.QualityProfiles.SearchWsResponse; -import org.sonar.batch.util.BatchUtils; -import org.apache.commons.io.IOUtils; -import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile; -import org.apache.commons.lang.mutable.MutableBoolean; -import org.sonar.batch.cache.WSLoaderResult; -import org.sonar.batch.cache.WSLoader; - -import javax.annotation.Nullable; - -import java.io.IOException; -import java.io.InputStream; -import java.util.List; - -public class DefaultQualityProfileLoader implements QualityProfileLoader { - private static final String WS_URL = "/api/qualityprofiles/search.protobuf"; - - private WSLoader wsLoader; - - public DefaultQualityProfileLoader(WSLoader wsLoader) { - this.wsLoader = wsLoader; - } - - @Override - public List<QualityProfile> loadDefault(@Nullable String profileName, @Nullable MutableBoolean fromCache) { - String url = WS_URL + "?defaults=true"; - if (profileName != null) { - url += "&profileName=" + BatchUtils.encodeForUrl(profileName); - } - return loadResource(url, fromCache); - } - - @Override - public List<QualityProfile> load(String projectKey, @Nullable String profileName, @Nullable MutableBoolean fromCache) { - String url = WS_URL + "?projectKey=" + BatchUtils.encodeForUrl(projectKey); - if (profileName != null) { - url += "&profileName=" + BatchUtils.encodeForUrl(profileName); - } - return loadResource(url, fromCache); - } - - private List<QualityProfile> loadResource(String url, @Nullable MutableBoolean fromCache) { - WSLoaderResult<InputStream> result = wsLoader.loadStream(url); - if (fromCache != null) { - fromCache.setValue(result.isFromCache()); - } - InputStream is = result.get(); - SearchWsResponse profiles = null; - - try { - profiles = SearchWsResponse.parseFrom(is); - } catch (IOException e) { - throw new IllegalStateException("Failed to load quality profiles", e); - } finally { - IOUtils.closeQuietly(is); - } - - List<QualityProfile> profilesList = profiles.getProfilesList(); - if (profilesList == null || profilesList.isEmpty()) { - throw MessageException.of("No quality profiles have been found, you probably don't have any language plugin installed."); - } - return profilesList; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultServerIssuesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultServerIssuesLoader.java deleted file mode 100644 index 853b4ae20db..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultServerIssuesLoader.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -import com.google.common.base.Function; -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 { - - private final WSLoader wsLoader; - - public DefaultServerIssuesLoader(WSLoader wsLoader) { - this.wsLoader = wsLoader; - } - - @Override - public boolean load(String componentKey, Function<ServerIssue, Void> consumer) { - WSLoaderResult<InputStream> result = wsLoader.loadStream("/batch/issues.protobuf?key=" + BatchUtils.encodeForUrl(componentKey)); - parseIssues(result.get(), consumer); - return result.isFromCache(); - } - - private static void parseIssues(InputStream is, Function<ServerIssue, Void> consumer) { - try { - ServerIssue previousIssue = ServerIssue.parseDelimitedFrom(is); - while (previousIssue != null) { - consumer.apply(previousIssue); - previousIssue = ServerIssue.parseDelimitedFrom(is); - } - } catch (IOException e) { - throw new IllegalStateException("Unable to get previous issues", e); - } finally { - IOUtils.closeQuietly(is); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/FileData.java b/sonar-batch/src/main/java/org/sonar/batch/repository/FileData.java deleted file mode 100644 index bcf470e8986..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/FileData.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -import javax.annotation.concurrent.Immutable; - -@Immutable -public class FileData { - private final String hash; - private final String revision; - - public FileData(String hash, String revision) { - this.hash = hash; - this.revision = revision; - } - - public String hash() { - return hash; - } - - public String revision() { - return revision; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/GlobalRepositoriesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/GlobalRepositoriesLoader.java deleted file mode 100644 index 7c32e2bf484..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/GlobalRepositoriesLoader.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -import org.apache.commons.lang.mutable.MutableBoolean; - -import javax.annotation.Nullable; - -import org.sonar.batch.protocol.input.GlobalRepositories; - -public interface GlobalRepositoriesLoader { - - GlobalRepositories load(@Nullable MutableBoolean fromCache); - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/GlobalRepositoriesProvider.java b/sonar-batch/src/main/java/org/sonar/batch/repository/GlobalRepositoriesProvider.java deleted file mode 100644 index 56f741a6c74..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/GlobalRepositoriesProvider.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -import org.apache.commons.lang.mutable.MutableBoolean; - -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; -import org.sonar.batch.protocol.input.GlobalRepositories; - -public class GlobalRepositoriesProvider extends ProviderAdapter { - - private static final Logger LOG = Loggers.get(GlobalRepositoriesProvider.class); - private static final String LOG_MSG = "Load global repositories"; - private GlobalRepositories globalReferentials; - - public GlobalRepositories provide(GlobalRepositoriesLoader loader) { - if (globalReferentials == null) { - Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - MutableBoolean fromCache = new MutableBoolean(); - globalReferentials = loader.load(fromCache); - profiler.stopInfo(fromCache.booleanValue()); - } - return globalReferentials; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositories.java b/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositories.java deleted file mode 100644 index baba0c52396..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositories.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -import com.google.common.collect.HashBasedTable; -import com.google.common.collect.Table; -import java.util.Date; -import java.util.Map; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - -public class ProjectRepositories { - private final Table<String, String, String> settingsByModule; - private final Table<String, String, FileData> fileDataByModuleAndPath; - private final Date lastAnalysisDate; - private final boolean exists; - - public ProjectRepositories() { - this.exists = false; - this.settingsByModule = HashBasedTable.create(); - this.fileDataByModuleAndPath = HashBasedTable.create(); - this.lastAnalysisDate = null; - } - - public ProjectRepositories(Table<String, String, String> settingsByModule, Table<String, String, FileData> fileDataByModuleAndPath, - @Nullable Date lastAnalysisDate) { - this.settingsByModule = settingsByModule; - this.fileDataByModuleAndPath = fileDataByModuleAndPath; - this.lastAnalysisDate = lastAnalysisDate; - this.exists = true; - } - - public boolean exists() { - return exists; - } - - public Map<String, FileData> fileDataByPath(String moduleKey) { - return fileDataByModuleAndPath.row(moduleKey); - } - - public Table<String, String, FileData> fileDataByModuleAndPath() { - return fileDataByModuleAndPath; - } - - public boolean moduleExists(String moduleKey) { - return settingsByModule.containsRow(moduleKey); - } - - public Map<String, String> settings(String moduleKey) { - return settingsByModule.row(moduleKey); - } - - @CheckForNull - public FileData fileData(String projectKey, String path) { - return fileDataByModuleAndPath.get(projectKey, path); - } - - @CheckForNull - public Date lastAnalysisDate() { - return lastAnalysisDate; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositoriesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositoriesLoader.java deleted file mode 100644 index 53de4805324..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositoriesLoader.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -import javax.annotation.Nullable; - -import org.apache.commons.lang.mutable.MutableBoolean; - -public interface ProjectRepositoriesLoader { - ProjectRepositories load(String projectKeyWithBranch, boolean issuesMode, @Nullable MutableBoolean fromCache); -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositoriesProvider.java b/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositoriesProvider.java deleted file mode 100644 index 1e0469651d5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/ProjectRepositoriesProvider.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -import org.sonar.api.utils.log.Profiler; - -import org.sonar.api.batch.bootstrap.ProjectKey; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - -import org.sonar.batch.analysis.DefaultAnalysisMode; -import org.apache.commons.lang.mutable.MutableBoolean; -import org.picocontainer.injectors.ProviderAdapter; - -public class ProjectRepositoriesProvider extends ProviderAdapter { - private static final Logger LOG = Loggers.get(ProjectRepositoriesProvider.class); - private static final String LOG_MSG = "Load project repositories"; - private ProjectRepositories project = null; - - public ProjectRepositories provide(ProjectRepositoriesLoader loader, ProjectKey projectKey, DefaultAnalysisMode mode) { - if (project == null) { - MutableBoolean fromCache = new MutableBoolean(false); - Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - if (mode.isNotAssociated()) { - project = createNonAssociatedProjectRepositories(); - profiler.stopInfo(); - } else { - project = loader.load(projectKey.get(), mode.isIssues(), fromCache); - checkProject(mode); - profiler.stopInfo(fromCache.booleanValue()); - } - - } - - return project; - } - - private void checkProject(DefaultAnalysisMode mode) { - if (mode.isIssues()) { - if (!project.exists()) { - LOG.warn("Project doesn't exist on the server. All issues will be marked as 'new'."); - } else if (project.lastAnalysisDate() == null && !mode.isNotAssociated()) { - LOG.warn("No analysis has been found on the server for this project. All issues will be marked as 'new'."); - } - } - } - - private static ProjectRepositories createNonAssociatedProjectRepositories() { - return new ProjectRepositories(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileLoader.java deleted file mode 100644 index a93bea66b11..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileLoader.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -import org.apache.commons.lang.mutable.MutableBoolean; -import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile; - -import javax.annotation.Nullable; - -import java.util.List; - -public interface QualityProfileLoader { - List<QualityProfile> load(String projectKey, @Nullable String profileName, @Nullable MutableBoolean fromCache); - - List<QualityProfile> loadDefault(@Nullable String profileName, @Nullable MutableBoolean fromCache); -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileProvider.java b/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileProvider.java deleted file mode 100644 index 6878d21ecfb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/QualityProfileProvider.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -import java.util.List; -import javax.annotation.CheckForNull; -import org.apache.commons.lang.mutable.MutableBoolean; -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.batch.bootstrap.ProjectKey; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; -import org.sonar.batch.analysis.AnalysisProperties; -import org.sonar.batch.analysis.DefaultAnalysisMode; -import org.sonar.batch.rule.ModuleQProfiles; -import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile; - -public class QualityProfileProvider extends ProviderAdapter { - private static final Logger LOG = Loggers.get(QualityProfileProvider.class); - private static final String LOG_MSG = "Load quality profiles"; - private ModuleQProfiles profiles = null; - - public ModuleQProfiles provide(ProjectKey projectKey, QualityProfileLoader loader, ProjectRepositories projectRepositories, AnalysisProperties props, DefaultAnalysisMode mode) { - if (this.profiles == null) { - List<QualityProfile> profileList; - MutableBoolean fromCache = new MutableBoolean(); - - Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - if (mode.isNotAssociated() || !projectRepositories.exists()) { - profileList = loader.loadDefault(getSonarProfile(props, mode), fromCache); - } else { - profileList = loader.load(projectKey.get(), getSonarProfile(props, mode), fromCache); - } - profiler.stopInfo(fromCache.booleanValue()); - profiles = new ModuleQProfiles(profileList); - } - - return profiles; - } - - @CheckForNull - private static String getSonarProfile(AnalysisProperties props, DefaultAnalysisMode mode) { - String profile = null; - if (!mode.isIssues() && props.properties().containsKey(ModuleQProfiles.SONAR_PROFILE_PROP)) { - profile = props.property(ModuleQProfiles.SONAR_PROFILE_PROP); - LOG.warn("Ability to set quality profile from command line using '" + ModuleQProfiles.SONAR_PROFILE_PROP - + "' is deprecated and will be dropped in a future SonarQube version. Please configure quality profile used by your project on SonarQube server."); - } - return profile; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/ServerIssuesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/ServerIssuesLoader.java deleted file mode 100644 index 7f9154a54ad..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/ServerIssuesLoader.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository; - -import com.google.common.base.Function; -import org.sonar.batch.protocol.input.BatchInput.ServerIssue; - -public interface ServerIssuesLoader { - - boolean load(String componentKey, Function<ServerIssue, Void> consumer); - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/language/DefaultLanguagesRepository.java b/sonar-batch/src/main/java/org/sonar/batch/repository/language/DefaultLanguagesRepository.java deleted file mode 100644 index 4f891e64f3d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/language/DefaultLanguagesRepository.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository.language; - -import org.picocontainer.Startable; - -import org.sonar.api.resources.Languages; - -import javax.annotation.CheckForNull; - -import java.util.ArrayList; -import java.util.Collection; - -/** - * Languages repository using {@link Languages} - * @since 4.4 - */ -public class DefaultLanguagesRepository implements LanguagesRepository, Startable { - - private Languages languages; - - public DefaultLanguagesRepository(Languages languages) { - this.languages = languages; - } - - @Override - public void start() { - if (languages.all().length == 0) { - throw new IllegalStateException("No language plugins are installed."); - } - } - - /** - * Get language. - */ - @Override - @CheckForNull - public Language get(String languageKey) { - org.sonar.api.resources.Language language = languages.get(languageKey); - return language != null ? new Language(language.getKey(), language.getName(), language.getFileSuffixes()) : null; - } - - /** - * Get list of all supported languages. - */ - @Override - public Collection<Language> all() { - org.sonar.api.resources.Language[] all = languages.all(); - Collection<Language> result = new ArrayList<>(all.length); - for (org.sonar.api.resources.Language language : all) { - result.add(new Language(language.getKey(), language.getName(), language.getFileSuffixes())); - } - return result; - } - - @Override - public void stop() { - // nothing to do - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/language/Language.java b/sonar-batch/src/main/java/org/sonar/batch/repository/language/Language.java deleted file mode 100644 index 1a9ae783536..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/language/Language.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository.language; - -import java.util.Arrays; -import java.util.Collection; - -public final class Language { - - private final String key; - private final String name; - private final String[] fileSuffixes; - - public Language(String key, String name, String... fileSuffixes) { - this.key = key; - this.name = name; - this.fileSuffixes = fileSuffixes; - } - - /** - * For example "java". - */ - public String key() { - return key; - } - - /** - * For example "Java" - */ - public String name() { - return name; - } - - /** - * For example ["jav", "java"]. - */ - public Collection<String> fileSuffixes() { - return Arrays.asList(fileSuffixes); - } - - @Override - public String toString() { - return name; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/language/LanguagesRepository.java b/sonar-batch/src/main/java/org/sonar/batch/repository/language/LanguagesRepository.java deleted file mode 100644 index da1bab7993f..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/language/LanguagesRepository.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository.language; - -import org.sonar.api.batch.BatchSide; - -import javax.annotation.CheckForNull; - -import java.util.Collection; - -/** - * Languages repository - * @since 4.4 - */ -@BatchSide -public interface LanguagesRepository { - - /** - * Get language. - */ - @CheckForNull - Language get(String languageKey); - - /** - * Get list of all supported languages. - */ - Collection<Language> all(); - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/language/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/repository/language/package-info.java deleted file mode 100644 index 03b2bf5f676..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/language/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.batch.repository.language; diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/repository/package-info.java deleted file mode 100644 index 3679d1bfbb1..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.repository; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/user/UserRepositoryLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/user/UserRepositoryLoader.java deleted file mode 100644 index 6a5ad2e06a0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/user/UserRepositoryLoader.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.repository.user; - -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 com.google.common.collect.Lists; -import com.google.common.base.Joiner; -import org.sonar.batch.util.BatchUtils; -import com.google.common.base.Function; -import org.sonar.batch.protocol.input.BatchInput; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -public class UserRepositoryLoader { - private final WSLoader wsLoader; - - public UserRepositoryLoader(WSLoader wsLoader) { - this.wsLoader = wsLoader; - } - - public BatchInput.User load(String userLogin) { - return load(userLogin, null); - } - - public BatchInput.User load(String userLogin, @Nullable MutableBoolean fromCache) { - 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. - */ - public Collection<BatchInput.User> load(List<String> userLogins, @Nullable MutableBoolean fromCache) { - if (userLogins.isEmpty()) { - return Collections.emptyList(); - } - InputStream is = loadQuery(Joiner.on(',').join(Lists.transform(userLogins, new UserEncodingFunction())), fromCache); - - return parseUsers(is); - } - - private InputStream loadQuery(String loginsQuery, @Nullable MutableBoolean fromCache) { - WSLoaderResult<InputStream> result = wsLoader.loadStream("/batch/users?logins=" + loginsQuery); - if (fromCache != null) { - fromCache.setValue(result.isFromCache()); - } - return result.get(); - } - - private static class UserEncodingFunction implements Function<String, String> { - @Override - public String apply(String input) { - return BatchUtils.encodeForUrl(input); - } - } - - 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(InputStream is) { - List<BatchInput.User> users = new ArrayList<>(); - - try { - BatchInput.User user = BatchInput.User.parseDelimitedFrom(is); - while (user != null) { - users.add(user); - user = BatchInput.User.parseDelimitedFrom(is); - } - } catch (IOException e) { - throw new IllegalStateException("Unable to get user details from server", e); - } finally { - IOUtils.closeQuietly(is); - } - - return users; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/user/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/repository/user/package-info.java deleted file mode 100644 index 6462fb00b8a..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/user/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.repository.user; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesLoader.java deleted file mode 100644 index 028929bd141..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesLoader.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import org.apache.commons.lang.mutable.MutableBoolean; - -import javax.annotation.Nullable; - -import java.util.List; - -public interface ActiveRulesLoader { - List<LoadedActiveRule> load(String qualityProfileKey, @Nullable MutableBoolean fromCache); -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java b/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java deleted file mode 100644 index 83034e22132..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.apache.commons.lang.mutable.MutableBoolean; -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; -import org.sonar.api.batch.rule.internal.NewActiveRule; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; - -/** - * Loads the rules that are activated on the Quality profiles - * used by the current project and builds {@link org.sonar.api.batch.rule.ActiveRules}. - */ -public class ActiveRulesProvider extends ProviderAdapter { - private static final Logger LOG = Loggers.get(ActiveRulesProvider.class); - private static final String LOG_MSG = "Load active rules"; - private ActiveRules singleton = null; - - public ActiveRules provide(ActiveRulesLoader loader, ModuleQProfiles qProfiles) { - if (singleton == null) { - Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - MutableBoolean fromCache = new MutableBoolean(); - singleton = load(loader, qProfiles, fromCache); - profiler.stopInfo(fromCache.booleanValue()); - } - return singleton; - } - - private static ActiveRules load(ActiveRulesLoader loader, ModuleQProfiles qProfiles, MutableBoolean fromCache) { - - Collection<String> qProfileKeys = getKeys(qProfiles); - Map<RuleKey, LoadedActiveRule> loadedRulesByKey = new HashMap<>(); - - try { - for (String qProfileKey : qProfileKeys) { - Collection<LoadedActiveRule> qProfileRules; - qProfileRules = load(loader, qProfileKey, fromCache); - - for (LoadedActiveRule r : qProfileRules) { - if (!loadedRulesByKey.containsKey(r.getRuleKey())) { - loadedRulesByKey.put(r.getRuleKey(), r); - } - } - } - } catch (IOException e) { - throw new IllegalStateException("Error loading active rules", e); - } - - return transform(loadedRulesByKey.values()); - } - - private static ActiveRules transform(Collection<LoadedActiveRule> loadedRules) { - ActiveRulesBuilder builder = new ActiveRulesBuilder(); - - for (LoadedActiveRule activeRule : loadedRules) { - NewActiveRule newActiveRule = builder.create(activeRule.getRuleKey()); - newActiveRule.setName(activeRule.getName()); - newActiveRule.setSeverity(activeRule.getSeverity()); - newActiveRule.setLanguage(activeRule.getLanguage()); - newActiveRule.setInternalKey(activeRule.getInternalKey()); - newActiveRule.setTemplateRuleKey(activeRule.getTemplateRuleKey()); - - // load parameters - if (activeRule.getParams() != null) { - for (Map.Entry<String, String> params : activeRule.getParams().entrySet()) { - newActiveRule.setParam(params.getKey(), params.getValue()); - } - } - - newActiveRule.activate(); - } - return builder.build(); - } - - private static List<LoadedActiveRule> load(ActiveRulesLoader loader, String qProfileKey, MutableBoolean fromCache) throws IOException { - return loader.load(qProfileKey, fromCache); - } - - private static Collection<String> getKeys(ModuleQProfiles qProfiles) { - List<String> keys = new ArrayList<>(qProfiles.findAll().size()); - - for (QProfile qp : qProfiles.findAll()) { - keys.add(qp.getKey()); - } - - return keys; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultActiveRulesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultActiveRulesLoader.java deleted file mode 100644 index 9485d99e1cb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultActiveRulesLoader.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import org.sonar.batch.util.BatchUtils; - -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import javax.annotation.Nullable; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.mutable.MutableBoolean; -import org.sonar.api.rule.RuleKey; -import org.sonar.batch.cache.WSLoader; -import org.sonar.batch.cache.WSLoaderResult; -import org.sonarqube.ws.Rules.Active; -import org.sonarqube.ws.Rules.Active.Param; -import org.sonarqube.ws.Rules.ActiveList; -import org.sonarqube.ws.Rules.Rule; -import org.sonarqube.ws.Rules.SearchResponse; - -public class DefaultActiveRulesLoader implements ActiveRulesLoader { - private static final String RULES_SEARCH_URL = "/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params,actives&activation=true"; - - private final WSLoader wsLoader; - - public DefaultActiveRulesLoader(WSLoader wsLoader) { - this.wsLoader = wsLoader; - } - - @Override - public List<LoadedActiveRule> load(String qualityProfileKey, @Nullable MutableBoolean fromCache) { - List<LoadedActiveRule> ruleList = new LinkedList<>(); - int page = 1; - int pageSize = 500; - int loaded = 0; - - while (true) { - WSLoaderResult<InputStream> result = wsLoader.loadStream(getUrl(qualityProfileKey, page, pageSize)); - SearchResponse response = loadFromStream(result.get()); - List<LoadedActiveRule> pageRules = readPage(response); - ruleList.addAll(pageRules); - loaded += response.getPs(); - - if (response.getTotal() <= loaded) { - break; - } - page++; - if (fromCache != null) { - fromCache.setValue(result.isFromCache()); - } - } - - return ruleList; - } - - private static String getUrl(String qualityProfileKey, int page, int pageSize) { - StringBuilder builder = new StringBuilder(1024); - builder.append(RULES_SEARCH_URL); - builder.append("&qprofile=").append(BatchUtils.encodeForUrl(qualityProfileKey)); - builder.append("&p=").append(page); - builder.append("&ps=").append(pageSize); - return builder.toString(); - } - - private static SearchResponse loadFromStream(InputStream is) { - try { - return SearchResponse.parseFrom(is); - } catch (IOException e) { - throw new IllegalStateException("Failed to load quality profiles", e); - } finally { - IOUtils.closeQuietly(is); - } - } - - private static List<LoadedActiveRule> readPage(SearchResponse response) { - List<LoadedActiveRule> loadedRules = new LinkedList<>(); - - List<Rule> rulesList = response.getRulesList(); - Map<String, ActiveList> actives = response.getActives().getActives(); - - for (Rule r : rulesList) { - ActiveList activeList = actives.get(r.getKey()); - Active active = activeList.getActiveList(0); - - LoadedActiveRule loadedRule = new LoadedActiveRule(); - - loadedRule.setRuleKey(RuleKey.parse(r.getKey())); - loadedRule.setName(r.getName()); - loadedRule.setSeverity(active.getSeverity()); - loadedRule.setLanguage(r.getLang()); - loadedRule.setInternalKey(r.getInternalKey()); - if (r.hasTemplateKey()) { - RuleKey templateRuleKey = RuleKey.parse(r.getTemplateKey()); - loadedRule.setTemplateRuleKey(templateRuleKey.rule()); - } - - Map<String, String> params = new HashMap<>(); - - for (org.sonarqube.ws.Rules.Rule.Param param : r.getParams().getParamsList()) { - params.put(param.getKey(), param.getDefaultValue()); - } - - // overrides defaultValue if the key is the same - for (Param param : active.getParamsList()) { - params.put(param.getKey(), param.getValue()); - } - loadedRule.setParams(params); - loadedRules.add(loadedRule); - } - - return loadedRules; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultRulesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultRulesLoader.java deleted file mode 100644 index 62292b4581b..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultRulesLoader.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -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 org.sonarqube.ws.Rules.ListResponse; - -import java.io.IOException; -import java.io.InputStream; -import java.util.List; - -public class DefaultRulesLoader implements RulesLoader { - private static final String RULES_SEARCH_URL = "/api/rules/list.protobuf"; - - private final WSLoader wsLoader; - - public DefaultRulesLoader(WSLoader wsLoader) { - this.wsLoader = wsLoader; - } - - @Override - public List<Rule> load(@Nullable MutableBoolean fromCache) { - 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 loadFromStream(InputStream is) { - try { - return ListResponse.parseFrom(is); - } catch (IOException e) { - throw new IllegalStateException("Unable to get rules", e); - } finally { - IOUtils.closeQuietly(is); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/LoadedActiveRule.java b/sonar-batch/src/main/java/org/sonar/batch/rule/LoadedActiveRule.java deleted file mode 100644 index 31e5919220f..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/LoadedActiveRule.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import java.util.Map; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.sonar.api.rule.RuleKey; - -public class LoadedActiveRule { - private RuleKey ruleKey; - private String severity; - private String name; - private String language; - private Map<String, String> params; - private String templateRuleKey; - private String internalKey; - - public RuleKey getRuleKey() { - return ruleKey; - } - - public void setRuleKey(RuleKey ruleKey) { - this.ruleKey = ruleKey; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getSeverity() { - return severity; - } - - public void setSeverity(String severity) { - this.severity = severity; - } - - public String getLanguage() { - return language; - } - - public void setLanguage(String language) { - this.language = language; - } - - public Map<String, String> getParams() { - return params; - } - - public void setParams(Map<String, String> params) { - this.params = params; - } - - @CheckForNull - public String getTemplateRuleKey() { - return templateRuleKey; - } - - public void setTemplateRuleKey(@Nullable String templateRuleKey) { - this.templateRuleKey = templateRuleKey; - } - - public String getInternalKey() { - return internalKey; - } - - public void setInternalKey(String internalKey) { - this.internalKey = internalKey; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleQProfiles.java b/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleQProfiles.java deleted file mode 100644 index 5a671de840f..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleQProfiles.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import org.sonar.api.utils.DateUtils; - -import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile; -import com.google.common.collect.ImmutableMap; -import org.sonar.api.batch.BatchSide; - -import javax.annotation.CheckForNull; - -import java.util.Collection; -import java.util.Map; - -/** - * Lists the Quality profiles enabled on the current module. - */ -@BatchSide -public class ModuleQProfiles { - - public static final String SONAR_PROFILE_PROP = "sonar.profile"; - private final Map<String, QProfile> byLanguage; - - public ModuleQProfiles(Collection<QualityProfile> profiles) { - ImmutableMap.Builder<String, QProfile> builder = ImmutableMap.builder(); - - for (QualityProfile qProfile : profiles) { - builder.put(qProfile.getLanguage(), - new QProfile() - .setKey(qProfile.getKey()) - .setName(qProfile.getName()) - .setLanguage(qProfile.getLanguage()) - .setRulesUpdatedAt(DateUtils.parseDateTime(qProfile.getRulesUpdatedAt()))); - } - byLanguage = builder.build(); - } - - public Collection<QProfile> findAll() { - return byLanguage.values(); - } - - @CheckForNull - public QProfile findByLanguage(String language) { - return byLanguage.get(language); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfile.java b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfile.java deleted file mode 100644 index 5abc9955c7d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfile.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import com.google.common.base.Objects; - -import java.util.Date; - -public class QProfile { - - private String key; - private String name; - private String language; - private Date rulesUpdatedAt; - - public String getKey() { - return key; - } - - public QProfile setKey(String key) { - this.key = key; - return this; - } - - public String getName() { - return name; - } - - public QProfile setName(String name) { - this.name = name; - return this; - } - - public String getLanguage() { - return language; - } - - public QProfile setLanguage(String language) { - this.language = language; - return this; - } - - public Date getRulesUpdatedAt() { - return rulesUpdatedAt; - } - - public QProfile setRulesUpdatedAt(Date d) { - this.rulesUpdatedAt = d; - return this; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - QProfile qProfile = (QProfile) o; - return key.equals(qProfile.key); - } - - @Override - public int hashCode() { - return key.hashCode(); - } - - @Override - public String toString() { - return Objects.toStringHelper(this) - .add("key", key) - .add("name", name) - .add("language", language) - .add("rulesUpdatedAt", rulesUpdatedAt) - .toString(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java deleted file mode 100644 index 758851dafc4..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.Sensor; -import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; -import org.sonar.api.resources.Project; - -/** - * Stores which Quality profiles have been used on the current module. - * - * TODO This information should not be stored as a measure but should be send as metadata in the {@link org.sonar.batch.protocol.output.BatchReport} - */ -public class QProfileSensor implements Sensor { - - private final ModuleQProfiles moduleQProfiles; - private final FileSystem fs; - private final AnalysisMode analysisMode; - - public QProfileSensor(ModuleQProfiles moduleQProfiles, FileSystem fs, AnalysisMode analysisMode) { - this.moduleQProfiles = moduleQProfiles; - this.fs = fs; - this.analysisMode = analysisMode; - } - - @Override - public boolean shouldExecuteOnProject(Project project) { - // Should be only executed on leaf modules - return project.getModules().isEmpty() - // Useless in issues mode - && !analysisMode.isIssues(); - } - - @Override - public void analyse(Project project, SensorContext context) { - UsedQProfiles used = new UsedQProfiles(); - for (String language : fs.languages()) { - QProfile profile = moduleQProfiles.findByLanguage(language); - if (profile != null) { - used.add(profile); - } - } - Measure<?> detailsMeasure = new Measure<>(CoreMetrics.QUALITY_PROFILES, used.toJson()); - context.saveMeasure(detailsMeasure); - } - - @Override - public String toString() { - return getClass().getSimpleName(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileVerifier.java b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileVerifier.java deleted file mode 100644 index 6206a289f17..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileVerifier.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import com.google.common.annotations.VisibleForTesting; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.MessageException; - -import static org.apache.commons.lang.StringUtils.isNotEmpty; - -@BatchSide -public class QProfileVerifier { - - private static final Logger LOG = LoggerFactory.getLogger(QProfileVerifier.class); - - private final Settings settings; - private final FileSystem fs; - private final ModuleQProfiles profiles; - - public QProfileVerifier(Settings settings, FileSystem fs, ModuleQProfiles profiles) { - this.settings = settings; - this.fs = fs; - this.profiles = profiles; - } - - public void execute() { - execute(LOG); - } - - @VisibleForTesting - void execute(Logger logger) { - String defaultName = settings.getString(ModuleQProfiles.SONAR_PROFILE_PROP); - boolean defaultNameUsed = StringUtils.isBlank(defaultName); - for (String lang : fs.languages()) { - QProfile profile = profiles.findByLanguage(lang); - if (profile == null) { - logger.warn("No Quality profile found for language " + lang); - } else { - logger.info("Quality profile for {}: {}", lang, profile.getName()); - if (isNotEmpty(defaultName) && defaultName.equals(profile.getName())) { - defaultNameUsed = true; - } - } - } - if (!defaultNameUsed && !fs.languages().isEmpty()) { - throw MessageException.of("sonar.profile was set to '" + defaultName + "' but didn't match any profile for any language. Please check your configuration."); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RuleFinderCompatibility.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RuleFinderCompatibility.java deleted file mode 100644 index fe763f873bd..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/RuleFinderCompatibility.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import org.sonar.api.batch.rule.Rules; - -import com.google.common.base.Function; -import com.google.common.collect.Collections2; -import org.apache.commons.lang.builder.ReflectionToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.rules.RuleQuery; - -import javax.annotation.CheckForNull; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; - -/** - * FIXME Waiting for the list of all server rules on batch side this is implemented by redirecting on ActiveRules. This is not correct - * since there is a difference between a rule that doesn't exists and a rule that is not activated in project quality profile. - * - */ -public class RuleFinderCompatibility implements RuleFinder { - - private final Rules rules; - private static Function<org.sonar.api.batch.rule.Rule, Rule> ruleTransformer = new Function<org.sonar.api.batch.rule.Rule, Rule>() { - @Override - public Rule apply(@Nonnull org.sonar.api.batch.rule.Rule input) { - return toRule(input); - } - }; - - public RuleFinderCompatibility(Rules rules) { - this.rules = rules; - } - - @Override - public Rule findById(int ruleId) { - throw new UnsupportedOperationException("Unable to find rule by id"); - } - - @Override - public Rule findByKey(String repositoryKey, String key) { - return findByKey(RuleKey.of(repositoryKey, key)); - } - - @Override - public Rule findByKey(RuleKey key) { - return toRule(rules.find(key)); - } - - @Override - public Rule find(RuleQuery query) { - Collection<Rule> all = findAll(query); - if (all.size() > 1) { - throw new IllegalArgumentException("Non unique result for rule query: " + ReflectionToStringBuilder.toString(query, ToStringStyle.SHORT_PREFIX_STYLE)); - } else if (all.isEmpty()) { - return null; - } else { - return all.iterator().next(); - } - } - - @Override - public Collection<Rule> findAll(RuleQuery query) { - if (query.getConfigKey() != null) { - if (query.getRepositoryKey() != null && query.getKey() == null) { - return byInternalKey(query); - } - } else if (query.getRepositoryKey() != null) { - if (query.getKey() != null) { - return byKey(query); - } else { - return byRepository(query); - } - } - throw new UnsupportedOperationException("Unable to find rule by query"); - } - - private Collection<Rule> byRepository(RuleQuery query) { - return Collections2.transform(rules.findByRepository(query.getRepositoryKey()), ruleTransformer); - } - - - - private Collection<Rule> byKey(RuleQuery query) { - Rule rule = toRule(rules.find(RuleKey.of(query.getRepositoryKey(), query.getKey()))); - return rule != null ? Arrays.asList(rule) : Collections.<Rule>emptyList(); - } - - private Collection<Rule> byInternalKey(RuleQuery query) { - return Collections2.transform(rules.findByInternalKey(query.getRepositoryKey(), query.getConfigKey()), ruleTransformer); - } - - @CheckForNull - private static Rule toRule(@Nullable org.sonar.api.batch.rule.Rule ar) { - return ar == null ? null : Rule.create(ar.key().repository(), ar.key().rule()).setName(ar.name()).setConfigKey(ar.internalKey()); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesLoader.java deleted file mode 100644 index 9572f628f2d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesLoader.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import java.util.List; -import javax.annotation.Nullable; -import org.apache.commons.lang.mutable.MutableBoolean; -import org.sonarqube.ws.Rules.ListResponse.Rule; - -public interface RulesLoader { - List<Rule> load(@Nullable MutableBoolean fromCache); -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java deleted file mode 100644 index de29cc352fc..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import com.google.common.collect.Lists; -import java.util.Collection; -import java.util.Map; -import org.apache.commons.lang.StringUtils; -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.config.Settings; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RulePriority; - -/** - * Ensures backward-compatibility with extensions that use {@link org.sonar.api.profiles.RulesProfile}. - */ -public class RulesProfileProvider extends ProviderAdapter { - - private RulesProfile singleton = null; - - public RulesProfile provide(ModuleQProfiles qProfiles, ActiveRules activeRules, Settings settings) { - if (singleton == null) { - String lang = settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY); - if (StringUtils.isNotBlank(lang)) { - // Backward-compatibility with single-language modules - singleton = loadSingleLanguageProfile(qProfiles, activeRules, lang); - } else { - singleton = loadProfiles(qProfiles, activeRules); - } - } - return singleton; - } - - private static RulesProfile loadSingleLanguageProfile(ModuleQProfiles qProfiles, ActiveRules activeRules, String language) { - QProfile qProfile = qProfiles.findByLanguage(language); - if (qProfile != null) { - return new RulesProfileWrapper(select(qProfile, activeRules)); - } - return new RulesProfileWrapper(Lists.<RulesProfile>newArrayList()); - } - - private static RulesProfile loadProfiles(ModuleQProfiles qProfiles, ActiveRules activeRules) { - Collection<RulesProfile> dtos = Lists.newArrayList(); - for (QProfile qProfile : qProfiles.findAll()) { - dtos.add(select(qProfile, activeRules)); - } - return new RulesProfileWrapper(dtos); - } - - private static RulesProfile select(QProfile qProfile, ActiveRules activeRules) { - RulesProfile deprecatedProfile = new RulesProfile(); - // TODO deprecatedProfile.setVersion(qProfile.version()); - deprecatedProfile.setName(qProfile.getName()); - deprecatedProfile.setLanguage(qProfile.getLanguage()); - for (org.sonar.api.batch.rule.ActiveRule activeRule : activeRules.findByLanguage(qProfile.getLanguage())) { - Rule rule = Rule.create(activeRule.ruleKey().repository(), activeRule.ruleKey().rule()); - rule.setConfigKey(activeRule.internalKey()); - - // SONAR-6706 - if (activeRule.templateRuleKey() != null) { - rule.setTemplate(Rule.create(activeRule.ruleKey().repository(), activeRule.templateRuleKey())); - } - - ActiveRule deprecatedActiveRule = deprecatedProfile.activateRule(rule, - RulePriority.valueOf(activeRule.severity())); - for (Map.Entry<String, String> param : activeRule.params().entrySet()) { - rule.createParameter(param.getKey()); - deprecatedActiveRule.setParameter(param.getKey(), param.getValue()); - } - } - return deprecatedProfile; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileWrapper.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileWrapper.java deleted file mode 100644 index 327c141ea34..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileWrapper.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import com.google.common.collect.Lists; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.Rule; -import org.sonar.api.utils.SonarException; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -/** - * This wrapper is used to try to preserve backward compatibility for plugins that used to - * depends on {@link org.sonar.api.profiles.RulesProfile} - * - * @since 4.2 - */ -public class RulesProfileWrapper extends RulesProfile { - - private static final Logger LOG = LoggerFactory.getLogger(RulesProfileWrapper.class); - private static final String DEPRECATED_USAGE_MESSAGE = "Please update your plugin to support multi-language analysis"; - - private final Collection<RulesProfile> profiles; - private final RulesProfile singleLanguageProfile; - - public RulesProfileWrapper(Collection<RulesProfile> profiles) { - this.profiles = profiles; - this.singleLanguageProfile = null; - } - - public RulesProfileWrapper(RulesProfile profile) { - this.profiles = Lists.newArrayList(profile); - this.singleLanguageProfile = profile; - } - - @Override - public Integer getId() { - return getSingleProfileOrFail().getId(); - } - - private RulesProfile getSingleProfileOrFail() { - if (singleLanguageProfile == null) { - throw new IllegalStateException(DEPRECATED_USAGE_MESSAGE); - } - return singleLanguageProfile; - } - - @Override - public String getName() { - return singleLanguageProfile != null ? singleLanguageProfile.getName() : "SonarQube"; - } - - @Override - public String getLanguage() { - if (singleLanguageProfile == null) { - // Multi-languages module - // This is a hack for CommonChecksDecorator that call this method in its constructor - LOG.debug(DEPRECATED_USAGE_MESSAGE, new SonarException(DEPRECATED_USAGE_MESSAGE)); - return ""; - } - return singleLanguageProfile.getLanguage(); - } - - @Override - public List<ActiveRule> getActiveRules() { - List<ActiveRule> activeRules = new ArrayList<>(); - for (RulesProfile profile : profiles) { - activeRules.addAll(profile.getActiveRules()); - } - return activeRules; - } - - @Override - public ActiveRule getActiveRule(String repositoryKey, String ruleKey) { - for (RulesProfile profile : profiles) { - ActiveRule activeRule = profile.getActiveRule(repositoryKey, ruleKey); - if (activeRule != null) { - return activeRule; - } - } - return null; - } - - @Override - public List<ActiveRule> getActiveRulesByRepository(String repositoryKey) { - List<ActiveRule> activeRules = new ArrayList<>(); - for (RulesProfile profile : profiles) { - activeRules.addAll(profile.getActiveRulesByRepository(repositoryKey)); - } - return activeRules; - } - - @Override - public List<ActiveRule> getActiveRules(boolean acceptDisabledRules) { - List<ActiveRule> activeRules = new ArrayList<>(); - for (RulesProfile profile : profiles) { - activeRules.addAll(profile.getActiveRules(acceptDisabledRules)); - } - return activeRules; - } - - @Override - public ActiveRule getActiveRule(Rule rule) { - for (RulesProfile profile : profiles) { - ActiveRule activeRule = profile.getActiveRule(rule); - if (activeRule != null) { - return activeRule; - } - } - return null; - } - - @Override - public Boolean getDefaultProfile() { - return getSingleProfileOrFail().getDefaultProfile(); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java deleted file mode 100644 index 7a3e0ea34f0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProvider.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import org.apache.commons.lang.mutable.MutableBoolean; - -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; - -import java.util.List; - -import org.sonarqube.ws.Rules.ListResponse.Rule; -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.batch.rule.internal.RulesBuilder; -import org.sonar.api.batch.rule.internal.NewRule; -import org.sonar.api.batch.rule.Rules; - -public class RulesProvider extends ProviderAdapter { - private static final Logger LOG = Loggers.get(RulesProvider.class); - private static final String LOG_MSG = "Load server rules"; - private Rules singleton = null; - - public Rules provide(RulesLoader ref) { - if (singleton == null) { - singleton = load(ref); - } - return singleton; - } - - private static Rules load(RulesLoader ref) { - Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - MutableBoolean fromCache = new MutableBoolean(); - List<Rule> loadedRules = ref.load(fromCache); - RulesBuilder builder = new RulesBuilder(); - - for (Rule r : loadedRules) { - NewRule newRule = builder.add(RuleKey.of(r.getRepository(), r.getKey())); - newRule.setName(r.getName()); - newRule.setInternalKey(r.getInternalKey()); - } - - profiler.stopInfo(fromCache.booleanValue()); - - return builder.build(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/UsedQProfiles.java b/sonar-batch/src/main/java/org/sonar/batch/rule/UsedQProfiles.java deleted file mode 100644 index 72186a5e959..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/UsedQProfiles.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.rule; - -import com.google.common.collect.Sets; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import org.sonar.api.utils.text.JsonWriter; -import org.sonar.core.util.UtcDateUtils; - -import javax.annotation.concurrent.Immutable; - -import java.io.StringWriter; -import java.util.Collection; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Map; -import java.util.SortedSet; - -@Immutable -public class UsedQProfiles { - - private final SortedSet<QProfile> profiles = Sets.newTreeSet(new Comparator<QProfile>() { - @Override - public int compare(QProfile o1, QProfile o2) { - int c = o1.getLanguage().compareTo(o2.getLanguage()); - if (c == 0) { - c = o1.getName().compareTo(o2.getName()); - } - return c; - } - }); - - public static UsedQProfiles fromJson(String json) { - UsedQProfiles result = new UsedQProfiles(); - JsonArray jsonRoot = new JsonParser().parse(json).getAsJsonArray(); - for (JsonElement jsonElt : jsonRoot) { - JsonObject jsonProfile = jsonElt.getAsJsonObject(); - QProfile profile = new QProfile(); - profile.setKey(jsonProfile.get("key").getAsString()); - profile.setName(jsonProfile.get("name").getAsString()); - profile.setLanguage(jsonProfile.get("language").getAsString()); - profile.setRulesUpdatedAt(UtcDateUtils.parseDateTime(jsonProfile.get("rulesUpdatedAt").getAsString())); - result.add(profile); - } - return result; - } - - public String toJson() { - StringWriter json = new StringWriter(); - JsonWriter writer = JsonWriter.of(json); - writer.beginArray(); - for (QProfile profile : profiles) { - writer - .beginObject() - .prop("key", profile.getKey()) - .prop("language", profile.getLanguage()) - .prop("name", profile.getName()) - .prop("rulesUpdatedAt", UtcDateUtils.formatDateTime(profile.getRulesUpdatedAt())) - .endObject(); - } - writer.endArray(); - writer.close(); - return json.toString(); - } - - public UsedQProfiles add(UsedQProfiles other) { - addAll(other.profiles); - return this; - } - - public UsedQProfiles add(QProfile profile) { - profiles.add(profile); - return this; - } - - public UsedQProfiles addAll(Collection<QProfile> profiles) { - this.profiles.addAll(profiles); - return this; - } - - public SortedSet<QProfile> profiles() { - return profiles; - } - - public Map<String, QProfile> profilesByKey() { - Map<String, QProfile> map = new HashMap<>(); - for (QProfile profile : profiles) { - map.put(profile.getKey(), profile); - } - return map; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/rule/package-info.java deleted file mode 100644 index 1bdc402f399..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.rule; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ImmutableProjectReactor.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ImmutableProjectReactor.java deleted file mode 100644 index 4bfa99c6da2..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ImmutableProjectReactor.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.Map; -import javax.annotation.CheckForNull; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.bootstrap.ProjectDefinition; - -/** - * Immutable copy of project reactor after all modifications have been applied (see {@link ImmutableProjectReactorProvider}). - */ -@BatchSide -public class ImmutableProjectReactor { - - private ProjectDefinition root; - private Map<String, ProjectDefinition> byKey = new LinkedHashMap<>(); - - public ImmutableProjectReactor(ProjectDefinition root) { - if (root.getParent() != null) { - throw new IllegalArgumentException("Not a root project: " + root); - } - this.root = root; - collectProjects(root); - } - - public Collection<ProjectDefinition> getProjects() { - return byKey.values(); - } - - /** - * Populates list of projects from hierarchy. - */ - private void collectProjects(ProjectDefinition def) { - if (byKey.containsKey(def.getKeyWithBranch())) { - throw new IllegalStateException("Duplicate module key in reactor: " + def.getKeyWithBranch()); - } - byKey.put(def.getKeyWithBranch(), def); - for (ProjectDefinition child : def.getSubProjects()) { - collectProjects(child); - } - } - - public ProjectDefinition getRoot() { - return root; - } - - @CheckForNull - public ProjectDefinition getProjectDefinition(String keyWithBranch) { - return byKey.get(keyWithBranch); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ImmutableProjectReactorProvider.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ImmutableProjectReactorProvider.java deleted file mode 100644 index ecf58a88482..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ImmutableProjectReactorProvider.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.batch.bootstrap.ProjectReactor; - -public class ImmutableProjectReactorProvider extends ProviderAdapter { - - private ImmutableProjectReactor singleton; - - public ImmutableProjectReactor provide(ProjectReactor reactor, ProjectBuildersExecutor projectBuildersExecutor, ProjectExclusions exclusions, ProjectReactorValidator validator) { - if (singleton == null) { - // 1 Apply project builders - projectBuildersExecutor.execute(reactor); - - // 2 Apply project exclusions - exclusions.apply(reactor); - - // 3 Validate final reactor - validator.validate(reactor); - - singleton = new ImmutableProjectReactor(reactor.getRoot()); - } - return singleton; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/LanguageVerifier.java b/sonar-batch/src/main/java/org/sonar/batch/scan/LanguageVerifier.java deleted file mode 100644 index 83e27324e64..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/LanguageVerifier.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import org.sonar.batch.repository.language.Language; -import org.sonar.batch.repository.language.LanguagesRepository; -import org.apache.commons.lang.StringUtils; -import org.picocontainer.Startable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.fs.internal.DefaultFileSystem; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.MessageException; - -/** - * Verifies that the property sonar.language is valid - */ -public class LanguageVerifier implements Startable { - - private static final Logger LOG = LoggerFactory.getLogger(LanguageVerifier.class); - - private final Settings settings; - private final LanguagesRepository languages; - private final DefaultFileSystem fs; - - public LanguageVerifier(Settings settings, LanguagesRepository languages, DefaultFileSystem fs) { - this.settings = settings; - this.languages = languages; - this.fs = fs; - } - - @Override - public void start() { - String languageKey = settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY); - if (StringUtils.isNotBlank(languageKey)) { - LOG.info("Language is forced to {}", languageKey); - Language language = languages.get(languageKey); - if (language == null) { - throw MessageException.of("You must install a plugin that supports the language '" + languageKey + "'"); - } - - // force the registration of the language, even if there are no related source files - fs.addLanguages(languageKey); - } - } - - @Override - public void stop() { - // nothing to do - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java deleted file mode 100644 index 56da7fd624d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java +++ /dev/null @@ -1,197 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.InstantiationStrategy; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.fs.internal.FileMetadata; -import org.sonar.api.batch.rule.CheckFactory; -import org.sonar.api.resources.Project; -import org.sonar.api.scan.filesystem.FileExclusions; -import org.sonar.batch.DefaultProjectTree; -import org.sonar.batch.bootstrap.BatchExtensionDictionnary; -import org.sonar.batch.bootstrap.ExtensionInstaller; -import org.sonar.batch.bootstrap.ExtensionMatcher; -import org.sonar.batch.bootstrap.ExtensionUtils; -import org.sonar.batch.deprecated.DeprecatedSensorContext; -import org.sonar.batch.deprecated.perspectives.BatchPerspectives; -import org.sonar.batch.events.EventBus; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.index.DefaultIndex; -import org.sonar.batch.issue.IssuableFactory; -import org.sonar.batch.issue.IssueFilters; -import org.sonar.batch.issue.ModuleIssues; -import org.sonar.batch.issue.ignore.EnforceIssuesFilter; -import org.sonar.batch.issue.ignore.IgnoreIssuesFilter; -import org.sonar.batch.issue.ignore.pattern.IssueExclusionPatternInitializer; -import org.sonar.batch.issue.ignore.pattern.IssueInclusionPatternInitializer; -import org.sonar.batch.issue.ignore.scanner.IssueExclusionsLoader; -import org.sonar.batch.issue.ignore.scanner.IssueExclusionsRegexpScanner; -import org.sonar.batch.phases.AbstractPhaseExecutor; -import org.sonar.batch.phases.InitializersExecutor; -import org.sonar.batch.phases.IssuesPhaseExecutor; -import org.sonar.batch.phases.PostJobsExecutor; -import org.sonar.batch.phases.ProjectInitializer; -import org.sonar.batch.phases.PublishPhaseExecutor; -import org.sonar.batch.phases.SensorsExecutor; -import org.sonar.batch.postjob.DefaultPostJobContext; -import org.sonar.batch.postjob.PostJobOptimizer; -import org.sonar.batch.rule.QProfileSensor; -import org.sonar.batch.rule.QProfileVerifier; -import org.sonar.batch.rule.RuleFinderCompatibility; -import org.sonar.batch.rule.RulesProfileProvider; -import org.sonar.batch.scan.filesystem.ComponentIndexer; -import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem; -import org.sonar.batch.scan.filesystem.DeprecatedFileFilters; -import org.sonar.batch.scan.filesystem.ExclusionFilters; -import org.sonar.batch.scan.filesystem.FileIndexer; -import org.sonar.batch.scan.filesystem.FileSystemLogger; -import org.sonar.batch.scan.filesystem.InputFileBuilderFactory; -import org.sonar.batch.scan.filesystem.LanguageDetectionFactory; -import org.sonar.batch.scan.filesystem.ModuleFileSystemInitializer; -import org.sonar.batch.scan.filesystem.ModuleInputFileCache; -import org.sonar.batch.scan.filesystem.StatusDetectionFactory; -import org.sonar.batch.scan.report.IssuesReports; -import org.sonar.batch.sensor.DefaultSensorStorage; -import org.sonar.batch.sensor.SensorOptimizer; -import org.sonar.batch.sensor.coverage.CoverageExclusions; -import org.sonar.batch.source.HighlightableBuilder; -import org.sonar.batch.source.SymbolizableBuilder; -import org.sonar.core.platform.ComponentContainer; - -public class ModuleScanContainer extends ComponentContainer { - private static final Logger LOG = LoggerFactory.getLogger(ModuleScanContainer.class); - private final Project module; - - public ModuleScanContainer(ProjectScanContainer parent, Project module) { - super(parent); - this.module = module; - } - - @Override - protected void doBeforeStart() { - LOG.info("------------- Scan {}", module.getName()); - addCoreComponents(); - addExtensions(); - } - - private void addCoreComponents() { - ProjectDefinition moduleDefinition = getComponentByType(DefaultProjectTree.class).getProjectDefinition(module); - add( - moduleDefinition, - module, - getComponentByType(BatchComponentCache.class).get(module).inputComponent(), - ModuleSettings.class); - - // hack to initialize settings before ExtensionProviders - ModuleSettings moduleSettings = getComponentByType(ModuleSettings.class); - module.setSettings(moduleSettings); - - if (getComponentByType(AnalysisMode.class).isIssues()) { - add(IssuesPhaseExecutor.class, - IssuesReports.class); - } else { - add(PublishPhaseExecutor.class); - } - - add( - EventBus.class, - RuleFinderCompatibility.class, - PostJobsExecutor.class, - SensorsExecutor.class, - InitializersExecutor.class, - ProjectInitializer.class, - - // file system - ModuleInputFileCache.class, - FileExclusions.class, - ExclusionFilters.class, - DeprecatedFileFilters.class, - InputFileBuilderFactory.class, - FileMetadata.class, - StatusDetectionFactory.class, - LanguageDetectionFactory.class, - FileIndexer.class, - ComponentIndexer.class, - LanguageVerifier.class, - FileSystemLogger.class, - DefaultModuleFileSystem.class, - ModuleFileSystemInitializer.class, - QProfileVerifier.class, - - SensorOptimizer.class, - PostJobOptimizer.class, - - DefaultPostJobContext.class, - DefaultSensorStorage.class, - DeprecatedSensorContext.class, - BatchExtensionDictionnary.class, - IssueFilters.class, - CoverageExclusions.class, - - // rules - new RulesProfileProvider(), - QProfileSensor.class, - CheckFactory.class, - - // issues - IssuableFactory.class, - ModuleIssues.class, - org.sonar.api.issue.NoSonarFilter.class, - - // issue exclusions - IssueInclusionPatternInitializer.class, - IssueExclusionPatternInitializer.class, - IssueExclusionsRegexpScanner.class, - IssueExclusionsLoader.class, - EnforceIssuesFilter.class, - IgnoreIssuesFilter.class, - - // Perspectives - BatchPerspectives.class, - HighlightableBuilder.class, - SymbolizableBuilder.class); - } - - private void addExtensions() { - ExtensionInstaller installer = getComponentByType(ExtensionInstaller.class); - installer.install(this, new ExtensionMatcher() { - @Override - public boolean accept(Object extension) { - return ExtensionUtils.isBatchSide(extension) && ExtensionUtils.isInstantiationStrategy(extension, InstantiationStrategy.PER_PROJECT); - } - }); - } - - @Override - protected void doAfterStart() { - DefaultIndex index = getComponentByType(DefaultIndex.class); - index.setCurrentProject(module, getComponentByType(DefaultSensorStorage.class)); - - getComponentByType(AbstractPhaseExecutor.class).execute(module); - - // Free memory since module settings are no more used - module.setSettings(null); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java deleted file mode 100644 index 2176ad347fe..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import com.google.common.collect.Lists; -import java.util.List; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.MessageException; -import org.sonar.batch.analysis.DefaultAnalysisMode; -import org.sonar.batch.bootstrap.GlobalSettings; -import org.sonar.batch.report.AnalysisContextReportPublisher; -import org.sonar.batch.repository.ProjectRepositories; - -/** - * @since 2.12 - */ -public class ModuleSettings extends Settings { - - private final ProjectRepositories projectRepos; - private final DefaultAnalysisMode analysisMode; - - public ModuleSettings(GlobalSettings batchSettings, ProjectDefinition moduleDefinition, ProjectRepositories projectSettingsRepo, - DefaultAnalysisMode analysisMode, AnalysisContextReportPublisher contextReportPublisher) { - super(batchSettings.getDefinitions()); - this.projectRepos = projectSettingsRepo; - this.analysisMode = analysisMode; - getEncryption().setPathToSecretKey(batchSettings.getString(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); - - init(moduleDefinition, batchSettings); - contextReportPublisher.dumpSettings(moduleDefinition); - } - - private ModuleSettings init(ProjectDefinition moduleDefinition, GlobalSettings batchSettings) { - addProjectProperties(moduleDefinition, batchSettings); - addBuildProperties(moduleDefinition); - return this; - } - - private void addProjectProperties(ProjectDefinition def, GlobalSettings batchSettings) { - addProperties(batchSettings.getProperties()); - do { - if (projectRepos.moduleExists(def.getKeyWithBranch())) { - addProperties(projectRepos.settings(def.getKeyWithBranch())); - break; - } - def = def.getParent(); - } while (def != null); - } - - private void addBuildProperties(ProjectDefinition project) { - List<ProjectDefinition> orderedProjects = getTopDownParentProjects(project); - for (ProjectDefinition p : orderedProjects) { - addProperties(p.properties()); - } - } - - /** - * From root to given project - */ - static List<ProjectDefinition> getTopDownParentProjects(ProjectDefinition project) { - List<ProjectDefinition> result = Lists.newArrayList(); - ProjectDefinition p = project; - while (p != null) { - result.add(0, p); - p = p.getParent(); - } - return result; - } - - @Override - protected void doOnGetProperties(String key) { - if (analysisMode.isIssues() && key.endsWith(".secured") && !key.contains(".license")) { - throw MessageException.of("Access to the secured property '" + key - + "' is not possible in issues mode. The SonarQube plugin which requires this property must be deactivated in issues mode."); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/MutableProjectReactorProvider.java b/sonar-batch/src/main/java/org/sonar/batch/scan/MutableProjectReactorProvider.java deleted file mode 100644 index afde1e18c4d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/MutableProjectReactorProvider.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.batch.bootstrap.ProjectReactor; - -public class MutableProjectReactorProvider extends ProviderAdapter { - private ProjectReactor reactor = null; - - public ProjectReactor provide(ProjectReactorBuilder builder) { - if (reactor == null) { - reactor = builder.execute(); - } - return reactor; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectBuildersExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectBuildersExecutor.java deleted file mode 100644 index 505a80bc117..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectBuildersExecutor.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import org.sonar.api.batch.bootstrap.ProjectBuilder; -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.api.batch.bootstrap.internal.ProjectBuilderContext; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; - -public class ProjectBuildersExecutor { - - private static final Logger LOG = Loggers.get(ProjectBuildersExecutor.class); - - private final ProjectBuilder[] projectBuilders; - - public ProjectBuildersExecutor(ProjectBuilder... projectBuilders) { - this.projectBuilders = projectBuilders; - } - - public ProjectBuildersExecutor() { - this(new ProjectBuilder[0]); - } - - public void execute(ProjectReactor reactor) { - if (projectBuilders.length > 0) { - Profiler profiler = Profiler.create(LOG).startInfo("Execute project builders"); - ProjectBuilderContext context = new ProjectBuilderContext(reactor); - - for (ProjectBuilder projectBuilder : projectBuilders) { - projectBuilder.build(context); - } - profiler.stopInfo(); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectExclusions.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectExclusions.java deleted file mode 100644 index 0bea9a81309..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectExclusions.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import org.apache.commons.lang.ArrayUtils; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.api.config.Settings; - -/** - * Exclude the sub-projects as defined by the properties sonar.skippedModules and sonar.includedModules - * - * @since 2.12 - */ -public class ProjectExclusions { - - private static final Logger LOG = LoggerFactory.getLogger(ProjectExclusions.class); - - private Settings settings; - - public ProjectExclusions(Settings settings) { - this.settings = settings; - } - - public void apply(ProjectReactor reactor) { - if (!reactor.getProjects().isEmpty() && StringUtils.isNotBlank(reactor.getProjects().get(0).getKey())) { - LOG.info("Apply project exclusions"); - - if (settings.hasKey(CoreProperties.CORE_INCLUDED_MODULES_PROPERTY)) { - LOG.warn("'sonar.includedModules' property is deprecated since version 4.3 and should not be used anymore."); - } - if (settings.hasKey(CoreProperties.CORE_SKIPPED_MODULES_PROPERTY)) { - LOG.warn("'sonar.skippedModules' property is deprecated since version 4.3 and should not be used anymore."); - } - - for (ProjectDefinition project : reactor.getProjects()) { - if (isExcluded(key(project), project == reactor.getRoot())) { - exclude(project); - } - } - } - } - - private boolean isExcluded(String projectKey, boolean isRoot) { - String[] includedKeys = settings.getStringArray(CoreProperties.CORE_INCLUDED_MODULES_PROPERTY); - boolean excluded = false; - if (!isRoot && includedKeys.length > 0) { - excluded = !ArrayUtils.contains(includedKeys, projectKey); - } - String skippedModulesProperty = CoreProperties.CORE_SKIPPED_MODULES_PROPERTY; - if (!excluded) { - String[] excludedKeys = settings.getStringArray(skippedModulesProperty); - excluded = ArrayUtils.contains(excludedKeys, projectKey); - } - if (excluded && isRoot) { - throw new IllegalArgumentException("The root project can't be excluded. Please check the parameters " + skippedModulesProperty + " and sonar.includedModules."); - } - return excluded; - } - - private void exclude(ProjectDefinition project) { - LOG.info(String.format("Exclude project: %s [%s]", project.getName(), project.getKey())); - project.remove(); - } - - static String key(ProjectDefinition project) { - String key = project.getKey(); - if (key.contains(":")) { - return StringUtils.substringAfter(key, ":"); - } - return key; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectLock.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectLock.java deleted file mode 100644 index d52f49cfd88..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectLock.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.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 java.io.IOException; -import java.nio.channels.OverlappingFileLockException; -import java.nio.file.Files; -import java.nio.file.Path; - -public class ProjectLock implements Startable { - private final DirectoryLock lock; - - public ProjectLock(ProjectReactor projectReactor) { - Path directory = projectReactor.getRoot().getWorkDir().toPath(); - try { - if (!Files.exists(directory)) { - Files.createDirectories(directory); - } - } catch (IOException e) { - throw new IllegalStateException("Failed to create work directory", e); - } - this.lock = new DirectoryLock(directory.toAbsolutePath(), new Slf4jLogger()); - } - - public void tryLock() { - try { - if (!lock.tryLock()) { - failAlreadyInProgress(null); - } - } catch (OverlappingFileLockException e) { - failAlreadyInProgress(e); - } - } - - private static void failAlreadyInProgress(Exception e) { - throw new IllegalStateException("Another SonarQube analysis is already in progress for this project", e); - } - - @Override - public void stop() { - lock.unlock(); - } - - @Override - public void start() { - // nothing to do - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorBuilder.java deleted file mode 100644 index 2d7e453cfaf..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorBuilder.java +++ /dev/null @@ -1,423 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; -import java.io.File; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.text.MessageFormat; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.apache.commons.lang.ArrayUtils; -import org.apache.commons.lang.ObjectUtils; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.api.utils.MessageException; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; -import org.sonar.batch.analysis.AnalysisProperties; -import org.sonar.batch.bootstrap.DroppedPropertyChecker; -import org.sonar.batch.util.BatchUtils; - -/** - * Class that creates a project definition based on a set of properties. - */ -public class ProjectReactorBuilder { - - private static final String INVALID_VALUE_OF_X_FOR_Y = "Invalid value of {0} for {1}"; - - /** - * A map of dropped properties as key and specific message to display for that property - * (what will happen, what should the user do, ...) as a value - */ - private static final Map<String, String> DROPPED_PROPERTIES = ImmutableMap.of( - "sonar.qualitygate", "It will be ignored."); - - private static final Logger LOG = Loggers.get(ProjectReactorBuilder.class); - - /** - * @since 4.1 but not yet exposed in {@link CoreProperties} - */ - private static final String MODULE_KEY_PROPERTY = "sonar.moduleKey"; - - protected static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir"; - private static final String PROPERTY_PROJECT_BUILDDIR = "sonar.projectBuildDir"; - private static final String PROPERTY_MODULES = "sonar.modules"; - - /** - * New properties, to be consistent with Sonar naming conventions - * - * @since 1.5 - */ - private static final String PROPERTY_SOURCES = "sonar.sources"; - private static final String PROPERTY_TESTS = "sonar.tests"; - - /** - * Array of all mandatory properties required for a project without child. - */ - private static final String[] MANDATORY_PROPERTIES_FOR_SIMPLE_PROJECT = { - PROPERTY_PROJECT_BASEDIR, CoreProperties.PROJECT_KEY_PROPERTY, CoreProperties.PROJECT_NAME_PROPERTY, - CoreProperties.PROJECT_VERSION_PROPERTY, PROPERTY_SOURCES - }; - - /** - * Array of all mandatory properties required for a project with children. - */ - private static final String[] MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT = {PROPERTY_PROJECT_BASEDIR, CoreProperties.PROJECT_KEY_PROPERTY, - CoreProperties.PROJECT_NAME_PROPERTY, CoreProperties.PROJECT_VERSION_PROPERTY}; - - /** - * Array of all mandatory properties required for a child project before its properties get merged with its parent ones. - */ - private static final String[] MANDATORY_PROPERTIES_FOR_CHILD = {MODULE_KEY_PROPERTY, CoreProperties.PROJECT_NAME_PROPERTY}; - - /** - * Properties that must not be passed from the parent project to its children. - */ - private static final List<String> NON_HERITED_PROPERTIES_FOR_CHILD = Lists.newArrayList(PROPERTY_PROJECT_BASEDIR, CoreProperties.WORKING_DIRECTORY, PROPERTY_MODULES, - CoreProperties.PROJECT_DESCRIPTION_PROPERTY); - - private static final String NON_ASSOCIATED_PROJECT_KEY = "project"; - - private final AnalysisProperties analysisProps; - private final AnalysisMode analysisMode; - private File rootProjectWorkDir; - - public ProjectReactorBuilder(AnalysisProperties props, AnalysisMode analysisMode) { - this.analysisProps = props; - this.analysisMode = analysisMode; - } - - public ProjectReactor execute() { - Profiler profiler = Profiler.create(LOG).startInfo("Process project properties"); - new DroppedPropertyChecker(analysisProps.properties(), DROPPED_PROPERTIES).checkDroppedProperties(); - Map<String, Map<String, String>> propertiesByModuleIdPath = new HashMap<>(); - extractPropertiesByModule(propertiesByModuleIdPath, "", "", analysisProps.properties()); - ProjectDefinition rootProject = defineRootProject(propertiesByModuleIdPath.get(""), null); - rootProjectWorkDir = rootProject.getWorkDir(); - defineChildren(rootProject, propertiesByModuleIdPath, ""); - cleanAndCheckProjectDefinitions(rootProject); - // Since task properties are now empty we should add root module properties - analysisProps.properties().putAll(propertiesByModuleIdPath.get("")); - profiler.stopDebug(); - return new ProjectReactor(rootProject); - } - - private static void extractPropertiesByModule(Map<String, Map<String, String>> propertiesByModuleIdPath, String currentModuleId, String currentModuleIdPath, - Map<String, String> parentProperties) { - if (propertiesByModuleIdPath.containsKey(currentModuleIdPath)) { - throw MessageException.of(String.format("Two modules have the same id: '%s'. Each module must have a unique id.", currentModuleId)); - } - - Map<String, String> currentModuleProperties = new HashMap<>(); - String prefix = !currentModuleId.isEmpty() ? (currentModuleId + ".") : ""; - int prefixLength = prefix.length(); - - // By default all properties starting with module prefix belong to current module - Iterator<Entry<String, String>> it = parentProperties.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry<String, String> e = it.next(); - String key = e.getKey(); - if (key.startsWith(prefix)) { - currentModuleProperties.put(key.substring(prefixLength), e.getValue()); - it.remove(); - } - } - String[] moduleIds = getListFromProperty(currentModuleProperties, PROPERTY_MODULES); - // Sort module by reverse lexicographic order to avoid issue when one module id is a prefix of another one - Arrays.sort(moduleIds); - ArrayUtils.reverse(moduleIds); - - propertiesByModuleIdPath.put(currentModuleIdPath, currentModuleProperties); - - for (String moduleId : moduleIds) { - String subModuleIdPath = currentModuleIdPath.isEmpty() ? moduleId : (currentModuleIdPath + "." + moduleId); - extractPropertiesByModule(propertiesByModuleIdPath, moduleId, subModuleIdPath, currentModuleProperties); - } - } - - private static void prepareNonAssociatedProject(Map<String, String> props, AnalysisMode mode) { - if (mode.isIssues() && !props.containsKey(CoreProperties.PROJECT_KEY_PROPERTY)) { - props.put(CoreProperties.PROJECT_KEY_PROPERTY, NON_ASSOCIATED_PROJECT_KEY); - } - } - - protected ProjectDefinition defineRootProject(Map<String, String> rootProperties, @Nullable ProjectDefinition parent) { - prepareNonAssociatedProject(rootProperties, analysisMode); - - if (rootProperties.containsKey(PROPERTY_MODULES)) { - checkMandatoryProperties(rootProperties, MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT); - } else { - checkMandatoryProperties(rootProperties, MANDATORY_PROPERTIES_FOR_SIMPLE_PROJECT); - } - File baseDir = new File(rootProperties.get(PROPERTY_PROJECT_BASEDIR)); - final String projectKey = rootProperties.get(CoreProperties.PROJECT_KEY_PROPERTY); - File workDir; - if (parent == null) { - validateDirectories(rootProperties, baseDir, projectKey); - workDir = initRootProjectWorkDir(baseDir, rootProperties); - } else { - workDir = initModuleWorkDir(baseDir, rootProperties); - } - - return ProjectDefinition.create().setProperties(rootProperties) - .setBaseDir(baseDir) - .setWorkDir(workDir) - .setBuildDir(initModuleBuildDir(baseDir, rootProperties)); - } - - @VisibleForTesting - protected File initRootProjectWorkDir(File baseDir, Map<String, String> rootProperties) { - String workDir = rootProperties.get(CoreProperties.WORKING_DIRECTORY); - if (StringUtils.isBlank(workDir)) { - return new File(baseDir, CoreProperties.WORKING_DIRECTORY_DEFAULT_VALUE); - } - - File customWorkDir = new File(workDir); - if (customWorkDir.isAbsolute()) { - return customWorkDir; - } - return new File(baseDir, customWorkDir.getPath()); - } - - @VisibleForTesting - protected File initModuleWorkDir(File moduleBaseDir, Map<String, String> moduleProperties) { - String workDir = moduleProperties.get(CoreProperties.WORKING_DIRECTORY); - if (StringUtils.isBlank(workDir)) { - return new File(rootProjectWorkDir, BatchUtils.cleanKeyForFilename(moduleProperties.get(CoreProperties.PROJECT_KEY_PROPERTY))); - } - - File customWorkDir = new File(workDir); - if (customWorkDir.isAbsolute()) { - return customWorkDir; - } - return new File(moduleBaseDir, customWorkDir.getPath()); - } - - @CheckForNull - private static File initModuleBuildDir(File moduleBaseDir, Map<String, String> moduleProperties) { - String buildDir = moduleProperties.get(PROPERTY_PROJECT_BUILDDIR); - if (StringUtils.isBlank(buildDir)) { - return null; - } - - File customBuildDir = new File(buildDir); - if (customBuildDir.isAbsolute()) { - return customBuildDir; - } - return new File(moduleBaseDir, customBuildDir.getPath()); - } - - private void defineChildren(ProjectDefinition parentProject, Map<String, Map<String, String>> propertiesByModuleIdPath, String parentModuleIdPath) { - Map<String, String> parentProps = parentProject.properties(); - if (parentProps.containsKey(PROPERTY_MODULES)) { - for (String moduleId : getListFromProperty(parentProps, PROPERTY_MODULES)) { - String moduleIdPath = parentModuleIdPath.isEmpty() ? moduleId : (parentModuleIdPath + "." + moduleId); - Map<String, String> moduleProps = propertiesByModuleIdPath.get(moduleIdPath); - ProjectDefinition childProject = loadChildProject(parentProject, moduleProps, moduleId); - // check the uniqueness of the child key - checkUniquenessOfChildKey(childProject, parentProject); - // the child project may have children as well - defineChildren(childProject, propertiesByModuleIdPath, moduleIdPath); - // and finally add this child project to its parent - parentProject.addSubProject(childProject); - } - } - } - - protected ProjectDefinition loadChildProject(ProjectDefinition parentProject, Map<String, String> moduleProps, String moduleId) { - final File baseDir; - if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) { - baseDir = resolvePath(parentProject.getBaseDir(), moduleProps.get(PROPERTY_PROJECT_BASEDIR)); - setProjectBaseDir(baseDir, moduleProps, moduleId); - } else { - baseDir = new File(parentProject.getBaseDir(), moduleId); - setProjectBaseDir(baseDir, moduleProps, moduleId); - } - - setModuleKeyAndNameIfNotDefined(moduleProps, moduleId, parentProject.getKey()); - - // and finish - checkMandatoryProperties(moduleProps, MANDATORY_PROPERTIES_FOR_CHILD); - validateDirectories(moduleProps, baseDir, moduleId); - - mergeParentProperties(moduleProps, parentProject.properties()); - - return defineRootProject(moduleProps, parentProject); - } - - @VisibleForTesting - protected static void setModuleKeyAndNameIfNotDefined(Map<String, String> childProps, String moduleId, String parentKey) { - if (!childProps.containsKey(MODULE_KEY_PROPERTY)) { - if (!childProps.containsKey(CoreProperties.PROJECT_KEY_PROPERTY)) { - childProps.put(MODULE_KEY_PROPERTY, parentKey + ":" + moduleId); - } else { - String childKey = childProps.get(CoreProperties.PROJECT_KEY_PROPERTY); - childProps.put(MODULE_KEY_PROPERTY, parentKey + ":" + childKey); - } - } - if (!childProps.containsKey(CoreProperties.PROJECT_NAME_PROPERTY)) { - childProps.put(CoreProperties.PROJECT_NAME_PROPERTY, moduleId); - } - // For backward compatibility with ProjectDefinition - childProps.put(CoreProperties.PROJECT_KEY_PROPERTY, childProps.get(MODULE_KEY_PROPERTY)); - } - - @VisibleForTesting - protected static void checkUniquenessOfChildKey(ProjectDefinition childProject, ProjectDefinition parentProject) { - for (ProjectDefinition definition : parentProject.getSubProjects()) { - if (definition.getKey().equals(childProject.getKey())) { - throw MessageException.of("Project '" + parentProject.getKey() + "' can't have 2 modules with the following key: " + childProject.getKey()); - } - } - } - - protected static void setProjectBaseDir(File baseDir, Map<String, String> childProps, String moduleId) { - if (!baseDir.isDirectory()) { - throw MessageException.of("The base directory of the module '" + moduleId + "' does not exist: " + baseDir.getAbsolutePath()); - } - childProps.put(PROPERTY_PROJECT_BASEDIR, baseDir.getAbsolutePath()); - } - - @VisibleForTesting - protected static void checkMandatoryProperties(Map<String, String> props, String[] mandatoryProps) { - StringBuilder missing = new StringBuilder(); - for (String mandatoryProperty : mandatoryProps) { - if (!props.containsKey(mandatoryProperty)) { - if (missing.length() > 0) { - missing.append(", "); - } - missing.append(mandatoryProperty); - } - } - String moduleKey = StringUtils.defaultIfBlank(props.get(MODULE_KEY_PROPERTY), props.get(CoreProperties.PROJECT_KEY_PROPERTY)); - if (missing.length() != 0) { - throw MessageException.of("You must define the following mandatory properties for '" + (moduleKey == null ? "Unknown" : moduleKey) + "': " + missing); - } - } - - protected static void validateDirectories(Map<String, String> props, File baseDir, String projectId) { - if (!props.containsKey(PROPERTY_MODULES)) { - // SONARPLUGINS-2285 Not an aggregator project so we can validate that paths are correct if defined - - // Check sonar.tests - String[] testPaths = getListFromProperty(props, PROPERTY_TESTS); - checkExistenceOfPaths(projectId, baseDir, testPaths, PROPERTY_TESTS); - } - } - - @VisibleForTesting - protected static void cleanAndCheckProjectDefinitions(ProjectDefinition project) { - if (project.getSubProjects().isEmpty()) { - cleanAndCheckModuleProperties(project); - } else { - cleanAndCheckAggregatorProjectProperties(project); - - // clean modules properties as well - for (ProjectDefinition module : project.getSubProjects()) { - cleanAndCheckProjectDefinitions(module); - } - } - } - - @VisibleForTesting - protected static void cleanAndCheckModuleProperties(ProjectDefinition project) { - Map<String, String> properties = project.properties(); - - // We need to check the existence of source directories - String[] sourcePaths = getListFromProperty(properties, PROPERTY_SOURCES); - checkExistenceOfPaths(project.getKey(), project.getBaseDir(), sourcePaths, PROPERTY_SOURCES); - } - - @VisibleForTesting - protected static void cleanAndCheckAggregatorProjectProperties(ProjectDefinition project) { - Map<String, String> properties = project.properties(); - - // SONARPLUGINS-2295 - String[] sourceDirs = getListFromProperty(properties, PROPERTY_SOURCES); - for (String path : sourceDirs) { - File sourceFolder = resolvePath(project.getBaseDir(), path); - if (sourceFolder.isDirectory()) { - LOG.warn("/!\\ A multi-module project can't have source folders, so '{}' won't be used for the analysis. " + - "If you want to analyse files of this folder, you should create another sub-module and move them inside it.", - sourceFolder.toString()); - } - } - - // "aggregator" project must not have the following properties: - properties.remove(PROPERTY_SOURCES); - properties.remove(PROPERTY_TESTS); - } - - @VisibleForTesting - protected static void mergeParentProperties(Map<String, String> childProps, Map<String, String> parentProps) { - for (Map.Entry<String, String> entry : parentProps.entrySet()) { - String key = entry.getKey(); - if ((!childProps.containsKey(key) || childProps.get(key).equals(entry.getValue())) - && !NON_HERITED_PROPERTIES_FOR_CHILD.contains(key)) { - childProps.put(entry.getKey(), entry.getValue()); - } - } - } - - @VisibleForTesting - protected static void checkExistenceOfPaths(String moduleRef, File baseDir, String[] paths, String propName) { - for (String path : paths) { - File sourceFolder = resolvePath(baseDir, path); - if (!sourceFolder.exists()) { - LOG.error(MessageFormat.format(INVALID_VALUE_OF_X_FOR_Y, propName, moduleRef)); - throw MessageException.of("The folder '" + path + "' does not exist for '" + moduleRef + - "' (base directory = " + baseDir.getAbsolutePath() + ")"); - } - } - } - - protected static File resolvePath(File baseDir, String path) { - Path filePath = Paths.get(path); - if (!filePath.isAbsolute()) { - filePath = baseDir.toPath().resolve(path); - } - return filePath.normalize().toFile(); - } - - /** - * Transforms a comma-separated list String property in to a array of trimmed strings. - * - * This works even if they are separated by whitespace characters (space char, EOL, ...) - * - */ - static String[] getListFromProperty(Map<String, String> properties, String key) { - return (String[]) ObjectUtils.defaultIfNull(StringUtils.stripAll(StringUtils.split(properties.get(key), ',')), new String[0]); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java deleted file mode 100644 index 7e36ab54958..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectReactorValidator.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import org.sonar.api.utils.MessageException; - -import org.sonar.batch.analysis.DefaultAnalysisMode; -import com.google.common.base.Joiner; - -import java.util.ArrayList; -import java.util.List; - -import javax.annotation.Nullable; - -import org.apache.commons.lang.StringUtils; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.api.config.Settings; -import org.sonar.core.component.ComponentKeys; - -/** - * This class aims at validating project reactor - * @since 3.6 - */ -public class ProjectReactorValidator { - - private static final String SONAR_PHASE = "sonar.phase"; - private final Settings settings; - private final DefaultAnalysisMode mode; - - public ProjectReactorValidator(Settings settings, DefaultAnalysisMode mode) { - this.settings = settings; - this.mode = mode; - } - - public void validate(ProjectReactor reactor) { - String branch = reactor.getRoot().getBranch(); - - List<String> validationMessages = new ArrayList<>(); - checkDeprecatedProperties(validationMessages); - - for (ProjectDefinition moduleDef : reactor.getProjects()) { - if (mode.isIssues()) { - validateModuleIssuesMode(moduleDef, validationMessages); - } else { - validateModule(moduleDef, validationMessages); - } - } - - validateBranch(validationMessages, branch); - - if (!validationMessages.isEmpty()) { - throw MessageException.of("Validation of project reactor failed:\n o " + Joiner.on("\n o ").join(validationMessages)); - } - } - - private static void validateModuleIssuesMode(ProjectDefinition moduleDef, List<String> validationMessages) { - if (!ComponentKeys.isValidModuleKeyIssuesMode(moduleDef.getKey())) { - validationMessages.add(String.format("\"%s\" is not a valid project or module key. " - + "Allowed characters in issues mode are alphanumeric, '-', '_', '.', '/' and ':', with at least one non-digit.", moduleDef.getKey())); - } - } - - private static void validateModule(ProjectDefinition moduleDef, List<String> validationMessages) { - if (!ComponentKeys.isValidModuleKey(moduleDef.getKey())) { - validationMessages.add(String.format("\"%s\" is not a valid project or module key. " - + "Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", moduleDef.getKey())); - } - } - - private void checkDeprecatedProperties(List<String> validationMessages) { - if (settings.getString(SONAR_PHASE) != null) { - validationMessages.add(String.format("Property \"%s\" is deprecated. Please remove it from your configuration.", SONAR_PHASE)); - } - } - - private static void validateBranch(List<String> validationMessages, @Nullable String branch) { - if (StringUtils.isNotEmpty(branch) && !ComponentKeys.isValidBranch(branch)) { - validationMessages.add(String.format("\"%s\" is not a valid branch name. " - + "Allowed characters are alphanumeric, '-', '_', '.' and '/'.", branch)); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java deleted file mode 100644 index ff8108e4871..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ /dev/null @@ -1,277 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import com.google.common.annotations.VisibleForTesting; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.InstantiationStrategy; -import org.sonar.api.config.Settings; -import org.sonar.api.resources.Languages; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.ResourceTypes; -import org.sonar.api.scan.filesystem.PathResolver; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.batch.DefaultFileLinesContextFactory; -import org.sonar.batch.DefaultProjectTree; -import org.sonar.batch.ProjectConfigurator; -import org.sonar.batch.analysis.AnalysisProperties; -import org.sonar.batch.analysis.AnalysisTempFolderProvider; -import org.sonar.batch.analysis.AnalysisWSLoaderProvider; -import org.sonar.batch.analysis.DefaultAnalysisMode; -import org.sonar.batch.bootstrap.ExtensionInstaller; -import org.sonar.batch.bootstrap.ExtensionMatcher; -import org.sonar.batch.bootstrap.ExtensionUtils; -import org.sonar.batch.bootstrap.MetricProvider; -import org.sonar.batch.cache.ProjectPersistentCacheProvider; -import org.sonar.batch.cpd.CpdExecutor; -import org.sonar.batch.cpd.index.SonarCpdBlockIndex; -import org.sonar.batch.events.EventBus; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.index.Caches; -import org.sonar.batch.index.DefaultIndex; -import org.sonar.batch.issue.DefaultIssueCallback; -import org.sonar.batch.issue.DefaultProjectIssues; -import org.sonar.batch.issue.IssueCache; -import org.sonar.batch.issue.tracking.DefaultServerLineHashesLoader; -import org.sonar.batch.issue.tracking.IssueTransition; -import org.sonar.batch.issue.tracking.LocalIssueTracking; -import org.sonar.batch.issue.tracking.ServerIssueRepository; -import org.sonar.batch.issue.tracking.ServerLineHashesLoader; -import org.sonar.batch.mediumtest.ScanTaskObservers; -import org.sonar.batch.phases.PhasesTimeProfiler; -import org.sonar.batch.profiling.PhasesSumUpTimeProfiler; -import org.sonar.batch.report.ActiveRulesPublisher; -import org.sonar.batch.report.AnalysisContextReportPublisher; -import org.sonar.batch.report.ComponentsPublisher; -import org.sonar.batch.report.CoveragePublisher; -import org.sonar.batch.report.MeasuresPublisher; -import org.sonar.batch.report.MetadataPublisher; -import org.sonar.batch.report.ReportPublisher; -import org.sonar.batch.report.SourcePublisher; -import org.sonar.batch.report.TestExecutionAndCoveragePublisher; -import org.sonar.batch.repository.DefaultProjectRepositoriesLoader; -import org.sonar.batch.repository.DefaultQualityProfileLoader; -import org.sonar.batch.repository.DefaultServerIssuesLoader; -import org.sonar.batch.repository.ProjectRepositories; -import org.sonar.batch.repository.ProjectRepositoriesLoader; -import org.sonar.batch.repository.ProjectRepositoriesProvider; -import org.sonar.batch.repository.QualityProfileLoader; -import org.sonar.batch.repository.QualityProfileProvider; -import org.sonar.batch.repository.ServerIssuesLoader; -import org.sonar.batch.repository.language.DefaultLanguagesRepository; -import org.sonar.batch.repository.user.UserRepositoryLoader; -import org.sonar.batch.rule.ActiveRulesLoader; -import org.sonar.batch.rule.ActiveRulesProvider; -import org.sonar.batch.rule.DefaultActiveRulesLoader; -import org.sonar.batch.rule.DefaultRulesLoader; -import org.sonar.batch.rule.RulesLoader; -import org.sonar.batch.rule.RulesProvider; -import org.sonar.batch.scan.filesystem.InputPathCache; -import org.sonar.batch.scan.measure.DefaultMetricFinder; -import org.sonar.batch.scan.measure.DeprecatedMetricFinder; -import org.sonar.batch.scan.measure.MeasureCache; -import org.sonar.batch.source.CodeColorizers; -import org.sonar.batch.test.TestPlanBuilder; -import org.sonar.batch.test.TestableBuilder; -import org.sonar.core.metric.BatchMetrics; -import org.sonar.core.platform.ComponentContainer; - -public class ProjectScanContainer extends ComponentContainer { - - private static final Logger LOG = Loggers.get(ProjectScanContainer.class); - - private final AnalysisProperties props; - private ProjectLock lock; - - public ProjectScanContainer(ComponentContainer globalContainer, AnalysisProperties props) { - super(globalContainer); - this.props = props; - } - - @Override - protected void doBeforeStart() { - addBatchComponents(); - lock = getComponentByType(ProjectLock.class); - lock.tryLock(); - getComponentByType(WorkDirectoryCleaner.class).execute(); - addBatchExtensions(); - Settings settings = getComponentByType(Settings.class); - if (settings != null && settings.getBoolean(CoreProperties.PROFILING_LOG_PROPERTY)) { - add(PhasesSumUpTimeProfiler.class); - } - if (isTherePreviousAnalysis()) { - addIssueTrackingComponents(); - } - } - - @Override - public ComponentContainer startComponents() { - try { - return super.startComponents(); - } catch (Exception e) { - // ensure that lock is released - if (lock != null) { - lock.stop(); - } - throw e; - } - } - - private void addBatchComponents() { - add( - props, - DefaultAnalysisMode.class, - ProjectReactorBuilder.class, - WorkDirectoryCleaner.class, - new MutableProjectReactorProvider(), - new ImmutableProjectReactorProvider(), - ProjectBuildersExecutor.class, - ProjectLock.class, - EventBus.class, - PhasesTimeProfiler.class, - ResourceTypes.class, - DefaultProjectTree.class, - ProjectExclusions.class, - ProjectReactorValidator.class, - new AnalysisWSLoaderProvider(), - CodeColorizers.class, - MetricProvider.class, - ProjectConfigurator.class, - DefaultIndex.class, - DefaultFileLinesContextFactory.class, - Caches.class, - BatchComponentCache.class, - DefaultIssueCallback.class, - new RulesProvider(), - new ProjectRepositoriesProvider(), - new ProjectPersistentCacheProvider(), - - // temp - new AnalysisTempFolderProvider(), - - // file system - InputPathCache.class, - PathResolver.class, - - // rules - new ActiveRulesProvider(), - new QualityProfileProvider(), - - // issues - IssueCache.class, - DefaultProjectIssues.class, - IssueTransition.class, - - // metrics - DefaultMetricFinder.class, - DeprecatedMetricFinder.class, - - // tests - TestPlanBuilder.class, - TestableBuilder.class, - - // lang - Languages.class, - DefaultLanguagesRepository.class, - - // Measures - MeasureCache.class, - - ProjectSettings.class, - - // Report - BatchMetrics.class, - ReportPublisher.class, - AnalysisContextReportPublisher.class, - MetadataPublisher.class, - ActiveRulesPublisher.class, - ComponentsPublisher.class, - MeasuresPublisher.class, - CoveragePublisher.class, - SourcePublisher.class, - TestExecutionAndCoveragePublisher.class, - - // Cpd - CpdExecutor.class, - SonarCpdBlockIndex.class, - - ScanTaskObservers.class, - UserRepositoryLoader.class); - - addIfMissing(DefaultRulesLoader.class, RulesLoader.class); - addIfMissing(DefaultActiveRulesLoader.class, ActiveRulesLoader.class); - addIfMissing(DefaultQualityProfileLoader.class, QualityProfileLoader.class); - addIfMissing(DefaultProjectRepositoriesLoader.class, ProjectRepositoriesLoader.class); - } - - private void addIssueTrackingComponents() { - add( - LocalIssueTracking.class, - ServerIssueRepository.class); - addIfMissing(DefaultServerIssuesLoader.class, ServerIssuesLoader.class); - addIfMissing(DefaultServerLineHashesLoader.class, ServerLineHashesLoader.class); - } - - private boolean isTherePreviousAnalysis() { - if (getComponentByType(DefaultAnalysisMode.class).isNotAssociated()) { - return false; - } - - return getComponentByType(ProjectRepositories.class).lastAnalysisDate() != null; - } - - private void addBatchExtensions() { - getComponentByType(ExtensionInstaller.class).install(this, new BatchExtensionFilter()); - } - - @Override - protected void doAfterStart() { - DefaultAnalysisMode analysisMode = getComponentByType(DefaultAnalysisMode.class); - analysisMode.printMode(); - LOG.debug("Start recursive analysis of project modules"); - DefaultProjectTree tree = getComponentByType(DefaultProjectTree.class); - scanRecursively(tree.getRootProject()); - if (analysisMode.isMediumTest()) { - getComponentByType(ScanTaskObservers.class).notifyEndOfScanTask(); - } - } - - private void scanRecursively(Project module) { - for (Project subModules : module.getModules()) { - scanRecursively(subModules); - } - scan(module); - } - - @VisibleForTesting - void scan(Project module) { - new ModuleScanContainer(this, module).execute(); - } - - static class BatchExtensionFilter implements ExtensionMatcher { - @Override - public boolean accept(Object extension) { - return ExtensionUtils.isBatchSide(extension) - && ExtensionUtils.isInstantiationStrategy(extension, InstantiationStrategy.PER_BATCH); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettings.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettings.java deleted file mode 100644 index 382d02424a3..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettings.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.api.config.PropertyDefinitions; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.MessageException; -import org.sonar.batch.analysis.DefaultAnalysisMode; -import org.sonar.batch.bootstrap.GlobalSettings; -import org.sonar.batch.repository.ProjectRepositories; - -public class ProjectSettings extends Settings { - - private final GlobalSettings globalSettings; - private final ProjectRepositories projectRepositories; - private final DefaultAnalysisMode mode; - - public ProjectSettings(ProjectReactor reactor, GlobalSettings globalSettings, PropertyDefinitions propertyDefinitions, - ProjectRepositories projectRepositories, DefaultAnalysisMode mode) { - super(propertyDefinitions); - this.mode = mode; - getEncryption().setPathToSecretKey(globalSettings.getString(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); - this.globalSettings = globalSettings; - this.projectRepositories = projectRepositories; - init(reactor); - } - - private void init(ProjectReactor reactor) { - addProperties(globalSettings.getProperties()); - - addProperties(projectRepositories.settings(reactor.getRoot().getKeyWithBranch())); - - addProperties(reactor.getRoot().properties()); - } - - @Override - protected void doOnGetProperties(String key) { - if (mode.isIssues() && key.endsWith(".secured") && !key.contains(".license")) { - throw MessageException.of("Access to the secured property '" + key - + "' is not possible in issues mode. The SonarQube plugin which requires this property must be deactivated in issues mode."); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/WorkDirectoryCleaner.java b/sonar-batch/src/main/java/org/sonar/batch/scan/WorkDirectoryCleaner.java deleted file mode 100644 index b4857bf6afb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/WorkDirectoryCleaner.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan; - -import org.sonar.api.batch.bootstrap.ProjectReactor; -import org.sonar.core.util.FileUtils; -import org.sonar.home.cache.DirectoryLock; - -import java.io.IOException; -import java.nio.file.DirectoryStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Iterator; - -public class WorkDirectoryCleaner { - private Path workDir; - - public WorkDirectoryCleaner(ProjectReactor projectReactor) { - workDir = projectReactor.getRoot().getWorkDir().toPath(); - } - - public void execute() { - if (!Files.exists(workDir)) { - return; - } - - try (DirectoryStream<Path> stream = list()) { - - Iterator<Path> it = stream.iterator(); - while (it.hasNext()) { - FileUtils.deleteQuietly(it.next().toFile()); - } - } catch (IOException e) { - throw new IllegalStateException("Failed to clean working directory: " + workDir.toString(), e); - } - } - - private DirectoryStream<Path> list() throws IOException { - return Files.newDirectoryStream(workDir, new DirectoryStream.Filter<Path>() { - @Override - public boolean accept(Path entry) throws IOException { - return !DirectoryLock.LOCK_FILE_NAME.equals(entry.getFileName().toString()); - } - }); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/AdditionalFilePredicates.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/AdditionalFilePredicates.java deleted file mode 100644 index b68b6a3625a..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/AdditionalFilePredicates.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.AbstractFilePredicate; -import org.sonar.api.batch.fs.internal.DefaultInputFile; - -/** - * Additional {@link org.sonar.api.batch.fs.FilePredicate}s that are - * not published in public API - */ -class AdditionalFilePredicates { - - private AdditionalFilePredicates() { - // only static inner classes - } - - static class KeyPredicate extends AbstractFilePredicate { - private final String key; - - KeyPredicate(String key) { - this.key = key; - } - - @Override - public boolean apply(InputFile f) { - return key.equals(((DefaultInputFile) f).key()); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java deleted file mode 100644 index 761f6d15823..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.SonarIndex; -import org.sonar.api.batch.fs.InputDir; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.resources.File; -import org.sonar.api.resources.Languages; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.Resource; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; - -/** - * Index all files/directories of the module in SQ database and importing source code. - * - * @since 4.2 - */ -@BatchSide -public class ComponentIndexer { - - private final Languages languages; - private final SonarIndex sonarIndex; - private final Project module; - private final BatchComponentCache componentCache; - - public ComponentIndexer(Project module, Languages languages, SonarIndex sonarIndex, BatchComponentCache componentCache) { - this.module = module; - this.languages = languages; - this.sonarIndex = sonarIndex; - this.componentCache = componentCache; - } - - public void execute(DefaultModuleFileSystem fs) { - module.setBaseDir(fs.baseDir()); - - for (InputFile inputFile : fs.inputFiles()) { - String languageKey = inputFile.language(); - boolean unitTest = InputFile.Type.TEST == inputFile.type(); - Resource sonarFile = File.create(inputFile.relativePath(), languages.get(languageKey), unitTest); - sonarIndex.index(sonarFile); - BatchComponent file = componentCache.get(sonarFile); - file.setInputComponent(inputFile); - Resource sonarDir = file.parent().resource(); - InputDir inputDir = fs.inputDir(inputFile.file().getParentFile()); - componentCache.get(sonarDir).setInputComponent(inputDir); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java deleted file mode 100644 index 125a73d0078..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java +++ /dev/null @@ -1,285 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.sonar.api.batch.fs.InputFile.Status; - -import org.sonar.batch.analysis.DefaultAnalysisMode; -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Function; -import com.google.common.collect.Collections2; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; - -import java.io.File; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - -import org.apache.commons.lang.StringUtils; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.fs.FilePredicate; -import org.sonar.api.batch.fs.internal.DefaultFileSystem; -import org.sonar.api.config.Settings; -import org.sonar.api.resources.Project; -import org.sonar.api.scan.filesystem.FileQuery; -import org.sonar.api.scan.filesystem.ModuleFileSystem; -import org.sonar.api.utils.MessageException; - -/** - * @since 3.5 - */ -public class DefaultModuleFileSystem extends DefaultFileSystem implements ModuleFileSystem { - - private String moduleKey; - private FileIndexer indexer; - private Settings settings; - - private File buildDir; - private List<File> sourceDirsOrFiles = Lists.newArrayList(); - private List<File> testDirsOrFiles = Lists.newArrayList(); - private List<File> binaryDirs = Lists.newArrayList(); - private ComponentIndexer componentIndexer; - private boolean initialized; - - public DefaultModuleFileSystem(ModuleInputFileCache moduleInputFileCache, Project project, - Settings settings, FileIndexer indexer, ModuleFileSystemInitializer initializer, ComponentIndexer componentIndexer, DefaultAnalysisMode mode) { - super(initializer.baseDir(), moduleInputFileCache); - setFields(project, settings, indexer, initializer, componentIndexer, mode); - } - - @VisibleForTesting - public DefaultModuleFileSystem(Project project, - Settings settings, FileIndexer indexer, ModuleFileSystemInitializer initializer, ComponentIndexer componentIndexer, DefaultAnalysisMode mode) { - super(initializer.baseDir().toPath()); - setFields(project, settings, indexer, initializer, componentIndexer, mode); - } - - private void setFields(Project project, - Settings settings, FileIndexer indexer, ModuleFileSystemInitializer initializer, ComponentIndexer componentIndexer, DefaultAnalysisMode mode) { - this.componentIndexer = componentIndexer; - this.moduleKey = project.getKey(); - this.settings = settings; - this.indexer = indexer; - setWorkDir(initializer.workingDir()); - this.buildDir = initializer.buildDir(); - this.sourceDirsOrFiles = initializer.sources(); - this.testDirsOrFiles = initializer.tests(); - this.binaryDirs = initializer.binaryDirs(); - - // filter the files sensors have access to - if (!mode.scanAllFiles()) { - setDefaultPredicate(predicates.not(predicates.hasStatus(Status.SAME))); - } - } - - public boolean isInitialized() { - return initialized; - } - - public String moduleKey() { - return moduleKey; - } - - @Override - @CheckForNull - public File buildDir() { - return buildDir; - } - - @Override - public List<File> sourceDirs() { - return keepOnlyDirs(sourceDirsOrFiles); - } - - public List<File> sources() { - return sourceDirsOrFiles; - } - - @Override - public List<File> testDirs() { - return keepOnlyDirs(testDirsOrFiles); - } - - public List<File> tests() { - return testDirsOrFiles; - } - - private static List<File> keepOnlyDirs(List<File> dirsOrFiles) { - List<File> result = new ArrayList<>(); - for (File f : dirsOrFiles) { - if (f.isDirectory()) { - result.add(f); - } - } - return result; - } - - @Override - public List<File> binaryDirs() { - return binaryDirs; - } - - @Override - public Charset encoding() { - final Charset charset; - String encoding = settings.getString(CoreProperties.ENCODING_PROPERTY); - if (StringUtils.isNotEmpty(encoding)) { - charset = Charset.forName(StringUtils.trim(encoding)); - } else { - charset = Charset.defaultCharset(); - } - return charset; - } - - @Override - public boolean isDefaultJvmEncoding() { - return !settings.hasKey(CoreProperties.ENCODING_PROPERTY); - } - - /** - * Should not be used - only for old plugins - * - * @deprecated since 4.0 - */ - @Deprecated - void addSourceDir(File dir) { - throw modificationNotPermitted(); - } - - /** - * Should not be used - only for old plugins - * - * @deprecated since 4.0 - */ - @Deprecated - void addTestDir(File dir) { - throw modificationNotPermitted(); - } - - private static UnsupportedOperationException modificationNotPermitted() { - return new UnsupportedOperationException("Modifications of the file system are not permitted"); - } - - /** - * @return - * @deprecated in 4.2. Replaced by {@link #encoding()} - */ - @Override - @Deprecated - public Charset sourceCharset() { - return encoding(); - } - - /** - * @deprecated in 4.2. Replaced by {@link #workDir()} - */ - @Deprecated - @Override - public File workingDir() { - return workDir(); - } - - @Override - public List<File> files(FileQuery query) { - doPreloadFiles(); - Collection<FilePredicate> predicates = Lists.newArrayList(); - for (Map.Entry<String, Collection<String>> entry : query.attributes().entrySet()) { - predicates.add(fromDeprecatedAttribute(entry.getKey(), entry.getValue())); - } - return ImmutableList.copyOf(files(predicates().and(predicates))); - } - - @Override - protected void doPreloadFiles() { - if (!initialized) { - throw MessageException.of("Accessing the filesystem before the Sensor phase is not supported. Please update your plugin."); - } - } - - public void index() { - if (initialized) { - throw MessageException.of("Module filesystem can only be indexed once"); - } - initialized = true; - indexer.index(this); - if (componentIndexer != null) { - componentIndexer.execute(this); - } - } - - private FilePredicate fromDeprecatedAttribute(String key, Collection<String> value) { - if ("TYPE".equals(key)) { - return predicates().or(Collections2.transform(value, new Function<String, FilePredicate>() { - @Override - public FilePredicate apply(@Nullable String s) { - return s == null ? predicates().all() : predicates().hasType(org.sonar.api.batch.fs.InputFile.Type.valueOf(s)); - } - })); - } - if ("STATUS".equals(key)) { - return predicates().or(Collections2.transform(value, new Function<String, FilePredicate>() { - @Override - public FilePredicate apply(@Nullable String s) { - return s == null ? predicates().all() : predicates().hasStatus(org.sonar.api.batch.fs.InputFile.Status.valueOf(s)); - } - })); - } - if ("LANG".equals(key)) { - return predicates().or(Collections2.transform(value, new Function<String, FilePredicate>() { - @Override - public FilePredicate apply(@Nullable String s) { - return s == null ? predicates().all() : predicates().hasLanguage(s); - } - })); - } - if ("CMP_KEY".equals(key)) { - return predicates().or(Collections2.transform(value, new Function<String, FilePredicate>() { - @Override - public FilePredicate apply(@Nullable String s) { - return s == null ? predicates().all() : new AdditionalFilePredicates.KeyPredicate(s); - } - })); - } - throw new IllegalArgumentException("Unsupported file attribute: " + key); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - DefaultModuleFileSystem that = (DefaultModuleFileSystem) o; - return moduleKey.equals(that.moduleKey); - } - - @Override - public int hashCode() { - return moduleKey.hashCode(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileFilters.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileFilters.java deleted file mode 100644 index 29968e7e102..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileFilters.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputFileFilter; -import org.sonar.api.scan.filesystem.FileSystemFilter; -import org.sonar.api.scan.filesystem.FileType; -import org.sonar.api.scan.filesystem.ModuleFileSystem; - -public class DeprecatedFileFilters implements InputFileFilter { - private final FileSystemFilter[] filters; - - public DeprecatedFileFilters(FileSystemFilter[] filters) { - this.filters = filters; - } - - public DeprecatedFileFilters() { - this(new FileSystemFilter[0]); - } - - @Override - public boolean accept(InputFile inputFile) { - if (filters.length > 0) { - DeprecatedContext context = new DeprecatedContext(inputFile); - for (FileSystemFilter filter : filters) { - if (!filter.accept(inputFile.file(), context)) { - return false; - } - } - } - return true; - } - - static class DeprecatedContext implements FileSystemFilter.Context { - private final InputFile inputFile; - - DeprecatedContext(InputFile inputFile) { - this.inputFile = inputFile; - } - - @Override - public ModuleFileSystem fileSystem() { - throw new UnsupportedOperationException("Not supported since 4.0"); - } - - @Override - public FileType type() { - String type = inputFile.type().name(); - return FileType.valueOf(type); - } - - @Override - public String relativePath() { - return inputFile.relativePath(); - } - - @Override - public String canonicalPath() { - return inputFile.absolutePath(); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilters.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilters.java deleted file mode 100644 index 2a2952884f5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilters.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.apache.commons.lang.ArrayUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.PathPattern; -import org.sonar.api.scan.filesystem.FileExclusions; - -@BatchSide -public class ExclusionFilters { - - private static final Logger LOG = LoggerFactory.getLogger(ExclusionFilters.class); - - private final FileExclusions exclusionSettings; - - private PathPattern[] mainInclusions; - private PathPattern[] mainExclusions; - private PathPattern[] testInclusions; - private PathPattern[] testExclusions; - - public ExclusionFilters(FileExclusions exclusions) { - this.exclusionSettings = exclusions; - } - - public void prepare() { - mainInclusions = prepareMainInclusions(); - mainExclusions = prepareMainExclusions(); - testInclusions = prepareTestInclusions(); - testExclusions = prepareTestExclusions(); - log("Included sources: ", mainInclusions); - log("Excluded sources: ", mainExclusions); - log("Included tests: ", testInclusions); - log("Excluded tests: ", testExclusions); - } - - public boolean hasPattern() { - return mainInclusions.length > 0 || mainExclusions.length > 0 || testInclusions.length > 0 || testExclusions.length > 0; - } - - private void log(String title, PathPattern[] patterns) { - if (patterns.length > 0) { - LOG.info(title); - for (PathPattern pattern : patterns) { - LOG.info(" " + pattern); - } - } - } - - public boolean accept(InputFile inputFile, InputFile.Type type) { - PathPattern[] inclusionPatterns; - PathPattern[] exclusionPatterns; - if (InputFile.Type.MAIN == type) { - inclusionPatterns = mainInclusions; - exclusionPatterns = mainExclusions; - } else if (InputFile.Type.TEST == type) { - inclusionPatterns = testInclusions; - exclusionPatterns = testExclusions; - } else { - throw new IllegalArgumentException("Unknown file type: " + type); - } - - if (inclusionPatterns.length > 0) { - boolean matchInclusion = false; - for (PathPattern pattern : inclusionPatterns) { - matchInclusion |= pattern.match(inputFile); - } - if (!matchInclusion) { - return false; - } - } - if (exclusionPatterns.length > 0) { - for (PathPattern pattern : exclusionPatterns) { - if (pattern.match(inputFile)) { - return false; - } - } - } - return true; - } - - PathPattern[] prepareMainInclusions() { - if (exclusionSettings.sourceInclusions().length > 0) { - // User defined params - return PathPattern.create(exclusionSettings.sourceInclusions()); - } - return new PathPattern[0]; - } - - PathPattern[] prepareTestInclusions() { - return PathPattern.create(computeTestInclusions()); - } - - private String[] computeTestInclusions() { - if (exclusionSettings.testInclusions().length > 0) { - // User defined params - return exclusionSettings.testInclusions(); - } - return ArrayUtils.EMPTY_STRING_ARRAY; - } - - PathPattern[] prepareMainExclusions() { - String[] patterns = (String[]) ArrayUtils.addAll( - exclusionSettings.sourceExclusions(), computeTestInclusions()); - return PathPattern.create(patterns); - } - - PathPattern[] prepareTestExclusions() { - return PathPattern.create(exclusionSettings.testExclusions()); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java deleted file mode 100644 index 9ebf91886a0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java +++ /dev/null @@ -1,268 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import com.google.common.util.concurrent.ThreadFactoryBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputFile.Type; -import org.sonar.api.batch.fs.InputFileFilter; -import org.sonar.api.batch.fs.internal.DefaultInputDir; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.scan.filesystem.PathResolver; -import org.sonar.api.utils.MessageException; -import org.sonar.batch.util.ProgressReport; -import org.sonar.home.cache.DirectoryLock; - -import java.io.File; -import java.io.IOException; -import java.nio.file.FileSystemLoopException; -import java.nio.file.FileVisitOption; -import java.nio.file.FileVisitResult; -import java.nio.file.FileVisitor; -import java.nio.file.Files; -import java.nio.file.LinkOption; -import java.nio.file.Path; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; - -/** - * Index input files into {@link InputPathCache}. - */ -@BatchSide -public class FileIndexer { - - private static final Logger LOG = LoggerFactory.getLogger(FileIndexer.class); - private final List<InputFileFilter> filters; - private final boolean isAggregator; - private final ExclusionFilters exclusionFilters; - private final InputFileBuilderFactory inputFileBuilderFactory; - - private ProgressReport progressReport; - private ExecutorService executorService; - private List<Future<Void>> tasks; - - public FileIndexer(List<InputFileFilter> filters, ExclusionFilters exclusionFilters, InputFileBuilderFactory inputFileBuilderFactory, - ProjectDefinition def) { - this.filters = filters; - this.exclusionFilters = exclusionFilters; - this.inputFileBuilderFactory = inputFileBuilderFactory; - this.isAggregator = !def.getSubProjects().isEmpty(); - } - - void index(DefaultModuleFileSystem fileSystem) { - if (isAggregator) { - // No indexing for an aggregator module - return; - } - progressReport = new ProgressReport("Report about progress of file indexation", TimeUnit.SECONDS.toMillis(10)); - progressReport.start("Index files"); - exclusionFilters.prepare(); - - Progress progress = new Progress(); - - InputFileBuilder inputFileBuilder = inputFileBuilderFactory.create(fileSystem); - int threads = Math.max(1, Runtime.getRuntime().availableProcessors() - 1); - executorService = Executors.newFixedThreadPool(threads, new ThreadFactoryBuilder().setNameFormat("FileIndexer-%d").build()); - tasks = new ArrayList<>(); - indexFiles(fileSystem, progress, inputFileBuilder, fileSystem.sources(), InputFile.Type.MAIN); - indexFiles(fileSystem, progress, inputFileBuilder, fileSystem.tests(), InputFile.Type.TEST); - - waitForTasksToComplete(); - - progressReport.stop(progress.count() + " files indexed"); - - if (exclusionFilters.hasPattern()) { - LOG.info(progress.excludedByPatternsCount() + " files ignored because of inclusion/exclusion patterns"); - } - } - - private void waitForTasksToComplete() { - executorService.shutdown(); - for (Future<Void> task : tasks) { - try { - task.get(); - } catch (ExecutionException e) { - // Unwrap ExecutionException - throw e.getCause() instanceof RuntimeException ? (RuntimeException) e.getCause() : new IllegalStateException(e.getCause()); - } catch (InterruptedException e) { - throw new IllegalStateException(e); - } - } - } - - private void indexFiles(DefaultModuleFileSystem fileSystem, Progress progress, InputFileBuilder inputFileBuilder, List<File> sources, InputFile.Type type) { - try { - for (File dirOrFile : sources) { - if (dirOrFile.isDirectory()) { - indexDirectory(inputFileBuilder, fileSystem, progress, dirOrFile, type); - } else { - indexFile(inputFileBuilder, fileSystem, progress, dirOrFile.toPath(), type); - } - } - } catch (IOException e) { - throw new IllegalStateException("Failed to index files", e); - } - } - - private void indexDirectory(final InputFileBuilder inputFileBuilder, final DefaultModuleFileSystem fileSystem, final Progress status, - final File dirToIndex, final InputFile.Type type) throws IOException { - Files.walkFileTree(dirToIndex.toPath().normalize(), Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, - new IndexFileVisitor(inputFileBuilder, fileSystem, status, type)); - } - - private void indexFile(InputFileBuilder inputFileBuilder, DefaultModuleFileSystem fileSystem, Progress progress, Path sourceFile, InputFile.Type type) throws IOException { - // get case of real file without resolving link - Path realFile = sourceFile.toRealPath(LinkOption.NOFOLLOW_LINKS); - DefaultInputFile inputFile = inputFileBuilder.create(realFile.toFile()); - if (inputFile != null) { - // Set basedir on input file prior to adding it to the FS since exclusions filters may require the absolute path - inputFile.setModuleBaseDir(fileSystem.baseDirPath()); - if (exclusionFilters.accept(inputFile, type)) { - indexFile(inputFileBuilder, fileSystem, progress, inputFile, type); - } else { - progress.increaseExcludedByPatternsCount(); - } - } - } - - private void indexFile(final InputFileBuilder inputFileBuilder, final DefaultModuleFileSystem fs, - final Progress status, final DefaultInputFile inputFile, final InputFile.Type type) { - - tasks.add(executorService.submit(new Callable<Void>() { - @Override - public Void call() { - DefaultInputFile completedInputFile = inputFileBuilder.completeAndComputeMetadata(inputFile, type); - if (completedInputFile != null && accept(completedInputFile)) { - fs.add(completedInputFile); - status.markAsIndexed(completedInputFile); - File parentDir = completedInputFile.file().getParentFile(); - String relativePath = new PathResolver().relativePath(fs.baseDir(), parentDir); - if (relativePath != null) { - DefaultInputDir inputDir = new DefaultInputDir(fs.moduleKey(), relativePath); - fs.add(inputDir); - } - } - return null; - } - })); - - } - - private boolean accept(InputFile inputFile) { - // InputFileFilter extensions - for (InputFileFilter filter : filters) { - if (!filter.accept(inputFile)) { - return false; - } - } - return true; - } - - private class IndexFileVisitor implements FileVisitor<Path> { - private InputFileBuilder inputFileBuilder; - private DefaultModuleFileSystem fileSystem; - private Progress status; - private Type type; - - IndexFileVisitor(InputFileBuilder inputFileBuilder, DefaultModuleFileSystem fileSystem, Progress status, InputFile.Type type) { - this.inputFileBuilder = inputFileBuilder; - this.fileSystem = fileSystem; - this.status = status; - this.type = type; - } - - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { - Path fileName = dir.getFileName(); - - if (fileName != null && fileName.toString().length() > 1 && fileName.toString().charAt(0) == '.') { - return FileVisitResult.SKIP_SUBTREE; - } - if (Files.isHidden(dir)) { - return FileVisitResult.SKIP_SUBTREE; - } - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if (!Files.isHidden(file) && !DirectoryLock.LOCK_FILE_NAME.equals(file.getFileName().toString())) { - indexFile(inputFileBuilder, fileSystem, status, file, type); - } - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { - if (exc != null && exc instanceof FileSystemLoopException) { - LOG.warn("Not indexing due to symlink loop: {}", file.toFile()); - return FileVisitResult.CONTINUE; - } - - throw exc; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - return FileVisitResult.CONTINUE; - } - } - - private class Progress { - private final Set<Path> indexed = new HashSet<>(); - private int excludedByPatternsCount = 0; - - synchronized void markAsIndexed(InputFile inputFile) { - if (indexed.contains(inputFile.path())) { - throw MessageException.of("File " + inputFile + " can't be indexed twice. Please check that inclusion/exclusion patterns produce " - + "disjoint sets for main and test files"); - } - indexed.add(inputFile.path()); - progressReport.message(indexed.size() + " files indexed... (last one was " + inputFile.relativePath() + ")"); - } - - void increaseExcludedByPatternsCount() { - excludedByPatternsCount++; - } - - public int excludedByPatternsCount() { - return excludedByPatternsCount; - } - - int count() { - return indexed.size(); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileSystemLogger.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileSystemLogger.java deleted file mode 100644 index 0d717301283..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileSystemLogger.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import com.google.common.annotations.VisibleForTesting; -import java.io.File; -import java.nio.charset.Charset; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.scan.filesystem.PathResolver; - -@BatchSide -public class FileSystemLogger { - - private final DefaultModuleFileSystem fs; - - public FileSystemLogger(DefaultModuleFileSystem fs) { - this.fs = fs; - } - - public void log() { - doLog(LoggerFactory.getLogger(getClass())); - } - - @VisibleForTesting - void doLog(Logger logger) { - logDir(logger, "Base dir: ", fs.baseDir()); - logDir(logger, "Working dir: ", fs.workDir()); - logPaths(logger, "Source paths: ", fs.baseDir(), fs.sources()); - logPaths(logger, "Test paths: ", fs.baseDir(), fs.tests()); - logPaths(logger, "Binary dirs: ", fs.baseDir(), fs.binaryDirs()); - logEncoding(logger, fs.encoding()); - } - - private void logEncoding(Logger logger, Charset charset) { - if (!fs.isDefaultJvmEncoding()) { - logger.info("Source encoding: " + charset.displayName() + ", default locale: " + Locale.getDefault()); - } else { - logger.warn("Source encoding is platform dependent (" + charset.displayName() + "), default locale: " + Locale.getDefault()); - } - } - - private static void logPaths(Logger logger, String label, File baseDir, List<File> paths) { - if (!paths.isEmpty()) { - PathResolver resolver = new PathResolver(); - StringBuilder sb = new StringBuilder(label); - for (Iterator<File> it = paths.iterator(); it.hasNext();) { - File file = it.next(); - String relativePathToBaseDir = resolver.relativePath(baseDir, file); - if (relativePathToBaseDir == null) { - sb.append(file); - } else if (StringUtils.isBlank(relativePathToBaseDir)) { - sb.append("."); - } else { - sb.append(relativePathToBaseDir); - } - if (it.hasNext()) { - sb.append(", "); - } - } - logger.info(sb.toString()); - } - } - - private void logDir(Logger logger, String label, File dir) { - if (dir != null) { - logger.info(label + dir.getAbsolutePath()); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilder.java deleted file mode 100644 index 49cbd423cf9..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilder.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.fs.internal.FileMetadata; -import org.sonar.api.config.Settings; -import org.sonar.api.scan.filesystem.PathResolver; - -import javax.annotation.CheckForNull; - -import java.io.File; - -class InputFileBuilder { - - private static final Logger LOG = LoggerFactory.getLogger(InputFileBuilder.class); - - private final String moduleKey; - private final PathResolver pathResolver; - private final LanguageDetection langDetection; - private final StatusDetection statusDetection; - private final DefaultModuleFileSystem fs; - private final Settings settings; - private final FileMetadata fileMetadata; - - InputFileBuilder(String moduleKey, PathResolver pathResolver, LanguageDetection langDetection, - StatusDetection statusDetection, DefaultModuleFileSystem fs, Settings settings, FileMetadata fileMetadata) { - this.moduleKey = moduleKey; - this.pathResolver = pathResolver; - this.langDetection = langDetection; - this.statusDetection = statusDetection; - this.fs = fs; - this.settings = settings; - this.fileMetadata = fileMetadata; - } - - String moduleKey() { - return moduleKey; - } - - PathResolver pathResolver() { - return pathResolver; - } - - LanguageDetection langDetection() { - return langDetection; - } - - StatusDetection statusDetection() { - return statusDetection; - } - - FileSystem fs() { - return fs; - } - - @CheckForNull - DefaultInputFile create(File file) { - String relativePath = pathResolver.relativePath(fs.baseDir(), file); - if (relativePath == null) { - LOG.warn("File '{}' is ignored. It is not located in module basedir '{}'.", file.getAbsolutePath(), fs.baseDir()); - return null; - } - return new DefaultInputFile(moduleKey, relativePath); - } - - /** - * Optimization to not compute InputFile metadata if the file is excluded from analysis. - */ - @CheckForNull - DefaultInputFile completeAndComputeMetadata(DefaultInputFile inputFile, InputFile.Type type) { - inputFile.setType(type); - inputFile.setModuleBaseDir(fs.baseDir().toPath()); - inputFile.setCharset(fs.encoding()); - - String lang = langDetection.language(inputFile); - if (lang == null && !settings.getBoolean(CoreProperties.IMPORT_UNKNOWN_FILES_KEY)) { - return null; - } - inputFile.setLanguage(lang); - - inputFile.initMetadata(fileMetadata.readMetadata(inputFile.file(), fs.encoding())); - - inputFile.setStatus(statusDetection.status(inputFile.moduleKey(), inputFile.relativePath(), inputFile.hash())); - - return inputFile; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactory.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactory.java deleted file mode 100644 index 46dc2b2fbcd..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactory.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.fs.internal.FileMetadata; -import org.sonar.api.config.Settings; -import org.sonar.api.scan.filesystem.PathResolver; - -@BatchSide -public class InputFileBuilderFactory { - - private final String moduleKey; - private final PathResolver pathResolver; - private final LanguageDetectionFactory langDetectionFactory; - private final StatusDetectionFactory statusDetectionFactory; - private final Settings settings; - private final FileMetadata fileMetadata; - - public InputFileBuilderFactory(ProjectDefinition def, PathResolver pathResolver, LanguageDetectionFactory langDetectionFactory, - StatusDetectionFactory statusDetectionFactory, Settings settings, FileMetadata fileMetadata) { - this.fileMetadata = fileMetadata; - this.moduleKey = def.getKeyWithBranch(); - this.pathResolver = pathResolver; - this.langDetectionFactory = langDetectionFactory; - this.statusDetectionFactory = statusDetectionFactory; - this.settings = settings; - } - - InputFileBuilder create(DefaultModuleFileSystem fs) { - return new InputFileBuilder(moduleKey, pathResolver, langDetectionFactory.create(), statusDetectionFactory.create(), fs, settings, fileMetadata); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java deleted file mode 100644 index 3375f92c7ff..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import com.google.common.collect.Table; -import com.google.common.collect.TreeBasedTable; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.fs.InputDir; -import org.sonar.api.batch.fs.InputFile; - -import javax.annotation.CheckForNull; - -/** - * Cache of all files and dirs. This cache is shared amongst all project modules. Inclusion and - * exclusion patterns are already applied. - */ -@BatchSide -public class InputPathCache { - - private final Table<String, String, InputFile> inputFileCache = TreeBasedTable.create(); - private final Table<String, String, InputDir> inputDirCache = TreeBasedTable.create(); - - public Iterable<InputFile> allFiles() { - return inputFileCache.values(); - } - - public Iterable<InputDir> allDirs() { - return inputDirCache.values(); - } - - public Iterable<InputFile> filesByModule(String moduleKey) { - return inputFileCache.row(moduleKey).values(); - } - - public Iterable<InputDir> dirsByModule(String moduleKey) { - return inputDirCache.row(moduleKey).values(); - } - - public InputPathCache removeModule(String moduleKey) { - inputFileCache.row(moduleKey).clear(); - inputDirCache.row(moduleKey).clear(); - return this; - } - - public InputPathCache remove(String moduleKey, InputFile inputFile) { - inputFileCache.remove(moduleKey, inputFile.relativePath()); - return this; - } - - public InputPathCache remove(String moduleKey, InputDir inputDir) { - inputDirCache.remove(moduleKey, inputDir.relativePath()); - return this; - } - - public InputPathCache put(String moduleKey, InputFile inputFile) { - inputFileCache.put(moduleKey, inputFile.relativePath(), inputFile); - return this; - } - - public InputPathCache put(String moduleKey, InputDir inputDir) { - inputDirCache.put(moduleKey, inputDir.relativePath(), inputDir); - return this; - } - - @CheckForNull - public InputFile getFile(String moduleKey, String relativePath) { - return inputFileCache.get(moduleKey, relativePath); - } - - @CheckForNull - public InputDir getDir(String moduleKey, String relativePath) { - return inputDirCache.get(moduleKey, relativePath); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetection.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetection.java deleted file mode 100644 index db1300e2a97..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetection.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.sonar.batch.repository.language.Language; -import org.sonar.batch.repository.language.LanguagesRepository; - -import com.google.common.base.Joiner; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.PathPattern; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.MessageException; - -import javax.annotation.CheckForNull; - -import java.text.MessageFormat; -import java.util.List; -import java.util.Map; - -/** - * Detect language of a source file based on its suffix and configured patterns. - */ -class LanguageDetection { - - private static final Logger LOG = LoggerFactory.getLogger(LanguageDetection.class); - - /** - * Lower-case extension -> languages - */ - private final Map<String, PathPattern[]> patternsByLanguage = Maps.newLinkedHashMap(); - private final List<String> languagesToConsider = Lists.newArrayList(); - private final String forcedLanguage; - - LanguageDetection(Settings settings, LanguagesRepository languages) { - for (Language language : languages.all()) { - String[] filePatterns = settings.getStringArray(getFileLangPatternPropKey(language.key())); - PathPattern[] pathPatterns = PathPattern.create(filePatterns); - if (pathPatterns.length > 0) { - patternsByLanguage.put(language.key(), pathPatterns); - } else { - // If no custom language pattern is defined then fallback to suffixes declared by language - String[] patterns = language.fileSuffixes().toArray(new String[language.fileSuffixes().size()]); - for (int i = 0; i < patterns.length; i++) { - String suffix = patterns[i]; - String extension = sanitizeExtension(suffix); - patterns[i] = new StringBuilder().append("**/*.").append(extension).toString(); - } - PathPattern[] defaultLanguagePatterns = PathPattern.create(patterns); - patternsByLanguage.put(language.key(), defaultLanguagePatterns); - LOG.debug("Declared extensions of language " + language + " were converted to " + getDetails(language.key())); - } - } - - forcedLanguage = StringUtils.defaultIfBlank(settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY), null); - // First try with lang patterns - if (forcedLanguage != null) { - if (!patternsByLanguage.containsKey(forcedLanguage)) { - throw MessageException.of("No language is installed with key '" + forcedLanguage + "'. Please update property '" + CoreProperties.PROJECT_LANGUAGE_PROPERTY + "'"); - } - languagesToConsider.add(forcedLanguage); - } else { - languagesToConsider.addAll(patternsByLanguage.keySet()); - } - } - - Map<String, PathPattern[]> patternsByLanguage() { - return patternsByLanguage; - } - - @CheckForNull - String language(InputFile inputFile) { - String detectedLanguage = null; - for (String languageKey : languagesToConsider) { - if (isCandidateForLanguage(inputFile, languageKey)) { - if (detectedLanguage == null) { - detectedLanguage = languageKey; - } else { - // Language was already forced by another pattern - throw MessageException.of(MessageFormat.format("Language of file ''{0}'' can not be decided as the file matches patterns of both {1} and {2}", - inputFile.relativePath(), getDetails(detectedLanguage), getDetails(languageKey))); - } - } - } - if (detectedLanguage != null) { - LOG.debug(String.format("Language of file '%s' is detected to be '%s'", inputFile.relativePath(), detectedLanguage)); - return detectedLanguage; - } - - // Check if deprecated sonar.language is used and we are on a language without declared extensions. - // Languages without declared suffixes match everything. - if (forcedLanguage != null && patternsByLanguage.get(forcedLanguage).length == 0) { - return forcedLanguage; - } - return null; - } - - private boolean isCandidateForLanguage(InputFile inputFile, String languageKey) { - PathPattern[] patterns = patternsByLanguage.get(languageKey); - if (patterns != null) { - for (PathPattern pathPattern : patterns) { - if (pathPattern.match(inputFile, false)) { - return true; - } - } - } - return false; - } - - private String getFileLangPatternPropKey(String languageKey) { - return "sonar.lang.patterns." + languageKey; - } - - private String getDetails(String detectedLanguage) { - return getFileLangPatternPropKey(detectedLanguage) + " : " + Joiner.on(",").join(patternsByLanguage.get(detectedLanguage)); - } - - static String sanitizeExtension(String suffix) { - return StringUtils.lowerCase(StringUtils.removeStart(suffix, ".")); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactory.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactory.java deleted file mode 100644 index b25a0038c64..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactory.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.sonar.api.batch.BatchSide; -import org.sonar.api.config.Settings; -import org.sonar.batch.repository.language.LanguagesRepository; - -@BatchSide -public class LanguageDetectionFactory { - private final Settings settings; - private final LanguagesRepository languages; - - public LanguageDetectionFactory(Settings settings, LanguagesRepository languages) { - this.settings = settings; - this.languages = languages; - } - - public LanguageDetection create() { - return new LanguageDetection(settings, languages); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java deleted file mode 100644 index faab666579a..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import com.google.common.collect.Lists; -import org.apache.commons.io.FileUtils; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.scan.filesystem.PathResolver; -import org.sonar.api.utils.TempFolder; - -import javax.annotation.CheckForNull; - -import java.io.File; -import java.util.List; - -/** - * @since 3.5 - */ -@BatchSide -public class ModuleFileSystemInitializer { - - private File baseDir; - private File workingDir; - private File buildDir; - private List<File> sourceDirsOrFiles = Lists.newArrayList(); - private List<File> testDirsOrFiles = Lists.newArrayList(); - private List<File> binaryDirs = Lists.newArrayList(); - - public ModuleFileSystemInitializer(ProjectDefinition module, TempFolder tempUtils, PathResolver pathResolver) { - baseDir = module.getBaseDir(); - buildDir = module.getBuildDir(); - initWorkingDir(module, tempUtils); - initBinaryDirs(module, pathResolver); - initSources(module, pathResolver); - initTests(module, pathResolver); - } - - private void initWorkingDir(ProjectDefinition module, TempFolder tempUtils) { - workingDir = module.getWorkDir(); - if (workingDir == null) { - workingDir = tempUtils.newDir("work"); - } else { - try { - FileUtils.forceMkdir(workingDir); - } catch (Exception e) { - throw new IllegalStateException("Fail to create working dir: " + workingDir.getAbsolutePath(), e); - } - } - } - - private void initSources(ProjectDefinition module, PathResolver pathResolver) { - for (String sourcePath : module.sources()) { - File dirOrFile = pathResolver.relativeFile(module.getBaseDir(), sourcePath); - if (dirOrFile.exists()) { - sourceDirsOrFiles.add(dirOrFile); - } - } - } - - private void initTests(ProjectDefinition module, PathResolver pathResolver) { - for (String testPath : module.tests()) { - File dirOrFile = pathResolver.relativeFile(module.getBaseDir(), testPath); - if (dirOrFile.exists()) { - testDirsOrFiles.add(dirOrFile); - } - } - } - - private void initBinaryDirs(ProjectDefinition module, PathResolver pathResolver) { - for (String path : module.getBinaries()) { - File dir = pathResolver.relativeFile(module.getBaseDir(), path); - binaryDirs.add(dir); - } - } - - File baseDir() { - return baseDir; - } - - File workingDir() { - return workingDir; - } - - @CheckForNull - File buildDir() { - return buildDir; - } - - List<File> sources() { - return sourceDirsOrFiles; - } - - List<File> tests() { - return testDirsOrFiles; - } - - /** - * @deprecated since 4.5.1 use SonarQube Java specific API - */ - @Deprecated - List<File> binaryDirs() { - return binaryDirs; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleInputFileCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleInputFileCache.java deleted file mode 100644 index 5326b3fda4b..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleInputFileCache.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.fs.InputDir; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultFileSystem; - -@BatchSide -public class ModuleInputFileCache extends DefaultFileSystem.Cache { - - private final String moduleKey; - private final InputPathCache inputPathCache; - - public ModuleInputFileCache(ProjectDefinition projectDef, InputPathCache projectCache) { - this.moduleKey = projectDef.getKeyWithBranch(); - this.inputPathCache = projectCache; - } - - @Override - public Iterable<InputFile> inputFiles() { - return inputPathCache.filesByModule(moduleKey); - } - - @Override - public InputFile inputFile(String relativePath) { - return inputPathCache.getFile(moduleKey, relativePath); - } - - @Override - public InputDir inputDir(String relativePath) { - return inputPathCache.getDir(moduleKey, relativePath); - } - - @Override - protected void doAdd(InputFile inputFile) { - inputPathCache.put(moduleKey, inputFile); - } - - @Override - protected void doAdd(InputDir inputDir) { - inputPathCache.put(moduleKey, inputDir); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/StatusDetection.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/StatusDetection.java deleted file mode 100644 index bd63a81dfe2..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/StatusDetection.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.sonar.batch.repository.FileData; - -import org.sonar.batch.repository.ProjectRepositories; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.batch.fs.InputFile; - -class StatusDetection { - - private final ProjectRepositories projectSettings; - - StatusDetection(ProjectRepositories projectSettings) { - this.projectSettings = projectSettings; - } - - InputFile.Status status(String projectKey, String relativePath, String hash) { - FileData fileDataPerPath = projectSettings.fileData(projectKey, relativePath); - if (fileDataPerPath == null) { - return InputFile.Status.ADDED; - } - String previousHash = fileDataPerPath.hash(); - if (StringUtils.equals(hash, previousHash)) { - return InputFile.Status.SAME; - } - if (StringUtils.isEmpty(previousHash)) { - return InputFile.Status.ADDED; - } - return InputFile.Status.CHANGED; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/StatusDetectionFactory.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/StatusDetectionFactory.java deleted file mode 100644 index b1a75c228c0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/StatusDetectionFactory.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.sonar.batch.repository.ProjectRepositories; - -import org.sonar.api.batch.BatchSide; - -@BatchSide -public class StatusDetectionFactory { - - private final ProjectRepositories projectReferentials; - - public StatusDetectionFactory(ProjectRepositories projectReferentials) { - this.projectReferentials = projectReferentials; - } - - StatusDetection create() { - return new StatusDetection(projectReferentials); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/package-info.java deleted file mode 100644 index d5f662e81ea..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/package-info.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -/** - * This package is a part of bootstrap process, so we should take care about backward compatibility. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.scan.filesystem; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DefaultMetricFinder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DefaultMetricFinder.java deleted file mode 100644 index 295e74aa2e2..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DefaultMetricFinder.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.measure; - -import com.google.common.collect.Lists; -import org.sonar.api.batch.measure.Metric; -import org.sonar.api.batch.measure.MetricFinder; -import org.sonar.api.measures.Metric.ValueType; -import org.sonar.batch.protocol.input.GlobalRepositories; - -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -public class DefaultMetricFinder implements MetricFinder { - - private Map<String, Metric> metricsByKey = new LinkedHashMap<>(); - - public DefaultMetricFinder(GlobalRepositories globalReferentials) { - for (org.sonar.batch.protocol.input.Metric metric : globalReferentials.metrics()) { - metricsByKey.put(metric.key(), new org.sonar.api.measures.Metric.Builder(metric.key(), metric.key(), ValueType.valueOf(metric.valueType())).create()); - } - } - - @Override - public Metric findByKey(String key) { - return metricsByKey.get(key); - } - - @Override - public Collection<Metric> findAll(List<String> metricKeys) { - List<Metric> result = Lists.newLinkedList(); - for (String metricKey : metricKeys) { - Metric metric = findByKey(metricKey); - if (metric != null) { - result.add(metric); - } - } - return result; - } - - @Override - public Collection<Metric> findAll() { - return metricsByKey.values(); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DeprecatedMetricFinder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DeprecatedMetricFinder.java deleted file mode 100644 index f866972fd78..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DeprecatedMetricFinder.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.measure; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import org.sonar.api.measures.Metric; -import org.sonar.api.measures.Metric.ValueType; -import org.sonar.api.measures.MetricFinder; -import org.sonar.batch.protocol.input.GlobalRepositories; - -import java.util.Collection; -import java.util.List; -import java.util.Map; - -public final class DeprecatedMetricFinder implements MetricFinder { - - private Map<String, Metric> metricsByKey = Maps.newLinkedHashMap(); - private Map<Integer, Metric> metricsById = Maps.newLinkedHashMap(); - - public DeprecatedMetricFinder(GlobalRepositories globalReferentials) { - for (org.sonar.batch.protocol.input.Metric metric : globalReferentials.metrics()) { - Metric hibernateMetric = new org.sonar.api.measures.Metric.Builder(metric.key(), metric.name(), ValueType.valueOf(metric.valueType())) - .create() - .setDirection(metric.direction()) - .setQualitative(metric.isQualitative()) - .setUserManaged(metric.isUserManaged()) - .setDescription(metric.description()) - .setOptimizedBestValue(metric.isOptimizedBestValue()) - .setBestValue(metric.bestValue()) - .setWorstValue(metric.worstValue()) - .setId(metric.id()); - metricsByKey.put(metric.key(), hibernateMetric); - metricsById.put(metric.id(), new org.sonar.api.measures.Metric.Builder(metric.key(), metric.key(), ValueType.valueOf(metric.valueType())).create().setId(metric.id())); - } - } - - @Override - public Metric findById(int metricId) { - return metricsById.get(metricId); - } - - @Override - public Metric findByKey(String key) { - return metricsByKey.get(key); - } - - @Override - public Collection<Metric> findAll(List<String> metricKeys) { - List<Metric> result = Lists.newLinkedList(); - for (String metricKey : metricKeys) { - Metric metric = findByKey(metricKey); - if (metric != null) { - result.add(metric); - } - } - return result; - } - - @Override - public Collection<Metric> findAll() { - return metricsByKey.values(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureCache.java deleted file mode 100644 index 963f7e14b91..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureCache.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.measure; - -import com.google.common.base.Preconditions; -import javax.annotation.CheckForNull; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.measure.MetricFinder; -import org.sonar.api.measures.Measure; -import org.sonar.api.resources.Resource; -import org.sonar.batch.index.Cache; -import org.sonar.batch.index.Cache.Entry; -import org.sonar.batch.index.Caches; - -/** - * Cache of all measures. This cache is shared amongst all project modules. - */ -@BatchSide -public class MeasureCache { - - private final Cache<Measure> cache; - - public MeasureCache(Caches caches, MetricFinder metricFinder) { - caches.registerValueCoder(Measure.class, new MeasureValueCoder(metricFinder)); - cache = caches.createCache("measures"); - } - - public Iterable<Entry<Measure>> entries() { - return cache.entries(); - } - - public Iterable<Measure> all() { - return cache.values(); - } - - public Iterable<Measure> byResource(Resource r) { - return byComponentKey(r.getEffectiveKey()); - } - - public Iterable<Measure> byComponentKey(String effectiveKey) { - return cache.values(effectiveKey); - } - - @CheckForNull - public Measure byMetric(Resource r, String metricKey) { - return byMetric(r.getEffectiveKey(), metricKey); - } - - @CheckForNull - public Measure byMetric(String resourceKey, String metricKey) { - return cache.get(resourceKey, metricKey); - } - - public MeasureCache put(Resource resource, Measure measure) { - Preconditions.checkNotNull(resource.getEffectiveKey()); - Preconditions.checkNotNull(measure.getMetricKey()); - cache.put(resource.getEffectiveKey(), measure.getMetricKey(), measure); - return this; - } - - public boolean contains(Resource resource, Measure measure) { - Preconditions.checkNotNull(resource.getEffectiveKey()); - Preconditions.checkNotNull(measure.getMetricKey()); - return cache.containsKey(resource.getEffectiveKey(), measure.getMetricKey()); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureValueCoder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureValueCoder.java deleted file mode 100644 index 019410fd06b..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureValueCoder.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.measure; - -import com.persistit.Value; -import com.persistit.encoding.CoderContext; -import com.persistit.encoding.ValueCoder; -import javax.annotation.Nullable; -import org.sonar.api.batch.measure.MetricFinder; -import org.sonar.api.measures.Measure; -import org.sonar.api.measures.Metric; -import org.sonar.api.measures.PersistenceMode; - -class MeasureValueCoder implements ValueCoder { - - private final MetricFinder metricFinder; - - public MeasureValueCoder(MetricFinder metricFinder) { - this.metricFinder = metricFinder; - } - - @Override - public void put(Value value, Object object, CoderContext context) { - Measure<?> m = (Measure) object; - value.putUTF(m.getMetricKey()); - value.put(m.getValue()); - putUTFOrNull(value, m.getData()); - putUTFOrNull(value, m.getDescription()); - value.putString(m.getAlertStatus() != null ? m.getAlertStatus().name() : null); - putUTFOrNull(value, m.getAlertText()); - value.putDate(m.getDate()); - value.put(m.getVariation1()); - value.put(m.getVariation2()); - value.put(m.getVariation3()); - value.put(m.getVariation4()); - value.put(m.getVariation5()); - putUTFOrNull(value, m.getUrl()); - Integer personId = m.getPersonId(); - value.put(personId != null ? personId.intValue() : null); - PersistenceMode persistenceMode = m.getPersistenceMode(); - value.putString(persistenceMode != null ? persistenceMode.name() : null); - } - - private static void putUTFOrNull(Value value, @Nullable String utfOrNull) { - if (utfOrNull != null) { - value.putUTF(utfOrNull); - } else { - value.putNull(); - } - } - - @Override - public Object get(Value value, Class clazz, CoderContext context) { - Measure<?> m = new Measure(); - String metricKey = value.getString(); - org.sonar.api.batch.measure.Metric metric = metricFinder.findByKey(metricKey); - if (metric == null) { - throw new IllegalStateException("Unknow metric with key " + metricKey); - } - m.setMetric((org.sonar.api.measures.Metric) metric); - m.setRawValue(value.isNull(true) ? null : value.getDouble()); - m.setData(value.getString()); - m.setDescription(value.getString()); - m.setAlertStatus(value.isNull(true) ? null : Metric.Level.valueOf(value.getString())); - m.setAlertText(value.getString()); - m.setDate(value.getDate()); - m.setVariation1(value.isNull(true) ? null : value.getDouble()); - m.setVariation2(value.isNull(true) ? null : value.getDouble()); - m.setVariation3(value.isNull(true) ? null : value.getDouble()); - m.setVariation4(value.isNull(true) ? null : value.getDouble()); - m.setVariation5(value.isNull(true) ? null : value.getDouble()); - m.setUrl(value.getString()); - m.setPersonId(value.isNull(true) ? null : value.getInt()); - m.setPersistenceMode(value.isNull(true) ? null : PersistenceMode.valueOf(value.getString())); - return m; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/package-info.java deleted file mode 100644 index f35eb86c95c..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/package-info.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -/** - * This package is a part of bootstrap process, so we should take care about backward compatibility. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.scan.measure; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/scan/package-info.java deleted file mode 100644 index af539896111..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.scan; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/ConsoleReport.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/ConsoleReport.java deleted file mode 100644 index 7e37b789c71..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/ConsoleReport.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import com.google.common.annotations.VisibleForTesting; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.Properties; -import org.sonar.api.Property; -import org.sonar.api.PropertyType; -import org.sonar.api.config.Settings; -import org.sonar.api.rule.Severity; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.batch.issue.IssueCache; -import org.sonar.batch.issue.tracking.TrackedIssue; -import org.sonar.batch.scan.filesystem.InputPathCache; - -@Properties({ - @Property(key = ConsoleReport.CONSOLE_REPORT_ENABLED_KEY, defaultValue = "false", name = "Enable console report", - description = "Set this to true to generate a report in console output", type = PropertyType.BOOLEAN)}) -public class ConsoleReport implements Reporter { - - @VisibleForTesting - public static final String HEADER = "------------- Issues Report -------------"; - - private static final Logger LOG = Loggers.get(ConsoleReport.class); - - public static final String CONSOLE_REPORT_ENABLED_KEY = "sonar.issuesReport.console.enable"; - private static final int LEFT_PAD = 10; - - private Settings settings; - private IssueCache issueCache; - private InputPathCache inputPathCache; - - @VisibleForTesting - public ConsoleReport(Settings settings, IssueCache issueCache, InputPathCache inputPathCache) { - this.settings = settings; - this.issueCache = issueCache; - this.inputPathCache = inputPathCache; - } - - private static class Report { - boolean noFile = false; - int totalNewIssues = 0; - int newBlockerIssues = 0; - int newCriticalIssues = 0; - int newMajorIssues = 0; - int newMinorIssues = 0; - int newInfoIssues = 0; - - public void process(TrackedIssue issue) { - if (issue.isNew()) { - totalNewIssues++; - switch (issue.severity()) { - case Severity.BLOCKER: - newBlockerIssues++; - break; - case Severity.CRITICAL: - newCriticalIssues++; - break; - case Severity.MAJOR: - newMajorIssues++; - break; - case Severity.MINOR: - newMinorIssues++; - break; - case Severity.INFO: - newInfoIssues++; - break; - default: - throw new IllegalStateException("Unknow severity: " + issue.severity()); - } - } - } - - public void setNoFile(boolean value) { - this.noFile = value; - } - } - - @Override - public void execute() { - if (settings.getBoolean(CONSOLE_REPORT_ENABLED_KEY)) { - Report r = new Report(); - r.setNoFile(!inputPathCache.allFiles().iterator().hasNext()); - for (TrackedIssue issue : issueCache.all()) { - r.process(issue); - } - printReport(r); - } - } - - public void printReport(Report r) { - StringBuilder sb = new StringBuilder(); - - sb.append("\n\n" + HEADER + "\n\n"); - if (r.noFile) { - sb.append(" No file analyzed\n"); - } else { - printNewIssues(r, sb); - } - sb.append("\n-------------------------------------------\n\n"); - - LOG.info(sb.toString()); - } - - private static void printNewIssues(Report r, StringBuilder sb) { - int newIssues = r.totalNewIssues; - if (newIssues > 0) { - sb.append(StringUtils.leftPad("+" + newIssues, LEFT_PAD)).append(" issue" + (newIssues > 1 ? "s" : "")).append("\n\n"); - printNewIssues(sb, r.newBlockerIssues, Severity.BLOCKER, "blocker"); - printNewIssues(sb, r.newCriticalIssues, Severity.CRITICAL, "critical"); - printNewIssues(sb, r.newMajorIssues, Severity.MAJOR, "major"); - printNewIssues(sb, r.newMinorIssues, Severity.MINOR, "minor"); - printNewIssues(sb, r.newInfoIssues, Severity.INFO, "info"); - } else { - sb.append(" No new issue").append("\n"); - } - } - - private static void printNewIssues(StringBuilder sb, int issueCount, String severity, String severityLabel) { - if (issueCount > 0) { - sb.append(StringUtils.leftPad("+" + issueCount, LEFT_PAD)).append(" ").append(severityLabel).append("\n"); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/HtmlReport.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/HtmlReport.java deleted file mode 100644 index 8111abd467e..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/HtmlReport.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import com.google.common.collect.Maps; -import freemarker.template.Template; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.net.URISyntaxException; -import java.util.Map; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.Properties; -import org.sonar.api.Property; -import org.sonar.api.PropertyType; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.config.Settings; - -@Properties({ - @Property(key = HtmlReport.HTML_REPORT_ENABLED_KEY, defaultValue = "false", name = "Enable HTML report", description = "Set this to true to generate an HTML report", - type = PropertyType.BOOLEAN), - @Property(key = HtmlReport.HTML_REPORT_LOCATION_KEY, defaultValue = HtmlReport.HTML_REPORT_LOCATION_DEFAULT, name = "HTML Report location", - description = "Location of the generated report. Can be absolute or relative to working directory", project = false, global = false, type = PropertyType.STRING), - @Property(key = HtmlReport.HTML_REPORT_NAME_KEY, defaultValue = HtmlReport.HTML_REPORT_NAME_DEFAULT, name = "HTML Report name", - description = "Name of the generated report. Will be suffixed by .html or -light.html", project = false, global = false, type = PropertyType.STRING), - @Property(key = HtmlReport.HTML_REPORT_LIGHTMODE_ONLY, defaultValue = "false", name = "Html report in light mode only", - description = "Set this to true to only generate the new issues report (light report)", project = true, type = PropertyType.BOOLEAN)}) -public class HtmlReport implements Reporter { - private static final Logger LOG = LoggerFactory.getLogger(HtmlReport.class); - - public static final String HTML_REPORT_ENABLED_KEY = "sonar.issuesReport.html.enable"; - public static final String HTML_REPORT_LOCATION_KEY = "sonar.issuesReport.html.location"; - public static final String HTML_REPORT_LOCATION_DEFAULT = "issues-report"; - public static final String HTML_REPORT_NAME_KEY = "sonar.issuesReport.html.name"; - public static final String HTML_REPORT_NAME_DEFAULT = "issues-report"; - public static final String HTML_REPORT_LIGHTMODE_ONLY = "sonar.issuesReport.lightModeOnly"; - - private final Settings settings; - private final FileSystem fs; - private final IssuesReportBuilder builder; - private final SourceProvider sourceProvider; - private final RuleNameProvider ruleNameProvider; - - public HtmlReport(Settings settings, FileSystem fs, IssuesReportBuilder builder, SourceProvider sourceProvider, RuleNameProvider ruleNameProvider) { - this.settings = settings; - this.fs = fs; - this.builder = builder; - this.sourceProvider = sourceProvider; - this.ruleNameProvider = ruleNameProvider; - } - - @Override - public void execute() { - if (settings.getBoolean(HTML_REPORT_ENABLED_KEY)) { - IssuesReport report = builder.buildReport(); - print(report); - } - } - - public void print(IssuesReport report) { - File reportFileDir = getReportFileDir(); - String reportName = settings.getString(HTML_REPORT_NAME_KEY); - if (!isLightModeOnly()) { - File reportFile = new File(reportFileDir, reportName + ".html"); - LOG.debug("Generating HTML Report to: " + reportFile.getAbsolutePath()); - writeToFile(report, reportFile, true); - LOG.info("HTML Issues Report generated: " + reportFile.getAbsolutePath()); - } - File lightReportFile = new File(reportFileDir, reportName + "-light.html"); - LOG.debug("Generating Light HTML Report to: " + lightReportFile.getAbsolutePath()); - writeToFile(report, lightReportFile, false); - LOG.info("Light HTML Issues Report generated: " + lightReportFile.getAbsolutePath()); - try { - copyDependencies(reportFileDir); - } catch (Exception e) { - throw new IllegalStateException("Fail to copy HTML report resources to: " + reportFileDir, e); - } - } - - private File getReportFileDir() { - String reportFileDirStr = settings.getString(HTML_REPORT_LOCATION_KEY); - File reportFileDir = new File(reportFileDirStr); - if (!reportFileDir.isAbsolute()) { - reportFileDir = new File(fs.workDir(), reportFileDirStr); - } - if (StringUtils.endsWith(reportFileDirStr, ".html")) { - LOG.warn(HTML_REPORT_LOCATION_KEY + " should indicate a directory. Using parent folder."); - reportFileDir = reportFileDir.getParentFile(); - } - try { - FileUtils.forceMkdir(reportFileDir); - } catch (IOException e) { - throw new IllegalStateException("Fail to create the directory " + reportFileDirStr, e); - } - return reportFileDir; - } - - public void writeToFile(IssuesReport report, File toFile, boolean complete) { - try { - freemarker.log.Logger.selectLoggerLibrary(freemarker.log.Logger.LIBRARY_NONE); - freemarker.template.Configuration cfg = new freemarker.template.Configuration(); - cfg.setClassForTemplateLoading(HtmlReport.class, ""); - - Map<String, Object> root = Maps.newHashMap(); - root.put("report", report); - root.put("ruleNameProvider", ruleNameProvider); - root.put("sourceProvider", sourceProvider); - root.put("complete", complete); - - Template template = cfg.getTemplate("issuesreport.ftl"); - - try (FileOutputStream fos = new FileOutputStream(toFile); Writer writer = new OutputStreamWriter(fos, fs.encoding())) { - template.process(root, writer); - writer.flush(); - } - } catch (Exception e) { - throw new IllegalStateException("Fail to generate HTML Issues Report to: " + toFile, e); - - } - } - - void copyDependencies(File toDir) throws URISyntaxException, IOException { - File target = new File(toDir, "issuesreport_files"); - FileUtils.forceMkdir(target); - - // I don't know how to extract a directory from classpath, that's why an exhaustive list of files - // is provided here : - copyDependency(target, "sonar.eot"); - copyDependency(target, "sonar.svg"); - copyDependency(target, "sonar.ttf"); - copyDependency(target, "sonar.woff"); - copyDependency(target, "favicon.ico"); - copyDependency(target, "PRJ.png"); - copyDependency(target, "DIR.png"); - copyDependency(target, "FIL.png"); - copyDependency(target, "jquery.min.js"); - copyDependency(target, "sep12.png"); - copyDependency(target, "sonar.css"); - copyDependency(target, "sonarqube-24x100.png"); - } - - private void copyDependency(File target, String filename) { - try (InputStream input = getClass().getResourceAsStream("/org/sonar/batch/scan/report/issuesreport_files/" + filename); - OutputStream output = new FileOutputStream(new File(target, filename))) { - IOUtils.copy(input, output); - } catch (IOException e) { - throw new IllegalStateException("Fail to copy file " + filename + " to " + target, e); - } - } - - public boolean isLightModeOnly() { - return settings.getBoolean(HTML_REPORT_LIGHTMODE_ONLY); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/IssueVariation.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/IssueVariation.java deleted file mode 100644 index 23ca58e7d8c..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/IssueVariation.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import org.apache.commons.lang.builder.ToStringBuilder; - -public class IssueVariation { - - private int countInCurrentAnalysis; - private int newIssuesCount; - private int resolvedIssuesCount; - - public int getCountInCurrentAnalysis() { - return countInCurrentAnalysis; - } - - public void incrementCountInCurrentAnalysis() { - this.countInCurrentAnalysis++; - } - - public int getNewIssuesCount() { - return newIssuesCount; - } - - public void incrementNewIssuesCount() { - this.newIssuesCount++; - } - - public int getResolvedIssuesCount() { - return resolvedIssuesCount; - } - - public void incrementResolvedIssuesCount() { - this.resolvedIssuesCount++; - } - - @Override - public String toString() { - return new ToStringBuilder(this). - append("countInCurrentAnalysis", countInCurrentAnalysis). - append("newIssuesCount", newIssuesCount). - append("resolvedIssuesCount", resolvedIssuesCount). - toString(); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/IssuesReport.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/IssuesReport.java deleted file mode 100644 index cede7a81d1c..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/IssuesReport.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import org.sonar.batch.issue.tracking.TrackedIssue; - -import com.google.common.collect.Maps; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; - -import org.sonar.api.batch.rule.Rule; -import org.sonar.api.rules.RulePriority; -import org.sonar.batch.index.BatchComponent; - -public class IssuesReport { - - public static final int TOO_MANY_ISSUES_THRESHOLD = 1000; - private String title; - private Date date; - private boolean noFile; - private final ReportSummary summary = new ReportSummary(); - private final Map<BatchComponent, ResourceReport> resourceReportsByResource = Maps.newLinkedHashMap(); - - public ReportSummary getSummary() { - return summary; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public Date getDate() { - return date; - } - - public void setDate(Date date) { - this.date = date; - } - - public boolean isNoFile() { - return noFile; - } - - public void setNoFile(boolean noFile) { - this.noFile = noFile; - } - - public Map<BatchComponent, ResourceReport> getResourceReportsByResource() { - return resourceReportsByResource; - } - - public List<ResourceReport> getResourceReports() { - return new ArrayList<>(resourceReportsByResource.values()); - } - - public List<BatchComponent> getResourcesWithReport() { - return new ArrayList<>(resourceReportsByResource.keySet()); - } - - public void addIssueOnResource(BatchComponent resource, TrackedIssue issue, Rule rule, RulePriority severity) { - addResource(resource); - getSummary().addIssue(issue, rule, severity); - resourceReportsByResource.get(resource).addIssue(issue, rule, RulePriority.valueOf(issue.severity())); - } - - public void addResolvedIssueOnResource(BatchComponent resource, TrackedIssue issue, Rule rule, RulePriority severity) { - addResource(resource); - getSummary().addResolvedIssue(issue, rule, severity); - resourceReportsByResource.get(resource).addResolvedIssue(rule, RulePriority.valueOf(issue.severity())); - } - - private void addResource(BatchComponent resource) { - if (!resourceReportsByResource.containsKey(resource)) { - resourceReportsByResource.put(resource, new ResourceReport(resource)); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/IssuesReportBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/IssuesReportBuilder.java deleted file mode 100644 index 079dabc2ceb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/IssuesReportBuilder.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import org.sonar.batch.issue.tracking.TrackedIssue; - -import javax.annotation.CheckForNull; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.rule.Rule; -import org.sonar.api.batch.rule.Rules; -import org.sonar.api.resources.Project; -import org.sonar.api.rules.RulePriority; -import org.sonar.batch.DefaultProjectTree; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.issue.IssueCache; -import org.sonar.batch.scan.filesystem.InputPathCache; - -@BatchSide -public class IssuesReportBuilder { - - private static final Logger LOG = LoggerFactory.getLogger(IssuesReportBuilder.class); - - private final IssueCache issueCache; - private final Rules rules; - private final BatchComponentCache resourceCache; - private final DefaultProjectTree projectTree; - private final InputPathCache inputPathCache; - - public IssuesReportBuilder(IssueCache issueCache, Rules rules, BatchComponentCache resourceCache, DefaultProjectTree projectTree, InputPathCache inputPathCache) { - this.issueCache = issueCache; - this.rules = rules; - this.resourceCache = resourceCache; - this.projectTree = projectTree; - this.inputPathCache = inputPathCache; - } - - public IssuesReport buildReport() { - Project project = projectTree.getRootProject(); - IssuesReport issuesReport = new IssuesReport(); - issuesReport.setNoFile(!inputPathCache.allFiles().iterator().hasNext()); - issuesReport.setTitle(project.getName()); - issuesReport.setDate(project.getAnalysisDate()); - - processIssues(issuesReport, issueCache.all()); - - return issuesReport; - } - - private void processIssues(IssuesReport issuesReport, Iterable<TrackedIssue> issues) { - for (TrackedIssue issue : issues) { - Rule rule = findRule(issue); - RulePriority severity = RulePriority.valueOf(issue.severity()); - BatchComponent resource = resourceCache.get(issue.componentKey()); - if (!validate(issue, rule, resource)) { - continue; - } - if (issue.resolution() != null) { - issuesReport.addResolvedIssueOnResource(resource, issue, rule, severity); - } else { - issuesReport.addIssueOnResource(resource, issue, rule, severity); - } - } - } - - private static boolean validate(TrackedIssue issue, Rule rule, BatchComponent resource) { - if (rule == null) { - LOG.warn("Unknow rule for issue {}", issue); - return false; - } - if (resource == null) { - LOG.debug("Unknow resource with key {}", issue.componentKey()); - return false; - } - return true; - } - - @CheckForNull - private Rule findRule(TrackedIssue issue) { - return rules.find(issue.getRuleKey()); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/IssuesReports.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/IssuesReports.java deleted file mode 100644 index ac4a5abb375..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/IssuesReports.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import org.sonar.api.batch.BatchSide; - -@BatchSide -public class IssuesReports { - - private final Reporter[] reporters; - - public IssuesReports(Reporter... reporters) { - this.reporters = reporters; - } - - public void execute() { - for (Reporter reporter : reporters) { - reporter.execute(); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java deleted file mode 100644 index f0c2e2b6da7..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/JSONReport.java +++ /dev/null @@ -1,247 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import com.google.common.annotations.VisibleForTesting; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.nio.charset.StandardCharsets; -import java.util.Collection; -import java.util.LinkedHashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.Properties; -import org.sonar.api.Property; -import org.sonar.api.PropertyType; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputDir; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultInputDir; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.rule.Rule; -import org.sonar.api.batch.rule.Rules; -import org.sonar.api.config.Settings; -import org.sonar.api.platform.Server; -import org.sonar.api.resources.Project; -import org.sonar.api.rule.RuleKey; -import org.sonar.api.utils.text.JsonWriter; -import org.sonar.batch.issue.IssueCache; -import org.sonar.batch.issue.tracking.TrackedIssue; -import org.sonar.batch.protocol.input.BatchInput; -import org.sonar.batch.protocol.input.BatchInput.User; -import org.sonar.batch.repository.user.UserRepositoryLoader; -import org.sonar.batch.scan.filesystem.InputPathCache; - -@Properties({ - @Property( - key = JSONReport.SONAR_REPORT_EXPORT_PATH, - name = "Report Results Export File", - type = PropertyType.STRING, - global = false, project = false)}) -public class JSONReport implements Reporter { - - static final String SONAR_REPORT_EXPORT_PATH = "sonar.report.export.path"; - private static final Logger LOG = LoggerFactory.getLogger(JSONReport.class); - private final Settings settings; - private final FileSystem fileSystem; - private final Server server; - private final Rules rules; - private final IssueCache issueCache; - private final InputPathCache fileCache; - private final Project rootModule; - private final UserRepositoryLoader userRepository; - - public JSONReport(Settings settings, FileSystem fileSystem, Server server, Rules rules, IssueCache issueCache, - Project rootModule, InputPathCache fileCache, UserRepositoryLoader userRepository) { - this.settings = settings; - this.fileSystem = fileSystem; - this.server = server; - this.rules = rules; - this.issueCache = issueCache; - this.rootModule = rootModule; - this.fileCache = fileCache; - this.userRepository = userRepository; - } - - @Override - public void execute() { - String exportPath = settings.getString(SONAR_REPORT_EXPORT_PATH); - if (exportPath != null) { - exportResults(exportPath); - } - } - - private void exportResults(String exportPath) { - File exportFile = new File(fileSystem.workDir(), exportPath); - - LOG.info("Export issues to {}", exportFile.getAbsolutePath()); - try (Writer output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile), StandardCharsets.UTF_8))) { - writeJson(output); - - } catch (IOException e) { - throw new IllegalStateException("Unable to write report results in file " + exportFile.getAbsolutePath(), e); - } - } - - @VisibleForTesting - void writeJson(Writer writer) { - try { - JsonWriter json = JsonWriter.of(writer); - json.beginObject(); - json.prop("version", server.getVersion()); - - Set<RuleKey> ruleKeys = new LinkedHashSet<>(); - Set<String> userLogins = new LinkedHashSet<>(); - writeJsonIssues(json, ruleKeys, userLogins); - writeJsonComponents(json); - writeJsonRules(json, ruleKeys); - writeUsers(json, userLogins); - json.endObject().close(); - - } catch (IOException e) { - throw new IllegalStateException("Unable to write JSON report", e); - } - } - - private void writeJsonIssues(JsonWriter json, Set<RuleKey> ruleKeys, Set<String> logins) throws IOException { - json.name("issues").beginArray(); - for (TrackedIssue issue : getIssues()) { - if (issue.resolution() == null) { - json - .beginObject() - .prop("key", issue.key()) - .prop("component", issue.componentKey()) - .prop("line", issue.startLine()) - .prop("startLine", issue.startLine()) - .prop("startOffset", issue.startLineOffset()) - .prop("endLine", issue.endLine()) - .prop("endOffset", issue.endLineOffset()) - .prop("message", issue.getMessage()) - .prop("severity", issue.severity()) - .prop("rule", issue.getRuleKey().toString()) - .prop("status", issue.status()) - .prop("resolution", issue.resolution()) - .prop("isNew", issue.isNew()) - .prop("assignee", issue.assignee()) - .prop("effortToFix", issue.gap()) - .propDateTime("creationDate", issue.creationDate()); - if (!StringUtils.isEmpty(issue.reporter())) { - logins.add(issue.reporter()); - } - if (!StringUtils.isEmpty(issue.assignee())) { - logins.add(issue.assignee()); - } - json.endObject(); - ruleKeys.add(issue.getRuleKey()); - } - } - json.endArray(); - } - - private void writeJsonComponents(JsonWriter json) throws IOException { - json.name("components").beginArray(); - // Dump modules - writeJsonModuleComponents(json, rootModule); - for (InputFile inputFile : fileCache.allFiles()) { - String key = ((DefaultInputFile) inputFile).key(); - json - .beginObject() - .prop("key", key) - .prop("path", inputFile.relativePath()) - .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) - .prop("status", inputFile.status().name()) - .endObject(); - } - for (InputDir inputDir : fileCache.allDirs()) { - String key = ((DefaultInputDir) inputDir).key(); - json - .beginObject() - .prop("key", key) - .prop("path", inputDir.relativePath()) - .prop("moduleKey", StringUtils.substringBeforeLast(key, ":")) - .endObject(); - - } - json.endArray(); - } - - private static void writeJsonModuleComponents(JsonWriter json, Project module) { - json - .beginObject() - .prop("key", module.getEffectiveKey()) - .prop("path", module.getPath()) - .endObject(); - for (Project subModule : module.getModules()) { - writeJsonModuleComponents(json, subModule); - } - } - - private void writeJsonRules(JsonWriter json, Set<RuleKey> ruleKeys) throws IOException { - json.name("rules").beginArray(); - for (RuleKey ruleKey : ruleKeys) { - json - .beginObject() - .prop("key", ruleKey.toString()) - .prop("rule", ruleKey.rule()) - .prop("repository", ruleKey.repository()) - .prop("name", getRuleName(ruleKey)) - .endObject(); - } - json.endArray(); - } - - private void writeUsers(JsonWriter json, Collection<String> userLogins) throws IOException { - List<BatchInput.User> users = new LinkedList<>(); - for (String userLogin : userLogins) { - User user = userRepository.load(userLogin); - if (user != null) { - users.add(user); - } - } - - json.name("users").beginArray(); - for (BatchInput.User user : users) { - json - .beginObject() - .prop("login", user.getLogin()) - .prop("name", user.getName()) - .endObject(); - } - json.endArray(); - } - - private String getRuleName(RuleKey ruleKey) { - Rule rule = rules.find(ruleKey); - return rule != null ? rule.name() : null; - } - - @VisibleForTesting - Iterable<TrackedIssue> getIssues() { - return issueCache.all(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/ReportRuleKey.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/ReportRuleKey.java deleted file mode 100644 index 1061d3c3238..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/ReportRuleKey.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import org.sonar.api.batch.rule.Rule; - -import org.apache.commons.lang.ObjectUtils; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.sonar.api.rules.RulePriority; - -/** - * A same rule can be present with different severity if severity was manually changed so we need this special key that - * include severity. - * - */ -public class ReportRuleKey implements Comparable<ReportRuleKey> { - private final Rule rule; - private final RulePriority severity; - - public ReportRuleKey(Rule rule, RulePriority severity) { - this.rule = rule; - this.severity = severity; - } - - public Rule getRule() { - return rule; - } - - public RulePriority getSeverity() { - return severity; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ReportRuleKey that = (ReportRuleKey) o; - return ObjectUtils.equals(rule, that.rule) && ObjectUtils.equals(severity, that.severity); - } - - @Override - public int hashCode() { - int result = rule.hashCode(); - result = 31 * result + severity.hashCode(); - return result; - } - - @Override - public int compareTo(ReportRuleKey o) { - if (severity == o.getSeverity()) { - return getRule().key().toString().compareTo(o.getRule().key().toString()); - } - return o.getSeverity().compareTo(severity); - } - - @Override - public String toString() { - return new ToStringBuilder(this). - append("rule", rule). - append("severity", severity). - toString(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/ReportSummary.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/ReportSummary.java deleted file mode 100644 index 435aee37450..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/ReportSummary.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import com.google.common.collect.Maps; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import org.sonar.api.batch.rule.Rule; -import org.sonar.api.rules.RulePriority; -import org.sonar.batch.issue.tracking.TrackedIssue; - -public class ReportSummary { - - private final IssueVariation total = new IssueVariation(); - - private final Map<ReportRuleKey, RuleReport> ruleReportByRuleKey = Maps.newLinkedHashMap(); - private final Map<String, IssueVariation> totalByRuleKey = Maps.newLinkedHashMap(); - private final Map<String, IssueVariation> totalBySeverity = Maps.newLinkedHashMap(); - - public IssueVariation getTotal() { - return total; - } - - public void addIssue(TrackedIssue issue, Rule rule, RulePriority severity) { - ReportRuleKey reportRuleKey = new ReportRuleKey(rule, severity); - initMaps(reportRuleKey); - ruleReportByRuleKey.get(reportRuleKey).getTotal().incrementCountInCurrentAnalysis(); - total.incrementCountInCurrentAnalysis(); - totalByRuleKey.get(rule.key().toString()).incrementCountInCurrentAnalysis(); - totalBySeverity.get(severity.toString()).incrementCountInCurrentAnalysis(); - if (issue.isNew()) { - total.incrementNewIssuesCount(); - ruleReportByRuleKey.get(reportRuleKey).getTotal().incrementNewIssuesCount(); - totalByRuleKey.get(rule.key().toString()).incrementNewIssuesCount(); - totalBySeverity.get(severity.toString()).incrementNewIssuesCount(); - } - } - - public Map<String, IssueVariation> getTotalBySeverity() { - return totalBySeverity; - } - - public Map<String, IssueVariation> getTotalByRuleKey() { - return totalByRuleKey; - } - - public void addResolvedIssue(TrackedIssue issue, Rule rule, RulePriority severity) { - ReportRuleKey reportRuleKey = new ReportRuleKey(rule, severity); - initMaps(reportRuleKey); - total.incrementResolvedIssuesCount(); - ruleReportByRuleKey.get(reportRuleKey).getTotal().incrementResolvedIssuesCount(); - totalByRuleKey.get(rule.key().toString()).incrementResolvedIssuesCount(); - totalBySeverity.get(severity.toString()).incrementResolvedIssuesCount(); - } - - private void initMaps(ReportRuleKey reportRuleKey) { - if (!ruleReportByRuleKey.containsKey(reportRuleKey)) { - ruleReportByRuleKey.put(reportRuleKey, new RuleReport(reportRuleKey)); - } - if (!totalByRuleKey.containsKey(reportRuleKey.getRule().key().toString())) { - totalByRuleKey.put(reportRuleKey.getRule().key().toString(), new IssueVariation()); - } - if (!totalBySeverity.containsKey(reportRuleKey.getSeverity().toString())) { - totalBySeverity.put(reportRuleKey.getSeverity().toString(), new IssueVariation()); - } - } - - public List<RuleReport> getRuleReports() { - List<RuleReport> result = new ArrayList<>(ruleReportByRuleKey.values()); - Collections.sort(result, new RuleReportComparator()); - return result; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/Reporter.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/Reporter.java deleted file mode 100644 index 9cb91dc2fad..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/Reporter.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import org.sonar.api.batch.BatchSide; - -@BatchSide -public interface Reporter { - - void execute(); - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/ResourceReport.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/ResourceReport.java deleted file mode 100644 index 6267335baf0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/ResourceReport.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import org.sonar.batch.issue.tracking.TrackedIssue; - -import org.sonar.api.batch.rule.Rule; -import com.google.common.collect.Maps; -import org.sonar.api.rules.RulePriority; -import org.sonar.batch.index.BatchComponent; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; - -public final class ResourceReport { - private final BatchComponent resource; - private final IssueVariation total = new IssueVariation(); - private final Map<ReportRuleKey, RuleReport> ruleReportByRuleKey = Maps.newHashMap(); - - private List<TrackedIssue> issues = new ArrayList<>(); - private Map<Integer, List<TrackedIssue>> issuesPerLine = Maps.newHashMap(); - private Map<Integer, List<TrackedIssue>> newIssuesPerLine = Maps.newHashMap(); - private Map<Rule, AtomicInteger> issuesByRule = Maps.newHashMap(); - private Map<RulePriority, AtomicInteger> issuesBySeverity = Maps.newHashMap(); - - public ResourceReport(BatchComponent resource) { - this.resource = resource; - } - - public BatchComponent getResourceNode() { - return resource; - } - - public String getName() { - return resource.resource().getName(); - } - - public String getType() { - return resource.resource().getScope(); - } - - public IssueVariation getTotal() { - return total; - } - - public List<TrackedIssue> getIssues() { - return issues; - } - - public Map<Integer, List<TrackedIssue>> getIssuesPerLine() { - return issuesPerLine; - } - - public List<TrackedIssue> getIssuesAtLine(int lineId, boolean all) { - if (all) { - if (issuesPerLine.containsKey(lineId)) { - return issuesPerLine.get(lineId); - } - } else if (newIssuesPerLine.containsKey(lineId)) { - return newIssuesPerLine.get(lineId); - } - return Collections.emptyList(); - } - - public void addIssue(TrackedIssue issue, Rule rule, RulePriority severity) { - ReportRuleKey reportRuleKey = new ReportRuleKey(rule, severity); - initMaps(reportRuleKey); - issues.add(issue); - Integer line = issue.startLine(); - line = line != null ? line : 0; - if (!issuesPerLine.containsKey(line)) { - issuesPerLine.put(line, new ArrayList<TrackedIssue>()); - } - issuesPerLine.get(line).add(issue); - if (!issuesByRule.containsKey(rule)) { - issuesByRule.put(rule, new AtomicInteger()); - } - issuesByRule.get(rule).incrementAndGet(); - if (!issuesBySeverity.containsKey(severity)) { - issuesBySeverity.put(severity, new AtomicInteger()); - } - issuesBySeverity.get(severity).incrementAndGet(); - ruleReportByRuleKey.get(reportRuleKey).getTotal().incrementCountInCurrentAnalysis(); - total.incrementCountInCurrentAnalysis(); - if (issue.isNew()) { - if (!newIssuesPerLine.containsKey(line)) { - newIssuesPerLine.put(line, new ArrayList<TrackedIssue>()); - } - newIssuesPerLine.get(line).add(issue); - total.incrementNewIssuesCount(); - ruleReportByRuleKey.get(reportRuleKey).getTotal().incrementNewIssuesCount(); - } - } - - public void addResolvedIssue(Rule rule, RulePriority severity) { - ReportRuleKey reportRuleKey = new ReportRuleKey(rule, severity); - initMaps(reportRuleKey); - total.incrementResolvedIssuesCount(); - ruleReportByRuleKey.get(reportRuleKey).getTotal().incrementResolvedIssuesCount(); - } - - private void initMaps(ReportRuleKey reportRuleKey) { - if (!ruleReportByRuleKey.containsKey(reportRuleKey)) { - ruleReportByRuleKey.put(reportRuleKey, new RuleReport(reportRuleKey)); - } - } - - public boolean isDisplayableLine(Integer lineNumber, boolean all) { - if (lineNumber == null || lineNumber < 1) { - return false; - } - for (int i = lineNumber - 2; i <= lineNumber + 2; i++) { - if (hasIssues(i, all)) { - return true; - } - } - return false; - } - - private boolean hasIssues(Integer lineId, boolean all) { - if (all) { - List<TrackedIssue> issuesAtLine = issuesPerLine.get(lineId); - return issuesAtLine != null && !issuesAtLine.isEmpty(); - } - List<TrackedIssue> newIssuesAtLine = newIssuesPerLine.get(lineId); - return newIssuesAtLine != null && !newIssuesAtLine.isEmpty(); - } - - public List<RuleReport> getRuleReports() { - List<RuleReport> result = new ArrayList<>(ruleReportByRuleKey.values()); - Collections.sort(result, new RuleReportComparator()); - return result; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/RuleNameProvider.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/RuleNameProvider.java deleted file mode 100644 index 90981fea342..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/RuleNameProvider.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import org.sonar.api.batch.rule.Rule; - -import org.sonar.api.batch.rule.Rules; -import org.apache.commons.lang.StringEscapeUtils; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.rule.RuleKey; - -import javax.annotation.CheckForNull; - -@BatchSide -public class RuleNameProvider { - private Rules rules; - - public RuleNameProvider(Rules rules) { - this.rules = rules; - } - - @CheckForNull - private String nameFromDB(RuleKey ruleKey) { - Rule r = rules.find(ruleKey); - return r != null ? r.name() : null; - } - - public String nameForHTML(RuleKey ruleKey) { - String name = nameFromDB(ruleKey); - return StringEscapeUtils.escapeHtml(name != null ? name : ruleKey.toString()); - } - - public String nameForJS(String ruleKey) { - String name = nameFromDB(RuleKey.parse(ruleKey)); - return StringEscapeUtils.escapeJavaScript(name != null ? name : ruleKey); - } - - public String nameForHTML(Rule rule) { - return StringEscapeUtils.escapeHtml(rule.name()); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/RuleReport.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/RuleReport.java deleted file mode 100644 index 4026ed387ab..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/RuleReport.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import org.sonar.api.batch.rule.Rule; - -import org.apache.commons.lang.builder.ToStringBuilder; - -public final class RuleReport { - private final ReportRuleKey reportRuleKey; - private final IssueVariation total = new IssueVariation(); - - public RuleReport(ReportRuleKey reportRuleKey) { - this.reportRuleKey = reportRuleKey; - } - - public IssueVariation getTotal() { - return total; - } - - public ReportRuleKey getReportRuleKey() { - return reportRuleKey; - } - - public String getSeverity() { - return reportRuleKey.getSeverity().toString(); - } - - public Rule getRule() { - return reportRuleKey.getRule(); - } - - @Override - public String toString() { - return new ToStringBuilder(this). - append("reportRuleKey", reportRuleKey). - append("total", total). - toString(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/RuleReportComparator.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/RuleReportComparator.java deleted file mode 100644 index b779537e8fd..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/RuleReportComparator.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import java.io.Serializable; -import java.util.Comparator; - -public class RuleReportComparator implements Comparator<RuleReport>, Serializable { - @Override - public int compare(RuleReport o1, RuleReport o2) { - if (bothHaveNoNewIssue(o1, o2)) { - return compareByRuleSeverityAndName(o1, o2); - } else if (bothHaveNewIssues(o1, o2)) { - if (sameSeverity(o1, o2) && !sameNewIssueCount(o1, o2)) { - return compareNewIssueCount(o1, o2); - } else { - return compareByRuleSeverityAndName(o1, o2); - } - } else { - return compareNewIssueCount(o1, o2); - } - } - - private static int compareByRuleSeverityAndName(RuleReport o1, RuleReport o2) { - return o1.getReportRuleKey().compareTo(o2.getReportRuleKey()); - } - - private static boolean sameNewIssueCount(RuleReport o1, RuleReport o2) { - return o2.getTotal().getNewIssuesCount() == o1.getTotal().getNewIssuesCount(); - } - - private static boolean sameSeverity(RuleReport o1, RuleReport o2) { - return o1.getSeverity().equals(o2.getSeverity()); - } - - private static int compareNewIssueCount(RuleReport o1, RuleReport o2) { - return o2.getTotal().getNewIssuesCount() - o1.getTotal().getNewIssuesCount(); - } - - private static boolean bothHaveNewIssues(RuleReport o1, RuleReport o2) { - return o1.getTotal().getNewIssuesCount() > 0 && o2.getTotal().getNewIssuesCount() > 0; - } - - private static boolean bothHaveNoNewIssue(RuleReport o1, RuleReport o2) { - return o1.getTotal().getNewIssuesCount() == 0 && o2.getTotal().getNewIssuesCount() == 0; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/SourceProvider.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/SourceProvider.java deleted file mode 100644 index 8017dbd753e..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/SourceProvider.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.report; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang.StringEscapeUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.batch.index.BatchComponent; - -@BatchSide -public class SourceProvider { - - private static final Logger LOG = LoggerFactory.getLogger(SourceProvider.class); - private final FileSystem fs; - - public SourceProvider(FileSystem fs) { - this.fs = fs; - } - - public List<String> getEscapedSource(BatchComponent component) { - if (!component.isFile()) { - // Folder - return Collections.emptyList(); - } - try { - InputFile inputFile = (InputFile) component.inputComponent(); - List<String> lines = FileUtils.readLines(inputFile.file(), fs.encoding()); - List<String> escapedLines = new ArrayList<>(lines.size()); - for (String line : lines) { - escapedLines.add(StringEscapeUtils.escapeHtml(line)); - } - return escapedLines; - } catch (IOException e) { - LOG.warn("Unable to read source code of resource {}", component, e); - return Collections.emptyList(); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/package-info.java deleted file mode 100644 index d6dacad1b54..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.scan.report; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scm/DefaultBlameInput.java b/sonar-batch/src/main/java/org/sonar/batch/scm/DefaultBlameInput.java deleted file mode 100644 index a85c6536c92..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scm/DefaultBlameInput.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scm; - -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.scm.BlameCommand.BlameInput; - -class DefaultBlameInput implements BlameInput { - - private FileSystem fs; - private Iterable<InputFile> filesToBlame; - - DefaultBlameInput(FileSystem fs, Iterable<InputFile> filesToBlame) { - this.fs = fs; - this.filesToBlame = filesToBlame; - } - - @Override - public FileSystem fileSystem() { - return fs; - } - - @Override - public Iterable<InputFile> filesToBlame() { - return filesToBlame; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scm/DefaultBlameOutput.java b/sonar-batch/src/main/java/org/sonar/batch/scm/DefaultBlameOutput.java deleted file mode 100644 index 9341e6fbe01..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scm/DefaultBlameOutput.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scm; - -import com.google.common.base.Preconditions; -import java.text.Normalizer; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.TimeUnit; -import java.util.regex.Pattern; -import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.scm.BlameCommand.BlameOutput; -import org.sonar.api.batch.scm.BlameLine; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReport.Changesets.Builder; -import org.sonar.batch.protocol.output.BatchReportWriter; -import org.sonar.batch.util.ProgressReport; - -class DefaultBlameOutput implements BlameOutput { - - private static final Logger LOG = Loggers.get(DefaultBlameOutput.class); - - private static final Pattern NON_ASCII_CHARS = Pattern.compile("[^\\x00-\\x7F]"); - private static final Pattern ACCENT_CODES = Pattern.compile("\\p{InCombiningDiacriticalMarks}+"); - - private final BatchReportWriter writer; - private final BatchComponentCache componentCache; - private final Set<InputFile> allFilesToBlame = new HashSet<>(); - private ProgressReport progressReport; - private int count; - private int total; - - DefaultBlameOutput(BatchReportWriter writer, BatchComponentCache componentCache, List<InputFile> filesToBlame) { - this.writer = writer; - this.componentCache = componentCache; - this.allFilesToBlame.addAll(filesToBlame); - count = 0; - total = filesToBlame.size(); - progressReport = new ProgressReport("Report about progress of SCM blame", TimeUnit.SECONDS.toMillis(10)); - progressReport.start(total + " files to be analyzed"); - } - - @Override - public synchronized void blameResult(InputFile file, List<BlameLine> lines) { - Preconditions.checkNotNull(file); - Preconditions.checkNotNull(lines); - Preconditions.checkArgument(allFilesToBlame.contains(file), "It was not expected to blame file %s", file.relativePath()); - - if (lines.size() != file.lines()) { - LOG.debug("Ignoring blame result since provider returned {} blame lines but file {} has {} lines", lines.size(), file.relativePath(), file.lines()); - return; - } - - BatchComponent batchComponent = componentCache.get(file); - Builder scmBuilder = BatchReport.Changesets.newBuilder(); - scmBuilder.setComponentRef(batchComponent.batchId()); - Map<String, Integer> changesetsIdByRevision = new HashMap<>(); - - int lineId = 1; - for (BlameLine line : lines) { - validateLine(line, lineId, file); - Integer changesetId = changesetsIdByRevision.get(line.revision()); - if (changesetId == null) { - addChangeset(scmBuilder, line); - changesetId = scmBuilder.getChangesetCount() - 1; - changesetsIdByRevision.put(line.revision(), changesetId); - } - scmBuilder.addChangesetIndexByLine(changesetId); - lineId++; - } - writer.writeComponentChangesets(scmBuilder.build()); - allFilesToBlame.remove(file); - count++; - progressReport.message(count + "/" + total + " files analyzed"); - } - - private static void validateLine(BlameLine line, int lineId, InputFile file) { - Preconditions.checkArgument(StringUtils.isNotBlank(line.revision()), "Blame revision is blank for file %s at line %s", file.relativePath(), lineId); - Preconditions.checkArgument(line.date() != null, "Blame date is null for file %s at line %s", file.relativePath(), lineId); - } - - private static void addChangeset(Builder scmBuilder, BlameLine line) { - BatchReport.Changesets.Changeset.Builder changesetBuilder = BatchReport.Changesets.Changeset.newBuilder(); - changesetBuilder.setRevision(line.revision()); - changesetBuilder.setDate(line.date().getTime()); - if (StringUtils.isNotBlank(line.author())) { - changesetBuilder.setAuthor(normalizeString(line.author())); - } - - scmBuilder.addChangeset(changesetBuilder.build()); - } - - private static String normalizeString(@Nullable String inputString) { - if (inputString == null) { - return ""; - } - String lowerCasedString = inputString.toLowerCase(); - String stringWithoutAccents = removeAccents(lowerCasedString); - return removeNonAsciiCharacters(stringWithoutAccents); - } - - private static String removeAccents(String inputString) { - String unicodeDecomposedString = Normalizer.normalize(inputString, Normalizer.Form.NFD); - return ACCENT_CODES.matcher(unicodeDecomposedString).replaceAll(""); - } - - private static String removeNonAsciiCharacters(String inputString) { - return NON_ASCII_CHARS.matcher(inputString).replaceAll("_"); - } - - public void finish() { - progressReport.stop(count + "/" + total + " files analyzed"); - if (!allFilesToBlame.isEmpty()) { - LOG.warn("Missing blame information for the following files:"); - for (InputFile f : allFilesToBlame) { - LOG.warn(" * " + f.absolutePath()); - } - LOG.warn("This may lead to missing/broken features in SonarQube"); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scm/ScmConfiguration.java b/sonar-batch/src/main/java/org/sonar/batch/scm/ScmConfiguration.java deleted file mode 100644 index f6a88bf4465..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scm/ScmConfiguration.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scm; - -import com.google.common.base.Joiner; -import java.util.LinkedHashMap; -import java.util.Map; -import org.apache.commons.lang.StringUtils; -import org.picocontainer.Startable; -import org.sonar.api.CoreProperties; -import org.sonar.api.Properties; -import org.sonar.api.Property; -import org.sonar.api.PropertyType; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.InstantiationStrategy; -import org.sonar.api.batch.scm.ScmProvider; -import org.sonar.api.config.Settings; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.batch.scan.ImmutableProjectReactor; - -@Properties({ - @Property( - key = ScmConfiguration.FORCE_RELOAD_KEY, - defaultValue = "false", - name = "Force reloading of SCM information for all files", - description = "By default only files modified since previous analysis are inspected. Set this parameter to true to force the reloading.", - category = CoreProperties.CATEGORY_SCM, - project = false, - module = false, - global = false, - type = PropertyType.BOOLEAN) -}) -@InstantiationStrategy(InstantiationStrategy.PER_BATCH) -@BatchSide -public final class ScmConfiguration implements Startable { - private static final Logger LOG = Loggers.get(ScmConfiguration.class); - - public static final String FORCE_RELOAD_KEY = "sonar.scm.forceReloadAll"; - - private final ImmutableProjectReactor projectReactor; - private final Settings settings; - private final Map<String, ScmProvider> providerPerKey = new LinkedHashMap<>(); - private final AnalysisMode analysisMode; - - private ScmProvider provider; - - public ScmConfiguration(ImmutableProjectReactor projectReactor, AnalysisMode analysisMode, Settings settings, ScmProvider... providers) { - this.projectReactor = projectReactor; - this.analysisMode = analysisMode; - this.settings = settings; - for (ScmProvider scmProvider : providers) { - providerPerKey.put(scmProvider.key(), scmProvider); - } - } - - public ScmConfiguration(ImmutableProjectReactor projectReactor, AnalysisMode analysisMode, Settings settings) { - this(projectReactor, analysisMode, settings, new ScmProvider[0]); - } - - @Override - public void start() { - if (analysisMode.isIssues()) { - return; - } - if (isDisabled()) { - LOG.debug("SCM Step is disabled by configuration"); - return; - } - if (settings.hasKey(CoreProperties.SCM_PROVIDER_KEY)) { - String forcedProviderKey = settings.getString(CoreProperties.SCM_PROVIDER_KEY); - setProviderIfSupported(forcedProviderKey); - } else { - autodetection(); - if (this.provider == null) { - considerOldScmUrl(); - } - if (this.provider == null) { - LOG.warn("SCM provider autodetection failed. No SCM provider claims to support this project. Please use " + CoreProperties.SCM_PROVIDER_KEY - + " to define SCM of your project."); - } - } - } - - private void setProviderIfSupported(String forcedProviderKey) { - if (providerPerKey.containsKey(forcedProviderKey)) { - this.provider = providerPerKey.get(forcedProviderKey); - } else { - String supportedProviders = providerPerKey.isEmpty() ? "No SCM provider installed" : ("Supported SCM providers are " + Joiner.on(",").join(providerPerKey.keySet())); - throw new IllegalArgumentException("SCM provider was set to \"" + forcedProviderKey + "\" but no SCM provider found for this key. " + supportedProviders); - } - } - - private void considerOldScmUrl() { - if (settings.hasKey(CoreProperties.LINKS_SOURCES_DEV)) { - String url = settings.getString(CoreProperties.LINKS_SOURCES_DEV); - if (StringUtils.startsWith(url, "scm:")) { - String[] split = url.split(":"); - if (split.length > 1) { - setProviderIfSupported(split[1]); - } - } - } - - } - - private void autodetection() { - for (ScmProvider installedProvider : providerPerKey.values()) { - if (installedProvider.supports(projectReactor.getRoot().getBaseDir())) { - if (this.provider == null) { - this.provider = installedProvider; - } else { - throw new IllegalStateException("SCM provider autodetection failed. Both " + this.provider.key() + " and " + installedProvider.key() - + " claim to support this project. Please use " + CoreProperties.SCM_PROVIDER_KEY + " to define SCM of your project."); - } - } - } - } - - public ScmProvider provider() { - return provider; - } - - public boolean isDisabled() { - return settings.getBoolean(CoreProperties.SCM_DISABLED_KEY); - } - - public boolean forceReloadAll() { - return settings.getBoolean(FORCE_RELOAD_KEY); - } - - @Override - public void stop() { - // Nothing to do - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scm/ScmSensor.java b/sonar-batch/src/main/java/org/sonar/batch/scm/ScmSensor.java deleted file mode 100644 index 078224bb6c6..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scm/ScmSensor.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scm; - -import java.util.LinkedList; -import java.util.List; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.CoreProperties; -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.batch.fs.InputFile.Status; -import org.sonar.api.batch.sensor.Sensor; -import org.sonar.api.batch.sensor.SensorContext; -import org.sonar.api.batch.sensor.SensorDescriptor; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.report.ReportPublisher; -import org.sonar.batch.repository.FileData; -import org.sonar.batch.repository.ProjectRepositories; - -public final class ScmSensor implements Sensor { - - private static final Logger LOG = Loggers.get(ScmSensor.class); - - private final ProjectDefinition projectDefinition; - private final ScmConfiguration configuration; - private final FileSystem fs; - private final ProjectRepositories projectRepositories; - private final BatchComponentCache resourceCache; - private final ReportPublisher publishReportJob; - - public ScmSensor(ProjectDefinition projectDefinition, ScmConfiguration configuration, - ProjectRepositories projectRepositories, FileSystem fs, BatchComponentCache resourceCache, ReportPublisher publishReportJob) { - this.projectDefinition = projectDefinition; - this.configuration = configuration; - this.projectRepositories = projectRepositories; - this.fs = fs; - this.resourceCache = resourceCache; - this.publishReportJob = publishReportJob; - } - - @Override - public void describe(SensorDescriptor descriptor) { - descriptor.name("SCM Sensor"); - } - - @Override - public void execute(SensorContext context) { - if (configuration.isDisabled()) { - LOG.info("SCM Publisher is disabled"); - return; - } - if (configuration.provider() == null) { - LOG.info("No SCM system was detected. You can use the '" + CoreProperties.SCM_PROVIDER_KEY + "' property to explicitly specify it."); - return; - } - - List<InputFile> filesToBlame = collectFilesToBlame(); - if (!filesToBlame.isEmpty()) { - String key = configuration.provider().key(); - LOG.info("SCM provider for this project is: " + key); - DefaultBlameOutput output = new DefaultBlameOutput(publishReportJob.getWriter(), resourceCache, filesToBlame); - try { - configuration.provider().blameCommand().blame(new DefaultBlameInput(fs, filesToBlame), output); - } finally { - output.finish(); - } - } - } - - private List<InputFile> collectFilesToBlame() { - if (configuration.forceReloadAll()) { - LOG.warn("Forced reloading of SCM data for all files."); - } - List<InputFile> filesToBlame = new LinkedList<>(); - for (InputFile f : fs.inputFiles(fs.predicates().all())) { - if (configuration.forceReloadAll() || f.status() != Status.SAME) { - addIfNotEmpty(filesToBlame, f); - } else { - // File status is SAME so that mean fileData exists - FileData fileData = projectRepositories.fileData(projectDefinition.getKeyWithBranch(), f.relativePath()); - if (StringUtils.isEmpty(fileData.revision())) { - addIfNotEmpty(filesToBlame, f); - } - } - } - return filesToBlame; - } - - private static void addIfNotEmpty(List<InputFile> filesToBlame, InputFile f) { - if (!f.isEmpty()) { - filesToBlame.add(f); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scm/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/scm/package-info.java deleted file mode 100644 index 925761a7298..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scm/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.scm; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorContext.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorContext.java deleted file mode 100644 index 1080dab084a..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorContext.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.sensor; - -import java.io.Serializable; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputModule; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.sensor.SensorContext; -import org.sonar.api.batch.sensor.coverage.NewCoverage; -import org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage; -import org.sonar.api.batch.sensor.cpd.NewCpdTokens; -import org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens; -import org.sonar.api.batch.sensor.highlighting.NewHighlighting; -import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.api.batch.sensor.issue.NewIssue; -import org.sonar.api.batch.sensor.issue.internal.DefaultIssue; -import org.sonar.api.batch.sensor.measure.NewMeasure; -import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; -import org.sonar.api.config.Settings; -import org.sonar.batch.sensor.noop.NoOpNewCpdTokens; -import org.sonar.batch.sensor.noop.NoOpNewHighlighting; - -public class DefaultSensorContext implements SensorContext { - - private static final NoOpNewHighlighting NO_OP_NEW_HIGHLIGHTING = new NoOpNewHighlighting(); - private static final NoOpNewCpdTokens NO_OP_NEW_CPD_TOKENS = new NoOpNewCpdTokens(); - - private final Settings settings; - private final FileSystem fs; - private final ActiveRules activeRules; - private final SensorStorage sensorStorage; - private final AnalysisMode analysisMode; - private final InputModule module; - - public DefaultSensorContext(InputModule module, Settings settings, FileSystem fs, ActiveRules activeRules, AnalysisMode analysisMode, SensorStorage sensorStorage) { - this.module = module; - this.settings = settings; - this.fs = fs; - this.activeRules = activeRules; - this.analysisMode = analysisMode; - this.sensorStorage = sensorStorage; - } - - @Override - public Settings settings() { - return settings; - } - - @Override - public FileSystem fileSystem() { - return fs; - } - - @Override - public ActiveRules activeRules() { - return activeRules; - } - - @Override - public InputModule module() { - return module; - } - - @Override - public <G extends Serializable> NewMeasure<G> newMeasure() { - return new DefaultMeasure<>(sensorStorage); - } - - @Override - public NewIssue newIssue() { - return new DefaultIssue(sensorStorage); - } - - @Override - public NewHighlighting newHighlighting() { - if (analysisMode.isIssues()) { - return NO_OP_NEW_HIGHLIGHTING; - } - return new DefaultHighlighting(sensorStorage); - } - - @Override - public NewCoverage newCoverage() { - return new DefaultCoverage(sensorStorage); - } - - @Override - public NewCpdTokens newCpdTokens() { - if (analysisMode.isIssues()) { - return NO_OP_NEW_CPD_TOKENS; - } - return new DefaultCpdTokens(sensorStorage); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorStorage.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorStorage.java deleted file mode 100644 index 096a115d425..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorStorage.java +++ /dev/null @@ -1,285 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.sensor; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Function; -import com.google.common.collect.Iterables; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Set; -import javax.annotation.Nonnull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputComponent; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.measure.MetricFinder; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.sensor.coverage.CoverageType; -import org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage; -import org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens; -import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting; -import org.sonar.api.batch.sensor.highlighting.internal.SyntaxHighlightingRule; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.api.batch.sensor.issue.Issue; -import org.sonar.api.batch.sensor.measure.Measure; -import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; -import org.sonar.api.config.Settings; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Metric; -import org.sonar.api.resources.File; -import org.sonar.api.resources.Resource; -import org.sonar.api.source.Symbol; -import org.sonar.api.utils.KeyValueFormat; -import org.sonar.api.utils.SonarException; -import org.sonar.batch.cpd.DefaultCpdBlockIndexer; -import org.sonar.batch.cpd.index.SonarCpdBlockIndex; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.issue.ModuleIssues; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReportWriter; -import org.sonar.batch.report.BatchReportUtils; -import org.sonar.batch.report.ReportPublisher; -import org.sonar.batch.scan.measure.MeasureCache; -import org.sonar.batch.sensor.coverage.CoverageExclusions; -import org.sonar.batch.source.DefaultSymbol; -import org.sonar.duplications.block.Block; -import org.sonar.duplications.internal.pmd.PmdBlockChunker; - -public class DefaultSensorStorage implements SensorStorage { - - private static final Logger LOG = LoggerFactory.getLogger(DefaultSensorStorage.class); - - private static final List<Metric> INTERNAL_METRICS = Arrays.<Metric>asList( - // Computed by LinesSensor - CoreMetrics.LINES); - - private static final List<String> DEPRECATED_METRICS_KEYS = Arrays.<String>asList( - CoreMetrics.DEPENDENCY_MATRIX_KEY, - CoreMetrics.DIRECTORY_CYCLES_KEY, - CoreMetrics.DIRECTORY_EDGES_WEIGHT_KEY, - CoreMetrics.DIRECTORY_FEEDBACK_EDGES_KEY, - CoreMetrics.DIRECTORY_TANGLE_INDEX_KEY, - CoreMetrics.DIRECTORY_TANGLES_KEY, - CoreMetrics.FILE_CYCLES_KEY, - CoreMetrics.FILE_EDGES_WEIGHT_KEY, - CoreMetrics.FILE_FEEDBACK_EDGES_KEY, - CoreMetrics.FILE_TANGLE_INDEX_KEY, - CoreMetrics.FILE_TANGLES_KEY, - CoreMetrics.DUPLICATIONS_DATA_KEY); - - private final MetricFinder metricFinder; - private final ModuleIssues moduleIssues; - private final CoverageExclusions coverageExclusions; - private final BatchComponentCache componentCache; - private final ReportPublisher reportPublisher; - private final MeasureCache measureCache; - private final SonarCpdBlockIndex index; - private final Settings settings; - - public DefaultSensorStorage(MetricFinder metricFinder, ModuleIssues moduleIssues, - Settings settings, FileSystem fs, ActiveRules activeRules, - CoverageExclusions coverageExclusions, BatchComponentCache componentCache, ReportPublisher reportPublisher, MeasureCache measureCache, SonarCpdBlockIndex index) { - this.metricFinder = metricFinder; - this.moduleIssues = moduleIssues; - this.settings = settings; - this.coverageExclusions = coverageExclusions; - this.componentCache = componentCache; - this.reportPublisher = reportPublisher; - this.measureCache = measureCache; - this.index = index; - } - - private Metric findMetricOrFail(String metricKey) { - Metric m = (Metric) metricFinder.findByKey(metricKey); - if (m == null) { - throw new IllegalStateException("Unknow metric with key: " + metricKey); - } - return m; - } - - @Override - public void store(Measure newMeasure) { - DefaultMeasure<?> measure = (DefaultMeasure<?>) newMeasure; - org.sonar.api.measures.Metric m = findMetricOrFail(measure.metric().key()); - org.sonar.api.measures.Measure measureToSave = new org.sonar.api.measures.Measure(m); - setValueAccordingToMetricType(newMeasure, m, measureToSave); - measureToSave.setFromCore(measure.isFromCore()); - InputComponent inputComponent = newMeasure.inputComponent(); - Resource resource = componentCache.get(inputComponent).resource(); - if (coverageExclusions.accept(resource, measureToSave)) { - saveMeasure(resource, measureToSave); - } - } - - public org.sonar.api.measures.Measure saveMeasure(Resource resource, org.sonar.api.measures.Measure measure) { - if (DEPRECATED_METRICS_KEYS.contains(measure.getMetricKey())) { - // Ignore deprecated metrics - return null; - } - org.sonar.api.batch.measure.Metric metric = metricFinder.findByKey(measure.getMetricKey()); - if (metric == null) { - throw new SonarException("Unknown metric: " + measure.getMetricKey()); - } - if (!measure.isFromCore() && INTERNAL_METRICS.contains(metric)) { - LOG.debug("Metric " + metric.key() + " is an internal metric computed by SonarQube. Provided value is ignored."); - return measure; - } - if (measureCache.contains(resource, measure)) { - throw new SonarException("Can not add the same measure twice on " + resource + ": " + measure); - } - measureCache.put(resource, measure); - return measure; - } - - private void setValueAccordingToMetricType(Measure<?> measure, org.sonar.api.measures.Metric<?> m, org.sonar.api.measures.Measure measureToSave) { - switch (m.getType()) { - case BOOL: - measureToSave.setValue(Boolean.TRUE.equals(measure.value()) ? 1.0 : 0.0); - break; - case INT: - case MILLISEC: - case WORK_DUR: - case FLOAT: - case PERCENT: - case RATING: - measureToSave.setValue(((Number) measure.value()).doubleValue()); - break; - case STRING: - case LEVEL: - case DATA: - case DISTRIB: - measureToSave.setData((String) measure.value()); - break; - default: - throw new UnsupportedOperationException("Unsupported type :" + m.getType()); - } - } - - @Override - public void store(Issue issue) { - moduleIssues.initAndAddIssue(issue); - } - - private File getFile(InputFile file) { - BatchComponent r = componentCache.get(file); - if (r == null) { - throw new IllegalStateException("Provided input file is not indexed"); - } - return (File) r.resource(); - } - - @Override - public void store(DefaultHighlighting highlighting) { - BatchReportWriter writer = reportPublisher.getWriter(); - DefaultInputFile inputFile = (DefaultInputFile) highlighting.inputFile(); - writer.writeComponentSyntaxHighlighting(componentCache.get(inputFile).batchId(), - Iterables.transform(highlighting.getSyntaxHighlightingRuleSet(), new BuildSyntaxHighlighting())); - } - - public void store(DefaultInputFile inputFile, Map<Symbol, Set<TextRange>> referencesBySymbol) { - BatchReportWriter writer = reportPublisher.getWriter(); - writer.writeComponentSymbols(componentCache.get(inputFile).batchId(), - Iterables.transform(referencesBySymbol.entrySet(), new Function<Map.Entry<Symbol, Set<TextRange>>, BatchReport.Symbol>() { - private BatchReport.Symbol.Builder builder = BatchReport.Symbol.newBuilder(); - private BatchReport.TextRange.Builder rangeBuilder = BatchReport.TextRange.newBuilder(); - - @Override - public BatchReport.Symbol apply(Map.Entry<Symbol, Set<TextRange>> input) { - builder.clear(); - rangeBuilder.clear(); - DefaultSymbol symbol = (DefaultSymbol) input.getKey(); - builder.setDeclaration(rangeBuilder.setStartLine(symbol.range().start().line()) - .setStartOffset(symbol.range().start().lineOffset()) - .setEndLine(symbol.range().end().line()) - .setEndOffset(symbol.range().end().lineOffset()) - .build()); - for (TextRange reference : input.getValue()) { - builder.addReference(rangeBuilder.setStartLine(reference.start().line()) - .setStartOffset(reference.start().lineOffset()) - .setEndLine(reference.end().line()) - .setEndOffset(reference.end().lineOffset()) - .build()); - } - return builder.build(); - } - - })); - } - - @Override - public void store(DefaultCoverage defaultCoverage) { - File file = getFile(defaultCoverage.inputFile()); - if (coverageExclusions.hasMatchingPattern(file)) { - return; - } - CoverageType type = defaultCoverage.type(); - if (defaultCoverage.linesToCover() > 0) { - saveMeasure(file, new org.sonar.api.measures.Measure(type.linesToCover(), (double) defaultCoverage.linesToCover())); - saveMeasure(file, new org.sonar.api.measures.Measure(type.uncoveredLines(), (double) (defaultCoverage.linesToCover() - defaultCoverage.coveredLines()))); - saveMeasure(file, new org.sonar.api.measures.Measure(type.lineHitsData()).setData(KeyValueFormat.format(defaultCoverage.hitsByLine()))); - } - if (defaultCoverage.conditions() > 0) { - saveMeasure(file, new org.sonar.api.measures.Measure(type.conditionsToCover(), (double) defaultCoverage.conditions())); - saveMeasure(file, new org.sonar.api.measures.Measure(type.uncoveredConditions(), (double) (defaultCoverage.conditions() - defaultCoverage.coveredConditions()))); - saveMeasure(file, new org.sonar.api.measures.Measure(type.coveredConditionsByLine()).setData(KeyValueFormat.format(defaultCoverage.coveredConditionsByLine()))); - saveMeasure(file, new org.sonar.api.measures.Measure(type.conditionsByLine()).setData(KeyValueFormat.format(defaultCoverage.conditionsByLine()))); - } - } - - private static class BuildSyntaxHighlighting implements Function<SyntaxHighlightingRule, BatchReport.SyntaxHighlighting> { - private BatchReport.SyntaxHighlighting.Builder builder = BatchReport.SyntaxHighlighting.newBuilder(); - private BatchReport.TextRange.Builder rangeBuilder = BatchReport.TextRange.newBuilder(); - - @Override - public BatchReport.SyntaxHighlighting apply(@Nonnull SyntaxHighlightingRule input) { - builder.setRange(rangeBuilder.setStartLine(input.range().start().line()) - .setStartOffset(input.range().start().lineOffset()) - .setEndLine(input.range().end().line()) - .setEndOffset(input.range().end().lineOffset()) - .build()); - builder.setType(BatchReportUtils.toProtocolType(input.getTextType())); - return builder.build(); - } - } - - @Override - public void store(DefaultCpdTokens defaultCpdTokens) { - InputFile inputFile = defaultCpdTokens.inputFile(); - PmdBlockChunker blockChunker = new PmdBlockChunker(getBlockSize(inputFile.language())); - List<Block> blocks = blockChunker.chunk(inputFile.key(), defaultCpdTokens.getTokenLines()); - index.insert(inputFile, blocks); - } - - @VisibleForTesting - int getBlockSize(String languageKey) { - int blockSize = settings.getInt("sonar.cpd." + languageKey + ".minimumLines"); - if (blockSize == 0) { - blockSize = DefaultCpdBlockIndexer.getDefaultBlockSize(languageKey); - } - return blockSize; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/SensorOptimizer.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/SensorOptimizer.java deleted file mode 100644 index 7e8dd63e93a..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/sensor/SensorOptimizer.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.sensor; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.fs.FilePredicate; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; -import org.sonar.api.config.Settings; - -@BatchSide -public class SensorOptimizer { - - private static final Logger LOG = LoggerFactory.getLogger(SensorOptimizer.class); - - private final FileSystem fs; - private final ActiveRules activeRules; - private final Settings settings; - - public SensorOptimizer(FileSystem fs, ActiveRules activeRules, Settings settings) { - this.fs = fs; - this.activeRules = activeRules; - this.settings = settings; - } - - /** - * Decide if the given Sensor should be executed. - */ - public boolean shouldExecute(DefaultSensorDescriptor descriptor) { - if (!fsCondition(descriptor)) { - LOG.debug("'{}' skipped because there is no related file in current project", descriptor.name()); - return false; - } - if (!activeRulesCondition(descriptor)) { - LOG.debug("'{}' skipped because there is no related rule activated in the quality profile", descriptor.name()); - return false; - } - if (!settingsCondition(descriptor)) { - LOG.debug("'{}' skipped because one of the required properties is missing", descriptor.name()); - return false; - } - return true; - } - - private boolean settingsCondition(DefaultSensorDescriptor descriptor) { - if (!descriptor.properties().isEmpty()) { - for (String propertyKey : descriptor.properties()) { - if (!settings.hasKey(propertyKey)) { - return false; - } - } - } - return true; - } - - private boolean activeRulesCondition(DefaultSensorDescriptor descriptor) { - if (!descriptor.ruleRepositories().isEmpty()) { - for (String repoKey : descriptor.ruleRepositories()) { - if (!activeRules.findByRepository(repoKey).isEmpty()) { - return true; - } - } - return false; - } - return true; - } - - private boolean fsCondition(DefaultSensorDescriptor descriptor) { - if (!descriptor.languages().isEmpty() || descriptor.type() != null) { - FilePredicate langPredicate = descriptor.languages().isEmpty() ? fs.predicates().all() : fs.predicates().hasLanguages(descriptor.languages()); - - FilePredicate typePredicate = descriptor.type() == null ? fs.predicates().all() : fs.predicates().hasType(descriptor.type()); - return fs.hasFiles(fs.predicates().and(langPredicate, typePredicate)); - } - return true; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/SensorWrapper.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/SensorWrapper.java deleted file mode 100644 index 3e82a222546..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/sensor/SensorWrapper.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.sensor; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.sensor.Sensor; -import org.sonar.api.batch.sensor.SensorContext; -import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; -import org.sonar.api.resources.Project; - -public class SensorWrapper implements org.sonar.api.batch.Sensor { - - private static final Logger LOG = LoggerFactory.getLogger(SensorWrapper.class); - - private Sensor wrappedSensor; - private SensorContext adaptor; - private DefaultSensorDescriptor descriptor; - private SensorOptimizer optimizer; - - public SensorWrapper(Sensor newSensor, SensorContext adaptor, SensorOptimizer optimizer) { - this.wrappedSensor = newSensor; - this.optimizer = optimizer; - descriptor = new DefaultSensorDescriptor(); - newSensor.describe(descriptor); - this.adaptor = adaptor; - } - - public Sensor wrappedSensor() { - return wrappedSensor; - } - - @Override - public boolean shouldExecuteOnProject(Project project) { - return optimizer.shouldExecute(descriptor); - } - - @Override - public void analyse(Project module, org.sonar.api.batch.SensorContext context) { - wrappedSensor.execute(adaptor); - } - - @Override - public String toString() { - return descriptor.name() + (LOG.isDebugEnabled() ? " (wrapped)" : ""); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/CoverageConstants.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/CoverageConstants.java deleted file mode 100644 index 569ac157e42..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/CoverageConstants.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.sensor.coverage; - -import com.google.common.collect.ImmutableList; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Metric; - -import java.util.Collection; - -public class CoverageConstants { - - public static final Collection<Metric> COVERAGE_METRICS = ImmutableList.<Metric>of(CoreMetrics.LINES_TO_COVER, CoreMetrics.UNCOVERED_LINES, CoreMetrics.NEW_LINES_TO_COVER, - CoreMetrics.NEW_UNCOVERED_LINES, CoreMetrics.CONDITIONS_TO_COVER, CoreMetrics.UNCOVERED_CONDITIONS, - CoreMetrics.NEW_CONDITIONS_TO_COVER, CoreMetrics.NEW_UNCOVERED_CONDITIONS); - - public static final Collection<Metric> LINE_COVERAGE_METRICS = ImmutableList.<Metric>of(CoreMetrics.UNCOVERED_LINES, CoreMetrics.LINES_TO_COVER, CoreMetrics.NEW_UNCOVERED_LINES, - CoreMetrics.NEW_LINES_TO_COVER); - - public static final Collection<Metric> BRANCH_COVERAGE_METRICS = ImmutableList.<Metric>of(CoreMetrics.UNCOVERED_CONDITIONS, CoreMetrics.CONDITIONS_TO_COVER, - CoreMetrics.NEW_UNCOVERED_CONDITIONS, CoreMetrics.NEW_CONDITIONS_TO_COVER); - - private CoverageConstants() { - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/CoverageExclusions.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/CoverageExclusions.java deleted file mode 100644 index 198b61141bb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/CoverageExclusions.java +++ /dev/null @@ -1,210 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.sensor.coverage; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableList.Builder; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; -import javax.annotation.CheckForNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.config.Settings; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; -import org.sonar.api.measures.Metric; -import org.sonar.api.resources.Resource; -import org.sonar.api.utils.KeyValueFormat; -import org.sonar.api.utils.WildcardPattern; - -public class CoverageExclusions { - - private static final Logger LOG = LoggerFactory.getLogger(CoverageExclusions.class); - - private final Settings settings; - private final Set<Metric> coverageMetrics; - private final Set<Metric> byLineMetrics; - private Collection<WildcardPattern> resourcePatterns; - - private final FileSystem fs; - - public CoverageExclusions(Settings settings, FileSystem fs) { - this.settings = settings; - this.fs = fs; - this.coverageMetrics = new HashSet<>(); - this.byLineMetrics = new HashSet<>(); - // UT - coverageMetrics.add(CoreMetrics.COVERAGE); - coverageMetrics.add(CoreMetrics.LINE_COVERAGE); - coverageMetrics.add(CoreMetrics.BRANCH_COVERAGE); - coverageMetrics.add(CoreMetrics.UNCOVERED_LINES); - coverageMetrics.add(CoreMetrics.LINES_TO_COVER); - coverageMetrics.add(CoreMetrics.UNCOVERED_CONDITIONS); - coverageMetrics.add(CoreMetrics.CONDITIONS_TO_COVER); - coverageMetrics.add(CoreMetrics.CONDITIONS_BY_LINE); - coverageMetrics.add(CoreMetrics.COVERED_CONDITIONS_BY_LINE); - coverageMetrics.add(CoreMetrics.COVERAGE_LINE_HITS_DATA); - coverageMetrics.add(CoreMetrics.NEW_LINES_TO_COVER); - coverageMetrics.add(CoreMetrics.NEW_UNCOVERED_LINES); - coverageMetrics.add(CoreMetrics.NEW_UNCOVERED_CONDITIONS); - // IT - coverageMetrics.add(CoreMetrics.IT_COVERAGE); - coverageMetrics.add(CoreMetrics.IT_LINE_COVERAGE); - coverageMetrics.add(CoreMetrics.IT_BRANCH_COVERAGE); - coverageMetrics.add(CoreMetrics.IT_UNCOVERED_LINES); - coverageMetrics.add(CoreMetrics.IT_LINES_TO_COVER); - coverageMetrics.add(CoreMetrics.IT_UNCOVERED_CONDITIONS); - coverageMetrics.add(CoreMetrics.IT_CONDITIONS_TO_COVER); - coverageMetrics.add(CoreMetrics.IT_CONDITIONS_BY_LINE); - coverageMetrics.add(CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE); - coverageMetrics.add(CoreMetrics.IT_COVERAGE_LINE_HITS_DATA); - coverageMetrics.add(CoreMetrics.NEW_IT_LINES_TO_COVER); - coverageMetrics.add(CoreMetrics.NEW_IT_UNCOVERED_LINES); - coverageMetrics.add(CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS); - // OVERALL - coverageMetrics.add(CoreMetrics.OVERALL_COVERAGE); - coverageMetrics.add(CoreMetrics.OVERALL_LINE_COVERAGE); - coverageMetrics.add(CoreMetrics.OVERALL_BRANCH_COVERAGE); - coverageMetrics.add(CoreMetrics.OVERALL_UNCOVERED_LINES); - coverageMetrics.add(CoreMetrics.OVERALL_LINES_TO_COVER); - coverageMetrics.add(CoreMetrics.OVERALL_UNCOVERED_CONDITIONS); - coverageMetrics.add(CoreMetrics.OVERALL_CONDITIONS_TO_COVER); - coverageMetrics.add(CoreMetrics.OVERALL_CONDITIONS_BY_LINE); - coverageMetrics.add(CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE); - coverageMetrics.add(CoreMetrics.OVERALL_COVERAGE_LINE_HITS_DATA); - coverageMetrics.add(CoreMetrics.NEW_OVERALL_LINES_TO_COVER); - coverageMetrics.add(CoreMetrics.NEW_OVERALL_UNCOVERED_LINES); - coverageMetrics.add(CoreMetrics.NEW_OVERALL_UNCOVERED_CONDITIONS); - - byLineMetrics.add(CoreMetrics.OVERALL_COVERAGE_LINE_HITS_DATA); - byLineMetrics.add(CoreMetrics.OVERALL_CONDITIONS_BY_LINE); - byLineMetrics.add(CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE); - byLineMetrics.add(CoreMetrics.COVERAGE_LINE_HITS_DATA); - byLineMetrics.add(CoreMetrics.COVERED_CONDITIONS_BY_LINE); - byLineMetrics.add(CoreMetrics.CONDITIONS_BY_LINE); - byLineMetrics.add(CoreMetrics.IT_COVERAGE_LINE_HITS_DATA); - byLineMetrics.add(CoreMetrics.IT_CONDITIONS_BY_LINE); - byLineMetrics.add(CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE); - - initPatterns(); - } - - private boolean isLineMetrics(Metric<?> metric) { - return this.byLineMetrics.contains(metric); - } - - public void validate(Measure<?> measure, InputFile inputFile) { - Metric<?> metric = measure.getMetric(); - - if (!isLineMetrics(metric)) { - return; - } - - Map<Integer, Integer> m = KeyValueFormat.parseIntInt(measure.getData()); - validatePositiveLine(m, inputFile.absolutePath()); - validateMaxLine(m, inputFile); - } - - @CheckForNull - private InputFile getInputFile(String filePath) { - return fs.inputFile(fs.predicates().hasRelativePath(filePath)); - } - - public void validate(Measure<?> measure, String filePath) { - Metric<?> metric = measure.getMetric(); - - if (!isLineMetrics(metric)) { - return; - } - - InputFile inputFile = getInputFile(filePath); - - if (inputFile == null) { - throw new IllegalStateException(String.format("Can't create measure for resource '%s': resource is not indexed as a file", filePath)); - } - - validate(measure, inputFile); - } - - private static void validateMaxLine(Map<Integer, Integer> m, InputFile inputFile) { - int maxLine = inputFile.lines(); - - for (int l : m.keySet()) { - if (l > maxLine) { - throw new IllegalStateException(String.format("Can't create measure for line %d for file '%s' with %d lines", l, inputFile.absolutePath(), maxLine)); - } - } - } - - private static void validatePositiveLine(Map<Integer, Integer> m, String filePath) { - for (int l : m.keySet()) { - if (l <= 0) { - throw new IllegalStateException(String.format("Measure with line %d for file '%s' must be > 0", l, filePath)); - } - } - } - - public boolean accept(Resource resource, Measure<?> measure) { - if (isCoverageMetric(measure.getMetric())) { - return !hasMatchingPattern(resource); - } else { - return true; - } - } - - private boolean isCoverageMetric(Metric<?> metric) { - return this.coverageMetrics.contains(metric); - } - - public boolean hasMatchingPattern(Resource resource) { - boolean found = false; - Iterator<WildcardPattern> iterator = resourcePatterns.iterator(); - while (!found && iterator.hasNext()) { - found = resource.matchFilePattern(iterator.next().toString()); - } - return found; - } - - @VisibleForTesting - final void initPatterns() { - Builder<WildcardPattern> builder = ImmutableList.builder(); - for (String pattern : settings.getStringArray(CoreProperties.PROJECT_COVERAGE_EXCLUSIONS_PROPERTY)) { - builder.add(WildcardPattern.create(pattern)); - } - resourcePatterns = builder.build(); - log("Excluded sources for coverage: ", resourcePatterns); - } - - private static void log(String title, Collection<WildcardPattern> patterns) { - if (!patterns.isEmpty()) { - LOG.info(title); - for (WildcardPattern pattern : patterns) { - LOG.info(" " + pattern); - } - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/package-info.java deleted file mode 100644 index b4c6be3d144..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.batch.sensor.coverage; diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/noop/NoOpNewCpdTokens.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/noop/NoOpNewCpdTokens.java deleted file mode 100644 index afd00476b2a..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/sensor/noop/NoOpNewCpdTokens.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.sensor.noop; - -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.sensor.cpd.NewCpdTokens; - -public class NoOpNewCpdTokens implements NewCpdTokens { - @Override - public void save() { - // Do nothing - } - - @Override - public NoOpNewCpdTokens onFile(InputFile inputFile) { - // Do nothing - return this; - } - - @Override - public NewCpdTokens addToken(TextRange range, String image) { - // Do nothing - return this; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/noop/NoOpNewHighlighting.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/noop/NoOpNewHighlighting.java deleted file mode 100644 index 7ead32f7f04..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/sensor/noop/NoOpNewHighlighting.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.sensor.noop; - -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.sensor.highlighting.NewHighlighting; -import org.sonar.api.batch.sensor.highlighting.TypeOfText; - -public class NoOpNewHighlighting implements NewHighlighting { - @Override - public void save() { - // Do nothing - } - - @Override - public NewHighlighting onFile(InputFile inputFile) { - // Do nothing - return this; - } - - @Override - public NewHighlighting highlight(int startOffset, int endOffset, TypeOfText typeOfText) { - // Do nothing - return this; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/noop/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/noop/package-info.java deleted file mode 100644 index 0a654a40800..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/sensor/noop/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.batch.sensor.noop; diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/package-info.java deleted file mode 100644 index 4c9f457a97f..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/sensor/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.batch.sensor; diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizerSensor.java b/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizerSensor.java deleted file mode 100644 index 158e02a9777..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizerSensor.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.source; - -import org.sonar.api.batch.Phase; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.sensor.Sensor; -import org.sonar.api.batch.sensor.SensorContext; -import org.sonar.api.batch.sensor.SensorDescriptor; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.output.BatchReportReader; -import org.sonar.batch.report.ReportPublisher; - -@Phase(name = Phase.Name.POST) -public final class CodeColorizerSensor implements Sensor { - - private final ReportPublisher reportPublisher; - private final BatchComponentCache resourceCache; - private final CodeColorizers codeColorizers; - - public CodeColorizerSensor(ReportPublisher reportPublisher, BatchComponentCache resourceCache, CodeColorizers codeColorizers) { - this.reportPublisher = reportPublisher; - this.resourceCache = resourceCache; - this.codeColorizers = codeColorizers; - } - - @Override - public void describe(SensorDescriptor descriptor) { - descriptor.name("Code Colorizer Sensor"); - } - - @Override - public void execute(final SensorContext context) { - FileSystem fs = context.fileSystem(); - for (InputFile f : fs.inputFiles(fs.predicates().all())) { - BatchReportReader reader = new BatchReportReader(reportPublisher.getReportDir()); - int batchId = resourceCache.get(f).batchId(); - String language = f.language(); - if (reader.hasSyntaxHighlighting(batchId) || language == null) { - continue; - } - codeColorizers.toSyntaxHighlighting(f.file(), fs.encoding(), language, context.newHighlighting().onFile(f)); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizers.java b/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizers.java deleted file mode 100644 index 39a4a0b1c98..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/CodeColorizers.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.source; - -import com.google.common.collect.Lists; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.nio.charset.Charset; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.annotation.CheckForNull; -import org.apache.commons.io.input.BOMInputStream; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.sensor.highlighting.NewHighlighting; -import org.sonar.api.web.CodeColorizerFormat; -import org.sonar.colorizer.JavaTokenizers; -import org.sonar.colorizer.Tokenizer; - -/** - * Central point for sonar-colorizer extensions - */ -@BatchSide -public class CodeColorizers { - - private static final Logger LOG = LoggerFactory.getLogger(CodeColorizers.class); - - private final Map<String, CodeColorizerFormat> byLang; - - public CodeColorizers(List<CodeColorizerFormat> formats) { - byLang = new HashMap<>(); - for (CodeColorizerFormat format : formats) { - byLang.put(format.getLanguageKey(), format); - } - - LOG.debug("Code colorizer, supported languages: " + StringUtils.join(byLang.keySet(), ",")); - } - - /** - * Used when no plugin is defining some CodeColorizerFormat - */ - public CodeColorizers() { - this(Lists.<CodeColorizerFormat>newArrayList()); - } - - @CheckForNull - public void toSyntaxHighlighting(File file, Charset charset, String language, NewHighlighting highlighting) { - CodeColorizerFormat format = byLang.get(language); - List<Tokenizer> tokenizers; - if (format == null) { - // Workaround for Java test code since Java plugin only provides highlighting for main source and no colorizer - // TODO can be dropped when Java plugin embed its own CodeColorizerFormat of (better) provides highlighting for tests - // See SONARJAVA-830 - if ("java".equals(language)) { - tokenizers = JavaTokenizers.forHtml(); - } else { - return; - } - } else { - tokenizers = format.getTokenizers(); - } - try (Reader reader = new BufferedReader(new InputStreamReader(new BOMInputStream(new FileInputStream(file)), charset))) { - new HighlightingRenderer().render(reader, tokenizers, highlighting); - } catch (Exception e) { - LOG.warn("Unable to perform colorization of file " + file, e); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java deleted file mode 100644 index 80403efc298..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.source; - -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.sensor.highlighting.TypeOfText; -import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.api.source.Highlightable; - -/** - * @since 3.6 - */ -public class DefaultHighlightable implements Highlightable { - - private static final HighlightingBuilder NO_OP_BUILDER = new NoOpHighlightingBuilder(); - private final DefaultInputFile inputFile; - private final SensorStorage sensorStorage; - private final AnalysisMode analysisMode; - - public DefaultHighlightable(DefaultInputFile inputFile, SensorStorage sensorStorage, AnalysisMode analysisMode) { - this.inputFile = inputFile; - this.sensorStorage = sensorStorage; - this.analysisMode = analysisMode; - } - - @Override - public HighlightingBuilder newHighlighting() { - if (analysisMode.isIssues()) { - return NO_OP_BUILDER; - } - DefaultHighlighting defaultHighlighting = new DefaultHighlighting(sensorStorage); - defaultHighlighting.onFile(inputFile); - return new DefaultHighlightingBuilder(defaultHighlighting); - } - - private static final class NoOpHighlightingBuilder implements HighlightingBuilder { - @Override - public HighlightingBuilder highlight(int startOffset, int endOffset, String typeOfText) { - // Do nothing - return this; - } - - @Override - public void done() { - // Do nothing - } - } - - private static class DefaultHighlightingBuilder implements HighlightingBuilder { - - private final DefaultHighlighting defaultHighlighting; - - public DefaultHighlightingBuilder(DefaultHighlighting defaultHighlighting) { - this.defaultHighlighting = defaultHighlighting; - } - - @Override - public HighlightingBuilder highlight(int startOffset, int endOffset, String typeOfText) { - TypeOfText type = org.sonar.api.batch.sensor.highlighting.TypeOfText.forCssClass(typeOfText); - defaultHighlighting.highlight(startOffset, endOffset, type); - return this; - } - - @Override - public void done() { - defaultHighlighting.save(); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbol.java b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbol.java deleted file mode 100644 index a317bfb2de8..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbol.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.source; - -import com.google.common.base.Objects; -import org.sonar.api.batch.fs.TextRange; - -import java.io.Serializable; - -public class DefaultSymbol implements org.sonar.api.source.Symbol, Serializable { - - private TextRange range; - private int length; - - public DefaultSymbol(TextRange range, int length) { - this.range = range; - this.length = length; - } - - @Override - public int getDeclarationStartOffset() { - throw new UnsupportedOperationException("getDeclarationStartOffset"); - } - - @Override - public int getDeclarationEndOffset() { - throw new UnsupportedOperationException("getDeclarationEndOffset"); - } - - @Override - public String getFullyQualifiedName() { - throw new UnsupportedOperationException("getFullyQualifiedName"); - } - - public TextRange range() { - return range; - } - - public int getLength() { - return length; - } - - @Override - public String toString() { - return Objects.toStringHelper("Symbol") - .add("range", range) - .toString(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolTable.java b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolTable.java deleted file mode 100644 index f4ecc8e3761..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolTable.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.source; - -import java.util.ArrayList; -import java.util.Comparator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.source.Symbol; -import org.sonar.api.source.Symbolizable; - -public class DefaultSymbolTable implements Symbolizable.SymbolTable { - - private Map<Symbol, Set<TextRange>> referencesBySymbol; - - private DefaultSymbolTable(Map<Symbol, Set<TextRange>> referencesBySymbol) { - this.referencesBySymbol = referencesBySymbol; - } - - public Map<Symbol, Set<TextRange>> getReferencesBySymbol() { - return referencesBySymbol; - } - - @Override - public List<Symbol> symbols() { - List<Symbol> result = new ArrayList<>(); - for (Symbol symbol : referencesBySymbol.keySet()) { - result.add((Symbol) symbol); - } - return result; - } - - @Override - public List<Integer> references(Symbol symbol) { - throw new UnsupportedOperationException("references"); - } - - public static class Builder implements Symbolizable.SymbolTableBuilder { - - private static final class FakeSymbol implements Symbol { - @Override - public String getFullyQualifiedName() { - return null; - } - - @Override - public int getDeclarationStartOffset() { - return 0; - } - - @Override - public int getDeclarationEndOffset() { - return 0; - } - } - - private final Map<Symbol, Set<TextRange>> referencesBySymbol = new LinkedHashMap<>(); - private final DefaultInputFile inputFile; - - public Builder(DefaultInputFile inputFile) { - this.inputFile = inputFile; - } - - @Override - public Symbol newSymbol(int fromOffset, int toOffset) { - TextRange declarationRange = inputFile.newRange(fromOffset, toOffset); - DefaultSymbol symbol = new DefaultSymbol(declarationRange, toOffset - fromOffset); - referencesBySymbol.put(symbol, new TreeSet<>(new Comparator<TextRange>() { - @Override - public int compare(TextRange o1, TextRange o2) { - return o1.start().compareTo(o2.start()); - } - })); - return symbol; - } - - @Override - public void newReference(Symbol symbol, int fromOffset) { - newReference(symbol, fromOffset, fromOffset + ((DefaultSymbol) symbol).getLength()); - } - - @Override - public void newReference(Symbol symbol, int fromOffset, int toOffset) { - if (!referencesBySymbol.containsKey(symbol)) { - throw new UnsupportedOperationException("Cannot add reference to a symbol in another file"); - } - TextRange referenceRange = inputFile.newRange(fromOffset, toOffset); - - if (referenceRange.overlap(((DefaultSymbol) symbol).range())) { - throw new UnsupportedOperationException("Cannot add reference (" + fromOffset + ") overlapping " + symbol + " in " + inputFile.key()); - } - referencesBySymbol.get(symbol).add(referenceRange); - } - - @Override - public Symbolizable.SymbolTable build() { - return new DefaultSymbolTable(referencesBySymbol); - } - - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolizable.java b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolizable.java deleted file mode 100644 index cddeabb9683..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolizable.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.source; - -import java.util.Collections; -import java.util.List; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.source.Symbol; -import org.sonar.api.source.Symbolizable; -import org.sonar.batch.sensor.DefaultSensorStorage; - -public class DefaultSymbolizable implements Symbolizable { - - private static final NoOpSymbolTableBuilder NO_OP_SYMBOL_TABLE_BUILDER = new NoOpSymbolTableBuilder(); - private static final NoOpSymbolTable NO_OP_SYMBOL_TABLE = new NoOpSymbolTable(); - private static final NoOpSymbol NO_OP_SYMBOL = new NoOpSymbol(); - - private static final class NoOpSymbolTableBuilder implements SymbolTableBuilder { - @Override - public Symbol newSymbol(int fromOffset, int toOffset) { - return NO_OP_SYMBOL; - } - - @Override - public void newReference(Symbol symbol, int fromOffset) { - // Do nothing - } - - @Override - public void newReference(Symbol symbol, int fromOffset, int toOffset) { - // Do nothing - } - - @Override - public SymbolTable build() { - return NO_OP_SYMBOL_TABLE; - } - } - - private static final class NoOpSymbolTable implements SymbolTable { - @Override - public List<Symbol> symbols() { - return Collections.emptyList(); - } - - @Override - public List<Integer> references(Symbol symbol) { - return Collections.emptyList(); - } - } - - private static final class NoOpSymbol implements Symbol { - @Override - public String getFullyQualifiedName() { - return null; - } - - @Override - public int getDeclarationStartOffset() { - return 0; - } - - @Override - public int getDeclarationEndOffset() { - return 0; - } - } - - private final DefaultInputFile inputFile; - private final DefaultSensorStorage sensorStorage; - private final AnalysisMode analysisMode; - - public DefaultSymbolizable(DefaultInputFile inputFile, DefaultSensorStorage sensorStorage, AnalysisMode analysisMode) { - this.inputFile = inputFile; - this.sensorStorage = sensorStorage; - this.analysisMode = analysisMode; - } - - @Override - public SymbolTableBuilder newSymbolTableBuilder() { - if (analysisMode.isIssues()) { - return NO_OP_SYMBOL_TABLE_BUILDER; - } - return new DefaultSymbolTable.Builder(inputFile); - } - - @Override - public void setSymbolTable(SymbolTable symbolTable) { - if (analysisMode.isIssues()) { - // No need for symbols in issues mode - return; - } - sensorStorage.store(inputFile, ((DefaultSymbolTable) symbolTable).getReferencesBySymbol()); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/HighlightableBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/source/HighlightableBuilder.java deleted file mode 100644 index d911516b984..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/HighlightableBuilder.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.source; - -import javax.annotation.CheckForNull; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.api.source.Highlightable; -import org.sonar.batch.deprecated.perspectives.PerspectiveBuilder; -import org.sonar.batch.index.BatchComponent; - -public class HighlightableBuilder extends PerspectiveBuilder<Highlightable> { - - private final SensorStorage sensorStorage; - private final AnalysisMode analysisMode; - - public HighlightableBuilder(SensorStorage sensorStorage, AnalysisMode analysisMode) { - super(Highlightable.class); - this.sensorStorage = sensorStorage; - this.analysisMode = analysisMode; - } - - @CheckForNull - @Override - public Highlightable loadPerspective(Class<Highlightable> perspectiveClass, BatchComponent component) { - if (component.isFile()) { - InputFile path = (InputFile) component.inputComponent(); - return new DefaultHighlightable((DefaultInputFile) path, sensorStorage, analysisMode); - } - return null; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/HighlightingCodeBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/source/HighlightingCodeBuilder.java deleted file mode 100644 index b807ee2d5e2..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/HighlightingCodeBuilder.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.source; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.sensor.highlighting.NewHighlighting; -import org.sonar.api.batch.sensor.highlighting.TypeOfText; -import org.sonar.colorizer.HtmlCodeBuilder; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class HighlightingCodeBuilder extends HtmlCodeBuilder { - - private static final Logger LOG = LoggerFactory.getLogger(HighlightingCodeBuilder.class); - - private int currentOffset = 0; - private static final Pattern START_TAG_PATTERN = Pattern.compile("<span class=\"(.+)\">"); - private static final Pattern END_TAG_PATTERN = Pattern.compile("</span>"); - private int startOffset = -1; - private String cssClass; - private final NewHighlighting highlighting; - - public HighlightingCodeBuilder(NewHighlighting highlighting) { - this.highlighting = highlighting; - } - - @Override - public Appendable append(CharSequence csq) { - for (int i = 0; i < csq.length(); i++) { - append(csq.charAt(i)); - } - return this; - } - - @Override - public Appendable append(char c) { - currentOffset++; - return this; - } - - @Override - public void appendWithoutTransforming(String htmlTag) { - if (startOffset == -1) { - Matcher startMatcher = START_TAG_PATTERN.matcher(htmlTag); - if (startMatcher.matches()) { - startOffset = currentOffset; - cssClass = startMatcher.group(1); - } else { - LOG.warn("Expected to match highlighting start html tag but was: " + htmlTag); - } - } else { - Matcher endMatcher = END_TAG_PATTERN.matcher(htmlTag); - if (endMatcher.matches()) { - highlighting.highlight(startOffset, currentOffset, TypeOfText.forCssClass(cssClass)); - startOffset = -1; - } else { - LOG.warn("Expected to match highlighting end html tag but was: " + htmlTag); - } - } - } - - @Override - public String toString() { - throw new UnsupportedOperationException(); - } - - @Override - public StringBuilder getColorizedCode() { - throw new UnsupportedOperationException(); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/HighlightingRenderer.java b/sonar-batch/src/main/java/org/sonar/batch/source/HighlightingRenderer.java deleted file mode 100644 index f4b9f3d399f..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/HighlightingRenderer.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.source; - -import org.sonar.api.batch.sensor.highlighting.NewHighlighting; -import org.sonar.channel.Channel; -import org.sonar.channel.CodeReader; -import org.sonar.colorizer.HtmlCodeBuilder; -import org.sonar.colorizer.TokenizerDispatcher; - -import java.io.Reader; -import java.util.ArrayList; -import java.util.List; - -public class HighlightingRenderer { - - public void render(Reader code, List<? extends Channel<HtmlCodeBuilder>> tokenizers, NewHighlighting highlighting) { - List<Channel<HtmlCodeBuilder>> allTokenizers = new ArrayList<>(); - HighlightingCodeBuilder codeBuilder = new HighlightingCodeBuilder(highlighting); - - allTokenizers.addAll(tokenizers); - - new TokenizerDispatcher(allTokenizers).colorize(new CodeReader(code), codeBuilder); - highlighting.save(); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/LinesSensor.java b/sonar-batch/src/main/java/org/sonar/batch/source/LinesSensor.java deleted file mode 100644 index d27c90953a0..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/LinesSensor.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.source; - -import org.sonar.api.batch.Phase; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputFile.Type; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.sensor.Sensor; -import org.sonar.api.batch.sensor.SensorContext; -import org.sonar.api.batch.sensor.SensorDescriptor; -import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; -import org.sonar.api.measures.CoreMetrics; - -@Phase(name = Phase.Name.PRE) -public final class LinesSensor implements Sensor { - - @Override - public void describe(SensorDescriptor descriptor) { - descriptor.name("Lines Sensor"); - } - - @Override - public void execute(final SensorContext context) { - FileSystem fs = context.fileSystem(); - for (InputFile f : fs.inputFiles(fs.predicates().hasType(Type.MAIN))) { - ((DefaultMeasure<Integer>) context.<Integer>newMeasure() - .on(f) - .forMetric(CoreMetrics.LINES) - .withValue(f.lines())) - .setFromCore() - .save(); - if (f.language() == null) { - // As an approximation for files with no language plugin we consider every non blank line as ncloc - ((DefaultMeasure<Integer>) context.<Integer>newMeasure() - .on(f) - .forMetric(CoreMetrics.NCLOC) - .withValue(((DefaultInputFile) f).nonBlankLines())) - .save(); - // No test and no coverage on those files - ((DefaultMeasure<Integer>) context.<Integer>newMeasure() - .on(f) - .forMetric(CoreMetrics.LINES_TO_COVER) - .withValue(0)) - .save(); - } - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/SymbolizableBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/source/SymbolizableBuilder.java deleted file mode 100644 index fda6c4c7166..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/SymbolizableBuilder.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.source; - -import javax.annotation.CheckForNull; -import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.source.Symbolizable; -import org.sonar.batch.deprecated.perspectives.PerspectiveBuilder; -import org.sonar.batch.index.BatchComponent; -import org.sonar.batch.sensor.DefaultSensorStorage; - -public class SymbolizableBuilder extends PerspectiveBuilder<Symbolizable> { - - private final DefaultSensorStorage sensorStorage; - private final AnalysisMode analysisMode; - - public SymbolizableBuilder(DefaultSensorStorage sensorStorage, AnalysisMode analysisMode) { - super(Symbolizable.class); - this.sensorStorage = sensorStorage; - this.analysisMode = analysisMode; - } - - @CheckForNull - @Override - public Symbolizable loadPerspective(Class<Symbolizable> perspectiveClass, BatchComponent component) { - if (component.isFile()) { - InputFile path = (InputFile) component.inputComponent(); - return new DefaultSymbolizable((DefaultInputFile) path, sensorStorage, analysisMode); - } - return null; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/ZeroCoverageSensor.java b/sonar-batch/src/main/java/org/sonar/batch/source/ZeroCoverageSensor.java deleted file mode 100644 index f211050dabc..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/ZeroCoverageSensor.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.source; - -import com.google.common.base.Function; -import com.google.common.collect.Sets; -import java.util.Map; -import java.util.Set; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.batch.Phase; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputFile.Type; -import org.sonar.api.batch.measure.Metric; -import org.sonar.api.batch.sensor.Sensor; -import org.sonar.api.batch.sensor.SensorContext; -import org.sonar.api.batch.sensor.SensorDescriptor; -import org.sonar.api.batch.sensor.coverage.CoverageType; -import org.sonar.api.batch.sensor.coverage.NewCoverage; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; -import org.sonar.api.utils.KeyValueFormat; -import org.sonar.batch.scan.measure.MeasureCache; - -import static com.google.common.collect.Iterables.concat; -import static com.google.common.collect.Iterables.transform; -import static com.google.common.collect.Sets.newHashSet; - -@Phase(name = Phase.Name.POST) -public final class ZeroCoverageSensor implements Sensor { - - private static final class MeasureToMetricKey implements Function<Measure, String> { - @Override - public String apply(Measure input) { - return input.getMetricKey(); - } - } - - private static final class MetricToKey implements Function<Metric, String> { - @Override - public String apply(Metric input) { - return input.key(); - } - } - - private final MeasureCache measureCache; - - public ZeroCoverageSensor(MeasureCache measureCache) { - this.measureCache = measureCache; - } - - @Override - public void describe(SensorDescriptor descriptor) { - descriptor.name("Zero Coverage Sensor"); - } - - @Override - public void execute(final SensorContext context) { - FileSystem fs = context.fileSystem(); - for (InputFile f : fs.inputFiles(fs.predicates().hasType(Type.MAIN))) { - if (!isCoverageMeasuresAlreadyDefined(f)) { - Measure execLines = measureCache.byMetric(f.key(), CoreMetrics.EXECUTABLE_LINES_DATA_KEY); - if (execLines != null) { - storeZeroCoverageForEachExecutableLine(context, f, execLines); - } - - } - } - } - - private static void storeZeroCoverageForEachExecutableLine(final SensorContext context, InputFile f, Measure execLines) { - NewCoverage newCoverage = context.newCoverage().ofType(CoverageType.UNIT).onFile(f); - Map<Integer, String> lineMeasures = KeyValueFormat.parseIntString((String) execLines.value()); - for (Map.Entry<Integer, String> lineMeasure : lineMeasures.entrySet()) { - int lineIdx = lineMeasure.getKey(); - if (lineIdx <= f.lines()) { - String value = lineMeasure.getValue(); - if (StringUtils.isNotEmpty(value) && Integer.parseInt(value) > 0) { - newCoverage.lineHits(lineIdx, 0); - } - } - } - newCoverage.save(); - } - - private boolean isCoverageMeasuresAlreadyDefined(InputFile f) { - Set<String> metricKeys = newHashSet(transform(measureCache.byComponentKey(f.key()), new MeasureToMetricKey())); - Function<Metric, String> metricToKey = new MetricToKey(); - Set<String> allCoverageMetricKeys = newHashSet(concat(transform(CoverageType.UNIT.allMetrics(), metricToKey), - transform(CoverageType.IT.allMetrics(), metricToKey), - transform(CoverageType.OVERALL.allMetrics(), metricToKey))); - return !Sets.intersection(metricKeys, allCoverageMetricKeys).isEmpty(); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/source/package-info.java deleted file mode 100644 index c33229b0ab2..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/source/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.source; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/task/ListTask.java b/sonar-batch/src/main/java/org/sonar/batch/task/ListTask.java deleted file mode 100644 index 7c535674d82..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/task/ListTask.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.task; - -import org.sonar.api.task.Task; -import org.sonar.api.task.TaskDefinition; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - -public class ListTask implements Task { - - private static final Logger LOG = Loggers.get(ListTask.class); - - public static final String KEY = "list"; - - public static final TaskDefinition DEFINITION = TaskDefinition.builder() - .key(KEY) - .description("List available tasks") - .taskClass(ListTask.class) - .build(); - - private final Tasks tasks; - - public ListTask(Tasks tasks) { - this.tasks = tasks; - } - - @Override - public void execute() { - StringBuilder sb = new StringBuilder(); - sb.append("\nAvailable tasks:\n"); - for (TaskDefinition def : tasks.definitions()) { - sb.append(" - " + def.key() + ": " + def.description() + "\n"); - } - sb.append("\n"); - LOG.info(sb.toString()); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/task/ScanTask.java b/sonar-batch/src/main/java/org/sonar/batch/task/ScanTask.java deleted file mode 100644 index c5ba59c3d37..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/task/ScanTask.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.task; - -import javax.annotation.CheckForNull; -import org.sonar.api.CoreProperties; -import org.sonar.api.task.Task; -import org.sonar.api.task.TaskDefinition; -import org.sonar.batch.analysis.AnalysisProperties; -import org.sonar.batch.analysis.DefaultAnalysisMode; -import org.sonar.batch.bootstrap.GlobalProperties; -import org.sonar.batch.cache.ProjectSyncContainer; -import org.sonar.batch.scan.ProjectScanContainer; -import org.sonar.core.platform.ComponentContainer; - -public class ScanTask implements Task { - public static final TaskDefinition DEFINITION = TaskDefinition.builder() - .description("Scan project") - .key(CoreProperties.SCAN_TASK) - .taskClass(ScanTask.class) - .build(); - - private final ComponentContainer taskContainer; - private final TaskProperties taskProps; - - public ScanTask(TaskContainer taskContainer, TaskProperties taskProps) { - this.taskContainer = taskContainer; - this.taskProps = taskProps; - } - - @Override - public void execute() { - AnalysisProperties props = new AnalysisProperties(taskProps.properties(), taskProps.property(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)); - if (isIssuesMode(props)) { - String projectKey = getProjectKeyWithBranch(props); - new ProjectSyncContainer(taskContainer, projectKey, false).execute(); - } - new ProjectScanContainer(taskContainer, props).execute(); - } - - @CheckForNull - private static String getProjectKeyWithBranch(AnalysisProperties props) { - String projectKey = props.property(CoreProperties.PROJECT_KEY_PROPERTY); - if (projectKey != null && props.property(CoreProperties.PROJECT_BRANCH_PROPERTY) != null) { - projectKey = projectKey + ":" + props.property(CoreProperties.PROJECT_BRANCH_PROPERTY); - } - return projectKey; - } - - private boolean isIssuesMode(AnalysisProperties props) { - DefaultAnalysisMode mode = new DefaultAnalysisMode(taskContainer.getComponentByType(GlobalProperties.class), props); - return mode.isIssues(); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/task/TaskContainer.java b/sonar-batch/src/main/java/org/sonar/batch/task/TaskContainer.java deleted file mode 100644 index eee91f63055..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/task/TaskContainer.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.task; - -import java.util.Map; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.InstantiationStrategy; -import org.sonar.api.config.EmailSettings; -import org.sonar.api.task.Task; -import org.sonar.api.task.TaskDefinition; -import org.sonar.api.utils.MessageException; -import org.sonar.batch.bootstrap.ExtensionInstaller; -import org.sonar.batch.bootstrap.ExtensionMatcher; -import org.sonar.batch.bootstrap.ExtensionUtils; -import org.sonar.batch.bootstrap.GlobalProperties; -import org.sonar.core.platform.ComponentContainer; - -public class TaskContainer extends ComponentContainer { - - private final Map<String, String> taskProperties; - private final Object[] components; - - public TaskContainer(ComponentContainer parent, Map<String, String> taskProperties, Object... components) { - super(parent); - this.taskProperties = taskProperties; - this.components = components; - } - - @Override - protected void doBeforeStart() { - addTaskExtensions(); - addCoreComponents(); - for (Object component : components) { - add(component); - } - } - - private void addCoreComponents() { - add(new TaskProperties(taskProperties, getParent().getComponentByType(GlobalProperties.class).property(CoreProperties.ENCRYPTION_SECRET_KEY_PATH))); - add(EmailSettings.class); - } - - private void addTaskExtensions() { - getComponentByType(ExtensionInstaller.class).install(this, new TaskExtensionFilter()); - } - - static class TaskExtensionFilter implements ExtensionMatcher { - @Override - public boolean accept(Object extension) { - return ExtensionUtils.isBatchSide(extension) - && ExtensionUtils.isInstantiationStrategy(extension, InstantiationStrategy.PER_TASK); - } - } - - @Override - public void doAfterStart() { - // default value is declared in CorePlugin - String taskKey = StringUtils.defaultIfEmpty(taskProperties.get(CoreProperties.TASK), CoreProperties.SCAN_TASK); - // Release memory - taskProperties.clear(); - - TaskDefinition def = getComponentByType(Tasks.class).definition(taskKey); - if (def == null) { - throw MessageException.of("Task '" + taskKey + "' does not exist. Please use '" + ListTask.KEY + "' task to see all available tasks."); - } - Task task = getComponentByType(def.taskClass()); - if (task != null) { - task.execute(); - } else { - throw new IllegalStateException("Task " + taskKey + " is badly defined"); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/task/TaskProperties.java b/sonar-batch/src/main/java/org/sonar/batch/task/TaskProperties.java deleted file mode 100644 index b8470ce4d00..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/task/TaskProperties.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.task; - -import java.util.Map; -import javax.annotation.Nullable; -import org.sonar.batch.bootstrap.UserProperties; - -/** - * Batch properties that are specific to a task (for example - * coming from sonar-project.properties). - */ -public class TaskProperties extends UserProperties { - - public TaskProperties(Map<String, String> properties, @Nullable String pathToSecretKey) { - super(properties, pathToSecretKey); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/task/Tasks.java b/sonar-batch/src/main/java/org/sonar/batch/task/Tasks.java deleted file mode 100644 index ee68b62b1db..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/task/Tasks.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.task; - -import com.google.common.collect.ImmutableSortedMap; -import com.google.common.collect.Maps; -import java.util.Collection; -import java.util.Map; -import java.util.SortedMap; -import org.sonar.api.batch.BatchSide; -import org.sonar.api.batch.InstantiationStrategy; -import org.sonar.api.task.Task; -import org.sonar.api.task.TaskDefinition; - -@BatchSide -@InstantiationStrategy(InstantiationStrategy.PER_TASK) -public class Tasks { - - private final SortedMap<String, TaskDefinition> byKey; - - public Tasks(TaskDefinition[] definitions) { - SortedMap<String, TaskDefinition> map = Maps.newTreeMap(); - for (TaskDefinition definition : definitions) { - if (map.containsKey(definition.key())) { - throw new IllegalStateException("Task '" + definition.key() + "' is declared twice"); - } - map.put(definition.key(), definition); - } - this.byKey = ImmutableSortedMap.copyOf(map); - } - - public TaskDefinition definition(String taskKey) { - return byKey.get(taskKey); - } - - public Collection<TaskDefinition> definitions() { - return byKey.values(); - } - - /** - * Perform validation of task definitions - */ - public void start() { - checkDuplicatedClasses(); - } - - private void checkDuplicatedClasses() { - Map<Class<? extends Task>, TaskDefinition> byClass = Maps.newHashMap(); - for (TaskDefinition def : definitions()) { - TaskDefinition other = byClass.get(def.taskClass()); - if (other == null) { - byClass.put(def.taskClass(), def); - } else { - throw new IllegalStateException("Task '" + def.taskClass().getName() + "' is defined twice: first by '" + other.key() + "' and then by '" + def.key() + "'"); - } - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/task/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/task/package-info.java deleted file mode 100644 index 5787c1e27c2..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/task/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.task; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/test/DefaultCoverageBlock.java b/sonar-batch/src/main/java/org/sonar/batch/test/DefaultCoverageBlock.java deleted file mode 100644 index 52c3ca7a76f..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/test/DefaultCoverageBlock.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.test; - -import java.util.List; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.test.CoverageBlock; -import org.sonar.api.test.TestCase; -import org.sonar.api.test.Testable; - -public class DefaultCoverageBlock implements CoverageBlock { - - private final TestCase testCase; - private final DefaultInputFile testable; - private final List<Integer> lines; - - public DefaultCoverageBlock(TestCase testCase, DefaultInputFile testable, List<Integer> lines) { - this.testCase = testCase; - this.testable = testable; - this.lines = lines; - } - - @Override - public TestCase testCase() { - return testCase; - } - - @Override - public Testable testable() { - return new DefaultTestable(testable); - } - - @Override - public List<Integer> lines() { - return lines; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/test/DefaultTestCase.java b/sonar-batch/src/main/java/org/sonar/batch/test/DefaultTestCase.java deleted file mode 100644 index 652d11a0ddb..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/test/DefaultTestCase.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.test; - -import com.google.common.base.Preconditions; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import javax.annotation.Nullable; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputFile.Type; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.test.CoverageBlock; -import org.sonar.api.test.MutableTestCase; -import org.sonar.api.test.TestPlan; -import org.sonar.api.test.Testable; -import org.sonar.api.test.exception.CoverageAlreadyExistsException; -import org.sonar.api.test.exception.IllegalDurationException; - -public class DefaultTestCase implements MutableTestCase { - - private final DefaultTestPlan testPlan; - private String type; - private Long durationInMs; - private Status status; - private String name; - private String message; - private String stackTrace; - private Map<DefaultInputFile, CoverageBlock> coverageBlocksByTestedFile = new LinkedHashMap<>(); - - public DefaultTestCase(DefaultTestPlan testPlan) { - this.testPlan = testPlan; - } - - @Override - public String type() { - return type; - } - - @Override - public MutableTestCase setType(@Nullable String s) { - this.type = s; - return this; - } - - @Override - public Long durationInMs() { - return durationInMs; - } - - @Override - public MutableTestCase setDurationInMs(@Nullable Long l) { - if (l != null && l < 0) { - throw new IllegalDurationException("Test duration must be positive (got: " + l + ")"); - } - this.durationInMs = l; - return this; - } - - @Override - public Status status() { - return status; - } - - @Override - public MutableTestCase setStatus(@Nullable Status s) { - this.status = s; - return this; - } - - @Override - public String name() { - return name; - } - - public MutableTestCase setName(String s) { - this.name = s; - return this; - } - - @Override - public String message() { - return message; - } - - @Override - public MutableTestCase setMessage(String s) { - this.message = s; - return this; - } - - @Override - public String stackTrace() { - return stackTrace; - } - - @Override - public MutableTestCase setStackTrace(String s) { - this.stackTrace = s; - return this; - } - - @Override - public MutableTestCase setCoverageBlock(Testable testable, List<Integer> lines) { - DefaultInputFile coveredFile = ((DefaultTestable) testable).inputFile(); - return setCoverageBlock(coveredFile, lines); - } - - @Override - public MutableTestCase setCoverageBlock(InputFile mainFile, List<Integer> lines) { - Preconditions.checkArgument(mainFile.type() == Type.MAIN, "Test file can only cover a main file"); - DefaultInputFile coveredFile = (DefaultInputFile) mainFile; - if (coverageBlocksByTestedFile.containsKey(coveredFile)) { - throw new CoverageAlreadyExistsException("The link between " + name() + " and " + coveredFile.key() + " already exists"); - } - coverageBlocksByTestedFile.put(coveredFile, new DefaultCoverageBlock(this, coveredFile, lines)); - return this; - } - - @Override - public TestPlan testPlan() { - return testPlan; - } - - @Override - public boolean doesCover() { - return !coverageBlocksByTestedFile.isEmpty(); - } - - @Override - public int countCoveredLines() { - throw new UnsupportedOperationException("Not supported since SQ 5.2"); - } - - @Override - public Iterable<CoverageBlock> coverageBlocks() { - return coverageBlocksByTestedFile.values(); - } - - @Override - public CoverageBlock coverageBlock(final Testable testable) { - DefaultInputFile coveredFile = ((DefaultTestable) testable).inputFile(); - return coverageBlocksByTestedFile.get(coveredFile); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/test/DefaultTestPlan.java b/sonar-batch/src/main/java/org/sonar/batch/test/DefaultTestPlan.java deleted file mode 100644 index 85b26082424..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/test/DefaultTestPlan.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.test; - -import com.google.common.collect.Lists; -import java.util.ArrayList; -import java.util.List; -import javax.annotation.CheckForNull; -import org.sonar.api.test.MutableTestCase; -import org.sonar.api.test.MutableTestPlan; - -public class DefaultTestPlan implements MutableTestPlan { - private List<MutableTestCase> testCases = new ArrayList<>(); - - @Override - @CheckForNull - public Iterable<MutableTestCase> testCasesByName(String name) { - List<MutableTestCase> result = Lists.newArrayList(); - for (MutableTestCase testCase : testCases()) { - if (name.equals(testCase.name())) { - result.add(testCase); - } - } - return result; - } - - @Override - public MutableTestCase addTestCase(String name) { - DefaultTestCase testCase = new DefaultTestCase(this); - testCase.setName(name); - testCases.add(testCase); - return testCase; - } - - @Override - public Iterable<MutableTestCase> testCases() { - return testCases; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/test/DefaultTestable.java b/sonar-batch/src/main/java/org/sonar/batch/test/DefaultTestable.java deleted file mode 100644 index c672253920e..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/test/DefaultTestable.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.test; - -import java.util.List; -import java.util.Map; -import java.util.SortedSet; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.test.CoverageBlock; -import org.sonar.api.test.MutableTestable; -import org.sonar.api.test.TestCase; - -public class DefaultTestable implements MutableTestable { - - private final DefaultInputFile inputFile; - - public DefaultTestable(DefaultInputFile inputFile) { - this.inputFile = inputFile; - } - - public DefaultInputFile inputFile() { - return inputFile; - } - - @Override - public List<TestCase> testCases() { - throw unsupported(); - } - - @Override - public TestCase testCaseByName(final String name) { - throw unsupported(); - } - - @Override - public int countTestCasesOfLine(Integer line) { - throw unsupported(); - } - - @Override - public Map<Integer, Integer> testCasesByLines() { - throw unsupported(); - } - - @Override - public List<TestCase> testCasesOfLine(int line) { - throw unsupported(); - } - - @Override - public SortedSet<Integer> testedLines() { - throw unsupported(); - } - - @Override - public CoverageBlock coverageBlock(final TestCase testCase) { - throw unsupported(); - } - - @Override - public Iterable<CoverageBlock> coverageBlocks() { - throw unsupported(); - } - - private static UnsupportedOperationException unsupported() { - return new UnsupportedOperationException("No more available since SQ 5.2"); - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/test/TestPlanBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/test/TestPlanBuilder.java deleted file mode 100644 index b3bab1ee6f5..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/test/TestPlanBuilder.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.test; - -import java.util.HashMap; -import java.util.Map; -import javax.annotation.CheckForNull; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputFile.Type; -import org.sonar.api.test.MutableTestPlan; -import org.sonar.batch.deprecated.perspectives.PerspectiveBuilder; -import org.sonar.batch.index.BatchComponent; - -public class TestPlanBuilder extends PerspectiveBuilder<MutableTestPlan> { - - private Map<InputFile, DefaultTestPlan> testPlanByFile = new HashMap<>(); - - public TestPlanBuilder() { - super(MutableTestPlan.class); - } - - @CheckForNull - @Override - public MutableTestPlan loadPerspective(Class<MutableTestPlan> perspectiveClass, BatchComponent component) { - if (component.isFile()) { - InputFile inputFile = (InputFile) component.inputComponent(); - if (inputFile.type() == Type.TEST) { - if (!testPlanByFile.containsKey(inputFile)) { - testPlanByFile.put(inputFile, new DefaultTestPlan()); - } - return testPlanByFile.get(inputFile); - } - } - return null; - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/test/TestableBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/test/TestableBuilder.java deleted file mode 100644 index 261b0b15a1d..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/test/TestableBuilder.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.test; - -import javax.annotation.CheckForNull; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputFile.Type; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.test.MutableTestable; -import org.sonar.batch.deprecated.perspectives.PerspectiveBuilder; -import org.sonar.batch.index.BatchComponent; - -public class TestableBuilder extends PerspectiveBuilder<MutableTestable> { - - public TestableBuilder() { - super(MutableTestable.class); - } - - @CheckForNull - @Override - public MutableTestable loadPerspective(Class<MutableTestable> perspectiveClass, BatchComponent component) { - if (component.isFile()) { - InputFile inputFile = (InputFile) component.inputComponent(); - if (inputFile.type() == Type.MAIN) { - return new DefaultTestable((DefaultInputFile) inputFile); - } - } - return null; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/test/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/test/package-info.java deleted file mode 100644 index cb0a2f6c3e7..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/test/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.batch.test; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/main/java/org/sonar/batch/util/BatchUtils.java b/sonar-batch/src/main/java/org/sonar/batch/util/BatchUtils.java deleted file mode 100644 index 81eed874378..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/util/BatchUtils.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.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.CheckForNull; -import javax.annotation.Nullable; -import org.apache.commons.io.IOUtils; -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() { - } - - /** - * Clean provided string to remove chars that are not valid as file name. - * @param projectKey e.g. my:file - */ - public static String cleanKeyForFilename(String projectKey) { - 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"); - - } catch (UnsupportedEncodingException e) { - throw new IllegalStateException("Encoding not supported", e); - } - } - - public static String describe(Object o) { - try { - if (o.getClass().getMethod("toString").getDeclaringClass() != Object.class) { - return o.toString(); - } - } catch (Exception e) { - // fallback - } - - return o.getClass().getName(); - } - - @CheckForNull - public static String getServerVersion() { - InputStream is = BatchUtils.class.getResourceAsStream("/sq-version.txt"); - if (is == null) { - LOG.warn("Failed to get SQ version"); - return null; - } - try (BufferedReader br = IOUtils.toBufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) { - return br.readLine(); - } catch (IOException e) { - LOG.warn("Failed to get SQ version", e); - return null; - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/util/ProgressReport.java b/sonar-batch/src/main/java/org/sonar/batch/util/ProgressReport.java deleted file mode 100644 index f2ea0406a01..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/util/ProgressReport.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.util; - -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; - -public class ProgressReport implements Runnable { - - private static final Logger LOG = Loggers.get(ProgressReport.class); - private final long period; - private String message = ""; - private final Thread thread; - private String stopMessage = ""; - - public ProgressReport(String threadName, long period) { - this.period = period; - thread = new Thread(this); - thread.setName(threadName); - thread.setDaemon(true); - } - - @Override - public void run() { - while (!Thread.interrupted()) { - try { - Thread.sleep(period); - log(message); - } catch (InterruptedException e) { - break; - } - } - log(stopMessage); - } - - public void start(String startMessage) { - log(startMessage); - thread.start(); - } - - public void message(String message) { - this.message = message; - } - - public void stop(String stopMessage) { - this.stopMessage = stopMessage; - thread.interrupt(); - try { - thread.join(); - } catch (InterruptedException e) { - // Ignore - } - } - - private static void log(String message) { - synchronized (LOG) { - LOG.info(message); - LOG.notifyAll(); - } - } - -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/util/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/util/package-info.java deleted file mode 100644 index 42cf7aa9450..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/util/package-info.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.batch.util; - |