Skip to content

Commit fa2136e

Browse files
committed
Escape Java options + parse java properties files properly
1 parent ef12f74 commit fa2136e

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

bin/spark-class

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,41 @@ if $cygwin; then
146146
fi
147147
export CLASSPATH
148148

149+
# Properly escape java options, dealing with whitespace, double quotes and backslashes
150+
# This accepts a string, and returns the escaped list through ESCAPED_JAVA_OPTS
151+
escape_java_options() {
152+
ESCAPED_JAVA_OPTS=() # return value
153+
option_buffer="" # buffer for collecting parts of an option
154+
opened_quotes=0 # whether we are expecting a closing double quotes
155+
for word in $1; do
156+
contains_quote=$(echo "$word" | grep \" | grep -v \\\\\")
157+
if [ -n "$contains_quote" ]; then
158+
# Flip the bit
159+
opened_quotes=$(((opened_quotes + 1) % 2))
160+
fi
161+
if [[ $opened_quotes == 0 ]]; then
162+
ESCAPED_JAVA_OPTS+=("$(echo "$option_buffer $word" | sed "s/^[[:space:]]*//" | sed "s/\([^\\]\)\"/\1/g")")
163+
option_buffer=""
164+
else
165+
option_buffer="$option_buffer $word"
166+
fi
167+
done
168+
# Something is wrong if we ended with open double quotes
169+
if [[ $opened_quotes == 1 ]]; then
170+
echo "Java options parse error! Expecting closing double quotes." 1>&2
171+
exit 1
172+
fi
173+
}
174+
175+
escape_java_options "$JAVA_OPTS"
176+
for option in "${ESCAPED_JAVA_OPTS[@]}"; do
177+
echo "$option"
178+
done
179+
149180
if [ "$SPARK_PRINT_LAUNCH_COMMAND" == "1" ]; then
150181
echo -n "Spark Command: " 1>&2
151-
echo "$RUNNER" -cp "$CLASSPATH" $JAVA_OPTS "$@" 1>&2
182+
echo "$RUNNER" -cp "$CLASSPATH" "${ESCAPED_JAVA_OPTS[@]}" "$@" 1>&2
152183
echo -e "========================================\n" 1>&2
153184
fi
154185

155-
exec "$RUNNER" -cp "$CLASSPATH" $JAVA_OPTS "$@"
186+
exec "$RUNNER" -cp "$CLASSPATH" "${ESCAPED_JAVA_OPTS[@]}" "$@"

bin/spark-submit

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ if [ $DEPLOY_MODE == "client" ]; then
5252
if [ -f "$PROPERTIES_FILE" ]; then
5353
echo "Using properties file $PROPERTIES_FILE." 1>&2
5454

55-
# Parse the value of the given config
56-
# This removes the "=" sign, whitespace, and double quotes around the value (if any)
55+
# Parse the value of the given config according to the specifications outlined in
56+
# http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.Reader)
5757
parse_config() {
5858
result=$( \
59-
sed "/^#/ d" "$PROPERTIES_FILE" | \
59+
sed "/^[#!]/ d" "conf/spark-defaults.conf" | \
6060
grep "$1" | \
61-
sed "s/$1//g" | \
62-
sed "s/^[[:space:]]*=//g" | \
63-
sed "s/^[[:space:]]*\"\(.*\)\"[[:space:]]*$/\1/g" | \
61+
sed "s/$1//" | \
62+
sed "s/^[[:space:]]*[:=]\{0,1\}//" | \
6463
sed "s/^[[:space:]]*\(.*\)[[:space:]]*$/\1/g" \
6564
)
6665
}

0 commit comments

Comments
 (0)