001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.bootstrap;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.sonar.api.CoreProperties;
024    import org.sonar.api.platform.ComponentContainer;
025    import org.sonar.api.resources.ResourceTypes;
026    import org.sonar.api.task.Task;
027    import org.sonar.api.task.TaskComponent;
028    import org.sonar.api.task.TaskDefinition;
029    import org.sonar.api.utils.MessageException;
030    import org.sonar.batch.bootstrapper.EnvironmentInformation;
031    import org.sonar.batch.components.PastMeasuresLoader;
032    import org.sonar.batch.scan.DeprecatedProjectReactorBuilder;
033    import org.sonar.batch.scan.ProjectReactorBuilder;
034    import org.sonar.batch.scan.ScanTask;
035    import org.sonar.batch.scan.measure.DefaultMetricFinder;
036    import org.sonar.batch.scan.measure.DeprecatedMetricFinder;
037    import org.sonar.batch.tasks.ListTask;
038    import org.sonar.batch.tasks.Tasks;
039    import org.sonar.core.permission.PermissionFacade;
040    import org.sonar.core.resource.DefaultResourcePermissions;
041    
042    import java.util.Map;
043    
044    public class TaskContainer extends ComponentContainer {
045    
046      private final Map<String, String> taskProperties;
047      private final Object[] components;
048    
049      public TaskContainer(ComponentContainer parent, Map<String, String> taskProperties, Object... components) {
050        super(parent);
051        this.taskProperties = taskProperties;
052        this.components = components;
053      }
054    
055      @Override
056      protected void doBeforeStart() {
057        installCoreTasks();
058        installTaskExtensions();
059        installComponentsUsingTaskExtensions();
060        addCoreComponents();
061        for (Object component : components) {
062          add(component);
063        }
064      }
065    
066      private void addCoreComponents() {
067        // Metrics
068        if (!getParent().getComponentByType(AnalysisMode.class).isSensorMode()) {
069          // Needed by dev cockpit task
070          add(DeprecatedMetricFinder.class,
071            PastMeasuresLoader.class);
072        }
073        add(DefaultMetricFinder.class);
074    
075      }
076    
077      void installCoreTasks() {
078        add(new TaskProperties(taskProperties, getParent().getComponentByType(BootstrapProperties.class).property(CoreProperties.ENCRYPTION_SECRET_KEY_PATH)));
079        add(
080          ScanTask.DEFINITION, ScanTask.class,
081          ListTask.DEFINITION, ListTask.class,
082          projectReactorBuilder());
083      }
084    
085      private void installTaskExtensions() {
086        getComponentByType(ExtensionInstaller.class).install(this, new ExtensionMatcher() {
087          public boolean accept(Object extension) {
088            return ExtensionUtils.isType(extension, TaskComponent.class);
089          }
090        });
091      }
092    
093      private Class<?> projectReactorBuilder() {
094        if (isRunnerVersionLessThan2Dot4()) {
095          return DeprecatedProjectReactorBuilder.class;
096        }
097        return ProjectReactorBuilder.class;
098      }
099    
100      private boolean isRunnerVersionLessThan2Dot4() {
101        EnvironmentInformation env = this.getComponentByType(EnvironmentInformation.class);
102        // Starting from SQ Runner 2.4 the key is "SonarQubeRunner"
103        return env != null && "SonarRunner".equals(env.getKey());
104      }
105    
106      private void installComponentsUsingTaskExtensions() {
107        add(
108          ResourceTypes.class,
109          PermissionFacade.class,
110          DefaultResourcePermissions.class,
111          Tasks.class);
112      }
113    
114      @Override
115      public void doAfterStart() {
116        // default value is declared in CorePlugin
117        String taskKey = StringUtils.defaultIfEmpty(taskProperties.get(CoreProperties.TASK), CoreProperties.SCAN_TASK);
118    
119        TaskDefinition def = getComponentByType(Tasks.class).definition(taskKey);
120        if (def == null) {
121          throw MessageException.of("Task " + taskKey + " does not exist");
122        }
123        Task task = getComponentByType(def.taskClass());
124        if (task != null) {
125          task.execute();
126        } else {
127          throw new IllegalStateException("Task " + taskKey + " is badly defined");
128        }
129      }
130    }