You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PostJobsExecutor.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2016 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.phases;
  21. import com.google.common.collect.Lists;
  22. import java.util.Collection;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.sonar.api.batch.PostJob;
  25. import org.sonar.api.batch.ScannerSide;
  26. import org.sonar.api.batch.SensorContext;
  27. import org.sonar.api.resources.Project;
  28. import org.sonar.api.utils.log.Logger;
  29. import org.sonar.api.utils.log.Loggers;
  30. import org.sonar.scanner.bootstrap.BatchExtensionDictionnary;
  31. import org.sonar.scanner.events.EventBus;
  32. import org.sonar.scanner.util.BatchUtils;
  33. @ScannerSide
  34. public class PostJobsExecutor {
  35. private static final Logger LOG = Loggers.get(PostJobsExecutor.class);
  36. private final BatchExtensionDictionnary selector;
  37. private final Project project;
  38. private final EventBus eventBus;
  39. public PostJobsExecutor(BatchExtensionDictionnary selector, Project project, EventBus eventBus) {
  40. this.selector = selector;
  41. this.project = project;
  42. this.eventBus = eventBus;
  43. }
  44. public void execute(SensorContext context) {
  45. Collection<PostJob> postJobs = selector.select(PostJob.class, project, true, null);
  46. eventBus.fireEvent(new PostJobPhaseEvent(Lists.newArrayList(postJobs), true));
  47. execute(context, postJobs);
  48. eventBus.fireEvent(new PostJobPhaseEvent(Lists.newArrayList(postJobs), false));
  49. }
  50. private void execute(SensorContext context, Collection<PostJob> postJobs) {
  51. logPostJobs(postJobs);
  52. for (PostJob postJob : postJobs) {
  53. LOG.info("Executing post-job {}", BatchUtils.describe(postJob));
  54. eventBus.fireEvent(new PostJobExecutionEvent(postJob, true));
  55. postJob.executeOn(project, context);
  56. eventBus.fireEvent(new PostJobExecutionEvent(postJob, false));
  57. }
  58. }
  59. private static void logPostJobs(Collection<PostJob> postJobs) {
  60. if (LOG.isDebugEnabled()) {
  61. LOG.debug(() -> "Post-jobs : " + StringUtils.join(postJobs, " -> "));
  62. }
  63. }
  64. }