File: update-jserv

package info (click to toggle)
jserv 1.1-3
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 4,616 kB
  • ctags: 3,003
  • sloc: sh: 7,718; java: 6,652; ansic: 4,755; makefile: 816; perl: 39; xml: 32
file content (273 lines) | stat: -rw-r--r-- 9,650 bytes parent folder | download
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/bin/sh -e

# This is a script for modifying JServ's configuration files - it can be
# used by the local sysadmin or by other packages to automatically set them
# up. It can perform 3 basic tasks:
#
# 1. Add/remove a JAR file (or a directory) from the JVM's classpath
# 2. Add/remove a file ending mapping to a servlet
# 3. Add/remove servlet parameters in the root servlet zones
#
# Note: This script does not activate the changes made to Apache.

# JServ's main configuration file
jservconf="/etc/jserv/jserv.conf"

# The servlet zone to which file endings are mapped and where servlet
# parameters are added
jservzone="root"


echo_debug() {
    if [ "$debug" = "true" ]; then
	echo "DEBUG: " $*
    fi
}


checkConfigfile() {

    # This function tests/sets the following variables:
    # $jservconf  JServ's configuation file (usually /etc/jserv/jserv.conf)
    # $jservprop  JServ's properties file (usually /etc/jserv/jserv.properties)
    # $jervzone   The name of the servlet zone to modify (usually root)
    # $zonemnt    The mount point (part of the URL) of this servlet zone
    #             (usually /servlets)
    # $zoneprop   The zone's properties file (usually
    #             /etc/jserv/zones/root.properties)

    if ! grep -q "^ApJServProperties" $jservconf; then
	echo >&2 "Invalid JServ configuration file $jervconf!"
	exit 1
    fi

    # Look for the properties file (parameter ApJServProperties inside the
    # configuration file)
    jservprop=`grep "^ApJServProperties" $jservconf | cut -d\  -f2`
    if [ ! -e $jsersvprop ]; then
	echo >&2 "Invalid JServ properties file $jservprop!"
	exit 1
    fi

    # Get the first mount point and properties file of our servlet zone
    zonemnt=`grep "^ApJServMount[[:space:]]\+/.*[[:space:]]\+/$jservzone$" $jservconf | head -1 | cut -d\  -f2`
    if [ "x$zonemnt" = "x" ]; then
	echo >&2 "Could not find a mount point for the $jservzone zone in $jservconf!"
	exit 1
    else
	zoneprop=`grep "^$jservzone.properties=" $jservprop | cut -d= -f2`
	if [ "x$zoneprop" = "x" ]; then
	    echo >&2 "Could not find the properties file for the $jservzone zone in $jservprop!"
	    exit 1
	fi
	echo_debug "$jservzone's mount point is $zonemnt ..."
	echo_debug "... and its properties file is $zoneprop"
    fi
}


# Beginning of main program: Parse options first

if [ "$1" = "-d" -o "$1" = "--debug" ]; then
    debug="true"
    shift
else
    debug="false"
