Commit 1e4e64aa authored by Andy Wilkinson's avatar Andy Wilkinson

Polish "Support inlining a conf script into the default launch script"

Closes gh-9590
parent 5ee28a08
/* /*
* 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.
...@@ -16,17 +16,21 @@ ...@@ -16,17 +16,21 @@
package org.springframework.boot.loader.tools; package org.springframework.boot.loader.tools;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; 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;
import org.springframework.util.FileCopyUtils;
/** /**
* Default implementation of {@link LaunchScript}. Provides the default Spring Boot launch * Default implementation of {@link LaunchScript}. Provides the default Spring Boot launch
* script or can load a specific script File. Also support mustache style template * script or can load a specific script File. Also support mustache style template
...@@ -40,10 +44,13 @@ public class DefaultLaunchScript implements LaunchScript { ...@@ -40,10 +44,13 @@ public class DefaultLaunchScript implements LaunchScript {
private static final Charset UTF_8 = Charset.forName("UTF-8"); private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final int BUFFER_SIZE = 4096;
private static final Pattern PLACEHOLDER_PATTERN = Pattern private static final Pattern PLACEHOLDER_PATTERN = Pattern
.compile("\\{\\{(\\w+)(:.*?)?\\}\\}(?!\\})"); .compile("\\{\\{(\\w+)(:.*?)?\\}\\}(?!\\})");
private static final List<String> FILE_PATH_KEYS = Arrays.asList("inlinedConfScript"); private static final Set<String> FILE_PATH_KEYS = Collections
.unmodifiableSet(new HashSet<>(Arrays.asList("inlinedConfScript")));
private final String content; private final String content;
...@@ -58,32 +65,34 @@ public class DefaultLaunchScript implements LaunchScript { ...@@ -58,32 +65,34 @@ public class DefaultLaunchScript implements LaunchScript {
this.content = expandPlaceholders(content, properties); this.content = expandPlaceholders(content, properties);
} }
/**
* Loads file contents.
* @param file File to load. If null, will load default launch.script
* @return String representation of file contents.
* @throws IOException if file is not found our can't be loaded.
*/
private String loadContent(File file) throws IOException { private String loadContent(File file) throws IOException {
final byte[] fileBytes;
if (file == null) { if (file == null) {
fileBytes = FileCopyUtils return loadContent(getClass().getResourceAsStream("launch.script"));
.copyToByteArray(getClass().getResourceAsStream("launch.script"));
} }
else { return loadContent(new FileInputStream(file));
fileBytes = FileCopyUtils.copyToByteArray(file); }
private String loadContent(InputStream inputStream) throws IOException {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
copy(inputStream, outputStream);
return new String(outputStream.toByteArray(), UTF_8);
}
finally {
inputStream.close();
} }
return new String(fileBytes, UTF_8);
} }
/** private void copy(InputStream inputStream, OutputStream outputStream)
* Replaces variable placeholders in file with specified property values. throws IOException {
* @param content String with variables defined in {{variable:default}} format. byte[] buffer = new byte[BUFFER_SIZE];
* @param properties Key value pairs for variables to replace int bytesRead = -1;
* @return Updated String while ((bytesRead = inputStream.read(buffer)) != -1) {
* @throws IOException if a file property value or path is specified and the file outputStream.write(buffer, 0, bytesRead);
* cannot be loaded. }
*/ outputStream.flush();
}
private String expandPlaceholders(String content, Map<?, ?> properties) private String expandPlaceholders(String content, Map<?, ?> properties)
throws IOException { throws IOException {
StringBuffer expanded = new StringBuffer(); StringBuffer expanded = new StringBuffer();
...@@ -111,13 +120,6 @@ public class DefaultLaunchScript implements LaunchScript { ...@@ -111,13 +120,6 @@ public class DefaultLaunchScript implements LaunchScript {
return expanded.toString(); return expanded.toString();
} }
/**
* Loads file based on File object or String path.
* @param propertyValue File Object or String path to file.
* @return File contents.
* @throws IOException if a file property value or path is specified and the file
* cannot be loaded.
*/
private String parseFilePropertyValue(Object propertyValue) throws IOException { private String parseFilePropertyValue(Object propertyValue) throws IOException {
if (propertyValue instanceof File) { if (propertyValue instanceof File) {
return loadContent((File) propertyValue); return loadContent((File) propertyValue);
...@@ -127,10 +129,6 @@ public class DefaultLaunchScript implements LaunchScript { ...@@ -127,10 +129,6 @@ public class DefaultLaunchScript implements LaunchScript {
} }
} }
/**
* The content of the launch script as a byte array.
* @return Byte representation of script.
*/
@Override @Override
public byte[] toByteArray() { public byte[] toByteArray() {
return this.content.getBytes(UTF_8); return this.content.getBytes(UTF_8);
......
...@@ -34,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -34,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 {
......
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