updated on Sat Jan 21 04:00:54 UTC 2012
[aur-mirror.git] / jetty7 / jetty.rc
blob7e647247957b5daae99ed5f275b5c6716f3552ec
1 #!/bin/bash
3 # general config
4 . /etc/rc.conf
5 . /etc/rc.d/functions
7 ##################################################
8 # Some utility functions
9 ##################################################
10 findDirectory()
12 local L OP=$1
13 shift
14 for L in "$@"; do
15 [ "$OP" "$L" ] || continue
16 printf %s "$L"
17 break
18 done
21 running()
23 local PID=$(cat "$1" 2>/dev/null) || return 1
24 kill -0 "$PID" 2>/dev/null
27 readConfig()
29 (( DEBUG )) && echo "Reading $1.."
30 source "$1"
35 ##################################################
36 # Get the action & configs
37 ##################################################
38 CONFIGS=()
39 NO_START=0
40 DEBUG=0
42 while [[ $1 = -* ]]; do
43 case $1 in
44 -d) DEBUG=1 ;;
45 esac
46 shift
47 done
48 ACTION=$1
49 shift
51 ##################################################
52 # Read any configuration files
53 ##################################################
54 for CONFIG in /etc/default/jetty{,7} $HOME/.jettyrc; do
55 if [ -f "$CONFIG" ] ; then
56 readConfig "$CONFIG"
58 done
60 if [ -z "$JETTY_HOME" ]; then
61 JETTY_HOME=/opt/jetty7
64 if [ -z "$JAVA_HOME" ]; then
65 JAVA_HOME=/usr
68 ##################################################
69 # Set tmp if not already set.
70 ##################################################
71 TMPDIR=${TMPDIR:-/tmp}
73 cd "$JETTY_HOME"
75 ##################################################
76 # Try to find this script's configuration file,
77 # but only if no configurations were given on the
78 # command line.
79 ##################################################
80 if [ -z "$JETTY_CONF" ]
81 then
82 if [ -f /etc/jetty.conf ]
83 then
84 JETTY_CONF=/etc/jetty.conf
85 elif [ -f "$JETTY_HOME/etc/jetty.conf" ]
86 then
87 JETTY_CONF=$JETTY_HOME/etc/jetty.conf
91 ##################################################
92 # Get the list of config.xml files from jetty.conf
93 ##################################################
94 if [ -z "$CONFIGS" ] && [ -f "$JETTY_CONF" ] && [ -r "$JETTY_CONF" ]
95 then
96 while read -r CONF
98 if expr "$CONF" : '#' >/dev/null ; then
99 continue
102 if [ -d "$CONF" ]
103 then
104 # assume it's a directory with configure.xml files
105 # for example: /etc/jetty.d/
106 # sort the files before adding them to the list of CONFIGS
107 for file in "$CONF/"*.xml
109 if [ -r "$FILE" ] && [ -f "$FILE" ]
110 then
111 CONFIGS+=("$FILE")
112 else
113 echo "** WARNING: Cannot read '$FILE' specified in '$JETTY_CONF'"
115 done
116 else
117 # assume it's a command line parameter (let start.jar deal with its validity)
118 CONFIGS+=("$CONF")
120 done < "$JETTY_CONF"
123 #####################################################
124 # Find a location for the pid file
125 #####################################################
126 if [ -z "$JETTY_RUN" ]
127 then
128 JETTY_RUN=$(findDirectory -w /var/run /usr/var/run /tmp)
131 #####################################################
132 # Find a PID for the pid file
133 #####################################################
134 if [ -z "$JETTY_PID" ]
135 then
136 JETTY_PID="$JETTY_RUN/jetty.pid"
139 ##################################################
140 # Determine which JVM of version >1.5
141 # Try to use JAVA_HOME
142 ##################################################
143 if [ -z "$JAVA" ] && [ "$JAVA_HOME" ]
144 then
145 if [ "$JAVACMD" ]
146 then
147 JAVA="$JAVACMD"
148 else
149 [ -x "$JAVA_HOME/bin/jre" -a ! -d "$JAVA_HOME/bin/jre" ] && JAVA=$JAVA_HOME/bin/jre
150 [ -x "$JAVA_HOME/bin/java" -a ! -d "$JAVA_HOME/bin/java" ] && JAVA=$JAVA_HOME/bin/java
154 if [ -z "$JAVA" ]
155 then
156 echo "Cannot find a JRE or JDK. Please set JAVA_HOME to a >=1.5 JRE" 2>&2
157 exit 1
160 JAVA_VERSION=$("$JAVA" -version 2>&1) || continue
161 IFS='"_' read _ JAVA_VERSION _ <<< "$JAVA_VERSION"
163 #####################################################
164 # See if JETTY_PORT is defined
165 #####################################################
166 if [ "$JETTY_PORT" ]
167 then
168 JAVA_OPTIONS+=("-Djetty.port=$JETTY_PORT")
171 #####################################################
172 # See if JETTY_LOGS is defined
173 #####################################################
174 if [ "$JETTY_LOGS" ]
175 then
176 JAVA_OPTIONS+=("-Djetty.logs=$JETTY_LOGS")
179 #####################################################
180 # Are we running on Windows? Could be, with Cygwin/NT.
181 #####################################################
182 case "`uname`" in
183 CYGWIN*) PATH_SEPARATOR=";";;
184 *) PATH_SEPARATOR=":";;
185 esac
188 #####################################################
189 # Add jetty properties to Java VM options.
190 #####################################################
191 JAVA_OPTIONS+=("-Djetty.home=$JETTY_HOME" "-Djava.io.tmpdir=$TMPDIR")
193 [ -f "$JETTY_HOME/etc/start.config" ] && JAVA_OPTIONS=("-DSTART=$JETTY_HOME/etc/start.config" "${JAVA_OPTIONS[@]}")
195 #####################################################
196 # This is how the Jetty server will be started
197 #####################################################
199 JETTY_START=$JETTY_HOME/start.jar
200 [ ! -f "$JETTY_START" ] && JETTY_START=$JETTY_HOME/lib/start.jar
202 START_INI=$(dirname $JETTY_START)/start.ini
203 [ -r "$START_INI" ] || START_INI=""
205 RUN_ARGS=("${JAVA_OPTIONS[@]}" -jar "$JETTY_START" $JETTY_ARGS "${CONFIGS[@]}")
206 RUN_CMD=("$JAVA" "${RUN_ARGS[@]}")
208 #####################################################
209 # Comment these out after you're happy with what
210 # the script is doing.
211 #####################################################
212 if (( DEBUG ))
213 then
214 echo "JETTY_HOME = $JETTY_HOME"
215 echo "JETTY_CONF = $JETTY_CONF"
216 echo "JETTY_RUN = $JETTY_RUN"
217 echo "JETTY_PID = $JETTY_PID"
218 echo "JETTY_ARGS = $JETTY_ARGS"
219 echo "CONFIGS = ${CONFIGS[*]}"
220 echo "JAVA_OPTIONS = ${JAVA_OPTIONS[*]}"
221 echo "JAVA = $JAVA"
222 echo "RUN_CMD = ${RUN_CMD}"
225 case "$ACTION" in
226 start)
227 stat_busy "Starting Jetty Servlet Container"
228 cd $JETTY_HOME
229 "$JAVA" "${RUN_ARGS[@]}" &> /dev/null &
230 echo $! >$JETTY_PID
231 if [ $? -gt 0 ]; then
232 stat_fail
233 else
234 add_daemon jetty7
235 stat_done
238 stop)
239 stat_busy "Stopping Jetty Servlet Container"
240 cd $JETTY_HOME
241 [ -e $JETTY_PID ] && PID=`cat $JETTY_PID`
242 [ ! -z "$PID" ] && kill $PID &> /dev/null
243 if [ $? -gt 0 ]; then
244 stat_fail
245 else
246 rm_daemon jetty7
247 rm -f $JETTY_PID
248 stat_done
251 restart)
252 $0 stop
253 sleep 5
254 $0 start
257 echo "usage: $0 {start|stop|restart}"
258 esac