Skip to content

Commit 66a3054

Browse files
committed
Added support for java options config file and log4j config
Fixes #75 Fixes #76
1 parent 1970afe commit 66a3054

File tree

4 files changed

+124
-8
lines changed

4 files changed

+124
-8
lines changed

src/main/assembly/assembly.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@
5353
<fileMode>0755</fileMode>
5454
<lineEnding>lf</lineEnding>
5555
</fileSet>
56+
<fileSet>
57+
<directory>${project.basedir}/src/main/config/</directory>
58+
<outputDirectory></outputDirectory>
59+
<includes>
60+
<include>java-options.conf</include>
61+
<include>log4j.xml</include>
62+
</includes>
63+
<directoryMode>0755</directoryMode>
64+
<fileMode>0644</fileMode>
65+
</fileSet>
5666
<fileSet>
5767
<directory>examples/</directory>
5868
<outputDirectory>examples/</outputDirectory>

src/main/config/java-options.conf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# JVM options in this file are passed to the java command.
2+
# Environment variables can be specified by prefixing them
3+
# with $ (e.g. $VAR). The variable $METAFACTURE_HOME refers
4+
# to the folder containing the flux start-up script.
5+
# Undefined variables remain in the configuration.
6+
7+
-Xmx512m
8+
-Dlog4j.configurationFile="$METAFACTURE_HOME/log4j.xml"
9+
10+
# Append additional options defined in the
11+
# environment (The start-up script ensures
12+
# that this variable is always defined):
13+
$FLUX_JAVA_OPTIONS

src/main/scripts/flux.bat

100644100755
Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,52 @@
1-
@echo off
2-
java -Xmx512M -jar "%~dp0${project.build.finalName}.jar" %*
1+
@ECHO OFF
2+
3+
SETLOCAL EnableDelayedExpansion
4+
5+
SET METAFACTURE_HOME=%~dp0
6+
7+
REM Use the java command available on the path by default.
8+
REM Define FLUX_JAVA_BIN in your environment to use a
9+
REM different executable.
10+
IF "x%FLUX_JAVA_BIN%" == "x" (
11+
SET FLUX_JAVA_BIN=java
12+
)
13+
14+
SET JAVA_OPTS_FILE="%METAFACTURE_HOME%java-options.conf"
15+
SET JAR_FILE="%METAFACTURE_HOME%${project.build.finalName}.jar"
16+
17+
REM Read JVM options from configuration file. Lines starting
18+
REM with # are treated as comments. Empty lines are ignored.
19+
REM
20+
REM The space character at the end of the following
21+
REM line is important and must not be removed:
22+
SET JAVA_OPTS=
23+
<%JAVA_OPTS_FILE% (
24+
FOR /F %%I IN ('FINDSTR /N "^" %JAVA_OPTS_FILE%') DO (
25+
SET /P LINE=
26+
IF NOT "x!LINE!" == "x" (
27+
SET FIRST=!LINE:~0,1!
28+
IF NOT "!FIRST!" == "#" (
29+
SET JAVA_OPTS=!JAVA_OPTS! !LINE!
30+
)
31+
)
32+
SET LINE=
33+
)
34+
)
35+
36+
REM Substitute environment variables in the configuration.
37+
REM Undefined variables remain in the configuration. Since
38+
REM FLUX_JAVA_OPTIONS is included in the configuration by
39+
REM default we make sure that it can always be substituted.
40+
IF "x%FLUX_JAVA_OPTIONS%" == "x" (
41+
REM The space character at the end of the following
42+
REM line is important and must not be removed:
43+
SET FLUX_JAVA_OPTIONS=
44+
)
45+
FOR /F "tokens=1,* delims==" %%I IN ('SET') DO (
46+
SET JAVA_OPTS=!JAVA_OPTS:$%%I=%%J!
47+
)
48+
49+
REM Start flux:
50+
%FLUX_JAVA_BIN% !JAVA_OPTS! -jar %JAR_FILE% %*
51+
52+
ENDLOCAL

src/main/scripts/flux.sh

100644100755
Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,56 @@
11
#!/bin/bash
22

3-
dir=`dirname $0`
3+
METAFACTURE_HOME=$( dirname "$( realpath "$0" )" )
44

5-
jarfile="${project.build.finalName}.jar"
5+
# Fix path if running under cygwin:
6+
if uname | grep -iq cygwin ; then
7+
METAFACTURE_HOME=$( cygpath -am "$METAFACTURE_HOME" )
8+
fi
9+
10+
# Use the java command available on the path by default.
11+
# Define FLUX_JAVA_BIN in your environment to use a
12+
# different executable.
13+
if [ -z "$FLUX_JAVA_BIN" ] ; then
14+
FLUX_JAVA_BIN=java
15+
fi
16+
17+
java_opts_file="$METAFACTURE_HOME/java-options.conf"
18+
jar_file="$METAFACTURE_HOME/${project.build.finalName}.jar"
19+
20+
# Load java options from configuration file. Lines starting
21+
# with # are treated as comments. Empty lines are ignored.
22+
java_opts=$( cat "$java_opts_file" | grep -v "^#" | tr "\n\r" " " )
623

7-
if uname | grep -iq cygwin; then
8-
java -Xmx512M -jar "`cygpath -am $dir/$jarfile`" $*
9-
else
10-
java -Xmx512M -jar "$dir/$jarfile" $*
24+
# Substitute environment variables. Undefined variables
25+
# remain in the configuration. Since FLUX_JAVA_OPTIONS is
26+
# included in the configuration by default we make sure that
27+
# it can always be substituted.
28+
if [ -z "$FLUX_JAVA_OPTIONS" ] ; then
29+
FLUX_JAVA_OPTIONS=
1130
fi
31+
# This sed script turns the output of set into a sed script
32+
# which replaces the dollar prefixed name of each environment
33+
# variable with its value:
34+
vars_to_script=$( cat <<'EOF'
35+
s/\\/\\\\/g ;
36+
s/!/\\!/g ;
37+
s/='(.*)'$/=\1/ ;
38+
s/^([^=]+)=(.*)$/s!\\$\1!\2!g ; /g
39+
EOF
40+
)
41+
substitute_vars_script=$( set | sed -r "$vars_to_script" )
42+
# Substitute environment variables in the java options:
43+
java_opts=$( echo "$java_opts" | sed "$substitute_vars_script")
1244

45+
# Turn java options string into an array to allow passing
46+
# the options as command parameters. Options may be partially
47+
# quoted with single or double quotes and may contain
48+
# escape sequences:
49+
option_pattern="[^\"' ]*(\"[^\"\\]*(\\\\.[^\"\\]*)*\"|'[^'\\]*(\\\\.[^'\\]*)*'|\\.[^\"' ]*)*"
50+
java_opts_array=()
51+
while read line ; do
52+
java_opts_array+=("$line")
53+
done < <( echo "$java_opts" | grep -Eo "$option_pattern" )
1354

55+
# Start flux:
56+
"$FLUX_JAVA_BIN" "${java_opts_array[@]}" -jar "$jar_file" "$@"

0 commit comments

Comments
 (0)