001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 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 org.apache.commons.lang.StringUtils; 023 import org.sonar.api.BatchExtension; 024 import org.sonar.api.Extension; 025 import org.sonar.api.batch.InstantiationStrategy; 026 import org.sonar.api.batch.SupportedEnvironment; 027 import org.sonar.api.utils.AnnotationUtils; 028 import org.sonar.batch.bootstrapper.EnvironmentInformation; 029 import org.sonar.core.NotDryRun; 030 031 public final class ExtensionUtils { 032 033 private ExtensionUtils() { 034 // only static methods 035 } 036 037 static boolean isInstantiationStrategy(Object extension, String strategy) { 038 Class clazz = (extension instanceof Class ? (Class) extension : extension.getClass()); 039 InstantiationStrategy extStrategy = AnnotationUtils.getClassAnnotation(clazz, InstantiationStrategy.class); 040 if (extStrategy != null) { 041 return strategy.equals(extStrategy.value()); 042 } 043 return InstantiationStrategy.PER_PROJECT.equals(strategy); 044 } 045 046 static boolean isBatchExtension(Object extension) { 047 return isType(extension, BatchExtension.class); 048 } 049 050 static boolean isSupportedEnvironment(Object extension, EnvironmentInformation environment) { 051 Class clazz = (extension instanceof Class ? (Class) extension : extension.getClass()); 052 SupportedEnvironment env = AnnotationUtils.getClassAnnotation(clazz, SupportedEnvironment.class); 053 if (env == null) { 054 return true; 055 } 056 for (String supported : env.value()) { 057 if (StringUtils.equalsIgnoreCase(environment.getKey(), supported)) { 058 return true; 059 } 060 } 061 return false; 062 } 063 064 static boolean checkDryRun(Object extension, boolean dryRun) { 065 return !dryRun || AnnotationUtils.getClassAnnotation(extension, NotDryRun.class)==null; 066 } 067 068 static boolean isMavenExtensionOnly(Object extension) { 069 Class clazz = (extension instanceof Class ? (Class) extension : extension.getClass()); 070 SupportedEnvironment env = AnnotationUtils.getClassAnnotation(clazz, SupportedEnvironment.class); 071 return env!=null && env.value().length==1 && StringUtils.equalsIgnoreCase("maven", env.value()[0]); 072 } 073 074 static boolean isType(Object extension, Class<? extends Extension> extensionClass) { 075 Class clazz = (extension instanceof Class ? (Class) extension : extension.getClass()); 076 return extensionClass.isAssignableFrom(clazz); 077 } 078 }