-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThinJarLauncher.java
252 lines (226 loc) · 7.56 KB
/
ThinJarLauncher.java
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
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.loader.thin;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.loader.ExecutableArchiveLauncher;
import org.springframework.boot.loader.LaunchedURLClassLoader;
import org.springframework.boot.loader.archive.Archive;
import org.springframework.boot.loader.archive.Archive.Entry;
import org.springframework.boot.loader.thin.AetherEngine.ProgressType;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.SimpleCommandLinePropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.StringUtils;
/**
*
* @author Dave Syer
*/
public class ThinJarLauncher extends ExecutableArchiveLauncher {
/**
* System property key for main class to launch. Defaults to finding it via
* Start-Class of the main archive.
*/
public static final String THIN_MAIN = "thin.main";
/**
* System property to signal a "dry run" where dependencies are resolved but the main
* method is not executed.
*/
public static final String THIN_DRYRUN = "thin.dryrun";
/**
* System property to signal a "classpath run" where dependencies are resolved but the
* main method is not executed and the output is in the form of a classpath.
*/
public static final String THIN_CLASSPATH = "thin.classpath";
/**
* System property holding the path to the root directory, where Maven repository and
* settings live. Defaults to <code>${user.home}/.m2</code>.
*/
public static final String THIN_ROOT = "thin.root";
/**
* System property used by wrapper to communicate the location of the main archive.
* Can also be used in a dryrun or classpath launch to override the archive location
* with a file or "maven://..." style URL.
*/
public static final String THIN_ARCHIVE = "thin.archive";
/**
* A parent archive (URL or "maven://..." locator) that controls the classpath and
* dependency management defaults for the main archive.
*/
public static final String THIN_PARENT = "thin.parent";
/**
* The name of the launchable (i.e. the properties file name).
*/
public static final String THIN_NAME = "thin.name";
/**
* The name of the profile to run, changing the location of the properties files to
* look up.
*/
public static final String THIN_PROFILE = "thin.profile";
private ArchiveUtils archives = new ArchiveUtils();
private StandardEnvironment environment = new StandardEnvironment();
private boolean debug;
public static void main(String[] args) throws Exception {
new ThinJarLauncher(args).launch(args);
}
protected ThinJarLauncher(String[] args) throws Exception {
super(computeArchive(args));
}
@Override
protected void launch(String[] args) throws Exception {
addCommandLineProperties(args);
args = removeThinArgs(args);
String root = environment.resolvePlaceholders("${" + THIN_ROOT + ":}");
boolean classpath = !"false".equals(
environment.resolvePlaceholders("${" + THIN_CLASSPATH + ":false}"));
if (classpath) {
this.debug = false;
this.archives.setProgress(ProgressType.NONE);
}
else {
this.debug = !"false"
.equals(environment.resolvePlaceholders("${debug:false}"));
}
if (debug) {
this.archives.setProgress(ProgressType.DETAILED);
}
if (StringUtils.hasText(root)) {
// There is a grape root that is used by the aether engine
// internally
System.setProperty("grape.root", root);
}
if (classpath) {
List<Archive> archives = getClassPathArchives();
System.out.println(classpath(archives));
return;
}
if (!"false".equals(
environment.resolvePlaceholders("${" + THIN_DRYRUN + ":false}"))) {
getClassPathArchives();
if (this.debug) {
System.out.println("Downloaded dependencies"
+ (!StringUtils.hasText(root) ? "" : " to " + root));
}
return;
}
super.launch(args);
}
private String[] removeThinArgs(String[] args) {
List<String> result = new ArrayList<>();
boolean escaped = false;
for (String arg : args) {
if ("--".equals(arg)) {
escaped = true;
continue;
}
if (!escaped && arg.startsWith("--thin.")) {
continue;
}
result.add(arg);
}
return result.toArray(new String[0]);
}
private String classpath(List<Archive> archives) throws Exception {
StringBuilder builder = new StringBuilder();
String separator = System.getProperty("path.separator");
boolean first = true;
for (Archive archive : archives) {
if (!first) {
first = false;
continue;
}
if (builder.length() > 0) {
builder.append(separator);
}
builder.append(new File(archive.getUrl().toURI()).getCanonicalPath());
}
return builder.toString();
}
private void addCommandLineProperties(String[] args) {
if (args == null || args.length == 0) {
return;
}
MutablePropertySources properties = environment.getPropertySources();
SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(
"commandArgs", args);
if (!properties.contains("commandArgs")) {
properties.addFirst(source);
}
else {
properties.replace("commandArgs", source);
}
}
@Override
protected ClassLoader createClassLoader(URL[] urls) throws Exception {
return new LaunchedURLClassLoader(
ArchiveUtils.addNestedClasses(getArchive(), urls, "BOOT-INF/classes/"),
getClass().getClassLoader().getParent());
}
@Override
protected String getMainClass() throws Exception {
String mainClass = environment.resolvePlaceholders("${" + THIN_MAIN + ":}");
if (StringUtils.hasText(mainClass)) {
return mainClass;
}
return ArchiveUtils.findMainClass(getArchive());
}
@Override
protected List<Archive> getClassPathArchives() throws Exception {
String parent = environment
.resolvePlaceholders("${" + ThinJarLauncher.THIN_PARENT + ":}");
String name = environment
.resolvePlaceholders("${" + ThinJarLauncher.THIN_NAME + ":thin}");
String[] profiles = environment
.resolvePlaceholders("${" + ThinJarLauncher.THIN_PROFILE + ":}")
.split(",");
Archive parentArchive = null;
if (StringUtils.hasText(parent)) {
parentArchive = ArchiveUtils.getArchive(parent);
}
List<Archive> archives = this.archives.combine(parentArchive, getArchive(), name,
profiles);
return archives;
}
@Override
protected boolean isNestedArchive(Entry entry) {
return false;
}
private static Archive computeArchive(String[] args) throws Exception {
String path = getProperty(THIN_ARCHIVE);
for (String arg : args) {
String prefix = "--" + THIN_ARCHIVE;
if (arg.startsWith(prefix)) {
// You can always override --thin.archive on the command line
if (arg.length() <= prefix.length() + 1) {
// ... even cancel it by setting it to empty
path = null;
}
else {
path = arg.substring(prefix.length() + 1);
}
}
}
return ArchiveUtils.getArchive(path);
}
static String getProperty(String key) {
if (System.getProperty(key) != null) {
return System.getProperty(key);
}
return System.getenv(key.replace(".", "_").toUpperCase());
}
}