1616# limitations under the License.
1717#
1818
19+ # * ---------------------------------------------------- *
20+ # | Utility functions for launching Spark applications |
21+ # * ---------------------------------------------------- *
22+
1923# Parse the value of a config from a java properties file according to the specifications in
2024# http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.Reader).
2125# This accepts the name of the config and returns the value through JAVA_PROPERTY_VALUE.
26+ # This currently does not support multi-line configs.
2227parse_java_property () {
2328 JAVA_PROPERTY_VALUE=$( \
2429 sed " /^[#!]/ d" " conf/spark-defaults.conf" | \
@@ -30,22 +35,24 @@ parse_java_property() {
3035 export JAVA_PROPERTY_VALUE
3136}
3237
33- # Properly escape java options, dealing with whitespace, double quotes and backslashes
34- # This accepts a string, and returns the escaped list through ESCAPED_JAVA_OPTS.
38+ # Properly escape java options, dealing with whitespace, double quotes and backslashes.
39+ # This accepts a string and returns the escaped list through ESCAPED_JAVA_OPTS.
3540escape_java_options () {
3641 ESCAPED_JAVA_OPTS=() # return value
3742 option_buffer=" " # buffer for collecting parts of an option
3843 opened_quotes=0 # whether we are expecting a closing double quotes
3944 for word in $1 ; do
4045 contains_quote=$( echo " $word " | grep \" | grep -v \\\\\" )
41- if [ -n " $contains_quote " ]; then
46+ if [[ -n " $contains_quote " ] ]; then
4247 # Flip the bit
4348 opened_quotes=$(( (opened_quotes + 1 ) % 2 ))
4449 fi
4550 if [[ $opened_quotes == 0 ]]; then
51+ # Remove all non-escaped quotes around the value
4652 ESCAPED_JAVA_OPTS+=(" $( echo " $option_buffer $word " | sed " s/^[[:space:]]*//" | sed " s/\([^\\ ]\)\" /\1/g" ) " )
4753 option_buffer=" "
4854 else
55+ # We are expecting a closing double quote, so keep buffering
4956 option_buffer=" $option_buffer $word "
5057 fi
5158 done
@@ -57,3 +64,18 @@ escape_java_options() {
5764 export ESCAPED_JAVA_OPTS
5865}
5966
67+ # Put double quotes around each of the given java options that is a system property.
68+ # This accepts a list and returns the quoted list through QUOTED_JAVA_OPTS
69+ quote_java_property () {
70+ QUOTED_JAVA_OPTS=()
71+ for opt in " $@ " ; do
72+ is_system_property=$( echo " $opt " | grep -e " ^-D" )
73+ if [[ -n " $is_system_property " ]]; then
74+ QUOTED_JAVA_OPTS+=(\" $opt \" )
75+ else
76+ QUOTED_JAVA_OPTS+=(" $opt " )
77+ fi
78+ done
79+ export QUOTED_JAVA_OPTS
80+ }
81+
0 commit comments