fi
case "$1" in
    add-apache)
	if [ "x$2" == "x" ]; then
            apacheconf="/etc/apache/httpd.conf"
        else
            apacheconf=$2
        fi

	if [ -f $apacheconf ]; then
            # First activate or add the JServ LoadModule line
            echo_debug "Activating JServ module in $apacheconf"
            perl -p -i -e 's:^#[#\s]*(LoadModule.*jserv):$1:' $apacheconf
            if ! grep -q "^LoadModule.*jserv" $apacheconf; then
                # Add the LoadModule line after all other LoadModule
                # statements (possibly commented)
                perl -00 -p -i -e "s:(\n([#\s]*LoadModule\s*.*\n)+):\1LoadModule jserv_module /usr/lib/apache/1.3/mod_jserv.so\n:" $apacheconf
            fi
	    if ! grep -q "^Include[[:space:]]\+$jservconf" $apacheconf; then
                echo_debug "Adding JServ configuration section to $apacheconf"
		savelog -c 100 $apacheconf >/dev/null
		cp -p $apacheconf.0 $apacheconf
		echo >> $apacheconf "<IfModule mod_jserv.c>"
		echo >> $apacheconf "# The following line is for apacheconfig - DO NOT REMOVE!"
		echo >> $apacheconf "ApJServLogFile DISABLED"
		echo >> $apacheconf "Include /etc/jserv/jserv.conf"
		echo >> $apacheconf "</IfModule>"
		echo "done."
            else
                echo_debug "JServ configuration section already present in $apacheconf"
	    fi
	else
	    echo >&2 "Apache configuration file $apacheconf not found!"
            exit 1
	fi
    ;;

    remove-apache)
        if [ "x$2" == "x" ]; then
            apacheconf="/etc/apache/httpd.conf"
        else
            apacheconf=$2
        fi

	# Remove all lines from <IfModule mod_jserv.c> to </IfModule> and
	# uncomment the Loadnodule line
	if [ -f $apacheconf ]; then
            # Comment JServ LoadModule line
            echo_debug "Deactivating JServ module in $apacheconf"
            perl -p -i -e 's:^(LoadModule.*jserv):# $1:' $apacheconf
            if grep -q "^Include[[:space:]]\+$jservconf" $apacheconf; then
                echo_debug "Removing JServ configuration section from $apacheconf"
                savelog -c 100 $apacheconf >/dev/null
	        perl >$apacheconf -p -e 'if (/^<IfModule mod_jserv.c>/) { while(!/^<\/IfModule>/) { $_=<>; } $_=""; }' $apacheconf.0
            else
                echo_debug "No JServ configuration section found in $apacheconf"
            fi
        else
            echo >&2 "Apache configuration file $apacheconf not found!"
            exit 1
        fi
    ;;

    add-classpath)
	checkConfigfile
	shift

	for path in $*; do
	    if grep -q "^wrapper.classpath=$path" $jservprop; then
		echo_debug "$path is already in $jservprop's classpath!"
	    else
                echo_debug "Adding $path to $jservprop's classpath"
	        # Replace the largest block of (commented) lines
		# "wrapper.classpath=...\n" with this block and an additional
		# line, i.e. add our line to the end of this block. I have to
		# use \1 instead of $1 in the replacing expression because
		# otherwise $1 would be relaced by the shell and I can't use
		# single quoted either because some variables have to be
		# replaced
		perl -00 -p -i -e "s:(\n([#\s]*wrapper.classpath\s*=\s*.*\n)+):\1wrapper.classpath=$path\n:" $jservprop
	    fi
	done
    ;;

    remove-classpath)
	checkConfigfile
	shift

	for path in $*; do
	    if grep -q "^wrapper.classpath=$path" $jservprop; then
		echo_debug "Removing $path from $1's classpath"
		perl -p -i -e "s:^wrapper.classpath=$path.*\n::" $jservprop
	    else
		echo_debug "$path is not in $jservprop's classpath!"
	    fi
	done
    ;;

    add-mapping)
	checkConfigfile
	if [ "x$2" == "x" -o "x$3" == "x" ]; then
	    echo >&2 "Required argument for add-mapping command missing!"
	    exit 1
	fi
	fileext=$2; urlpath="$zonemnt/$3"

	# First check if there is already a commented entry with this mapping:
	# yes: Remove comment from this entry (possibly overwriting the old
	#      urlpath)
	#  no: create a new entry after all other ApJServAction statements
	if ! grep -q "^ApJServAction[[:space:]]\+\.$fileext" $jservconf; then
	    if grep -q "^#[#[:space:]]*ApJServAction[[:space:]]\+\.$fileext" $jservconf; then
		echo_debug "Activating *.$fileext mapping to $urlpath"
		perl -p -i -e "s:#[#\s]*(ApJServAction\s+\.$fileext\s+).*:\1$urlpath:" $jservconf
	    else
		echo_debug "Adding *.$fileext mapping to $urlpath" 
		perl -00 -p -i -e "s:(\n([#\s]*ApJServAction.*\n)+):\1ApJServAction .$fileext $urlpath\n:" $jservconf
	    fi
	else
	    echo_debug "$fileext mapping already present in $jservconf"
	fi
    ;;

    remove-mapping)
	checkConfigfile
	if [ "x$2" == "x" ]; then
	    echo >&2 "Required argument for remove-mapping command missing!"
	    exit 1
	fi
	fileext=$2

	if grep -q "^ApJServAction[[:space:]]\+\.$fileext" $jservconf; then
	    echo_debug "Removing $fileext mapping from $jservconf"
	    perl -p -i -e "s:^(ApJServAction\s+\.$fileext.*\n):#\1:" $jservconf
	else
	    echo_debug "$fileext mapping not present in $jservconf"
	fi
    ;;

    add-option)
	checkConfigfile
	if [ "x$2" == "x" -o "x$3" == "x" -o "x$4" == "x" ]; then
	    echo >&2 "Required argument for add-option command missing!"
	    exit 1
	fi
	option=$2; parameter=$3; value=$4

	# Check if there is already a line with this option present, if so
	# add $parameter=$value to this option (or substitue the old value)
	if grep -q "^$option=" $zoneprop; then
	    if grep -q "^$option=.*$parameter=" $zoneprop; then
	        perl -p -i -e "s:^($option=.*)$parameter=.*?([,\n]):\1$parameter=$value\2:" $zoneprop
	    else
		perl -p -i -e "s:^($option=.*)\n:\1,$parameter=$value\n:" $zoneprop
	    fi
	else
	    echo >> $zoneprop "$option=$parameter=$value"
	fi
    ;;

    remove-option)
	checkConfigfile
	if [ "x$2" == "x" ]; then
	    echo >&2 "Required argument for remove-option command missing!"
	    exit 1
	fi
	option=$2; parameter=$3

	if [ "x$parameter" == "x" ]; then
	    # Remove the whole line
	    perl -p -i -e "s:^$option=.*\n::" $zoneprop
	else
	    # The 2nd substitution is in case this paramter is the last one,
	    # the 3rd one in case his parameter is the onle one
	    perl -p -i -e "s:^($option=.*)$parameter=.+?,:\1:; s:^($option=.*),$parameter=.+:\1:; s:^$option=$parameter=.+\n::" $zoneprop
	fi
    ;;

    *)
	echo >&2 "Usage: `basename $0` [options] command [arguments]"
	echo >&2 ""
	echo >&2 "Possible options are:"
	echo >&2 "-d/--debug                         Print debug messages"
	echo >&2 ""
	echo >&2 "Possible commands are:"
	echo >&2 "add-apache [<configfile>]            Include JServ into Apache's configuration"
	echo >&2 "remove-apache [<configfile>]         Remove JServ from Apache's configuration"
	echo >&2 "add-classpath <path> ...             Add <path> to JServ's JVM classpath"
	echo >&2 "remove-classpath <path> ...          Remove <path> from JServ's JVM classpath"
	echo >&2 "add-mapping <fileext> <urlpath>      Add a mapping for *.fileext to urlpath"
	echo >&2 "remove-mapping <fileext>             Remove the mapping for *.fileext"
	echo >&2 "add-option <option> <param> <value>  Add <option> to the root servlet zone"
	echo >&2 "remove-option <option> [<parameter>}  Remove <option> from the root servlet zone"
	exit 1
	;;
esac

exit 0