Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TaskContainer.java 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info 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.task;
  21. import java.util.Map;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.sonar.api.CoreProperties;
  24. import org.sonar.api.task.Task;
  25. import org.sonar.api.task.TaskDefinition;
  26. import org.sonar.api.utils.MessageException;
  27. import org.sonar.core.extension.CoreExtensionsInstaller;
  28. import org.sonar.core.platform.ComponentContainer;
  29. import org.sonar.scanner.bootstrap.ExtensionInstaller;
  30. import org.sonar.scanner.bootstrap.GlobalProperties;
  31. import static org.sonar.api.batch.InstantiationStrategy.PER_TASK;
  32. import static org.sonar.scanner.bootstrap.ExtensionUtils.isInstantiationStrategy;
  33. import static org.sonar.scanner.bootstrap.ExtensionUtils.isScannerSide;
  34. public class TaskContainer extends ComponentContainer {
  35. private final Map<String, String> taskProperties;
  36. private final Object[] components;
  37. public TaskContainer(ComponentContainer parent, Map<String, String> taskProperties, Object... components) {
  38. super(parent);
  39. this.taskProperties = taskProperties;
  40. this.components = components;
  41. }
  42. @Override
  43. protected void doBeforeStart() {
  44. addTaskExtensions();
  45. addCoreComponents();
  46. for (Object component : components) {
  47. add(component);
  48. }
  49. }
  50. private void addCoreComponents() {
  51. add(new TaskProperties(taskProperties, getParent().getComponentByType(GlobalProperties.class).property(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)));
  52. }
  53. private void addTaskExtensions() {
  54. getComponentByType(ExtensionInstaller.class)
  55. .install(this, extension -> isScannerSide(extension) && isInstantiationStrategy(extension, PER_TASK));
  56. getComponentByType(CoreExtensionsInstaller.class)
  57. .install(this, t -> isInstantiationStrategy(t, PER_TASK));
  58. }
  59. @Override
  60. public void doAfterStart() {
  61. // default value is declared in CorePlugin
  62. String taskKey = StringUtils.defaultIfEmpty(taskProperties.get(CoreProperties.TASK), CoreProperties.SCAN_TASK);
  63. // Release memory
  64. taskProperties.clear();
  65. TaskDefinition def = getComponentByType(Tasks.class).definition(taskKey);
  66. if (def == null) {
  67. throw MessageException.of("Task '" + taskKey + "' does not exist. Please use '" + ListTask.KEY + "' task to see all available tasks.");
  68. }
  69. Task task = getComponentByType(def.taskClass());
  70. if (task != null) {
  71. task.execute();
  72. } else {
  73. throw new IllegalStateException("Task " + taskKey + " is badly defined");
  74. }
  75. }
  76. }