Skip to content

Commit 46764c2

Browse files
committed
Support vmoptions syntax more fully
Signed-off-by: Mitch Gaffigan <mitch.gaffigan@comcast.net>
1 parent 815b7a1 commit 46764c2

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

docker/mirth-connect.sh

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@
22

33
# Mirth Connect Server Launcher Script
44

5-
# Get the directory where this script is located
6-
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
5+
APP_ARGS=("$@")
76

87
# Set MIRTH_HOME to the script directory
9-
export MIRTH_HOME="$SCRIPT_DIR"
8+
MIRTH_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
9+
CLASSPATH="$MIRTH_HOME/mirth-server-launcher.jar"
10+
VMOPTIONS=()
11+
12+
# Default JAVA_PATH based on env, JAVA_HOME, or "java"
13+
if [[ -n "$JAVA_PATH" ]]; then
14+
JAVA_PATH="$JAVA_PATH"
15+
elif [[ -n "$JAVA_HOME" && -x "$JAVA_HOME/bin/java" ]]; then
16+
JAVA_PATH="$JAVA_HOME/bin/java"
17+
else
18+
# Default to "java" if no other options are available
19+
JAVA_PATH="java"
20+
fi
1021

1122
# Set Java options
1223
parse_vmoptions() {
1324
local file="$1"
14-
local current_options="${2:-}" # Initialize with existing options, or empty
1525

1626
if [[ ! -f "$file" ]]; then
1727
echo "Error: VM options file not found: $file" >&2
@@ -28,6 +38,13 @@ parse_vmoptions() {
2838
continue
2939
fi
3040

41+
# Evaluate environment variables to be their actual values
42+
# Question: Is this a security risk?
43+
# Answer: No: vmoptions is trusted. We allow replacing JAVA_PATH and
44+
# CLASSPATH, so there is a by-design ability for someone with
45+
# write privileges to the vmoptions to execute arbitrary code.
46+
line=$(eval echo "$line")
47+
3148
# Check for -include-options directive
3249
if [[ "$line" =~ ^-include-options[[:space:]]+(.+) ]]; then
3350
local included_file="${BASH_REMATCH[1]}"
@@ -38,27 +55,39 @@ parse_vmoptions() {
3855
fi
3956

4057
# Recursively call parse_vmoptions for the included file
41-
local included_opts
42-
if ! included_opts=$(parse_vmoptions "$included_file" "$current_options"); then
43-
echo "Error processing included options from $included_file" >&2
44-
return 1
45-
fi
46-
current_options="$included_opts"
58+
parse_vmoptions "$included_file"
59+
elif [[ "$line" =~ ^-classpath[[:space:]]+(.+) ]]; then
60+
# Handle -classpath directive
61+
CLASSPATH="${BASH_REMATCH[1]}"
62+
elif [[ "$line" =~ ^-classpath/a[[:space:]]+(.+) ]]; then
63+
# Handle -classpath/a directive (append to existing classpath)
64+
CLASSPATH="${CLASSPATH}:${BASH_REMATCH[1]}"
65+
elif [[ "$line" =~ ^-classpath/p[[:space:]]+(.+) ]]; then
66+
# Handle -classpath/p directive (prepend to existing classpath)
67+
CLASSPATH="${BASH_REMATCH[1]}:${CLASSPATH}"
68+
elif [[ "$line" =~ ^-java-cmd[[:space:]]+(.+) ]]; then
69+
# Handle -java-cmd directive (set JAVA_PATH)
70+
JAVA_PATH="${BASH_REMATCH[1]}"
4771
else
4872
# Add the option to the accumulated string
49-
current_options+="${current_options:+" "}$line"
73+
VMOPTIONS+=("$line")
5074
fi
5175
done < "$file"
5276

53-
echo "$current_options"
5477
return 0
5578
}
56-
JAVA_OPTS=$(parse_vmoptions "oieserver.vmoptions")
57-
JAVA_OPTS="$JAVA_OPTS -Dmirth.home=$MIRTH_HOME"
5879

59-
# Launch Mirth Connect
60-
echo "Starting Mirth Connect..."
61-
echo "MIRTH_HOME: $MIRTH_HOME"
62-
echo "JAVA_OPTS: $JAVA_OPTS"
80+
# Recursively parse the VM options file
81+
parse_vmoptions "$MIRTH_HOME/oieserver.vmoptions"
6382

64-
java $JAVA_OPTS -jar "$MIRTH_HOME/mirth-server-launcher.jar"
83+
JAVA_OPTS=("${VMOPTIONS[@]}"
84+
"-cp" "$CLASSPATH"
85+
"com.mirth.connect.server.launcher.MirthLauncher"
86+
"${APP_ARGS[@]}")
87+
88+
# Launch Mirth Connect (as this PID with exec)
89+
echo "Starting Mirth Connect..."
90+
# This doesn't include quotes, which could be confusing. Not sure if there's a
91+
# better way to do this.
92+
echo "$JAVA_PATH ${JAVA_OPTS[*]}"
93+
exec "$JAVA_PATH" "${JAVA_OPTS[@]}"

0 commit comments

Comments
 (0)