Skip to content

Commit 1e4e64a

Browse files
committed
Polish "Support inlining a conf script into the default launch script"
Closes gh-9590
1 parent 5ee28a0 commit 1e4e64a

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

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

Lines changed: 34 additions & 36 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.
@@ -16,17 +16,21 @@
1616

1717
package org.springframework.boot.loader.tools;
1818

19+
import java.io.ByteArrayOutputStream;
1920
import java.io.File;
21+
import java.io.FileInputStream;
2022
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.OutputStream;
2125
import java.nio.charset.Charset;
2226
import java.util.Arrays;
23-
import java.util.List;
27+
import java.util.Collections;
28+
import java.util.HashSet;
2429
import java.util.Map;
30+
import java.util.Set;
2531
import java.util.regex.Matcher;
2632
import java.util.regex.Pattern;
2733

28-
import org.springframework.util.FileCopyUtils;
29-
3034
/**
3135
* Default implementation of {@link LaunchScript}. Provides the default Spring Boot launch
3236
* script or can load a specific script File. Also support mustache style template
@@ -40,10 +44,13 @@ public class DefaultLaunchScript implements LaunchScript {
4044

4145
private static final Charset UTF_8 = Charset.forName("UTF-8");
4246

47+
private static final int BUFFER_SIZE = 4096;
48+
4349
private static final Pattern PLACEHOLDER_PATTERN = Pattern
4450
.compile("\\{\\{(\\w+)(:.*?)?\\}\\}(?!\\})");
4551

46-
private static final List<String> FILE_PATH_KEYS = Arrays.asList("inlinedConfScript");
52+
private static final Set<String> FILE_PATH_KEYS = Collections
53+
.unmodifiableSet(new HashSet<>(Arrays.asList("inlinedConfScript")));
4754

4855
private final String content;
4956

@@ -58,32 +65,34 @@ public DefaultLaunchScript(File file, Map<?, ?> properties) throws IOException {
5865
this.content = expandPlaceholders(content, properties);
5966
}
6067

61-
/**
62-
* Loads file contents.
63-
* @param file File to load. If null, will load default launch.script
64-
* @return String representation of file contents.
65-
* @throws IOException if file is not found our can't be loaded.
66-
*/
6768
private String loadContent(File file) throws IOException {
68-
final byte[] fileBytes;
6969
if (file == null) {
70-
fileBytes = FileCopyUtils
71-
.copyToByteArray(getClass().getResourceAsStream("launch.script"));
70+
return loadContent(getClass().getResourceAsStream("launch.script"));
7271
}
73-
else {
74-
fileBytes = FileCopyUtils.copyToByteArray(file);
72+
return loadContent(new FileInputStream(file));
73+
}
74+
75+
private String loadContent(InputStream inputStream) throws IOException {
76+
try {
77+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
78+
copy(inputStream, outputStream);
79+
return new String(outputStream.toByteArray(), UTF_8);
80+
}
81+
finally {
82+
inputStream.close();
7583
}
76-
return new String(fileBytes, UTF_8);
7784
}
7885

79-
/**
80-
* Replaces variable placeholders in file with specified property values.
81-
* @param content String with variables defined in {{variable:default}} format.
82-
* @param properties Key value pairs for variables to replace
83-
* @return Updated String
84-
* @throws IOException if a file property value or path is specified and the file
85-
* cannot be loaded.
86-
*/
86+
private void copy(InputStream inputStream, OutputStream outputStream)
87+
throws IOException {
88+
byte[] buffer = new byte[BUFFER_SIZE];
89+
int bytesRead = -1;
90+
while ((bytesRead = inputStream.read(buffer)) != -1) {
91+
outputStream.write(buffer, 0, bytesRead);
92+
}
93+
outputStream.flush();
94+
}
95+
8796
private String expandPlaceholders(String content, Map<?, ?> properties)
8897
throws IOException {
8998
StringBuffer expanded = new StringBuffer();
@@ -111,13 +120,6 @@ private String expandPlaceholders(String content, Map<?, ?> properties)
111120
return expanded.toString();
112121
}
113122

114-
/**
115-
* Loads file based on File object or String path.
116-
* @param propertyValue File Object or String path to file.
117-
* @return File contents.
118-
* @throws IOException if a file property value or path is specified and the file
119-
* cannot be loaded.
120-
*/
121123
private String parseFilePropertyValue(Object propertyValue) throws IOException {
122124
if (propertyValue instanceof File) {
123125
return loadContent((File) propertyValue);
@@ -127,10 +129,6 @@ private String parseFilePropertyValue(Object propertyValue) throws IOException {
127129
}
128130
}
129131

130-
/**
131-
* The content of the launch script as a byte array.
132-
* @return Byte representation of script.
133-
*/
134132
@Override
135133
public byte[] toByteArray() {
136134
return this.content.getBytes(UTF_8);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
*
3535
* @author Phillip Webb
3636
* @author Andy Wilkinson
37+
* @author Justin Rosenberg
3738
*/
3839
public class DefaultLaunchScriptTests {
3940

0 commit comments

Comments
 (0)