import javax.annotation.Nullable;
import org.sonar.api.ExtensionProvider;
import org.sonar.api.SonarPlugin;
-import org.sonar.batch.bootstrapper.EnvironmentInformation;
import org.sonar.core.platform.ComponentContainer;
import org.sonar.core.platform.PluginInfo;
import org.sonar.core.platform.PluginRepository;
public class ExtensionInstaller {
private final PluginRepository pluginRepository;
- private final EnvironmentInformation env;
- public ExtensionInstaller(PluginRepository pluginRepository, EnvironmentInformation env) {
+ public ExtensionInstaller(PluginRepository pluginRepository) {
this.pluginRepository = pluginRepository;
- this.env = env;
}
public ExtensionInstaller install(ComponentContainer container, ExtensionMatcher matcher) {
return this;
}
- private void doInstall(ComponentContainer container, ExtensionMatcher matcher, @Nullable PluginInfo pluginInfo, Object extension) {
- if (ExtensionUtils.supportsEnvironment(extension, env)
- && matcher.accept(extension)) {
+ private static void doInstall(ComponentContainer container, ExtensionMatcher matcher, @Nullable PluginInfo pluginInfo, Object extension) {
+ if (matcher.accept(extension)) {
container.addExtension(pluginInfo, extension);
} else {
container.declareExtension(pluginInfo, extension);
*/
package org.sonar.batch.bootstrap;
-import org.apache.commons.lang.StringUtils;
import org.sonar.api.batch.BatchSide;
import org.sonar.api.batch.InstantiationStrategy;
-import org.sonar.api.batch.SupportedEnvironment;
import org.sonar.api.utils.AnnotationUtils;
-import org.sonar.batch.bootstrapper.EnvironmentInformation;
public class ExtensionUtils {
return AnnotationUtils.getAnnotation(extension, BatchSide.class) != null;
}
- public static boolean supportsEnvironment(Object extension, EnvironmentInformation environment) {
- SupportedEnvironment env = AnnotationUtils.getAnnotation(extension, SupportedEnvironment.class);
- if (env == null) {
- return true;
- }
- for (String supported : env.value()) {
- if (StringUtils.equalsIgnoreCase(environment.getKey(), supported)) {
- return true;
- }
- }
- return false;
- }
-
- public static boolean isMavenExtensionOnly(Object extension) {
- SupportedEnvironment env = AnnotationUtils.getAnnotation(extension, SupportedEnvironment.class);
- return env != null && env.value().length == 1 && StringUtils.equalsIgnoreCase("maven", env.value()[0]);
- }
-
public static boolean isType(Object extension, Class<?> extensionClass) {
Class clazz = extension instanceof Class ? (Class) extension : extension.getClass();
return extensionClass.isAssignableFrom(clazz);
import org.sonar.api.BatchExtension;
import org.sonar.api.ExtensionProvider;
import org.sonar.api.SonarPlugin;
-import org.sonar.api.batch.SupportedEnvironment;
-import org.sonar.batch.bootstrapper.EnvironmentInformation;
import org.sonar.core.platform.ComponentContainer;
import org.sonar.core.platform.PluginInfo;
when(pluginRepository.getPluginInstance("foo")).thenReturn(newPluginInstance(Foo.class, Bar.class));
ComponentContainer container = new ComponentContainer();
- ExtensionInstaller installer = new ExtensionInstaller(pluginRepository, new EnvironmentInformation("ant", "1.7"));
+ ExtensionInstaller installer = new ExtensionInstaller(pluginRepository);
installer.install(container, new FooMatcher());
assertThat(container.getComponentByType(Foo.class)).isNotNull();
when(pluginRepository.getPluginInfos()).thenReturn(Arrays.asList(new PluginInfo("foo")));
when(pluginRepository.getPluginInstance("foo")).thenReturn(newPluginInstance(new FooProvider(), new BarProvider()));
ComponentContainer container = new ComponentContainer();
- ExtensionInstaller installer = new ExtensionInstaller(pluginRepository, new EnvironmentInformation("ant", "1.7"));
+ ExtensionInstaller installer = new ExtensionInstaller(pluginRepository);
installer.install(container, new FooMatcher());
when(pluginRepository.getPluginInfos()).thenReturn(Arrays.asList(new PluginInfo("foo")));
when(pluginRepository.getPluginInstance("foo")).thenReturn(newPluginInstance(new FooBarProvider()));
ComponentContainer container = new ComponentContainer();
- ExtensionInstaller installer = new ExtensionInstaller(pluginRepository, new EnvironmentInformation("ant", "1.7"));
+ ExtensionInstaller installer = new ExtensionInstaller(pluginRepository);
installer.install(container, new TrueMatcher());
assertThat(container.getComponentByType(Bar.class)).isNotNull();
}
- @Test
- public void should_not_install_on_unsupported_environment() {
- when(pluginRepository.getPluginInfos()).thenReturn(Arrays.asList(new PluginInfo("foo")));
- when(pluginRepository.getPluginInstance("foo")).thenReturn(newPluginInstance(Foo.class, MavenExtension.class, AntExtension.class, new BarProvider()));
- ComponentContainer container = new ComponentContainer();
- ExtensionInstaller installer = new ExtensionInstaller(pluginRepository, new EnvironmentInformation("ant", "1.7"));
-
- installer.install(container, new TrueMatcher());
-
- assertThat(container.getComponentByType(MavenExtension.class)).isNull();
- assertThat(container.getComponentByType(AntExtension.class)).isNotNull();
- assertThat(container.getComponentByType(Foo.class)).isNotNull();
- assertThat(container.getComponentByType(Bar.class)).isNotNull();
- }
-
private static class FooMatcher implements ExtensionMatcher {
public boolean accept(Object extension) {
return extension.equals(Foo.class) || ClassUtils.isAssignable(Foo.class, extension.getClass()) || ClassUtils.isAssignable(FooProvider.class, extension.getClass());
}
- @SupportedEnvironment("maven")
- public static class MavenExtension implements BatchExtension {
-
- }
-
- @SupportedEnvironment("ant")
- public static class AntExtension implements BatchExtension {
-
- }
-
public static class FooProvider extends ExtensionProvider implements BatchExtension {
@Override
public Object provide() {
import org.sonar.api.BatchComponent;
import org.sonar.api.batch.BatchSide;
import org.sonar.api.batch.InstantiationStrategy;
-import org.sonar.api.batch.SupportedEnvironment;
import org.sonar.api.server.ServerSide;
-import org.sonar.batch.bootstrapper.EnvironmentInformation;
import static org.assertj.core.api.Assertions.assertThat;
assertThat(ExtensionUtils.isBatchSide(new ServerService())).isFalse();
}
- @Test
- public void shouldCheckEnvironment() {
- assertThat(ExtensionUtils.supportsEnvironment(new MavenService(), new EnvironmentInformation("maven", "2.2.1"))).isTrue();
- assertThat(ExtensionUtils.supportsEnvironment(new BuildToolService(), new EnvironmentInformation("maven", "2.2.1"))).isTrue();
- assertThat(ExtensionUtils.supportsEnvironment(new DefaultService(), new EnvironmentInformation("maven", "2.2.1"))).isTrue();
-
- assertThat(ExtensionUtils.supportsEnvironment(new BuildToolService(), new EnvironmentInformation("eclipse", "0.1"))).isFalse();
- }
-
- @Test
- public void shouldBeMavenExtensionOnly() {
- assertThat(ExtensionUtils.isMavenExtensionOnly(DefaultService.class)).isFalse();
- assertThat(ExtensionUtils.isMavenExtensionOnly(new DefaultService())).isFalse();
- assertThat(ExtensionUtils.isMavenExtensionOnly(MavenService.class)).isTrue();
- assertThat(ExtensionUtils.isMavenExtensionOnly(new MavenService())).isTrue();
- assertThat(ExtensionUtils.isMavenExtensionOnly(BuildToolService.class)).isFalse();
- assertThat(ExtensionUtils.isMavenExtensionOnly(new BuildToolService())).isFalse();
- }
-
@BatchSide
@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
public static class BatchService {
}
- @BatchSide
- @SupportedEnvironment("maven")
- public static class MavenService {
-
- }
-
- @BatchSide
- @SupportedEnvironment({"maven", "ant", "gradle"})
- public static class BuildToolService {
-
- }
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.api.batch;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-/**
- * This annotation allows to specify in which environments {@link BatchSide} components would be active.
- * For example: "maven", "ant".
- * Usage of this annotation is discouraged and we strictly recommend you to not overuse it.
- * Most preferable is to design components to work in all environments.
- *
- * @since 2.6
- */
-@Retention(RetentionPolicy.RUNTIME)
-@Target({ElementType.TYPE})
-public @interface SupportedEnvironment {
-
- String[] value();
-
-}