aboutsummaryrefslogtreecommitdiff
path: root/www/tomcat7/files/tomcat7.in
blob: 3ff0ef97249c0a32fd749f49211f23f807ed8a10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: tomcat7
# REQUIRE: LOGIN
# KEYWORD: shutdown

#
# Configuration settings for tomcat7 in /etc/rc.conf:
#
# tomcat7_enable (bool):
#   Set to "NO" by default.
#   Set it to "YES" to enable tomcat7
#
# tomcat7_flags (str):
#   Set to "" by default.
#   Extra flags passed to start command
#
# tomcat7_catalina_base (str)
#   Set to "%%TOMCAT_HOME%%" by default.
#   Set the CATALINA_BASE variable for the Tomcat process
#
# tomcat7_catalina_tmpdir (str)
#   Set to "%%TOMCAT_HOME%%/temp" by default.
#   Set the CATALINA_TMPDIR variable for the Tomcat process
#
# tomcat7_catalina_log (str)
#   Set to ">> %%TOMCAT_HOME%%/logs/catalina.out 2>&1" by default.
#   Set the Tomcat Console logger
#
# tomcat7_stop_timeout (num)
#   Set to "10" by default.
#   Sets the timeout in seconds to allow tomcat to shutdown.
#   After the timeout has elapsed, tomcat will be killed.
#
# tomcat7_java_home (str):
# tomcat7_java_vendor (str):
# tomcat7_java_version (str):
# tomcat7_java_os (str):
#   Specify the requirements of the Java VM to use. See javavm(1).
#
# tomcat7_classpath (str):
#   Set to "" by default.
#   Addtional classes to add to the CLASSPATH
#
# tomcat7_java_opts (str):
#   Set to "" by default.
#   Java VM args to use.
#

tomcat7_enable="${tomcat7_enable:-"NO"}"
tomcat7_java_version="${tomcat7_java_version:-"%%JAVA_VERSION%%"}"
tomcat7_user="${tomcat7_user:-"%%TOMCAT_USER%%"}"
tomcat7_catalina_home="%%TOMCAT_HOME%%"
tomcat7_catalina_base="${tomcat7_catalina_base:-"%%TOMCAT_HOME%%"}"
tomcat7_catalina_tmpdir="${tomcat7_catalina_tmpdir:-"%%TOMCAT_HOME%%/temp"}"
tomcat7_catalina_log="${tomcat7_catalina_log:-">> %%TOMCAT_HOME%%/logs/catalina.out 2>&1"}"
tomcat7_stop_timeout="${tomcat7_stop_timeout:-"10"}"

. /etc/rc.subr

name="tomcat7"
rcvar=`set_rcvar`
pidfile="/var/run/tomcat7.pid"

load_rc_config "${name}"

if [ -n "${tomcat7_java_home}" ] ; then
	export JAVA_HOME="${tomcat7_java_home}"
fi

if [ -n "${tomcat7_java_version}" ] ; then
	export JAVA_VERSION="${tomcat7_java_version}"
fi

if [ -n "${tomcat7_java_vendor}" ] ; then
	export JAVA_VENDOR="${tomcat7_java_vendor}"
fi

if [ -n "${tomcat7_java_os}" ] ; then
	export JAVA_OS="${tomcat7_java_os}"
fi

java_class="org.apache.catalina.startup.Bootstrap"
java_command="%%LOCALBASE%%/bin/java \
	${tomcat7_java_opts} \
	-Djava.endorsed.dirs=%%TOMCAT_HOME%%/endorsed \
	-classpath %%TOMCAT_HOME%%/bin/bootstrap.jar:%%TOMCAT_HOME%%/bin/tomcat-juli.jar${tomcat7_classpath} \
	-Dcatalina.home=${tomcat7_catalina_home} \
	-Dcatalina.base=${tomcat7_catalina_base} \
	-Djava.io.tmpdir=${tomcat7_catalina_tmpdir} \
	${java_class}"

required_files="${tomcat7_catalina_base}/conf/server.xml"

command="/usr/sbin/daemon"
flags="-p ${pidfile} ${java_command} start ${tomcat7_flags} ${tomcat7_catalina_log}"

start_precmd="pid_touch"
stop_cmd="tomcat_stop"
status_cmd="tomcat_status"
poll_cmd="tomcat_poll"

pid_touch() {
	touch $pidfile
	chown $tomcat7_user $pidfile
}

tomcat_stop() {
	rc_pid=$(tomcat_check_pidfile $pidfile)

	if [ -z "$rc_pid" ]; then
		[ -n "$rc_fast" ] && return 0
		echo "${name} not running? (check $pidfile)."
		return 1
	fi
	
	echo "Stopping ${name}."
	${java_command} stop
	tomcat_wait_max_for_pid ${tomcat7_stop_timeout} ${rc_pid}
	kill -KILL ${rc_pid} 2> /dev/null && echo "Killed."
	rm -f ${pidfile}
}

tomcat_status() {
	rc_pid=$(tomcat_check_pidfile $pidfile)

	if [ -n "$rc_pid" ]; then
		echo "${name} is running as pid $rc_pid."
	else
		echo "${name} is not running."
		return 1
	fi
}

tomcat_poll() {
	rc_pid=$(tomcat_check_pidfile $pidfile)

	_prefix=
	while (true) ; do
		[ -z "$rc_pid" ] && break
		echo -n ${_prefix:-"Waiting for PIDS: "}$rc_pid
		_prefix=", "
		sleep 2
	done
	if [ -n "$_prefix" ]; then
	echo "."
	fi
}

tomcat_check_pidfile() {
	_pidfile=$1
	if [ -z "$_pidfile" ]; then
		err 3 'USAGE: tomcat_check_pidfile pidfile'
	fi
	if [ ! -f $_pidfile ]; then
		debug "pid file ($_pidfile): not readable."
		return
	fi
	read _pid _junk < $_pidfile
	if [ -z "$_pid" ]; then
		debug "pid file ($_pidfile): no pid in file."
		return
	fi
	if [ -n "`%%LOCALBASE%%/bin/jps -l | grep -e "^$_pid $java_class\$"`" ]; then
		echo -n $_pid
	fi
}

tomcat_wait_max_for_pid() {
	_timeout=$1
	shift
	_pid=$1
	_prefix=
	while [ $_timeout -gt 0 ] ; do
		echo -n ${_prefix:-"Waiting (max $_timeout secs) for PIDS: "}$_pid
		_prefix=", "
		sleep 2
		kill -0 $_pid 2> /dev/null || break
		_timeout=$(($_timeout-2))
	done
	if [ -n "$_prefix" ]; then
		echo "."
	fi
}

run_rc_command "$1"