aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/sonar/runner/Runner.java
blob: f6685d1e2e5f76987859b701f5459d8d6cfb2148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Sonar Runner
 * Copyright (C) 2011 SonarSource
 * dev@sonar.codehaus.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.runner;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * <p>
 * Sonar Runner class that can be used to launch Sonar analyses.
 * </p>
 * <p>
 * Configuration is all done through properties:
 * </p>
 * <ul>
 * <li>"sonar.projectDir": the base directory of the project to analyse (this can also be passed via the {@link #create(Properties, File)} constructor)</li>
 * <li>"sonar.working.directory": the working directory, which is "${sonar.projectDir}/.sonar" by default.</li>
 * <li>"sonar.verbose": if set to "true", more information is displayed in the log</li>
 * <li>"sonar.environment.information.key" and "sonar.environment.information.version": can be used to overwrite environment information (can also be
 * set via {@link #setEnvironmentInformation(String, String)} method)</li>
 * <li>... plus all the other Sonar and Sonar plugins properties.</li>
 * </ul>
 *
 * @since 1.1
 */
public final class Runner {

  /**
   * Old property used to activate debug level for logging.
   *
   * @deprecated Replaced by sonar.verbose since 1.2
   */
  @Deprecated
  public static final String PROPERTY_OLD_DEBUG_MODE = "runner.debug";

  /**
   * Property used to increase logging information.
   *
   * @since 1.2
   */
  public static final String PROPERTY_VERBOSE = "sonar.verbose";

  /**
   * Property used to specify the working directory for the runner. May be a relative or absolute path.
   *
   * @since 1.4
   */
  public static final String PROPERTY_WORK_DIRECTORY = "sonar.working.directory";

  /**
   * Default value of the working directory.
   */
  public static final String DEF_VALUE_WORK_DIRECTORY = ".sonar";

  /**
   * Property used to specify the base directory of the project to analyse.
   *
   * @since 1.5
   */
  public static final String PROPERTY_SONAR_PROJECT_BASEDIR = "sonar.projectBaseDir";

  /**
   * Property used to specify the name of the tool that will run a Sonar analysis.
   *
   * @since 1.5
   */
  public static final String PROPERTY_ENVIRONMENT_INFORMATION_KEY = "sonar.environment.information.key";

  /**
   * Property used to specify the version of the tool that will run a Sonar analysis.
   *
   * @since 1.5
   */
  public static final String PROPERTY_ENVIRONMENT_INFORMATION_VERSION = "sonar.environment.information.version";

  /**
   * Array of prefixes of versions of Sonar without support of this runner.
   */
  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"};
  private static final String[] UNSUPPORTED_VERSIONS_FOR_TASKS = {"1", "2", "3.0", "3.1", "3.2", "3.3", "3.4"};

  private static final String PROPERTY_SOURCE_ENCODING = "sonar.sourceEncoding";

  private String command;
  private File projectDir;
  private File workDir;
  private String[] unmaskedPackages;
  private List<Object> containerExtensions = new ArrayList<Object>();
  private Properties globalProperties;
  private Properties projectProperties;
  private boolean isEncodingPlatformDependant;

  private Runner(String command, Properties globalProperties, Properties projectProperties) {
    this.command = command;
    this.globalProperties = globalProperties;
    this.projectProperties = projectProperties;
    this.unmaskedPackages = new String[0];
    // set the default values for the Sonar Runner - they can be overriden with #setEnvironmentInformation
    this.globalProperties.put(PROPERTY_ENVIRONMENT_INFORMATION_KEY, "Runner");
    this.globalProperties.put(PROPERTY_ENVIRONMENT_INFORMATION_VERSION, Version.getVersion());
    // sets the encoding if not forced
    if (!globalProperties.containsKey(PROPERTY_SOURCE_ENCODING) && !projectProperties.containsKey(PROPERTY_SOURCE_ENCODING)) {
      isEncodingPlatformDependant = true;
      globalProperties.setProperty(PROPERTY_SOURCE_ENCODING, Charset.defaultCharset().name());
    }
    // and init the directories
    initDirs();
  }

  /**
   * Creates a Runner based only on the given properties.
   */
  public static Runner create(Properties props) {
    return create(null, new Properties(), props);
  }

  /**
   * Creates a Runner based only on the given properties.
   */
  public static Runner create(String command, Properties globalProperties, Properties projectProperties) {
    return new Runner(command, globalProperties, projectProperties);
  }

  /**
   * Creates a Runner based only on the properties and with the given base directory.
   */
  public static Runner create(Properties props, File basedir) {
    return create(null, new Properties(), props, basedir);
  }

  /**
   * Creates a Runner based only on the properties and with the given base directory.
   */
  public static Runner create(String command, Properties globalProperties, Properties projectProperties, File basedir) {
    projectProperties.put(PROPERTY_SONAR_PROJECT_BASEDIR, basedir.getAbsolutePath());
    return new Runner(command, globalProperties, projectProperties);
  }

  /**
   * Runs a Sonar analysis.
   */
  public void execute() {
    Bootstrapper bootstrapper = new Bootstrapper("SonarRunner/" + Version.getVersion(), getSonarServerURL(), getWorkDir());
    checkSonarVersion(bootstrapper);
    delegateExecution(createClassLoader(bootstrapper));
  }

  public String getSonarServerURL() {
    return projectProperties.getProperty("sonar.host.url", globalProperties.getProperty("sonar.host.url", "http://localhost:9000"));
  }

  private void initDirs() {
    String path = projectProperties.getProperty(PROPERTY_SONAR_PROJECT_BASEDIR, ".");
    projectDir = new File(path);
    if (!projectDir.isDirectory()) {
      throw new RunnerException("Project home must be an existing directory: " + path);
    }
    // project home exists: add its absolute path as "sonar.runner.projectDir" property
    projectProperties.put(PROPERTY_SONAR_PROJECT_BASEDIR, projectDir.getAbsolutePath());
    workDir = initWorkDir();
  }

  private File initWorkDir() {
    File newWorkDir;
    String customWorkDir = projectProperties.getProperty(PROPERTY_WORK_DIRECTORY, globalProperties.getProperty(PROPERTY_WORK_DIRECTORY));
    if (customWorkDir == null || "".equals(customWorkDir.trim())) {
      newWorkDir = new File(getProjectDir(), DEF_VALUE_WORK_DIRECTORY);
    }
    else {
      newWorkDir = defineCustomizedWorkDir(new File(customWorkDir));
    }
    IOUtils.deleteQuietly(newWorkDir);
    return newWorkDir;
  }

  private File defineCustomizedWorkDir(File customWorkDir) {
    if (customWorkDir.isAbsolute()) {
      return customWorkDir;
    }
    return new File(getProjectDir(), customWorkDir.getPath());
  }

  /**
   * @return the project base directory
   */
  public File getProjectDir() {
    return projectDir;
  }

  /**
   * @return work directory, default is ".sonar" in project directory
   */
  public File getWorkDir() {
    return workDir;
  }

  /**
   * @return the source code encoding that will be used by Sonar
   */
  public String getSourceCodeEncoding() {
    return projectProperties.getProperty(PROPERTY_SOURCE_ENCODING, globalProperties.getProperty(PROPERTY_SOURCE_ENCODING));
  }

  /**
   * @return true if the property "sonar.sourceEncoding" hasn't been forced
   */
  public boolean isEncodingPlatformDependant() {
    return isEncodingPlatformDependant;
  }

  public String getCommand() {
    return command;
  }

  /**
   * @return global properties, project properties and command-line properties
   */
  protected Properties getProperties() {
    Properties props = new Properties();
    props.putAll(globalProperties);
    props.putAll(projectProperties);
    return props;
  }

  protected void checkSonarVersion(Bootstrapper bootstrapper) {
    String serverVersion = bootstrapper.getServerVersion();
    if (isUnsupportedVersion(serverVersion)) {
      throw new RunnerException("Sonar " + serverVersion
        + " is not supported. Please upgrade Sonar to version 2.11 or more.");
    }
    if (command != null && isUnsupportedVersionForTasks(serverVersion)) {
      throw new RunnerException("Sonar " + serverVersion
        + " doesn't support tasks. Please upgrade Sonar to version 3.5 or more.");
    }
  }

  private BootstrapClassLoader createClassLoader(Bootstrapper bootstrapper) {
    URL url = getJarPath();
    return bootstrapper.createClassLoader(
        new URL[] {url}, // Add JAR with Sonar Runner - it's a Jar which contains this class
        getClass().getClassLoader(),
        unmaskedPackages);
  }

  /**
   * For unknown reasons <code>getClass().getProtectionDomain().getCodeSource().getLocation()</code> doesn't work under Ant 1.7.0.
   * So this is a workaround.
   *
   * @return Jar which contains this class
   */
  public static URL getJarPath() {
    String pathToClass = "/" + Runner.class.getName().replace('.', '/') + ".class";
    URL url = Runner.class.getResource(pathToClass);
    if (url != null) {
      String path = url.toString();
      String uri = null;
      if (path.startsWith("jar:file:")) {
        int bang = path.indexOf('!');
        uri = path.substring(4, bang);
      } else if (path.startsWith("file:")) {
        int tail = path.indexOf(pathToClass);
        uri = path.substring(0, tail);
      }
      if (uri != null) {
        try {
          return new URL(uri);
        } catch (MalformedURLException e) { // NOSONAR
        }
      }
    }
    return null;
  }

  static boolean isUnsupportedVersion(String version) {
    for (String unsupportedVersion : UNSUPPORTED_VERSIONS) {
      if (isVersion(version, unsupportedVersion)) {
        return true;
      }
    }
    return false;
  }

  static boolean isUnsupportedVersionForTasks(String version) {
    for (String unsupportedVersion : UNSUPPORTED_VERSIONS_FOR_TASKS) {
      if (isVersion(version, unsupportedVersion)) {
        return true;
      }
    }
    return false;
  }

  static boolean isVersion(String version, String prefix) {
    return version.startsWith(prefix + ".") || version.equals(prefix);
  }

  private void delegateExecution(BootstrapClassLoader sonarClassLoader) {
    ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(sonarClassLoader);
      Class<?> launcherClass = sonarClassLoader.findClass("org.sonar.runner.internal.batch.Launcher");
      Constructor<?> constructor = launcherClass.getConstructor(String.class, Properties.class, Properties.class, List.class);
      Object launcher = constructor.newInstance(getCommand(), globalProperties, projectProperties, containerExtensions);
      Method method = launcherClass.getMethod("execute");
      method.invoke(launcher);
    } catch (InvocationTargetException e) {
      // Unwrap original exception
      throw new RunnerException("Unable to execute Sonar", e.getTargetException());
    } catch (Exception e) {
      // Catch all other exceptions, which relates to reflection
      throw new RunnerException("Unable to execute Sonar", e);
    } finally {
      Thread.currentThread().setContextClassLoader(oldContextClassLoader);
    }
  }

  /**
   * Allows to overwrite the environment information when Sonar Runner is embedded in a specific tool (for instance, with the Sonar Ant Task).
   *
   * @param key     the key of the tool that embeds Sonar Runner
   * @param version the version of this tool
   */
  public void setEnvironmentInformation(String key, String version) {
    this.globalProperties.put(PROPERTY_ENVIRONMENT_INFORMATION_KEY, key);
    this.globalProperties.put(PROPERTY_ENVIRONMENT_INFORMATION_VERSION, version);
  }

  public void setUnmaskedPackages(String... unmaskedPackages) {
    this.unmaskedPackages = unmaskedPackages;
  }

  public void addContainerExtension(Object extension) {
    containerExtensions.add(extension);
  }

}