1#!/bin/bash
2#
3# Startup script for jetty under *nix systems (it works under NT/cygwin too).
4#
5# Configuration files
6#
7# /etc/default/jetty
8#   If it exists, this is read at the start of script. It may perform any
9#   sequence of shell commands, like setting relevant environment variables.
10#
11# $HOME/.jettyrc
12#   If it exists, this is read at the start of script. It may perform any
13#   sequence of shell commands, like setting relevant environment variables.
14#
15# /etc/jetty.conf
16#   If found, and no configurations were given on the command line,
17#   the file will be used as this script's configuration.
18#   Each line in the file may contain:
19#     - A comment denoted by the pound (#) sign as first non-blank character.
20#     - The path to a regular file, which will be passed to jetty as a
21#       config.xml file.
22#     - The path to a directory. Each *.xml file in the directory will be
23#       passed to jetty as a config.xml file.
24#
25#   The files will be checked for existence before being passed to jetty.
26#
27# $JETTY_HOME/etc/jetty.xml
28#   If found, used as this script's configuration file, but only if
29#   /etc/jetty.conf was not present. See above.
30#
31# Configuration variables
32#
33# JAVA_HOME
34#   Home of Java installation.
35#
36# JAVA
37#   Command to invoke Java. If not set, $JAVA_HOME/bin/java will be
38#   used.
39#
40# JAVA_OPTIONS
41#   Extra options to pass to the JVM
42#
43# JETTY_HOME
44#   Where Jetty is installed. If not set, the script will try go
45#   guess it by first looking at the invocation path for the script,
46#   and then by looking in standard locations as $HOME/opt/jetty
47#   and /opt/jetty. The java system property "jetty.home" will be
48#   set to this value for use by configure.xml files, f.e.:
49#
50#    <Arg><SystemProperty name="jetty.home" default="."/>/webapps/jetty.war</Arg>
51#
52# JETTY_PORT
53#   Override the default port for Jetty servers. If not set then the
54#   default value in the xml configuration file will be used. The java
55#   system property "jetty.port" will be set to this value for use in
56#   configure.xml files. For example, the following idiom is widely
57#   used in the demo config files to respect this property in Listener
58#   configuration elements:
59#
60#    <Set name="Port"><SystemProperty name="jetty.port" default="8080"/></Set>
61#
62#   Note: that the config file could ignore this property simply by saying:
63#
64#    <Set name="Port">8080</Set>
65#
66# JETTY_RUN
67#   Where the jetty.pid file should be stored. It defaults to the
68#   first available of /var/run, /usr/var/run, and /tmp if not set.
69#
70# JETTY_PID
71#   The Jetty PID file, defaults to $JETTY_RUN/jetty.pid
72#
73# JETTY_ARGS
74#   The default arguments to pass to jetty.
75#
76# JETTY_USER
77#   if set, then used as a username to run the server as
78#
79
80usage()
81{
82    echo "Usage: $0 {start|stop|run|restart|check|supervise} [ CONFIGS ... ] "
83    exit 1
84}
85
86[ $# -gt 0 ] || usage
87
88
89##################################################
90# Some utility functions
91##################################################
92findDirectory()
93{
94    OP=$1
95    shift
96    for L in $* ; do
97        [ $OP $L ] || continue
98        echo $L
99        break
100    done
101}
102
103running()
104{
105    [ -f $1 ] || return 1
106    PID=$(cat $1)
107    ps -p $PID >/dev/null 2>/dev/null || return 1
108    return 0
109}
110
111
112
113
114
115
116
117##################################################
118# Get the action & configs
119##################################################
120
121ACTION=$1
122shift
123ARGS="$*"
124CONFIGS=""
125NO_START=0
126
127##################################################
128# See if there's a default configuration file
129##################################################
130if [ -f /etc/default/jetty6 ] ; then
131  . /etc/default/jetty6
132elif [ -f /etc/default/jetty ] ; then
133  . /etc/default/jetty
134fi
135
136
137##################################################
138# See if there's a user-specific configuration file
139##################################################
140if [ -f $HOME/.jettyrc ] ; then
141  . $HOME/.jettyrc
142fi
143
144##################################################
145# Set tmp if not already set.
146##################################################
147
148if [ -z "$TMP" ]
149then
150  TMP=/tmp
151fi
152
153##################################################
154# Jetty's hallmark
155##################################################
156JETTY_INSTALL_TRACE_FILE="etc/jetty.xml"
157TMPJ=$TMP/j$$
158
159
160##################################################
161# Try to determine JETTY_HOME if not set
162##################################################
163if [ -z "$JETTY_HOME" ]
164then
165  JETTY_HOME_1=`dirname "$0"`
166  JETTY_HOME_1=`dirname "$JETTY_HOME_1"`
167  if [ -f "${JETTY_HOME_1}/${JETTY_INSTALL_TRACE_FILE}" ] ;
168  then
169     JETTY_HOME=${JETTY_HOME_1}
170  fi
171fi
172
173
174##################################################
175# if no JETTY_HOME, search likely locations.
176##################################################
177if [ "$JETTY_HOME" = "" ] ; then
178  STANDARD_LOCATIONS="           \
179        /usr/share               \
180        /usr/share/java          \
181        $HOME                    \
182        $HOME/src                \
183        ${HOME}/opt/             \
184        /opt                     \
185        /java                    \
186        /usr/local               \
187        /usr/local/share         \
188        /usr/local/share/java    \
189        /home                    \
190        "
191  JETTY_DIR_NAMES="              \
192        jetty-6                  \
193        jetty6                   \
194        jetty-6.*                \
195        jetty                    \
196        Jetty-6                  \
197        Jetty6                   \
198        Jetty-6.*                \
199        Jetty                    \
200        "
201
202  JETTY_HOME=
203  for L in $STANDARD_LOCATIONS
204  do
205     for N in $JETTY_DIR_NAMES
206     do
207         if [ -d $L/$N ] && [ -f "$L/${N}/${JETTY_INSTALL_TRACE_FILE}" ] ;
208         then
209            JETTY_HOME="$L/$N"
210         fi
211     done
212     [ ! -z "$JETTY_HOME" ] && break
213  done
214fi
215
216
217##################################################
218# No JETTY_HOME yet? We're out of luck!
219##################################################
220if [ -z "$JETTY_HOME" ] ; then
221    echo "** ERROR: JETTY_HOME not set, you need to set it or install in a standard location"
222    exit 1
223fi
224
225cd $JETTY_HOME
226JETTY_HOME=`pwd`
227
228
229#####################################################
230# Check that jetty is where we think it is
231#####################################################
232if [ ! -r $JETTY_HOME/$JETTY_INSTALL_TRACE_FILE ]
233then
234   echo "** ERROR: Oops! Jetty doesn't appear to be installed in $JETTY_HOME"
235   echo "** ERROR:  $JETTY_HOME/$JETTY_INSTALL_TRACE_FILE is not readable!"
236   exit 1
237fi
238
239
240###########################################################
241# Get the list of config.xml files from the command line.
242###########################################################
243if [ ! -z "$ARGS" ]
244then
245  for A in $ARGS
246  do
247    if [ -f $A ]
248    then
249       CONF="$A"
250    elif [ -f $JETTY_HOME/etc/$A ]
251    then
252       CONF="$JETTY_HOME/etc/$A"
253    elif [ -f ${A}.xml ]
254    then
255       CONF="${A}.xml"
256    elif [ -f $JETTY_HOME/etc/${A}.xml ]
257    then
258       CONF="$JETTY_HOME/etc/${A}.xml"
259    else
260       echo "** ERROR: Cannot find configuration '$A' specified in the command line."
261       exit 1
262    fi
263    if [ ! -r $CONF ]
264    then
265       echo "** ERROR: Cannot read configuration '$A' specified in the command line."
266       exit 1
267    fi
268    CONFIGS="$CONFIGS $CONF"
269  done
270fi
271
272
273##################################################
274# Try to find this script's configuration file,
275# but only if no configurations were given on the
276# command line.
277##################################################
278if [ -z "$JETTY_CONF" ]
279then
280  if [ -f /etc/jetty.conf ]
281  then
282     JETTY_CONF=/etc/jetty.conf
283  elif [ -f "${JETTY_HOME}/etc/jetty.conf" ]
284  then
285     JETTY_CONF="${JETTY_HOME}/etc/jetty.conf"
286  fi
287fi
288
289##################################################
290# Read the configuration file if one exists
291##################################################
292CONFIG_LINES=
293if [ -z "$CONFIGS" ] && [ -f "$JETTY_CONF" ] && [ -r "$JETTY_CONF" ]
294then
295  CONFIG_LINES=`cat $JETTY_CONF | grep -v "^[:space:]*#" | tr "\n" " "`
296fi
297
298##################################################
299# Get the list of config.xml files from jetty.conf
300##################################################
301if [ ! -z "${CONFIG_LINES}" ]
302then
303  for CONF in ${CONFIG_LINES}
304  do
305    if [ ! -r "$CONF" ]
306    then
307      echo "** WARNING: Cannot read '$CONF' specified in '$JETTY_CONF'"
308    elif [ -f "$CONF" ]
309    then
310      # assume it's a configure.xml file
311      CONFIGS="$CONFIGS $CONF"
312    elif [ -d "$CONF" ]
313    then
314      # assume it's a directory with configure.xml files
315      # for example: /etc/jetty.d/
316      # sort the files before adding them to the list of CONFIGS
317      XML_FILES=`ls ${CONF}/*.xml | sort | tr "\n" " "`
318      for FILE in ${XML_FILES}
319      do
320         if [ -r "$FILE" ] && [ -f "$FILE" ]
321         then
322            CONFIGS="$CONFIGS $FILE"
323         else
324           echo "** WARNING: Cannot read '$FILE' specified in '$JETTY_CONF'"
325         fi
326      done
327    else
328      echo "** WARNING: Don''t know what to do with '$CONF' specified in '$JETTY_CONF'"
329    fi
330  done
331fi
332
333#####################################################
334# Run the standard server if there's nothing else to run
335#####################################################
336if [ -z "$CONFIGS" ]
337then
338    CONFIGS="${JETTY_HOME}/etc/jetty-logging.xml ${JETTY_HOME}/etc/jetty.xml"
339fi
340
341
342#####################################################
343# Find a location for the pid file
344#####################################################
345if [  -z "$JETTY_RUN" ]
346then
347  JETTY_RUN=`findDirectory -w /var/run /usr/var/run /tmp`
348fi
349
350#####################################################
351# Find a PID for the pid file
352#####################################################
353if [  -z "$JETTY_PID" ]
354then
355  JETTY_PID="$JETTY_RUN/jetty.pid"
356fi
357
358
359##################################################
360# Check for JAVA_HOME
361##################################################
362if [ -z "$JAVA_HOME" ]
363then
364    # If a java runtime is not defined, search the following
365    # directories for a JVM and sort by version. Use the highest
366    # version number.
367
368    # Java search path
369    JAVA_LOCATIONS="\
370        /usr/java \
371        /usr/bin \
372        /usr/local/bin \
373        /usr/local/java \
374        /usr/local/jdk \
375        /usr/local/jre \
376	/usr/lib/jvm \
377        /opt/java \
378        /opt/jdk \
379        /opt/jre \
380    "
381    JAVA_NAMES="java jdk jre"
382    for N in $JAVA_NAMES ; do
383        for L in $JAVA_LOCATIONS ; do
384            [ -d $L ] || continue
385            find $L -name "$N" ! -type d | grep -v threads | while read J ; do
386                [ -x $J ] || continue
387                VERSION=`eval $J -version 2>&1`
388                [ $? = 0 ] || continue
389                VERSION=`expr "$VERSION" : '.*"\(1.[0-9\.]*\)["_]'`
390                [ "$VERSION" = "" ] && continue
391                expr $VERSION \< 1.2 >/dev/null && continue
392                echo $VERSION:$J
393            done
394        done
395    done | sort | tail -1 > $TMPJ
396    JAVA=`cat $TMPJ | cut -d: -f2`
397    JVERSION=`cat $TMPJ | cut -d: -f1`
398
399    JAVA_HOME=`dirname $JAVA`
400    while [ ! -z "$JAVA_HOME" -a "$JAVA_HOME" != "/" -a ! -f "$JAVA_HOME/lib/tools.jar" ] ; do
401        JAVA_HOME=`dirname $JAVA_HOME`
402    done
403    [ "$JAVA_HOME" = "" ] && JAVA_HOME=
404
405    echo "Found JAVA=$JAVA in JAVA_HOME=$JAVA_HOME"
406fi
407
408
409##################################################
410# Determine which JVM of version >1.2
411# Try to use JAVA_HOME
412##################################################
413if [ "$JAVA" = "" -a "$JAVA_HOME" != "" ]
414then
415  if [ ! -z "$JAVACMD" ]
416  then
417     JAVA="$JAVACMD"
418  else
419    [ -x $JAVA_HOME/bin/jre -a ! -d $JAVA_HOME/bin/jre ] && JAVA=$JAVA_HOME/bin/jre
420    [ -x $JAVA_HOME/bin/java -a ! -d $JAVA_HOME/bin/java ] && JAVA=$JAVA_HOME/bin/java
421  fi
422fi
423
424if [ "$JAVA" = "" ]
425then
426    echo "Cannot find a JRE or JDK. Please set JAVA_HOME to a >=1.2 JRE" 2>&2
427    exit 1
428fi
429
430JAVA_VERSION=`expr "$($JAVA -version 2>&1 | head -1)" : '.*1\.\([0-9]\)'`
431
432#####################################################
433# See if JETTY_PORT is defined
434#####################################################
435if [ "$JETTY_PORT" != "" ]
436then
437  JAVA_OPTIONS="$JAVA_OPTIONS -Djetty.port=$JETTY_PORT"
438fi
439
440#####################################################
441# See if JETTY_LOGS is defined
442#####################################################
443if [ "$JETTY_LOGS" != "" ]
444then
445  JAVA_OPTIONS="$JAVA_OPTIONS -Djetty.logs=$JETTY_LOGS"
446fi
447
448#####################################################
449# Are we running on Windows? Could be, with Cygwin/NT.
450#####################################################
451case "`uname`" in
452CYGWIN*) PATH_SEPARATOR=";";;
453*) PATH_SEPARATOR=":";;
454esac
455
456
457#####################################################
458# Add jetty properties to Java VM options.
459#####################################################
460JAVA_OPTIONS="$JAVA_OPTIONS -Djetty.home=$JETTY_HOME -Djava.io.tmpdir=$TMP"
461
462[ -f $JETTY_HOME/etc/start.config ] && JAVA_OPTIONS="-DSTART=$JETTY_HOME/etc/start.config $JAVA_OPTIONS"
463
464#####################################################
465# This is how the Jetty server will be started
466#####################################################
467
468JETTY_START=$JETTY_HOME/start.jar
469[ ! -f $JETTY_START ] && JETTY_START=$JETTY_HOME/lib/start.jar
470
471RUN_ARGS="$JAVA_OPTIONS -jar $JETTY_START $JETTY_ARGS $CONFIGS"
472RUN_CMD="$JAVA $RUN_ARGS"
473
474#####################################################
475# Comment these out after you're happy with what
476# the script is doing.
477#####################################################
478#echo "JETTY_HOME     =  $JETTY_HOME"
479#echo "JETTY_CONF     =  $JETTY_CONF"
480#echo "JETTY_RUN      =  $JETTY_RUN"
481#echo "JETTY_PID      =  $JETTY_PID"
482#echo "JETTY_ARGS     =  $JETTY_ARGS"
483#echo "CONFIGS        =  $CONFIGS"
484#echo "JAVA_OPTIONS   =  $JAVA_OPTIONS"
485#echo "JAVA           =  $JAVA"
486
487
488##################################################
489# Do the action
490##################################################
491case "$ACTION" in
492  start)
493        echo -n "Starting Jetty: "
494
495        if [ "$NO_START" = "1" ]; then
496	  echo "Not starting jetty - NO_START=1 in /etc/default/jetty6";
497          exit 0;
498	fi
499
500
501	if type start-stop-daemon > /dev/null 2>&1
502	then
503          [ x$JETTY_USER = x ] && JETTY_USER=$(whoami)
504	  [ $UID = 0 ] && CH_USER="-c $JETTY_USER"
505	  if start-stop-daemon -S -p$JETTY_PID $CH_USER -d $JETTY_HOME -b -m -a $JAVA -- $RUN_ARGS
506	  then
507	      sleep 1
508	      if running $JETTY_PID
509	      then
510                  echo OK
511              else
512                  echo FAILED
513              fi
514	  fi
515
516	else
517
518          if [ -f $JETTY_PID ]
519          then
520            if running $JETTY_PID
521            then
522              echo "Already Running!!"
523              exit 1
524            else
525              # dead pid file - remove
526              rm -f $JETTY_PID
527            fi
528          fi
529
530          if [ x$JETTY_USER != x ]
531          then
532              touch $JETTY_PID
533              chown $JETTY_USER $JETTY_PID
534              su - $JETTY_USER -c "
535                $RUN_CMD &
536                PID=\$!
537                disown \$PID
538                echo \$PID > $JETTY_PID"
539          else
540              $RUN_CMD &
541              PID=$!
542              disown $PID
543              echo $PID > $JETTY_PID
544          fi
545
546          echo "STARTED Jetty `date`"
547        fi
548
549        ;;
550
551  stop)
552        echo -n "Stopping Jetty: "
553	if type start-stop-daemon > /dev/null 2>&1; then
554	  start-stop-daemon -K -p $JETTY_PID -d $JETTY_HOME -a $JAVA -s HUP
555	  sleep 1
556	  if running $JETTY_PID
557	  then
558	      sleep 3
559	      if running $JETTY_PID
560	      then
561		  sleep 30
562	          if running $JETTY_PID
563	          then
564	             start-stop-daemon -K -p $JETTY_PID -d $JETTY_HOME -a $JAVA -s KILL
565		  fi
566              fi
567	  fi
568
569	  rm -f $JETTY_PID
570          echo OK
571	else
572	  PID=`cat $JETTY_PID 2>/dev/null`
573          TIMEOUT=30
574          while running $JETTY_PID && [ $TIMEOUT -gt 0 ]
575          do
576            kill $PID 2>/dev/null
577            sleep 1
578            let TIMEOUT=$TIMEOUT-1
579          done
580
581          [ $TIMEOUT -gt 0 ] || kill -9 $PID 2>/dev/null
582
583	  rm -f $JETTY_PID
584          echo OK
585	fi
586        ;;
587
588  restart)
589        JETTY_SH=$0
590        if [ ! -f $JETTY_SH ]; then
591          if [ ! -f $JETTY_HOME/bin/jetty.sh ]; then
592            echo "$JETTY_HOME/bin/jetty.sh does not exist."
593            exit 1
594          fi
595          JETTY_SH=$JETTY_HOME/bin/jetty.sh
596        fi
597        $JETTY_SH stop $*
598        sleep 5
599        $JETTY_SH start $*
600        ;;
601
602  supervise)
603       #
604       # Under control of daemontools supervise monitor which
605       # handles restarts and shutdowns via the svc program.
606       #
607         exec $RUN_CMD
608         ;;
609
610  run|demo)
611        echo "Running Jetty: "
612
613        if [ -f $JETTY_PID ]
614        then
615            if running $JETTY_PID
616            then
617              echo "Already Running!!"
618              exit 1
619            else
620              # dead pid file - remove
621              rm -f $JETTY_PID
622            fi
623        fi
624
625        exec $RUN_CMD
626        ;;
627
628  check)
629        echo "Checking arguments to Jetty: "
630        echo "JETTY_HOME     =  $JETTY_HOME"
631        echo "JETTY_CONF     =  $JETTY_CONF"
632        echo "JETTY_RUN      =  $JETTY_RUN"
633        echo "JETTY_PID      =  $JETTY_PID"
634        echo "JETTY_PORT     =  $JETTY_PORT"
635        echo "JETTY_LOGS     =  $JETTY_LOGS"
636        echo "CONFIGS        =  $CONFIGS"
637        echo "JAVA_OPTIONS   =  $JAVA_OPTIONS"
638        echo "JAVA           =  $JAVA"
639        echo "CLASSPATH      =  $CLASSPATH"
640        echo "RUN_CMD        =  $RUN_CMD"
641        echo
642
643        if [ -f $JETTY_RUN/jetty.pid ]
644        then
645            echo "Jetty running pid="`cat $JETTY_RUN/jetty.pid`
646            exit 0
647        fi
648        exit 1
649        ;;
650
651*)
652        usage
653	;;
654esac
655
656exit 0
657
658
659
660