001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar 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 * Sonar 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 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.batch.bootstrap; 021 022 import com.google.common.collect.ArrayListMultimap; 023 import com.google.common.collect.ListMultimap; 024 import org.sonar.api.BatchComponent; 025 import org.sonar.api.Extension; 026 import org.sonar.api.ExtensionProvider; 027 import org.sonar.api.Plugin; 028 import org.sonar.api.batch.CoverageExtension; 029 import org.sonar.api.batch.InstantiationStrategy; 030 import org.sonar.api.measures.Metric; 031 import org.sonar.api.measures.Metrics; 032 import org.sonar.api.platform.PluginMetadata; 033 import org.sonar.batch.bootstrapper.EnvironmentInformation; 034 035 import java.util.Map; 036 037 public final class BatchExtensionInstaller implements BatchComponent { 038 039 private BatchPluginRepository pluginRepository; 040 private EnvironmentInformation environment; 041 private DryRun dryRun; 042 043 public BatchExtensionInstaller(BatchPluginRepository pluginRepository, EnvironmentInformation environment, DryRun dryRun) { 044 this.pluginRepository = pluginRepository; 045 this.environment = environment; 046 this.dryRun = dryRun; 047 } 048 049 public void install(Module module) { 050 ListMultimap<PluginMetadata, Object> installedExtensionsByPlugin = ArrayListMultimap.create(); 051 for (Map.Entry<PluginMetadata, Plugin> entry : pluginRepository.getPluginsByMetadata().entrySet()) { 052 PluginMetadata metadata = entry.getKey(); 053 Plugin plugin = entry.getValue(); 054 055 module.addExtension(metadata, plugin); 056 057 for (Object extension : plugin.getExtensions()) { 058 if (installExtension(module, metadata, extension)) { 059 installedExtensionsByPlugin.put(metadata, extension); 060 } else { 061 module.declareExtension(metadata, extension); 062 } 063 } 064 } 065 for (Map.Entry<PluginMetadata, Object> entry : installedExtensionsByPlugin.entries()) { 066 PluginMetadata plugin = entry.getKey(); 067 Object extension = entry.getValue(); 068 if (isExtensionProvider(extension)) { 069 ExtensionProvider provider = (ExtensionProvider) module.getComponentByKey(extension); 070 installProvider(module, plugin, provider); 071 } 072 } 073 installMetrics(module); 074 } 075 076 static boolean isExtensionProvider(Object extension) { 077 return isType(extension, ExtensionProvider.class) || extension instanceof ExtensionProvider; 078 } 079 080 static boolean isType(Object extension, Class<? extends Extension> extensionClass) { 081 Class clazz = (extension instanceof Class ? (Class) extension : extension.getClass()); 082 return extensionClass.isAssignableFrom(clazz); 083 } 084 085 private void installMetrics(Module module) { 086 for (Metrics metrics : module.getComponents(Metrics.class)) { 087 for (Metric metric : metrics.getMetrics()) { 088 module.addCoreSingleton(metric); 089 } 090 } 091 } 092 093 private void installProvider(Module module, PluginMetadata plugin, ExtensionProvider provider) { 094 Object obj = provider.provide(); 095 if (obj != null) { 096 if (obj instanceof Iterable) { 097 for (Object ext : (Iterable) obj) { 098 installExtension(module, plugin, ext); 099 } 100 } else { 101 installExtension(module, plugin, obj); 102 } 103 } 104 } 105 106 boolean installExtension(Module module, PluginMetadata plugin, Object extension) { 107 if (ExtensionUtils.isBatchExtension(extension) && 108 ExtensionUtils.isSupportedEnvironment(extension, environment) && 109 ExtensionUtils.checkDryRun(extension, dryRun.isEnabled()) && 110 ExtensionUtils.isInstantiationStrategy(extension, InstantiationStrategy.PER_BATCH)) { 111 if (ExtensionUtils.isType(extension, CoverageExtension.class)) { 112 throw new IllegalArgumentException("Instantiation strategy " + InstantiationStrategy.PER_BATCH + " is not supported on CoverageExtension components: " + extension); 113 } 114 module.addExtension(plugin, extension); 115 return true; 116 } 117 return false; 118 } 119 }