Skip to content

Commit f52c081

Browse files
committed
Merge pull request #9590 from Justin Rosenberg
* gh-9590: Polish "Support inlining a conf script into the default launch script" Support inlining a conf script into the default launch script
2 parents c79b768 + 1e4e64a commit f52c081

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

spring-boot-docs/src/main/asciidoc/deployment.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,11 @@ for Gradle and to `${project.name}` for Maven.
760760
|`confFolder`
761761
|The default value for `CONF_FOLDER`. Defaults to the folder containing the jar.
762762

763+
|`inlinedConfScript`
764+
|Reference to a file script that should be inlined in the default launch script.
765+
This can be used to set environmental variables such as `JAVA_OPTS` before
766+
any external config files are loaded.
767+
763768
|`logFolder`
764769
|The default value for `LOG_FOLDER`. Only valid for an `init.d` service.
765770

spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,11 @@
2323
import java.io.InputStream;
2424
import java.io.OutputStream;
2525
import java.nio.charset.Charset;
26+
import java.util.Arrays;
27+
import java.util.Collections;
28+
import java.util.HashSet;
2629
import java.util.Map;
30+
import java.util.Set;
2731
import java.util.regex.Matcher;
2832
import java.util.regex.Pattern;
2933

@@ -33,6 +37,7 @@
3337
* expansion of the form <code>{{name:default}}</code>.
3438
*
3539
* @author Phillip Webb
40+
* @author Justin Rosenberg
3641
* @since 1.3.0
3742
*/
3843
public class DefaultLaunchScript implements LaunchScript {
@@ -44,6 +49,9 @@ public class DefaultLaunchScript implements LaunchScript {
4449
private static final Pattern PLACEHOLDER_PATTERN = Pattern
4550
.compile("\\{\\{(\\w+)(:.*?)?\\}\\}(?!\\})");
4651

52+
private static final Set<String> FILE_PATH_KEYS = Collections
53+
.unmodifiableSet(new HashSet<>(Arrays.asList("inlinedConfScript")));
54+
4755
private final String content;
4856

4957
/**
@@ -85,24 +93,42 @@ private void copy(InputStream inputStream, OutputStream outputStream)
8593
outputStream.flush();
8694
}
8795

88-
private String expandPlaceholders(String content, Map<?, ?> properties) {
96+
private String expandPlaceholders(String content, Map<?, ?> properties)
97+
throws IOException {
8998
StringBuffer expanded = new StringBuffer();
9099
Matcher matcher = PLACEHOLDER_PATTERN.matcher(content);
91100
while (matcher.find()) {
92101
String name = matcher.group(1);
93-
String value = matcher.group(2);
102+
final String value;
103+
String defaultValue = matcher.group(2);
94104
if (properties != null && properties.containsKey(name)) {
95-
value = (String) properties.get(name);
105+
Object propertyValue = properties.get(name);
106+
if (FILE_PATH_KEYS.contains(name)) {
107+
value = parseFilePropertyValue(properties.get(name));
108+
}
109+
else {
110+
value = propertyValue.toString();
111+
}
96112
}
97113
else {
98-
value = (value == null ? matcher.group(0) : value.substring(1));
114+
value = (defaultValue == null ? matcher.group(0)
115+
: defaultValue.substring(1));
99116
}
100117
matcher.appendReplacement(expanded, value.replace("$", "\\$"));
101118
}
102119
matcher.appendTail(expanded);
103120
return expanded.toString();
104121
}
105122

123+
private String parseFilePropertyValue(Object propertyValue) throws IOException {
124+
if (propertyValue instanceof File) {
125+
return loadContent((File) propertyValue);
126+
}
127+
else {
128+
return loadContent(new File(propertyValue.toString()));
129+
}
130+
}
131+
106132
@Override
107133
public byte[] toByteArray() {
108134
return this.content.getBytes(UTF_8);

spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script

100755100644
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ done
4646
jarfolder="$( (cd "$(dirname "$jarfile")" && pwd -P) )"
4747
cd "$WORKING_DIR" || exit 1
4848

49+
# Inline script specified in build properties
50+
{{inlinedConfScript:}}
51+
4952
# Source any config file
5053
configfile="$(basename "${jarfile%.*}.conf")"
5154

spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/DefaultLaunchScriptTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.loader.tools;
1818

1919
import java.io.File;
20+
import java.io.IOException;
2021
import java.util.HashMap;
2122
import java.util.Map;
2223

@@ -33,6 +34,7 @@
3334
*
3435
* @author Phillip Webb
3536
* @author Andy Wilkinson
37+
* @author Justin Rosenberg
3638
*/
3739
public class DefaultLaunchScriptTests {
3840

@@ -126,6 +128,14 @@ public void stopWaitTimeCanBeReplaced() throws Exception {
126128
assertThatPlaceholderCanBeReplaced("stopWaitTime");
127129
}
128130

131+
@Test
132+
public void inlinedConfScriptFileLoad() throws IOException {
133+
DefaultLaunchScript script = new DefaultLaunchScript(null,
134+
createProperties("inlinedConfScript:src/test/resources/example.script"));
135+
String content = new String(script.toByteArray());
136+
assertThat(content).contains("FOO=BAR");
137+
}
138+
129139
@Test
130140
public void defaultForUseStartStopDaemonIsTrue() throws Exception {
131141
DefaultLaunchScript script = new DefaultLaunchScript(null, null);
@@ -185,6 +195,15 @@ public void expandVariablesWithDefaults() throws Exception {
185195
assertThat(content).isEqualTo("hello");
186196
}
187197

198+
@Test
199+
public void expandVariablesCanDefaultToBlank() throws Exception {
200+
File file = this.temporaryFolder.newFile();
201+
FileCopyUtils.copy("s{{p:}}{{r:}}ing".getBytes(), file);
202+
DefaultLaunchScript script = new DefaultLaunchScript(file, null);
203+
String content = new String(script.toByteArray());
204+
assertThat(content).isEqualTo("sing");
205+
}
206+
188207
@Test
189208
public void expandVariablesWithDefaultsOverride() throws Exception {
190209
File file = this.temporaryFolder.newFile();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FOO=BAR

0 commit comments

Comments
 (0)