Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Commit

Permalink
Borrow some features from the bazel java_binary wrapper (#23)
Browse files Browse the repository at this point in the history
* Make --jvm_flag and --jvm_flags work
* Keep $JVM_FLAGS if it is set
* Don't break if $classpath gets ridiculously huge
  • Loading branch information
benley authored and kchodorow committed Oct 26, 2016
1 parent 30aebd0 commit a645e2c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
bazel-*
57 changes: 48 additions & 9 deletions appengine/appengine_runner.sh.template
Expand Up @@ -24,12 +24,12 @@ if [[ -z "$JAVA_RUNFILES" ]]; then
fi
fi

jvm_bin=${JAVA_RUNFILES}/%{java}
jvm_bin="${JAVA_RUNFILES}/%{java}"
if [[ ! -x ${jvm_bin} ]]; then
jvm_bin=$(which java)
fi

APP_ENGINE_ROOT=${JAVA_RUNFILES}/%{appengine_sdk}
APP_ENGINE_ROOT="${JAVA_RUNFILES}/%{appengine_sdk}"
main_class="com.google.appengine.tools.development.DevAppServerMain"
classpath="%{classpath}"

Expand All @@ -41,12 +41,51 @@ classpath="%{classpath}"
cd "%{data_path}"

mkdir -p WEB-INF/lib
for i in $(echo $classpath | tr ":" "\n")
do
jar="WEB-INF/lib/$(basename $i)"
rm -f "$jar"
ln -s "$i" "$jar"
for i in $(echo $classpath | tr ":" "\n"); do
jar="WEB-INF/lib/$(basename "$i")"
rm -f "$jar"
ln -s "$i" "$jar"
done

${jvm_bin} -Dappengine.sdk.root=${APP_ENGINE_ROOT} -cp "${classpath}" \
${main_class} "." "$@"
JVM_FLAGS_CMDLINE=()

# Processes an argument for the wrapper. Returns 0 if the given argument
# was recognized as an argument for this wrapper, and 1 if it was not.
function process_wrapper_argument() {
case "$1" in
--jvm_flag=*) JVM_FLAGS_CMDLINE+=( "${1#--jvm_flag=}" ) ;;
--jvm_flags=*) JVM_FLAGS_CMDLINE+=( ${1#--jvm_flags=} ) ;;
*)
return 1 ;;
esac
return 0
}

# Parse arguments sequentially until the first unrecognized arg is encountered.
# Scan the remaining args for --wrapper_script_flag=X options and process them.
ARGS=()
for ARG in "$@"; do
if [[ "$ARG" == --wrapper_script_flag=* ]]; then
process_wrapper_argument "${ARG#--wrapper_script_flag=}" \
|| die "invalid wrapper argument '%s'" "$ARG"
elif [[ "${#ARGS}" -gt 0 ]] || ! process_wrapper_argument "$ARG"; then
ARGS+=( "$ARG" )
fi
done

ARGS=(
${JVM_FLAGS}
"${JVM_FLAGS_CMDLINE[@]}"
"-Dappengine.sdk.root=${APP_ENGINE_ROOT}"
${main_class}
.
"${ARGS[@]}"
)

# Linux per-arg limit MAX_ARG_STRLEN == 128k!
if (("${#classpath}" > 120000)); then
set +o posix # Enable process substitution.
exec "${jvm_bin}" -classpath @<(echo "${classpath}") "${ARGS[@]}"
else
exec "${jvm_bin}" -classpath "${classpath}" "${ARGS[@]}"
fi

0 comments on commit a645e2c

Please sign in to comment.