001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * This program 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 * This program 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 */ 020package org.sonar.api; 021 022import java.util.ArrayList; 023import java.util.Collection; 024import java.util.List; 025import org.sonar.api.utils.Version; 026 027import static java.util.Arrays.asList; 028import static java.util.Objects.requireNonNull; 029 030/** 031 * Entry-point for plugins to inject extensions into SonarQube. 032 * <p>The JAR manifest must declare the name of the implementation class in the property <code>Plugin-Class</code>. 033 * This property is automatically set by sonar-packaging-maven-plugin when building plugin. 034 * <p>Example of implementation 035 * <pre> 036 * package com.mycompany.sonarqube; 037 * public class MyPlugin implements Plugin { 038 * {@literal @}Override 039 * public void define(Context context) { 040 * context.addExtensions(MySensor.class, MyRules.class); 041 * if (context.getSonarQubeVersion().isGreaterThanOrEqual(SonarQubeVersion.V5_6)) { 042 * // Extension which supports only versions 5.6 and greater 043 * // See org.sonar.api.SonarQubeVersion for more details. 044 * context.addExtension(MyNewExtension.class); 045 * } 046 * } 047 * } 048 * </pre> 049 * 050 * <p>Example of pom.xml 051 * <pre> 052 * <project> 053 * ... 054 * <packaging>sonar-plugin</packaging> 055 * 056 * <build> 057 * <plugins> 058 * <plugin> 059 * <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId> 060 * <artifactId>sonar-packaging-maven-plugin</artifactId> 061 * <extensions>true</extensions> 062 * <configuration> 063 * <pluginClass>com.mycompany.sonarqube.MyPlugin</pluginClass> 064 * </configuration> 065 * </plugin> 066 * </plugins> 067 * </build> 068 * </project> 069 * </pre> 070 * 071 * <p>Example of test 072 * <pre> 073 * MyPlugin underTest = new MyPlugin(); 074 * 075 *{@literal @}Test 076 * public void test_plugin_extensions_compatible_with_5_5() { 077 * Plugin.Context context = new Plugin.Context(SonarQubeVersion.V5_5); 078 * underTest.define(context); 079 * assertThat(context.getExtensions()).hasSize(4); 080 * } 081 * </pre> 082 * 083 * @since 5.5 084 */ 085public interface Plugin { 086 087 class Context { 088 private final Version version; 089 private final List extensions = new ArrayList(); 090 091 public Context(Version version) { 092 this.version = version; 093 } 094 095 /** 096 * Runtime version of SonarQube 097 */ 098 public Version getSonarQubeVersion() { 099 return version; 100 } 101 102 /** 103 * Add an extension as : 104 * <ul> 105 * <li>a Class that is annotated with {@link org.sonar.api.batch.BatchSide}, {@link org.sonar.api.server.ServerSide} 106 * or {@link org.sonar.api.ce.ComputeEngineSide}. The extension will be instantiated once. Its dependencies are 107 * injected through constructor parameters.</li> 108 * <li>an instance that is annotated with {@link org.sonar.api.batch.BatchSide}, {@link org.sonar.api.server.ServerSide} 109 * or {@link org.sonar.api.ce.ComputeEngineSide}.</li> 110 * </ul> 111 * Only a single component can be registered for a class. It's not allowed for example to register: 112 * <ul> 113 * <li>two MyExtension.class</li> 114 * <li>MyExtension.class and new MyExtension()</li> 115 * </ul> 116 */ 117 public Context addExtension(Object extension) { 118 requireNonNull(extension); 119 this.extensions.add(extension); 120 return this; 121 } 122 123 /** 124 * @see #addExtension(Object) 125 */ 126 public Context addExtensions(Collection extensions) { 127 this.extensions.addAll(extensions); 128 return this; 129 } 130 131 /** 132 * @see #addExtension(Object) 133 */ 134 public Context addExtensions(Object first, Object second, Object... others) { 135 addExtension(first); 136 addExtension(second); 137 addExtensions(asList(others)); 138 return this; 139 } 140 141 public List getExtensions() { 142 return extensions; 143 } 144 } 145 146 /** 147 * This method is executed at runtime when: 148 * <ul> 149 * <li>Web Server starts</li> 150 * <li>Compute Engine starts</li> 151 * <li>Scanner starts</li> 152 * </ul> 153 */ 154 void define(Context context); 155}