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