diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-01-27 16:48:54 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-01-27 16:48:54 +0100 |
commit | f5ff50a3a00fac3e9d7237b653771b812b01aa3e (patch) | |
tree | 1c2197c342d51351b6991982159a0be8773fa0e2 /plugins/sonar-dbcleaner-plugin/src/main | |
parent | 0a77863d625d55d3ae67f21aa6dce53903cd83d2 (diff) | |
download | sonarqube-f5ff50a3a00fac3e9d7237b653771b812b01aa3e.tar.gz sonarqube-f5ff50a3a00fac3e9d7237b653771b812b01aa3e.zip |
SONAR-2757 extract purge task into a dedicated component
Diffstat (limited to 'plugins/sonar-dbcleaner-plugin/src/main')
4 files changed, 169 insertions, 98 deletions
diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java index ca35757b6e0..0155f2c3187 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DbCleanerPlugin.java @@ -51,7 +51,6 @@ import java.util.List; public final class DbCleanerPlugin extends SonarPlugin { public List getExtensions() { - return Arrays.asList( - DefaultPeriodCleaner.class, ProjectPurgePostJob.class); + return Arrays.asList(DefaultPeriodCleaner.class, DefaultPurgeTask.class, ProjectPurgePostJob.class); } } diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java new file mode 100644 index 00000000000..0027fcc3bf4 --- /dev/null +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java @@ -0,0 +1,133 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.dbcleaner; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.Properties; +import org.sonar.api.Property; +import org.sonar.api.config.Settings; +import org.sonar.api.resources.Scopes; +import org.sonar.core.purge.PurgeDao; +import org.sonar.core.purge.PurgeSnapshotQuery; +import org.sonar.plugins.dbcleaner.api.DbCleanerConstants; +import org.sonar.plugins.dbcleaner.api.PurgeTask; +import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner; + +/** + * @since 2.14 + */ +@Properties({ + @Property( + key = DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY, + defaultValue = "false", + name = "Clean history data of directories/packages") +}) +public class DefaultPurgeTask implements PurgeTask { + private static final Logger LOG = LoggerFactory.getLogger(ProjectPurgePostJob.class); + + private PurgeDao purgeDao; + private Settings settings; + private DefaultPeriodCleaner periodCleaner; + + public DefaultPurgeTask(PurgeDao purgeDao, Settings settings, DefaultPeriodCleaner periodCleaner) { + this.purgeDao = purgeDao; + this.settings = settings; + this.periodCleaner = periodCleaner; + } + + public PurgeTask purgeProject(long projectId) { + cleanHistoricalData(projectId); + deleteAbortedBuilds(projectId); + deleteFileHistory(projectId); + if (settings.getBoolean(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY)) { + deleteDirectoryHistory(projectId); + } + purgeProjectResources(projectId); + return this; + } + + public PurgeTask deleteProject(long projectId) { + purgeDao.deleteProject(projectId); + return this; + } + + private void cleanHistoricalData(long projectId) { + try { + LOG.debug("Clean project historical data [id=" + projectId + "]"); + periodCleaner.purge(projectId); + } catch (Exception e) { + // purge errors must no fail the batch + LOG.error("Fail to clean project historical data [id=" + projectId + "]", e); + } + } + + private void purgeProjectResources(long projectId) { + try { + LOG.debug("Purge project [id=" + projectId + "]"); + purgeDao.purgeProject(projectId); + } catch (Exception e) { + // purge errors must no fail the batch + LOG.error("Fail to purge project [id=" + projectId + "]", e); + } + } + + private void deleteDirectoryHistory(long projectId) { + try { + LOG.debug("Delete historical data of directories [id=" + projectId + "]"); + PurgeSnapshotQuery query = PurgeSnapshotQuery.create() + .setRootProjectId(projectId) + .setIslast(false) + .setScopes(new String[]{Scopes.DIRECTORY}); + purgeDao.deleteSnapshots(query); + } catch (Exception e) { + // purge errors must no fail the batch + LOG.error("Fail to delete historical data of directories [id=" + projectId + "]", e); + } + } + + private void deleteFileHistory(long projectId) { + try { + LOG.debug("Delete historical data of files [id=" + projectId + "]"); + PurgeSnapshotQuery query = PurgeSnapshotQuery.create() + .setRootProjectId(projectId) + .setIslast(false) + .setScopes(new String[]{Scopes.FILE}); + purgeDao.deleteSnapshots(query); + } catch (Exception e) { + // purge errors must no fail the batch + LOG.error("Fail to delete historical data of files [id=" + projectId + "]", e); + } + } + + private void deleteAbortedBuilds(long projectId) { + try { + LOG.debug("Delete aborted builds [id=" + projectId + "]"); + PurgeSnapshotQuery query = PurgeSnapshotQuery.create() + .setRootProjectId(projectId) + .setIslast(false) + .setStatus(new String[]{"U"}); + purgeDao.deleteSnapshots(query); + } catch (Exception e) { + // purge errors must no fail the batch + LOG.error("Fail to delete historical aborted builds [id=" + projectId + "]", e); + } + } +} diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJob.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJob.java index 4bbc9d38807..ba8150cdbd6 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJob.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/ProjectPurgePostJob.java @@ -19,113 +19,22 @@ */ package org.sonar.plugins.dbcleaner; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.Properties; -import org.sonar.api.Property; import org.sonar.api.batch.PostJob; import org.sonar.api.batch.SensorContext; -import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; -import org.sonar.api.resources.Scopes; import org.sonar.core.NotDryRun; -import org.sonar.core.purge.PurgeDao; -import org.sonar.core.purge.PurgeSnapshotQuery; -import org.sonar.plugins.dbcleaner.api.DbCleanerConstants; -import org.sonar.plugins.dbcleaner.period.DefaultPeriodCleaner; +import org.sonar.plugins.dbcleaner.api.PurgeTask; -@Properties({ - @Property( - key = DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY, - defaultValue = "false", - name = "Clean history data of directories/packages") -}) @NotDryRun public class ProjectPurgePostJob implements PostJob { - private static final Logger LOG = LoggerFactory.getLogger(ProjectPurgePostJob.class); + private PurgeTask purgeTask; - private PurgeDao purgeDao; - private Settings settings; - private DefaultPeriodCleaner periodCleaner; - - public ProjectPurgePostJob(PurgeDao purgeDao, Settings settings, DefaultPeriodCleaner periodCleaner) { - this.purgeDao = purgeDao; - this.settings = settings; - this.periodCleaner = periodCleaner; + public ProjectPurgePostJob(PurgeTask purgeTask) { + this.purgeTask = purgeTask; } public void executeOn(final Project project, SensorContext context) { - long projectId = (long) project.getId(); - - cleanHistoricalData(projectId); - deleteAbortedBuilds(projectId); - deleteFileHistory(projectId); - if (settings.getBoolean(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY)) { - deleteDirectoryHistory(projectId); - } - purgeProject(projectId); - } - - private void cleanHistoricalData(long projectId) { - try { - LOG.debug("Clean project historical data"); - periodCleaner.purge(projectId); - } catch (Exception e) { - // purge errors must no fail the batch - LOG.error("Fail to clean project historical data", e); - } - } - - private void purgeProject(long projectId) { - try { - LOG.debug("Purge project"); - purgeDao.purgeProject(projectId); - } catch (Exception e) { - // purge errors must no fail the batch - LOG.error("Fail to purge project", e); - } - } - - private void deleteDirectoryHistory(long projectId) { - try { - LOG.debug("Delete historical data of directories"); - PurgeSnapshotQuery query = PurgeSnapshotQuery.create() - .setRootProjectId(projectId) - .setIslast(false) - .setScopes(new String[]{Scopes.DIRECTORY}); - purgeDao.deleteSnapshots(query); - } catch (Exception e) { - // purge errors must no fail the batch - LOG.error("Fail to delete historical data of directories", e); - } - } - - private void deleteFileHistory(long projectId) { - try { - LOG.debug("Delete historical data of files"); - PurgeSnapshotQuery query = PurgeSnapshotQuery.create() - .setRootProjectId(projectId) - .setIslast(false) - .setScopes(new String[]{Scopes.FILE}); - purgeDao.deleteSnapshots(query); - } catch (Exception e) { - // purge errors must no fail the batch - LOG.error("Fail to delete historical data of files", e); - } - } - - private void deleteAbortedBuilds(long projectId) { - try { - LOG.debug("Delete aborted builds"); - PurgeSnapshotQuery query = PurgeSnapshotQuery.create() - .setRootProjectId(projectId) - .setIslast(false) - .setStatus(new String[]{"U"}); - purgeDao.deleteSnapshots(query); - } catch (Exception e) { - // purge errors must no fail the batch - LOG.error("Fail to delete historical aborted builds", e); - } + purgeTask.purgeProject((long) project.getId()); } } diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeTask.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeTask.java new file mode 100644 index 00000000000..326143d8d8e --- /dev/null +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/api/PurgeTask.java @@ -0,0 +1,30 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.dbcleaner.api; + +import org.sonar.api.BatchExtension; + +/** + * @since 2.14 + */ +public interface PurgeTask extends BatchExtension { + PurgeTask purgeProject(long projectId); + PurgeTask deleteProject(long projectId); +} |