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
|
/*
* Sonar Standalone 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 org.sonar.runner.bootstrapper.BootstrapClassLoader;
import org.sonar.runner.bootstrapper.BootstrapException;
import org.sonar.runner.bootstrapper.Bootstrapper;
import org.sonar.runner.bootstrapper.BootstrapperIOUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Properties;
/**
* @since 1.1
*/
public final class Runner {
public static final String PROPERTY_PROJECT_DIR = "sonar.runner.projectDir";
public static final String PROPERTY_RUNNER_VERSION = "sonar.runner.version";
/**
* @deprecated Replaced by sonar.verbose since 1.2
*/
@Deprecated
public static final String PROPERTY_OLD_DEBUG_MODE = "runner.debug";
/**
* @since 1.2
*/
public static final String PROPERTY_VERBOSE = "sonar.verbose";
/**
* @since 1.4
*/
public static final String PROPERTY_WORK_DIRECTORY = "sonar.working.directory";
public static final String DEF_VALUE_WORK_DIRECTORY = ".sonar";
/**
* 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"};
/**
* Array of all mandatory properties required to execute runner.
*/
private static final String[] MANDATORY_PROPERTIES = {"sonar.projectKey", "sonar.projectName", "sonar.projectVersion", "sources"};
private File projectDir;
private File workDir;
private Properties properties;
private Runner(Properties props) {
this.properties = props;
initDirs();
}
public static Runner create(Properties props) {
return new Runner(props);
}
public void execute() {
checkMandatoryProperties();
Bootstrapper bootstrapper = new Bootstrapper("SonarRunner/" + getRunnerVersion(), getServerURL(), getWorkDir());
checkSonarVersion(bootstrapper);
delegateExecution(createClassLoader(bootstrapper));
}
void checkMandatoryProperties() {
StringBuilder missing = new StringBuilder();
for (String mandatoryProperty : MANDATORY_PROPERTIES) {
if (!properties.containsKey(mandatoryProperty)) {
if (missing.length() > 0) {
missing.append(", ");
}
missing.append(mandatoryProperty);
}
}
if (missing.length() != 0) {
throw new RunnerException("You must define mandatory properties: " + missing);
}
}
public String getServerURL() {
return properties.getProperty("sonar.host.url", "http://localhost:9000");
}
private void initDirs() {
String path = properties.getProperty("project.home", ".");
projectDir = new File(path);
if (!projectDir.isDirectory() || !projectDir.exists()) {
throw new IllegalArgumentException("Project home must be an existing directory: " + path);
}
// project home exist, add its absolute path as "sonar.runner.projectDir" property
properties.put(PROPERTY_PROJECT_DIR, projectDir.getAbsolutePath());
workDir = initWorkDir();
}
private File initWorkDir() {
String customWorkDir = properties.getProperty(PROPERTY_WORK_DIRECTORY);
if (customWorkDir == null || customWorkDir.trim().length() == 0) {
return new File(projectDir, DEF_VALUE_WORK_DIRECTORY);
}
return defineCustomizedWorkDir(new File(customWorkDir));
}
private File defineCustomizedWorkDir(File customWorkDir) {
if (customWorkDir.isAbsolute()) {
return customWorkDir;
}
return new File(projectDir, customWorkDir.getPath());
}
public File getProjectDir() {
return projectDir;
}
/**
* @return work directory, default is ".sonar" in project directory
*/
public File getWorkDir() {
return workDir;
}
/**
* @return global properties, project properties and command-line properties
*/
public Properties getProperties() {
return properties;
}
public String getRunnerVersion() {
InputStream in = null;
try {
in = Runner.class.getResourceAsStream("/org/sonar/runner/version.txt");
Properties props = new Properties();
props.load(in);
return props.getProperty("version");
} catch (IOException e) {
throw new BootstrapException("Could not load the version information for Sonar Standalone Runner", e);
} finally {
BootstrapperIOUtils.closeQuietly(in);
}
}
protected void checkSonarVersion(Bootstrapper bootstrapper) {
String serverVersion = bootstrapper.getServerVersion();
if (isUnsupportedVersion(serverVersion)) {
throw new BootstrapException("Sonar " + serverVersion
+ " does not support Standalone Runner. Please upgrade Sonar to version 2.6 or more.");
}
}
private BootstrapClassLoader createClassLoader(Bootstrapper bootstrapper) {
URL url = Main.class.getProtectionDomain().getCodeSource().getLocation();
return bootstrapper.createClassLoader(
new URL[] {url}, // Add JAR with Sonar Runner - it's a Jar which contains this class
getClass().getClassLoader());
}
static boolean isUnsupportedVersion(String version) {
for (String unsupportedVersion : UNSUPPORTED_VERSIONS) {
if (isVersion(version, unsupportedVersion)) {
return true;
}
}
return false;
}
static boolean isVersion(String version, String prefix) {
return version.startsWith(prefix + ".") || version.equals(prefix);
}
/**
* Loads {@link Launcher} from specified {@link org.sonar.batch.bootstrapper.BootstrapClassLoader} and passes control to it.
*
* @see Launcher#execute()
*/
private void delegateExecution(BootstrapClassLoader sonarClassLoader) {
ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(sonarClassLoader);
Class<?> launcherClass = sonarClassLoader.findClass("org.sonar.runner.Launcher");
Constructor<?> constructor = launcherClass.getConstructor(Properties.class);
Object launcher = constructor.newInstance(getProperties());
Method method = launcherClass.getMethod("execute");
method.invoke(launcher);
} catch (InvocationTargetException e) {
// Unwrap original exception
throw new BootstrapException(e.getTargetException());
} catch (Exception e) {
// Catch all other exceptions, which relates to reflection
throw new BootstrapException(e);
} finally {
Thread.currentThread().setContextClassLoader(oldContextClassLoader);
}
}
}
|