bd4051
#!/usr/bin/env bash
bd4051
# Configuration files
bd4051
#
bd4051
# /etc/default/jetty
bd4051
#   If it exists, this is read at the start of script. It may perform any
bd4051
#   sequence of shell commands, like setting relevant environment variables.
bd4051
#
bd4051
# /etc/jetty.conf
bd4051
#   If found, and no configurations were given on the command line,
bd4051
#   the file will be used as this script's configuration.
bd4051
#   Each line in the file may contain:
bd4051
#     - A comment denoted by the pound (#) sign as first non-blank character.
bd4051
#     - The path to a regular file, which will be passed to jetty as a
bd4051
#       config.xml file.
bd4051
#     - The path to a directory. Each *.xml file in the directory will be
bd4051
#       passed to jetty as a config.xml file.
bd4051
#     - All other lines will be passed, as-is to the start.jar
bd4051
#
bd4051
#   The files will be checked for existence before being passed to jetty.
bd4051
#
bd4051
# Configuration variables
bd4051
#
bd4051
# JAVA
bd4051
#   Command to invoke Java. If not set, java (from the PATH) will be used.
bd4051
#
bd4051
# JAVA_OPTIONS
bd4051
#   Extra options to pass to the JVM
bd4051
#
bd4051
# JETTY_HOME
bd4051
#   Where Jetty is installed. If not set, the script will try go
bd4051
#   guess it by first looking at the invocation path for the script,
bd4051
#   and then by looking in standard locations as $HOME/opt/jetty
bd4051
#   and /opt/jetty. The java system property "jetty.home" will be
bd4051
#   set to this value for use by configure.xml files, f.e.:
bd4051
#
bd4051
#    <Arg><Property name="jetty.home" default="."/>/webapps/jetty.war</Arg>
bd4051
#
bd4051
# JETTY_BASE
bd4051
#   Where your Jetty base directory is.  If not set, the value from
bd4051
#   $JETTY_HOME will be used.
bd4051
#
bd4051
# JETTY_ARGS
bd4051
#   The default arguments to pass to jetty.
bd4051
#   For example
bd4051
#      JETTY_ARGS=jetty.port=8080 jetty.spdy.port=8443 jetty.secure.port=443
bd4051
#
bd4051
bd4051
set -e -C
bd4051
bd4051
readConfig()
bd4051
{
bd4051
  echo "Reading $1.."
bd4051
  source "$1"
bd4051
}
bd4051
bd4051
CONFIGS=()
bd4051
bd4051
if [ -f /etc/default/jetty ]; then
bd4051
  readConfig /etc/default/jetty
bd4051
fi
bd4051
bd4051
if [ -z "$JETTY_HOME" ]; then
bd4051
    JETTY_HOME=/usr/share/jetty
bd4051
fi
bd4051
bd4051
if [ -z "$JETTY_BASE" ]; then
bd4051
  JETTY_BASE="$JETTY_HOME"
bd4051
fi
bd4051
bd4051
cd "$JETTY_BASE"
bd4051
JETTY_BASE="$PWD"
bd4051
bd4051
if [ -z "$JETTY_CONF" ]
bd4051
then
bd4051
  JETTY_CONF=/etc/jetty.conf
bd4051
fi
bd4051
bd4051
if [ -f "$JETTY_CONF" ] && [ -r "$JETTY_CONF" ]
bd4051
then
bd4051
  while read -r CONF
bd4051
  do
bd4051
    if expr "$CONF" : '#' >/dev/null ; then
bd4051
      continue
bd4051
    fi
bd4051
bd4051
    if [ -d "$CONF" ]
bd4051
    then
bd4051
      # assume it's a directory with configure.xml files
bd4051
      # for example: /etc/jetty.d/
bd4051
      # sort the files before adding them to the list of JETTY_ARGS
bd4051
      for XMLFILE in "$CONF/"*.xml
bd4051
      do
bd4051
        if [ -r "$XMLFILE" ] && [ -f "$XMLFILE" ]
bd4051
        then
bd4051
          JETTY_ARGS+=("$XMLFILE")
bd4051
        else
bd4051
          echo "** WARNING: Cannot read '$XMLFILE' specified in '$JETTY_CONF'"
bd4051
        fi
bd4051
      done
bd4051
    else
bd4051
      # assume it's a command line parameter (let start.jar deal with its validity)
bd4051
      JETTY_ARGS+=("$CONF")
bd4051
    fi
bd4051
  done < "$JETTY_CONF"
bd4051
fi
bd4051
bd4051
if [ -z "$JAVA" ]
bd4051
then
bd4051
    . /usr/share/java-utils/java-functions
bd4051
    set_jvm
bd4051
    set_javacmd
bd4051
    JAVA="$JAVACMD"
bd4051
fi
bd4051
bd4051
if [ -z "$JETTY_LOGS" ] && [ -d $JETTY_BASE/logs ]
bd4051
then
bd4051
  JETTY_LOGS=/var/log/jetty/logs
bd4051
fi
bd4051
JAVA_OPTIONS+=("-Djetty.logs=$JETTY_LOGS")
bd4051
bd4051
JAVA_OPTIONS+=("-Djetty.home=$JETTY_HOME" "-Djetty.base=$JETTY_BASE")
bd4051
bd4051
JETTY_START="$JETTY_HOME/start.jar"
bd4051
START_INI="$JETTY_BASE/start.ini"
bd4051
if [ ! -f "$START_INI" ]
bd4051
then
bd4051
  echo "Cannot find a start.ini in your JETTY_BASE directory: $JETTY_BASE" 2>&2
bd4051
  exit 1
bd4051
fi
bd4051
bd4051
RUN_ARGS=(${JAVA_OPTIONS[@]} -jar "$JETTY_START" ${JETTY_ARGS[*]})
bd4051
RUN_CMD=("$JAVA" ${RUN_ARGS[@]})
bd4051
bd4051
echo -n "Starting Jetty: "
bd4051
${RUN_CMD[*]}