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.BatchExtension;
024 import org.sonar.api.batch.InstantiationStrategy;
025 import org.sonar.api.batch.SupportedEnvironment;
026 import org.sonar.api.utils.AnnotationUtils;
027 import org.sonar.batch.bootstrapper.EnvironmentInformation;
028 import org.sonar.core.DryRunIncompatible;
029
030 public class ExtensionUtils {
031
032 private ExtensionUtils() {
033 // only static methods
034 }
035
036 public static boolean isInstantiationStrategy(Object extension, String strategy) {
037 InstantiationStrategy annotation = AnnotationUtils.getAnnotation(extension, InstantiationStrategy.class);
038 if (annotation != null) {
039 return strategy.equals(annotation.value());
040 }
041 return InstantiationStrategy.PER_PROJECT.equals(strategy);
042 }
043
044 public static boolean isBatchExtension(Object extension) {
045 return isType(extension, BatchExtension.class);
046 }
047
048 public static boolean supportsEnvironment(Object extension, EnvironmentInformation environment) {
049 SupportedEnvironment env = AnnotationUtils.getAnnotation(extension, SupportedEnvironment.class);
050 if (env == null) {
051 return true;
052 }
053 for (String supported : env.value()) {
054 if (StringUtils.equalsIgnoreCase(environment.getKey(), supported)) {
055 return true;
056 }
057 }
058 return false;
059 }
060
061 public static boolean supportsPreview(Object extension) {
062 return AnnotationUtils.getAnnotation(extension, DryRunIncompatible.class) == null;
063 }
064
065 public static boolean isMavenExtensionOnly(Object extension) {
066 SupportedEnvironment env = AnnotationUtils.getAnnotation(extension, SupportedEnvironment.class);
067 return env != null && env.value().length == 1 && StringUtils.equalsIgnoreCase("maven", env.value()[0]);
068 }
069
070 public static boolean isType(Object extension, Class<?> extensionClass) {
071 Class clazz = extension instanceof Class ? (Class) extension : extension.getClass();
072 return extensionClass.isAssignableFrom(clazz);
073 }
074 }