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.sonar.api.ExtensionProvider;
023    import org.sonar.api.Plugin;
024    import org.sonar.api.platform.ComponentContainer;
025    import org.sonar.api.platform.PluginMetadata;
026    import org.sonar.batch.bootstrapper.EnvironmentInformation;
027    
028    import javax.annotation.Nullable;
029    
030    import java.util.List;
031    import java.util.Map;
032    
033    public class ExtensionInstaller {
034    
035      private final BatchPluginRepository pluginRepository;
036      private final EnvironmentInformation env;
037      private final AnalysisMode analysisMode;
038    
039      public ExtensionInstaller(BatchPluginRepository pluginRepository, EnvironmentInformation env, AnalysisMode analysisMode) {
040        this.pluginRepository = pluginRepository;
041        this.env = env;
042        this.analysisMode = analysisMode;
043      }
044    
045      public ExtensionInstaller install(ComponentContainer container, ExtensionMatcher matcher) {
046    
047        // core components
048        for (Object o : BatchComponents.all()) {
049          doInstall(container, matcher, null, o);
050        }
051    
052        // plugin extensions
053        for (Map.Entry<PluginMetadata, Plugin> entry : pluginRepository.getPluginsByMetadata().entrySet()) {
054          PluginMetadata metadata = entry.getKey();
055          Plugin plugin = entry.getValue();
056          for (Object extension : plugin.getExtensions()) {
057            doInstall(container, matcher, metadata, extension);
058          }
059        }
060        List<ExtensionProvider> providers = container.getComponentsByType(ExtensionProvider.class);
061        for (ExtensionProvider provider : providers) {
062          Object object = provider.provide();
063          if (object instanceof Iterable) {
064            for (Object extension : (Iterable) object) {
065              doInstall(container, matcher, null, extension);
066            }
067          } else {
068            doInstall(container, matcher, null, object);
069          }
070        }
071        return this;
072      }
073    
074      private void doInstall(ComponentContainer container, ExtensionMatcher matcher, @Nullable PluginMetadata metadata, Object extension) {
075        if (ExtensionUtils.supportsEnvironment(extension, env)
076          && (!analysisMode.isPreview() || ExtensionUtils.supportsPreview(extension))
077          && matcher.accept(extension)) {
078          container.addExtension(metadata, extension);
079        } else {
080          container.declareExtension(metadata, extension);
081        }
082      }
083    
084    }