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