You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Runner.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Sonar Runner - API
  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.apache.commons.io.FileUtils;
  22. import org.apache.commons.lang.StringUtils;
  23. import java.io.File;
  24. import java.lang.reflect.Constructor;
  25. import java.lang.reflect.InvocationTargetException;
  26. import java.lang.reflect.Method;
  27. import java.net.MalformedURLException;
  28. import java.net.URL;
  29. import java.nio.charset.Charset;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import java.util.Properties;
  33. /**
  34. * <p>
  35. * Sonar Runner class that can be used to launch Sonar analyses.
  36. * </p>
  37. * <p>
  38. * Configuration is all done through properties:
  39. * </p>
  40. * <ul>
  41. * <li>"sonar.projectDir": the base directory of the project to analyse (this can also be passed via the {@link #create(Properties, File)} constructor)</li>
  42. * <li>"sonar.working.directory": the working directory, which is "${sonar.projectDir}/.sonar" by default.</li>
  43. * <li>"sonar.verbose": if set to "true", more information is displayed in the log</li>
  44. * <li>"sonar.environment.information.key" and "sonar.environment.information.version": can be used to overwrite environment information (can also be
  45. * set via {@link #setEnvironmentInformation(String, String)} method)</li>
  46. * <li>... plus all the other Sonar and Sonar plugins properties.</li>
  47. * </ul>
  48. *
  49. * @since 1.1
  50. */
  51. public final class Runner {
  52. /**
  53. * Old property used to activate debug level for logging.
  54. *
  55. * @deprecated Replaced by sonar.verbose since 1.2
  56. */
  57. @Deprecated
  58. public static final String PROPERTY_OLD_DEBUG_MODE = "runner.debug";
  59. /**
  60. * Property used to increase logging information.
  61. *
  62. * @since 1.2
  63. */
  64. public static final String PROPERTY_VERBOSE = "sonar.verbose";
  65. /**
  66. * Property used to specify the working directory for the runner. May be a relative or absolute path.
  67. *
  68. * @since 1.4
  69. */
  70. public static final String PROPERTY_WORK_DIRECTORY = "sonar.working.directory";
  71. /**
  72. * Default value of the working directory.
  73. */
  74. public static final String DEF_VALUE_WORK_DIRECTORY = ".sonar";
  75. /**
  76. * Property used to specify the base directory of the project to analyse.
  77. *
  78. * @since 1.5
  79. */
  80. public static final String PROPERTY_SONAR_PROJECT_BASEDIR = "sonar.projectBaseDir";
  81. /**
  82. * Property used to specify the name of the tool that will run a Sonar analysis.
  83. *
  84. * @since 1.5
  85. */
  86. public static final String PROPERTY_ENVIRONMENT_INFORMATION_KEY = "sonar.environment.information.key";
  87. /**
  88. * Property used to specify the version of the tool that will run a Sonar analysis.
  89. *
  90. * @since 1.5
  91. */
  92. public static final String PROPERTY_ENVIRONMENT_INFORMATION_VERSION = "sonar.environment.information.version";
  93. /**
  94. * Property used to define cache location (default to ~/.sonar/cache).
  95. *
  96. * @since 2.1
  97. */
  98. String ENV_SONAR_USER_HOME = "SONAR_USER_HOME";
  99. String PROPERTY_SONAR_USER_HOME = "sonar.userHome";
  100. /**
  101. * Array of prefixes of versions of Sonar without support of this runner.
  102. */
  103. 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"};
  104. private static final String[] UNSUPPORTED_VERSIONS_FOR_TASKS = {"1", "2", "3.0", "3.1", "3.2", "3.3", "3.4"};
  105. private static final String PROPERTY_SOURCE_ENCODING = "sonar.sourceEncoding";
  106. private String command;
  107. private File projectDir;
  108. private File sonarUserHomeDir;
  109. private File workDir;
  110. private String[] unmaskedPackages;
  111. private List<Object> containerExtensions = new ArrayList<Object>();
  112. private Properties globalProperties;
  113. private Properties projectProperties;
  114. private boolean isEncodingPlatformDependant;
  115. private SonarCache cache;
  116. private Runner(String command, Properties globalProperties, Properties projectProperties) {
  117. this.command = command;
  118. this.globalProperties = globalProperties;
  119. this.projectProperties = projectProperties;
  120. this.unmaskedPackages = new String[0];
  121. // set the default values for the Sonar Runner - they can be overriden with #setEnvironmentInformation
  122. this.globalProperties.put(PROPERTY_ENVIRONMENT_INFORMATION_KEY, "Runner");
  123. this.globalProperties.put(PROPERTY_ENVIRONMENT_INFORMATION_VERSION, Version.getVersion());
  124. // sets the encoding if not forced
  125. if (!globalProperties.containsKey(PROPERTY_SOURCE_ENCODING) && !projectProperties.containsKey(PROPERTY_SOURCE_ENCODING)) {
  126. isEncodingPlatformDependant = true;
  127. globalProperties.setProperty(PROPERTY_SOURCE_ENCODING, Charset.defaultCharset().name());
  128. }
  129. // and init the directories
  130. initDirs();
  131. // init the cache
  132. // Try to get Sonar user home from property
  133. cache = SonarCache.create(getSonarUserHomeDir()).build();
  134. }
  135. /**
  136. * Creates a Runner based only on the given properties.
  137. * @deprecated Use {@link Runner#create(String, Properties, Properties)}
  138. */
  139. @Deprecated
  140. public static Runner create(Properties props) {
  141. return create(null, new Properties(), props);
  142. }
  143. /**
  144. * Creates a Runner based only on the given properties.
  145. */
  146. public static Runner create(String command, Properties globalProperties, Properties projectProperties) {
  147. return new Runner(command, globalProperties, projectProperties);
  148. }
  149. /**
  150. * Creates a Runner based only on the properties and with the given base directory.
  151. * @deprecated Use {@link Runner#create(String, Properties, Properties, File)}
  152. */
  153. @Deprecated
  154. public static Runner create(Properties props, File basedir) {
  155. return create(null, new Properties(), props, basedir);
  156. }
  157. /**
  158. * Creates a Runner based only on the properties and with the given base directory.
  159. */
  160. public static Runner create(String command, Properties globalProperties, Properties projectProperties, File basedir) {
  161. projectProperties.put(PROPERTY_SONAR_PROJECT_BASEDIR, basedir.getAbsolutePath());
  162. return new Runner(command, globalProperties, projectProperties);
  163. }
  164. /**
  165. * Runs a Sonar analysis.
  166. */
  167. public void execute() {
  168. Bootstrapper bootstrapper = new Bootstrapper("SonarRunner/" + Version.getVersion(), getSonarServerURL(), getWorkDir(), getCache());
  169. checkSonarVersion(bootstrapper);
  170. delegateExecution(createClassLoader(bootstrapper));
  171. }
  172. public String getSonarServerURL() {
  173. return projectProperties.getProperty("sonar.host.url", globalProperties.getProperty("sonar.host.url", "http://localhost:9000"));
  174. }
  175. public SonarCache getCache() {
  176. return cache;
  177. }
  178. private void initDirs() {
  179. projectDir = initProjectDir();
  180. // project home exists: add its absolute path as "sonar.projectBaseDir" property
  181. projectProperties.put(PROPERTY_SONAR_PROJECT_BASEDIR, projectDir.getAbsolutePath());
  182. workDir = initWorkDir();
  183. sonarUserHomeDir = initSonarUserHomeDir();
  184. }
  185. private File initSonarUserHomeDir() {
  186. String sonarUserHome = globalProperties.getProperty(PROPERTY_SONAR_USER_HOME);
  187. if (StringUtils.isBlank(sonarUserHome)) {
  188. // Try to get Sonar user home from environment variable
  189. sonarUserHome = System.getenv(ENV_SONAR_USER_HOME);
  190. }
  191. if (StringUtils.isBlank(sonarUserHome)) {
  192. // Default Sonar user home
  193. sonarUserHome = System.getProperty("user.home") + File.separator + ".sonar";
  194. }
  195. return new File(sonarUserHome);
  196. }
  197. private File initProjectDir() {
  198. String path = projectProperties.getProperty(PROPERTY_SONAR_PROJECT_BASEDIR, ".");
  199. File dir = new File(path);
  200. if (!dir.isDirectory()) {
  201. throw new RunnerException("Project home must be an existing directory: " + path);
  202. }
  203. return dir;
  204. }
  205. private File initWorkDir() {
  206. File newWorkDir;
  207. String customWorkDir = projectProperties.getProperty(PROPERTY_WORK_DIRECTORY, globalProperties.getProperty(PROPERTY_WORK_DIRECTORY));
  208. if (customWorkDir == null || "".equals(customWorkDir.trim())) {
  209. newWorkDir = new File(getProjectDir(), DEF_VALUE_WORK_DIRECTORY);
  210. }
  211. else {
  212. newWorkDir = defineCustomizedWorkDir(new File(customWorkDir));
  213. }
  214. FileUtils.deleteQuietly(newWorkDir);
  215. return newWorkDir;
  216. }
  217. private File defineCustomizedWorkDir(File customWorkDir) {
  218. if (customWorkDir.isAbsolute()) {
  219. return customWorkDir;
  220. }
  221. return new File(getProjectDir(), customWorkDir.getPath());
  222. }
  223. /**
  224. * @return the project base directory
  225. */
  226. public File getProjectDir() {
  227. return projectDir;
  228. }
  229. /**
  230. * @return work directory, default is ".sonar" in project directory
  231. */
  232. public File getWorkDir() {
  233. return workDir;
  234. }
  235. /**
  236. * @return sonar user home directory
  237. */
  238. public File getSonarUserHomeDir() {
  239. return sonarUserHomeDir;
  240. }
  241. /**
  242. * @return the source code encoding that will be used by Sonar
  243. */
  244. public String getSourceCodeEncoding() {
  245. return projectProperties.getProperty(PROPERTY_SOURCE_ENCODING, globalProperties.getProperty(PROPERTY_SOURCE_ENCODING));
  246. }
  247. /**
  248. * @return true if the property "sonar.sourceEncoding" hasn't been forced
  249. */
  250. public boolean isEncodingPlatformDependant() {
  251. return isEncodingPlatformDependant;
  252. }
  253. public String getCommand() {
  254. return command;
  255. }
  256. /**
  257. * @return global properties, project properties and command-line properties
  258. */
  259. public Properties getProperties() {
  260. Properties props = new Properties();
  261. props.putAll(globalProperties);
  262. props.putAll(projectProperties);
  263. return props;
  264. }
  265. protected void checkSonarVersion(Bootstrapper bootstrapper) {
  266. String serverVersion = bootstrapper.getServerVersion();
  267. if (isUnsupportedVersion(serverVersion)) {
  268. throw new RunnerException("Sonar " + serverVersion
  269. + " is not supported. Please upgrade Sonar to version 2.11 or more.");
  270. }
  271. if (command != null && isUnsupportedVersionForTasks(serverVersion)) {
  272. throw new RunnerException("Sonar " + serverVersion
  273. + " doesn't support tasks. Please upgrade Sonar to version 3.5 or more.");
  274. }
  275. }
  276. private BootstrapClassLoader createClassLoader(Bootstrapper bootstrapper) {
  277. URL url = getJarPath();
  278. return bootstrapper.createClassLoader(
  279. // Add JAR with Sonar Runner - it's a Jar which contains this class
  280. new URL[] {url},
  281. getClass().getClassLoader(),
  282. unmaskedPackages);
  283. }
  284. /**
  285. * For unknown reasons <code>getClass().getProtectionDomain().getCodeSource().getLocation()</code> doesn't work under Ant 1.7.0.
  286. * So this is a workaround.
  287. *
  288. * @return Jar which contains this class
  289. */
  290. public static URL getJarPath() {
  291. String pathToClass = "/" + Runner.class.getName().replace('.', '/') + ".class";
  292. URL url = Runner.class.getResource(pathToClass);
  293. if (url != null) {
  294. String path = url.toString();
  295. String uri = null;
  296. if (path.startsWith("jar:file:")) {
  297. int bang = path.indexOf('!');
  298. uri = path.substring(4, bang);
  299. } else if (path.startsWith("file:")) {
  300. int tail = path.indexOf(pathToClass);
  301. uri = path.substring(0, tail);
  302. }
  303. if (uri != null) {
  304. try {
  305. return new URL(uri);
  306. } catch (MalformedURLException e) {
  307. }
  308. }
  309. }
  310. return null;
  311. }
  312. static boolean isUnsupportedVersion(String version) {
  313. return VersionUtils.isUnsupportedVersion(version, UNSUPPORTED_VERSIONS);
  314. }
  315. static boolean isUnsupportedVersionForTasks(String version) {
  316. return VersionUtils.isUnsupportedVersion(version, UNSUPPORTED_VERSIONS_FOR_TASKS);
  317. }
  318. private void delegateExecution(BootstrapClassLoader sonarClassLoader) {
  319. ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
  320. try {
  321. Thread.currentThread().setContextClassLoader(sonarClassLoader);
  322. Class<?> launcherClass = sonarClassLoader.findClass("org.sonar.runner.internal.batch.Launcher");
  323. Constructor<?> constructor = launcherClass.getConstructor(String.class, Properties.class, Properties.class, List.class);
  324. Object launcher = constructor.newInstance(getCommand(), globalProperties, projectProperties, containerExtensions);
  325. Method method = launcherClass.getMethod("execute");
  326. method.invoke(launcher);
  327. } catch (InvocationTargetException e) {
  328. // Unwrap original exception
  329. throw new RunnerException("Unable to execute Sonar", e.getTargetException());
  330. } catch (Exception e) {
  331. // Catch all other exceptions, which relates to reflection
  332. throw new RunnerException("Unable to execute Sonar", e);
  333. } finally {
  334. Thread.currentThread().setContextClassLoader(oldContextClassLoader);
  335. }
  336. }
  337. /**
  338. * Allows to overwrite the environment information when Sonar Runner is embedded in a specific tool (for instance, with the Sonar Ant Task).
  339. *
  340. * @param key the key of the tool that embeds Sonar Runner
  341. * @param version the version of this tool
  342. */
  343. public void setEnvironmentInformation(String key, String version) {
  344. this.globalProperties.put(PROPERTY_ENVIRONMENT_INFORMATION_KEY, key);
  345. this.globalProperties.put(PROPERTY_ENVIRONMENT_INFORMATION_VERSION, version);
  346. }
  347. public void setUnmaskedPackages(String... unmaskedPackages) {
  348. this.unmaskedPackages = unmaskedPackages;
  349. }
  350. public void addContainerExtension(Object extension) {
  351. containerExtensions.add(extension);
  352. }
  353. }