Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
5ee28a08
Commit
5ee28a08
authored
Jun 11, 2017
by
JustinKSU
Committed by
Andy Wilkinson
Sep 26, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support inlining a conf script into the default launch script
See gh-9590
parent
c79b7684
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
86 additions
and
31 deletions
+86
-31
deployment.adoc
spring-boot-docs/src/main/asciidoc/deployment.adoc
+5
-0
DefaultLaunchScript.java
...pringframework/boot/loader/tools/DefaultLaunchScript.java
+59
-31
launch.script
...urces/org/springframework/boot/loader/tools/launch.script
+3
-0
DefaultLaunchScriptTests.java
...framework/boot/loader/tools/DefaultLaunchScriptTests.java
+18
-0
example.script
...pring-boot-loader-tools/src/test/resources/example.script
+1
-0
No files found.
spring-boot-docs/src/main/asciidoc/deployment.adoc
View file @
5ee28a08
...
...
@@ -760,6 +760,11 @@ for Gradle and to `${project.name}` for Maven.
|`confFolder`
|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`
|The default value for `LOG_FOLDER`. Only valid for an `init.d` service.
...
...
spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/DefaultLaunchScript.java
View file @
5ee28a08
...
...
@@ -16,34 +16,35 @@
package
org
.
springframework
.
boot
.
loader
.
tools
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.nio.charset.Charset
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
org.springframework.util.FileCopyUtils
;
/**
* Default implementation of {@link LaunchScript}. Provides the default Spring Boot launch
* script or can load a specific script File. Also support mustache style template
* expansion of the form <code>{{name:default}}</code>.
*
* @author Phillip Webb
* @author Justin Rosenberg
* @since 1.3.0
*/
public
class
DefaultLaunchScript
implements
LaunchScript
{
private
static
final
Charset
UTF_8
=
Charset
.
forName
(
"UTF-8"
);
private
static
final
int
BUFFER_SIZE
=
4096
;
private
static
final
Pattern
PLACEHOLDER_PATTERN
=
Pattern
.
compile
(
"\\{\\{(\\w+)(:.*?)?\\}\\}(?!\\})"
);
private
static
final
List
<
String
>
FILE_PATH_KEYS
=
Arrays
.
asList
(
"inlinedConfScript"
);
private
final
String
content
;
/**
...
...
@@ -57,45 +58,52 @@ public class DefaultLaunchScript implements LaunchScript {
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
{
final
byte
[]
fileBytes
;
if
(
file
==
null
)
{
return
loadContent
(
getClass
().
getResourceAsStream
(
"launch.script"
));
fileBytes
=
FileCopyUtils
.
copyToByteArray
(
getClass
().
getResourceAsStream
(
"launch.script"
));
}
return
loadContent
(
new
FileInputStream
(
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
();
else
{
fileBytes
=
FileCopyUtils
.
copyToByteArray
(
file
);
}
return
new
String
(
fileBytes
,
UTF_8
);
}
private
void
copy
(
InputStream
inputStream
,
OutputStream
outputStream
)
/**
* Replaces variable placeholders in file with specified property values.
* @param content String with variables defined in {{variable:default}} format.
* @param properties Key value pairs for variables to replace
* @return Updated String
* @throws IOException if a file property value or path is specified and the file
* cannot be loaded.
*/
private
String
expandPlaceholders
(
String
content
,
Map
<?,
?>
properties
)
throws
IOException
{
byte
[]
buffer
=
new
byte
[
BUFFER_SIZE
];
int
bytesRead
=
-
1
;
while
((
bytesRead
=
inputStream
.
read
(
buffer
))
!=
-
1
)
{
outputStream
.
write
(
buffer
,
0
,
bytesRead
);
}
outputStream
.
flush
();
}
private
String
expandPlaceholders
(
String
content
,
Map
<?,
?>
properties
)
{
StringBuffer
expanded
=
new
StringBuffer
();
Matcher
matcher
=
PLACEHOLDER_PATTERN
.
matcher
(
content
);
while
(
matcher
.
find
())
{
String
name
=
matcher
.
group
(
1
);
String
value
=
matcher
.
group
(
2
);
final
String
value
;
String
defaultValue
=
matcher
.
group
(
2
);
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
{
value
=
(
value
==
null
?
matcher
.
group
(
0
)
:
value
.
substring
(
1
));
value
=
(
defaultValue
==
null
?
matcher
.
group
(
0
)
:
defaultValue
.
substring
(
1
));
}
matcher
.
appendReplacement
(
expanded
,
value
.
replace
(
"$"
,
"\\$"
));
}
...
...
@@ -103,6 +111,26 @@ public class DefaultLaunchScript implements LaunchScript {
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
{
if
(
propertyValue
instanceof
File
)
{
return
loadContent
((
File
)
propertyValue
);
}
else
{
return
loadContent
(
new
File
(
propertyValue
.
toString
()));
}
}
/**
* The content of the launch script as a byte array.
* @return Byte representation of script.
*/
@Override
public
byte
[]
toByteArray
()
{
return
this
.
content
.
getBytes
(
UTF_8
);
...
...
spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script
100755 → 100644
View file @
5ee28a08
...
...
@@ -46,6 +46,9 @@ done
jarfolder
=
"
$(
(
cd
"
$(
dirname
"
$jarfile
"
)
"
&&
pwd
-P
)
)"
cd
"
$WORKING_DIR
"
||
exit
1
# Inline script specified in build properties
{{
inlinedConfScript:
}}
# Source any config file
configfile
=
"
$(
basename
"
${
jarfile
%.*
}
.conf"
)
"
...
...
spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/DefaultLaunchScriptTests.java
View file @
5ee28a08
...
...
@@ -17,6 +17,7 @@
package
org
.
springframework
.
boot
.
loader
.
tools
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -126,6 +127,14 @@ public class DefaultLaunchScriptTests {
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
public
void
defaultForUseStartStopDaemonIsTrue
()
throws
Exception
{
DefaultLaunchScript
script
=
new
DefaultLaunchScript
(
null
,
null
);
...
...
@@ -185,6 +194,15 @@ public class DefaultLaunchScriptTests {
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
public
void
expandVariablesWithDefaultsOverride
()
throws
Exception
{
File
file
=
this
.
temporaryFolder
.
newFile
();
...
...
spring-boot-tools/spring-boot-loader-tools/src/test/resources/example.script
0 → 100644
View file @
5ee28a08
FOO=BAR
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment