Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Runner.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Sonar Standalone Runner
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner;
  21. import org.sonar.runner.internal.bootstrapper.BootstrapClassLoader;
  22. import org.sonar.runner.internal.bootstrapper.BootstrapException;
  23. import org.sonar.runner.internal.bootstrapper.Bootstrapper;
  24. import org.sonar.runner.utils.SonarRunnerVersion;
  25. import java.io.File;
  26. import java.lang.reflect.Constructor;
  27. import java.lang.reflect.InvocationTargetException;
  28. import java.lang.reflect.Method;
  29. import java.net.URL;
  30. import java.util.Properties;
  31. /**
  32. * <p>
  33. * Sonar Runner class that can be used to launch Sonar analyses.
  34. * </p>
  35. * <p>
  36. * Configuration is all done through properties:
  37. * </p>
  38. * <ul>
  39. * <li>"sonar.projectDir": the base directory of the project to analyse (this can also be passed via the {@link #create(Properties, File)} constructor)</li>
  40. * <li>"sonar.working.directory": the working directory, which is "${sonar.projectDir}/.sonar" by default.</li>
  41. * <li>"sonar.verbose": if set to "true", more information is displayed in the log</li>
  42. * <li>"sonar.environment.information.key" and "sonar.environment.information.version": can be used to overwrite environment information (can also be
  43. * set via {@link #setEnvironmentInformation(String, String)} method)</li>
  44. * <li>... plus all the other Sonar and Sonar plugins properties.</li>
  45. * </ul>
  46. *
  47. * @since 1.1
  48. */
  49. public final class Runner {
  50. /**
  51. * Old property used to activate debug level for logging.
  52. *
  53. * @deprecated Replaced by sonar.verbose since 1.2
  54. */
  55. @Deprecated
  56. public static final String PROPERTY_OLD_DEBUG_MODE = "runner.debug";
  57. /**
  58. * Property used to increase logging information.
  59. *
  60. * @since 1.2
  61. */
  62. public static final String PROPERTY_VERBOSE = "sonar.verbose";
  63. /**
  64. * Property used to specify the working directory for the runner. May be a relative or absolute path.
  65. *
  66. * @since 1.4
  67. */
  68. public static final String PROPERTY_WORK_DIRECTORY = "sonar.working.directory";
  69. /**
  70. * Default value of the working directory.
  71. */
  72. public static final String DEF_VALUE_WORK_DIRECTORY = ".sonar";
  73. /**
  74. * Property used to specify the base directory of the project to analyse.
  75. *
  76. * @since 1.5
  77. */
  78. public static final String PROPERTY_SONAR_PROJECT_BASEDIR = "sonar.projectBaseDir";
  79. /**
  80. * Property used to specify the name of the tool that will run a Sonar analysis.
  81. *
  82. * @since 1.5
  83. */
  84. public static final String PROPERTY_ENVIRONMENT_INFORMATION_KEY = "sonar.environment.information.key";
  85. /**
  86. * Property used to specify the version of the tool that will run a Sonar analysis.
  87. *
  88. * @since 1.5
  89. */
  90. public static final String PROPERTY_ENVIRONMENT_INFORMATION_VERSION = "sonar.environment.information.version";
  91. /**
  92. * Array of prefixes of versions of Sonar without support of this runner.
  93. */
  94. private static final String[] UNSUPPORTED_VERSIONS = {"1", "2.0", "2.1", "2.2", "2.3", "2.4", "2.5", "2.6", "2.7", "2.8", "2.9", "2.10"};
  95. private File projectDir;
  96. private File workDir;
  97. private Properties properties;
  98. private Runner(Properties props) {
  99. this.properties = props;
  100. // set the default values for the Sonar Runner - they can be overriden with #setEnvironmentInformation
  101. this.properties.put(PROPERTY_ENVIRONMENT_INFORMATION_KEY, "Runner");
  102. this.properties.put(PROPERTY_ENVIRONMENT_INFORMATION_VERSION, SonarRunnerVersion.getVersion());
  103. // and init the directories
  104. initDirs();
  105. }
  106. /**
  107. * Creates a Runner based only on the given properties.
  108. */
  109. public static Runner create(Properties props) {
  110. return new Runner(props);
  111. }
  112. /**
  113. * Creates a Runner based only on the properties and with the given base directory.
  114. */
  115. public static Runner create(Properties props, File basedir) {
  116. props.put(PROPERTY_SONAR_PROJECT_BASEDIR, basedir.getAbsolutePath());
  117. return new Runner(props);
  118. }
  119. /**
  120. * Runs a Sonar analysis.
  121. */
  122. public void execute() {
  123. Bootstrapper bootstrapper = new Bootstrapper("SonarRunner/" + SonarRunnerVersion.getVersion(), getSonarServerURL(), getWorkDir());
  124. checkSonarVersion(bootstrapper);
  125. delegateExecution(createClassLoader(bootstrapper));
  126. }
  127. public String getSonarServerURL() {
  128. return properties.getProperty("sonar.host.url", "http://localhost:9000");
  129. }
  130. private void initDirs() {
  131. String path = properties.getProperty(PROPERTY_SONAR_PROJECT_BASEDIR, ".");
  132. projectDir = new File(path);
  133. if (!projectDir.isDirectory()) {
  134. throw new RunnerException("Project home must be an existing directory: " + path);
  135. }
  136. // project home exists: add its absolute path as "sonar.runner.projectDir" property
  137. properties.put(PROPERTY_SONAR_PROJECT_BASEDIR, projectDir.getAbsolutePath());
  138. workDir = initWorkDir();
  139. }
  140. private File initWorkDir() {
  141. String customWorkDir = properties.getProperty(PROPERTY_WORK_DIRECTORY);
  142. if (customWorkDir == null || customWorkDir.trim().length() == 0) {
  143. return new File(getProjectDir(), DEF_VALUE_WORK_DIRECTORY);
  144. }
  145. return defineCustomizedWorkDir(new File(customWorkDir));
  146. }
  147. private File defineCustomizedWorkDir(File customWorkDir) {
  148. if (customWorkDir.isAbsolute()) {
  149. return customWorkDir;
  150. }
  151. return new File(getProjectDir(), customWorkDir.getPath());
  152. }
  153. /**
  154. * @return the project base directory
  155. */
  156. public File getProjectDir() {
  157. return projectDir;
  158. }
  159. /**
  160. * @return work directory, default is ".sonar" in project directory
  161. */
  162. public File getWorkDir() {
  163. return workDir;
  164. }
  165. /**
  166. * @return global properties, project properties and command-line properties
  167. */
  168. protected Properties getProperties() {
  169. return properties;
  170. }
  171. protected void checkSonarVersion(Bootstrapper bootstrapper) {
  172. String serverVersion = bootstrapper.getServerVersion();
  173. if (isUnsupportedVersion(serverVersion)) {
  174. throw new BootstrapException("Sonar " + serverVersion
  175. + " does not support Standalone Runner. Please upgrade Sonar to version 2.11 or more.");
  176. }
  177. }
  178. private BootstrapClassLoader createClassLoader(Bootstrapper bootstrapper) {
  179. URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
  180. return bootstrapper.createClassLoader(
  181. new URL[] {url}, // Add JAR with Sonar Runner - it's a Jar which contains this class
  182. getClass().getClassLoader());
  183. }
  184. static boolean isUnsupportedVersion(String version) {
  185. for (String unsupportedVersion : UNSUPPORTED_VERSIONS) {
  186. if (isVersion(version, unsupportedVersion)) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. static boolean isVersion(String version, String prefix) {
  193. return version.startsWith(prefix + ".") || version.equals(prefix);
  194. }
  195. /**
  196. * Loads Launcher class from specified {@link org.sonar.batch.bootstrapper.BootstrapClassLoader} and passes control to it.
  197. *
  198. * @see Launcher#execute()
  199. */
  200. private void delegateExecution(BootstrapClassLoader sonarClassLoader) {
  201. ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
  202. try {
  203. Thread.currentThread().setContextClassLoader(sonarClassLoader);
  204. Class<?> launcherClass = sonarClassLoader.findClass("org.sonar.runner.internal.batch.Launcher");
  205. Constructor<?> constructor = launcherClass.getConstructor(Properties.class);
  206. Object launcher = constructor.newInstance(getProperties());
  207. Method method = launcherClass.getMethod("execute");
  208. method.invoke(launcher);
  209. } catch (InvocationTargetException e) {
  210. // Unwrap original exception
  211. throw new BootstrapException(e.getTargetException());
  212. } catch (Exception e) {
  213. // Catch all other exceptions, which relates to reflection
  214. throw new BootstrapException(e);
  215. } finally {
  216. Thread.currentThread().setContextClassLoader(oldContextClassLoader);
  217. }
  218. }
  219. /**
  220. * Allows to overwrite the environment information when Sonar Runner is embedded in a specific tool (for instance, with the Sonar Ant Task).
  221. *
  222. * @param key the key of the tool that embeds Sonar Runner
  223. * @param version the version of this tool
  224. */
  225. public void setEnvironmentInformation(String key, String version) {
  226. this.properties.put(PROPERTY_ENVIRONMENT_INFORMATION_KEY, key);
  227. this.properties.put(PROPERTY_ENVIRONMENT_INFORMATION_VERSION, version);
  228. }
  229. }