Commit f52c081e authored by Andy Wilkinson's avatar Andy Wilkinson

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
parents c79b7684 1e4e64aa
...@@ -760,6 +760,11 @@ for Gradle and to `${project.name}` for Maven. ...@@ -760,6 +760,11 @@ for Gradle and to `${project.name}` for Maven.
|`confFolder` |`confFolder`
|The default value for `CONF_FOLDER`. Defaults to the folder containing the jar. |The default value for `CONF_FOLDER`. Defaults to the folder containing the jar.
|`inlinedConfScript`
|Reference to a file script that should be inlined in the default launch script.
This can be used to set environmental variables such as `JAVA_OPTS` before
any external config files are loaded.
|`logFolder` |`logFolder`
|The default value for `LOG_FOLDER`. Only valid for an `init.d` service. |The default value for `LOG_FOLDER`. Only valid for an `init.d` service.
......
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -23,7 +23,11 @@ import java.io.IOException; ...@@ -23,7 +23,11 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -33,6 +37,7 @@ import java.util.regex.Pattern; ...@@ -33,6 +37,7 @@ import java.util.regex.Pattern;
* expansion of the form <code>{{name:default}}</code>. * expansion of the form <code>{{name:default}}</code>.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Justin Rosenberg
* @since 1.3.0 * @since 1.3.0
*/ */
public class DefaultLaunchScript implements LaunchScript { public class DefaultLaunchScript implements LaunchScript {
...@@ -44,6 +49,9 @@ public class DefaultLaunchScript implements LaunchScript { ...@@ -44,6 +49,9 @@ public class DefaultLaunchScript implements LaunchScript {
private static final Pattern PLACEHOLDER_PATTERN = Pattern private static final Pattern PLACEHOLDER_PATTERN = Pattern
.compile("\\{\\{(\\w+)(:.*?)?\\}\\}(?!\\})"); .compile("\\{\\{(\\w+)(:.*?)?\\}\\}(?!\\})");
private static final Set<String> FILE_PATH_KEYS = Collections
.unmodifiableSet(new HashSet<>(Arrays.asList("inlinedConfScript")));
private final String content; private final String content;
/** /**
...@@ -85,17 +93,26 @@ public class DefaultLaunchScript implements LaunchScript { ...@@ -85,17 +93,26 @@ public class DefaultLaunchScript implements LaunchScript {
outputStream.flush(); outputStream.flush();
} }
private String expandPlaceholders(String content, Map<?, ?> properties) { private String expandPlaceholders(String content, Map<?, ?> properties)
throws IOException {
StringBuffer expanded = new StringBuffer(); StringBuffer expanded = new StringBuffer();
Matcher matcher = PLACEHOLDER_PATTERN.matcher(content); Matcher matcher = PLACEHOLDER_PATTERN.matcher(content);
while (matcher.find()) { while (matcher.find()) {
String name = matcher.group(1); String name = matcher.group(1);
String value = matcher.group(2); final String value;
String defaultValue = matcher.group(2);
if (properties != null && properties.containsKey(name)) { if (properties != null && properties.containsKey(name)) {
value = (String) properties.get(name); Object propertyValue = properties.get(name);
if (FILE_PATH_KEYS.contains(name)) {
value = parseFilePropertyValue(properties.get(name));
}
else {
value = propertyValue.toString();
}
} }
else { else {
value = (value == null ? matcher.group(0) : value.substring(1)); value = (defaultValue == null ? matcher.group(0)
: defaultValue.substring(1));
} }
matcher.appendReplacement(expanded, value.replace("$", "\\$")); matcher.appendReplacement(expanded, value.replace("$", "\\$"));
} }
...@@ -103,6 +120,15 @@ public class DefaultLaunchScript implements LaunchScript { ...@@ -103,6 +120,15 @@ public class DefaultLaunchScript implements LaunchScript {
return expanded.toString(); return expanded.toString();
} }
private String parseFilePropertyValue(Object propertyValue) throws IOException {
if (propertyValue instanceof File) {
return loadContent((File) propertyValue);
}
else {
return loadContent(new File(propertyValue.toString()));
}
}
@Override @Override
public byte[] toByteArray() { public byte[] toByteArray() {
return this.content.getBytes(UTF_8); return this.content.getBytes(UTF_8);
......
...@@ -46,6 +46,9 @@ done ...@@ -46,6 +46,9 @@ done
jarfolder="$( (cd "$(dirname "$jarfile")" && pwd -P) )" jarfolder="$( (cd "$(dirname "$jarfile")" && pwd -P) )"
cd "$WORKING_DIR" || exit 1 cd "$WORKING_DIR" || exit 1
# Inline script specified in build properties
{{inlinedConfScript:}}
# Source any config file # Source any config file
configfile="$(basename "${jarfile%.*}.conf")" configfile="$(basename "${jarfile%.*}.conf")"
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.springframework.boot.loader.tools; package org.springframework.boot.loader.tools;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -33,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -33,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Justin Rosenberg
*/ */
public class DefaultLaunchScriptTests { public class DefaultLaunchScriptTests {
...@@ -126,6 +128,14 @@ public class DefaultLaunchScriptTests { ...@@ -126,6 +128,14 @@ public class DefaultLaunchScriptTests {
assertThatPlaceholderCanBeReplaced("stopWaitTime"); assertThatPlaceholderCanBeReplaced("stopWaitTime");
} }
@Test
public void inlinedConfScriptFileLoad() throws IOException {
DefaultLaunchScript script = new DefaultLaunchScript(null,
createProperties("inlinedConfScript:src/test/resources/example.script"));
String content = new String(script.toByteArray());
assertThat(content).contains("FOO=BAR");
}
@Test @Test
public void defaultForUseStartStopDaemonIsTrue() throws Exception { public void defaultForUseStartStopDaemonIsTrue() throws Exception {
DefaultLaunchScript script = new DefaultLaunchScript(null, null); DefaultLaunchScript script = new DefaultLaunchScript(null, null);
...@@ -185,6 +195,15 @@ public class DefaultLaunchScriptTests { ...@@ -185,6 +195,15 @@ public class DefaultLaunchScriptTests {
assertThat(content).isEqualTo("hello"); assertThat(content).isEqualTo("hello");
} }
@Test
public void expandVariablesCanDefaultToBlank() throws Exception {
File file = this.temporaryFolder.newFile();
FileCopyUtils.copy("s{{p:}}{{r:}}ing".getBytes(), file);
DefaultLaunchScript script = new DefaultLaunchScript(file, null);
String content = new String(script.toByteArray());
assertThat(content).isEqualTo("sing");
}
@Test @Test
public void expandVariablesWithDefaultsOverride() throws Exception { public void expandVariablesWithDefaultsOverride() throws Exception {
File file = this.temporaryFolder.newFile(); File file = this.temporaryFolder.newFile();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment