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.

Launcher.java 5.2KB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 java.io.File;
  22. import java.io.FileFilter;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.util.Properties;
  26. import ch.qos.logback.classic.LoggerContext;
  27. import ch.qos.logback.classic.joran.JoranConfigurator;
  28. import ch.qos.logback.core.joran.spi.JoranException;
  29. import org.apache.commons.configuration.*;
  30. import org.apache.commons.io.IOUtils;
  31. import org.apache.commons.io.filefilter.AndFileFilter;
  32. import org.apache.commons.io.filefilter.FileFileFilter;
  33. import org.apache.commons.io.filefilter.WildcardFileFilter;
  34. import org.apache.commons.lang.StringUtils;
  35. import org.slf4j.LoggerFactory;
  36. import org.sonar.api.utils.SonarException;
  37. import org.sonar.batch.Batch;
  38. import org.sonar.batch.bootstrapper.EnvironmentInformation;
  39. import org.sonar.batch.bootstrapper.ProjectDefinition;
  40. import org.sonar.batch.bootstrapper.Reactor;
  41. public class Launcher {
  42. private Runner runner;
  43. public Launcher(Runner runner) {
  44. this.runner = runner;
  45. }
  46. /**
  47. * This method invoked from {@link Main}. Do not rename it.
  48. */
  49. public void execute() {
  50. initLogging();
  51. executeBatch();
  52. }
  53. private void executeBatch() {
  54. ProjectDefinition project = defineProject();
  55. Reactor reactor = new Reactor(project);
  56. Batch batch = new Batch(getInitialConfiguration(project), new EnvironmentInformation("Runner", runner.getRunnerVersion()), reactor);
  57. batch.execute();
  58. }
  59. private void initLogging() {
  60. LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
  61. JoranConfigurator jc = new JoranConfigurator();
  62. jc.setContext(context);
  63. context.reset();
  64. InputStream input = Batch.class.getResourceAsStream("/org/sonar/batch/logback.xml");
  65. System.setProperty("ROOT_LOGGER_LEVEL", runner.isDebug() ? "DEBUG" : "INFO");
  66. try {
  67. jc.doConfigure(input);
  68. } catch (JoranException e) {
  69. throw new SonarException("can not initialize logging", e);
  70. } finally {
  71. IOUtils.closeQuietly(input);
  72. }
  73. }
  74. private ProjectDefinition defineProject() {
  75. File baseDir = runner.getProjectDir();
  76. Properties properties = runner.getProperties();
  77. ProjectDefinition definition = new ProjectDefinition(baseDir, runner.getWorkDir(), properties);
  78. for (String dir : getList(properties, "sources")) {
  79. definition.addSourceDir(dir);
  80. }
  81. for (String dir : getList(properties, "tests")) {
  82. definition.addTestDir(dir);
  83. }
  84. for (String dir : getList(properties, "binaries")) {
  85. definition.addBinaryDir(dir);
  86. }
  87. for (String pattern : getList(properties, "libraries")) {
  88. for (File file : getLibraries(baseDir, pattern)) {
  89. definition.addLibrary(file.getAbsolutePath());
  90. }
  91. }
  92. return definition;
  93. }
  94. /**
  95. * Returns files matching specified pattern.
  96. * Visibility has been relaxed to make code testable.
  97. */
  98. static File[] getLibraries(File baseDir, String pattern) {
  99. final int i = Math.max(pattern.lastIndexOf('/'), pattern.lastIndexOf('\\'));
  100. final String dirPath, filePattern;
  101. if (i == -1) {
  102. dirPath = ".";
  103. filePattern = pattern;
  104. } else {
  105. dirPath = pattern.substring(0, i);
  106. filePattern = pattern.substring(i + 1);
  107. }
  108. FileFilter fileFilter = new AndFileFilter(FileFileFilter.FILE, new WildcardFileFilter(filePattern));
  109. File dir = resolvePath(baseDir, dirPath);
  110. File[] files = dir.listFiles(fileFilter);
  111. if (files == null || files.length == 0) {
  112. throw new RunnerException("No files matching pattern \"" + filePattern + "\" in directory \"" + dir + "\"");
  113. }
  114. return files;
  115. }
  116. private static File resolvePath(File baseDir, String path) {
  117. File file = new File(path);
  118. if (!file.isAbsolute()) {
  119. try {
  120. file = new File(baseDir, path).getCanonicalFile();
  121. } catch (IOException e) {
  122. throw new RunnerException("Unable to resolve path \"" + path + "\"", e);
  123. }
  124. }
  125. return file;
  126. }
  127. private String[] getList(Properties properties, String key) {
  128. return StringUtils.split(properties.getProperty(key, ""), ',');
  129. }
  130. private Configuration getInitialConfiguration(ProjectDefinition project) {
  131. CompositeConfiguration configuration = new CompositeConfiguration();
  132. configuration.addConfiguration(new SystemConfiguration());
  133. configuration.addConfiguration(new EnvironmentConfiguration());
  134. configuration.addConfiguration(new MapConfiguration(project.getProperties()));
  135. return configuration;
  136. }
  137. }