| 1 |
fourmond |
5313 |
# wrappers.sh: various functions to be used by Java script wrappers
|
| 2 |
|
|
# Copyright 2008 by Vincent Fourmond <fourmond@debian.org>
|
| 3 |
|
|
#
|
| 4 |
|
|
# This library is free software; you can redistribute it and/or
|
| 5 |
|
|
# modify it under the terms of the GNU Lesser General Public
|
| 6 |
|
|
# License as published by the Free Software Foundation; either
|
| 7 |
|
|
# version 2.1 of the License, or (at your option) any later version.
|
| 8 |
fourmond |
5267 |
|
| 9 |
fourmond |
5313 |
# This library is distributed in the hope that it will be useful,
|
| 10 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 12 |
|
|
# Lesser General Public License for more details.
|
| 13 |
|
|
|
| 14 |
|
|
# You should have received a copy of the GNU Lesser General Public
|
| 15 |
|
|
# License along with this library; if not, write to the Free Software
|
| 16 |
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
| 17 |
|
|
|
| 18 |
fourmond |
5306 |
# Display a debugging message
|
| 19 |
|
|
java_debug() {
|
| 20 |
fourmond |
5313 |
if [ "$DEBUG_WRAPPER" ]; then
|
| 21 |
fourmond |
5267 |
echo "[debug] $0: $@" >&2;
|
| 22 |
|
|
fi
|
| 23 |
|
|
}
|
| 24 |
|
|
|
| 25 |
fourmond |
5306 |
# Displays a warning
|
| 26 |
|
|
java_warning() {
|
| 27 |
|
|
echo "[warning] $0: $@" >&2;
|
| 28 |
|
|
}
|
| 29 |
fourmond |
5267 |
|
| 30 |
fourmond |
5306 |
# Exit with an error message
|
| 31 |
|
|
java_fail() {
|
| 32 |
|
|
echo "[error] $0: $@" >&2;
|
| 33 |
|
|
exit 1;
|
| 34 |
|
|
}
|
| 35 |
|
|
|
| 36 |
fourmond |
5338 |
# Some initializations:
|
| 37 |
|
|
if [ "$JAVA_CLASSPATH" ]; then
|
| 38 |
|
|
java_debug "Building classpath on JAVA_CLASSPATH = '$JAVA_CLASSPATH'"
|
| 39 |
|
|
else
|
| 40 |
|
|
JAVA_CLASSPATH=
|
| 41 |
|
|
fi
|
| 42 |
|
|
if [ "$DESTDIR" ]; then
|
| 43 |
|
|
java_debug "Using DESTDIR = '$DESTDIR'"
|
| 44 |
|
|
else
|
| 45 |
|
|
DESTDIR=""
|
| 46 |
|
|
fi
|
| 47 |
fourmond |
5306 |
|
| 48 |
fourmond |
5338 |
if [ "$JAVA_JARPATH" ]; then
|
| 49 |
|
|
java_debug "Jar lookup is done in JAVA_JARPATH = '$JAVA_JARPATH'"
|
| 50 |
|
|
else
|
| 51 |
|
|
JAVA_JARPATH=$DESTDIR/usr/share/java
|
| 52 |
|
|
fi
|
| 53 |
|
|
|
| 54 |
|
|
|
| 55 |
fourmond |
5313 |
# Try to find a Java runtime and set JAVA_HOME and JAVA_CMD accordingly.
|
| 56 |
|
|
# If JAVA_CMD exists, nothing is done. If JAVA_HOME exists, only that
|
| 57 |
fourmond |
5267 |
# is searched.
|
| 58 |
|
|
#
|
| 59 |
fourmond |
5910 |
# In the other cases, the runtime is looked for according to the
|
| 60 |
fourmond |
5267 |
# following arguments:
|
| 61 |
|
|
# * 2 : java runtime 2
|
| 62 |
|
|
# * swing : a JVM that has swing
|
| 63 |
|
|
# * fullxml: a JVM that has all XML classes, including
|
| 64 |
|
|
# javax.xml.stream.util.StreamReaderDelegate
|
| 65 |
fourmond |
5306 |
# * sun: sun's JVM, for stuff depending on the infamous com.sun classes
|
| 66 |
|
|
# * sun6: sun's JVM version 6
|
| 67 |
fourmond |
5910 |
# * icedtea: icedtea
|
| 68 |
fourmond |
5267 |
#
|
| 69 |
fourmond |
5910 |
# More than one argument can be specified; they will be taken into account
|
| 70 |
|
|
# in that order.
|
| 71 |
|
|
#
|
| 72 |
fourmond |
5313 |
# If JAVA_DEBUGGER is set, we try to use jdb rather than java, if it is
|
| 73 |
fourmond |
5267 |
# present.
|
| 74 |
|
|
#
|
| 75 |
|
|
# This information is currently *far from complete* !!!
|
| 76 |
|
|
find_java_runtime() {
|
| 77 |
fourmond |
5306 |
# First, known runtimes:
|
| 78 |
fourmond |
5338 |
|
| 79 |
|
|
sun5="/usr/lib/jvm/java-1.5.0-sun /usr/lib/j2*1.5-sun"
|
| 80 |
|
|
sun4="/usr/lib/j2*1.4-sun"
|
| 81 |
|
|
sun6="/usr/lib/jvm/java-6-sun /usr/lib/j2*1.6-sun"
|
| 82 |
|
|
|
| 83 |
|
|
sun_java="$sun4 $sun5 $sun6"
|
| 84 |
|
|
|
| 85 |
fourmond |
5267 |
gcj2="/usr/lib/jvm/java-*-gcj-4.* "
|
| 86 |
fourmond |
5306 |
sablevm="/usr/lib/sablevm"
|
| 87 |
|
|
kaffe="/usr/lib/kaffe /usr/lib/kaffe/pthreads /usr/lib/kaffe/jthreads"
|
| 88 |
fourmond |
5267 |
icedtea="/usr/lib/jvm/java-7-icedtea"
|
| 89 |
fourmond |
5306 |
cacao="/usr/lib/jvm/cacao"
|
| 90 |
|
|
|
| 91 |
fourmond |
5313 |
# IBM, coming from argouml.sh
|
| 92 |
|
|
ibm="/usr/lib/j2*1.[456]-ibm"
|
| 93 |
|
|
|
| 94 |
fourmond |
5306 |
# Then, classes of JVM:
|
| 95 |
fourmond |
5313 |
all_runtimes="$gcj2 $cacao $sablevm $kaffe $icedtea $sun_java $ibm /usr/lib/jvm/*"
|
| 96 |
fourmond |
5306 |
|
| 97 |
|
|
# Java2 runtimes:
|
| 98 |
fourmond |
5313 |
java2_runtimes="$gcj2 $iced_tea $sun_java $ibm"
|
| 99 |
fourmond |
5306 |
|
| 100 |
|
|
# Full swing runtimes:
|
| 101 |
fourmond |
5313 |
full_swing_runtimes="$iced_tea $sun_java $ibm"
|
| 102 |
fourmond |
5306 |
|
| 103 |
|
|
# Sun java apparently has some XML functions more than concurrents:
|
| 104 |
|
|
xml_extra="/usr/lib/jvm/java-6-sun /usr/lib/jvm/java-1.5.0-sun"
|
| 105 |
|
|
|
| 106 |
fourmond |
5313 |
if [ "$JAVA_CMD" ]; then
|
| 107 |
fourmond |
5338 |
if which "$JAVA_CMD" > /dev/null; then
|
| 108 |
|
|
java_debug "Using already set JAVA_CMD = '$JAVA_CMD' => '"`which "$JAVA_CMD"`"'"
|
| 109 |
|
|
return 0; # Nothing to do
|
| 110 |
|
|
else
|
| 111 |
|
|
java_warning "JAVA_CMD was set to '$JAVA_CMD', but which(1) does not find it."
|
| 112 |
|
|
java_warning "Therefore ignoring JAVA_CMD"
|
| 113 |
|
|
fi
|
| 114 |
fourmond |
5267 |
fi
|
| 115 |
|
|
|
| 116 |
fourmond |
5895 |
if [ "$JAVA_BINDIR" ]; then
|
| 117 |
fourmond |
5338 |
if [ "$JAVA_DEBUGGER" ] && [ -x "$JAVA_BINDIR/jdb" ]; then
|
| 118 |
|
|
JAVA_CMD="$JAVA_BINDIR/jdb"
|
| 119 |
|
|
elif [ -x "$JAVA_BINDIR/java" ]; then
|
| 120 |
|
|
JAVA_CMD="$JAVA_BINDIR/java"
|
| 121 |
|
|
fi
|
| 122 |
|
|
if [ "$JAVA_CMD" ]; then
|
| 123 |
|
|
java_debug "Using '$JAVA_CMD' from JAVA_BINDIR = '$JAVA_BINDIR'"
|
| 124 |
|
|
return 0;
|
| 125 |
|
|
else
|
| 126 |
|
|
java_warning "JAVA_BINDIR = '$JAVA_BINDIR' does not point to a java binary"
|
| 127 |
|
|
fi
|
| 128 |
|
|
fi
|
| 129 |
|
|
|
| 130 |
fourmond |
5267 |
if [ -z "$JAVA_HOME" ]; then
|
| 131 |
fourmond |
5910 |
|
| 132 |
fourmond |
5267 |
# We now try to look for a reasonable JAVA_HOME.
|
| 133 |
|
|
# First, narrow the choices, approximately according to what
|
| 134 |
|
|
# was asked
|
| 135 |
fourmond |
5910 |
|
| 136 |
|
|
DIRS=""
|
| 137 |
|
|
while test "$1"
|
| 138 |
|
|
do
|
| 139 |
|
|
case $1 in
|
| 140 |
|
|
# A java2 runtime
|
| 141 |
|
|
2) DIRS="$DIRS $java2_runtimes"
|
| 142 |
|
|
;;
|
| 143 |
|
|
swing) DIRS="$DIRS $icedtea $sun_java";
|
| 144 |
|
|
;;
|
| 145 |
|
|
sun) DIRS="$DIRS $sun_java"
|
| 146 |
|
|
;;
|
| 147 |
|
|
sunmax5) DIRS="$DIRS $sun4 $sun5"
|
| 148 |
|
|
;;
|
| 149 |
|
|
sunmin5) DIRS="$DIRS $sun5 $sun6"
|
| 150 |
|
|
;;
|
| 151 |
|
|
sun6) DIRS="$DIRS $sun6"
|
| 152 |
|
|
;;
|
| 153 |
|
|
fullxml) DIRS="$DIRS $xml_extra"
|
| 154 |
|
|
;;
|
| 155 |
|
|
icedtea) DIRS="$icedtea"
|
| 156 |
|
|
;;
|
| 157 |
|
|
*) ;;
|
| 158 |
|
|
esac
|
| 159 |
|
|
shift
|
| 160 |
|
|
done
|
| 161 |
|
|
# Nothing means everything
|
| 162 |
|
|
if test -z "$DIRS"; then
|
| 163 |
|
|
DIRS=$all_runtimes
|
| 164 |
|
|
fi
|
| 165 |
|
|
# And pick up the first one that works reasonably
|
| 166 |
fourmond |
5267 |
for dir in $DIRS; do
|
| 167 |
|
|
if [ -x $dir/bin/java ]; then
|
| 168 |
|
|
JAVA_HOME=$dir
|
| 169 |
|
|
break;
|
| 170 |
|
|
fi
|
| 171 |
|
|
done
|
| 172 |
fourmond |
5338 |
else
|
| 173 |
|
|
java_debug "Using provided JAVA_HOME = '$JAVA_HOME'"
|
| 174 |
fourmond |
5267 |
fi
|
| 175 |
|
|
if [ "$JAVA_HOME" ] ; then
|
| 176 |
fourmond |
5313 |
if [ "$JAVA_DEBUGGER" ] && [ -x "$JAVA_HOME/bin/jdb" ]; then
|
| 177 |
|
|
JAVA_CMD="$JAVA_HOME/bin/jdb"
|
| 178 |
fourmond |
5267 |
else
|
| 179 |
fourmond |
5313 |
JAVA_CMD="$JAVA_HOME/bin/java"
|
| 180 |
fourmond |
5267 |
fi
|
| 181 |
fourmond |
5338 |
java_debug "Found JAVA_HOME = '$JAVA_HOME'"
|
| 182 |
|
|
java_debug "Found JAVA_CMD = '$JAVA_CMD'"
|
| 183 |
fourmond |
5267 |
return 0 # Fine
|
| 184 |
|
|
else
|
| 185 |
fourmond |
5306 |
java_warning "No java runtime was found for flavor '${1:-none}'"
|
| 186 |
|
|
return 1;
|
| 187 |
fourmond |
5267 |
fi
|
| 188 |
fourmond |
5306 |
}
|
| 189 |
fourmond |
5267 |
|
| 190 |
fourmond |
5306 |
# Same as find_java_runtime, but fails with an error if
|
| 191 |
|
|
# nothing is found.
|
| 192 |
|
|
require_java_runtime() {
|
| 193 |
|
|
find_java_runtime "$@" || \
|
| 194 |
fourmond |
5313 |
java_fail "Unable to find an appropriate java runtime. See java_wrappers(7) for help"
|
| 195 |
fourmond |
5267 |
}
|
| 196 |
|
|
|
| 197 |
fourmond |
5338 |
# Looks for a jar file and returns its location as the
|
| 198 |
|
|
# found_jar variable, or fails if no jar was found.
|
| 199 |
|
|
locate_jar() {
|
| 200 |
|
|
jar="$1"
|
| 201 |
|
|
if [ -r $JAVA_JARPATH/$jar ]; then
|
| 202 |
|
|
found_jar=$JAVA_JARPATH/$jar
|
| 203 |
|
|
elif [ -r $JAVA_JARPATH/$jar.jar ]; then
|
| 204 |
|
|
found_jar=$JAVA_JARPATH/$jar.jar
|
| 205 |
|
|
elif [ -r $jar ]; then
|
| 206 |
|
|
# Maybe issue a warning that jars should not be looked
|
| 207 |
|
|
# for absolutely ?
|
| 208 |
|
|
found_jar=$JAVA_JARPATH/$jar
|
| 209 |
|
|
elif [ -r $jar.jar ]; then
|
| 210 |
|
|
# Maybe issue a warning that jars should not be looked
|
| 211 |
|
|
# for absolutely ?
|
| 212 |
|
|
found_jar=$JAVA_JARPATH/$jar.jar
|
| 213 |
|
|
else
|
| 214 |
|
|
return 1 # Not found
|
| 215 |
|
|
fi
|
| 216 |
|
|
return 0 # Found
|
| 217 |
|
|
}
|
| 218 |
|
|
|
| 219 |
fourmond |
5267 |
# Find jars and add them to the classpath
|
| 220 |
|
|
find_jars() {
|
| 221 |
fourmond |
5313 |
looked_for_jars=1
|
| 222 |
fourmond |
5267 |
for jar in $@ ; do
|
| 223 |
fourmond |
5338 |
if locate_jar $jar; then
|
| 224 |
|
|
JAVA_CLASSPATH=$JAVA_CLASSPATH:$found_jar
|
| 225 |
fourmond |
5267 |
else
|
| 226 |
fourmond |
5338 |
java_warning "Unable to locate $jar in $JAVA_JARPATH"
|
| 227 |
fourmond |
5267 |
fi
|
| 228 |
|
|
done
|
| 229 |
|
|
}
|
| 230 |
|
|
|
| 231 |
fourmond |
5306 |
# Adds the first jar found to the classpath. Useful for alternative
|
| 232 |
|
|
# dependencies.
|
| 233 |
|
|
find_one_jar_in() {
|
| 234 |
fourmond |
5313 |
looked_for_jars=1
|
| 235 |
fourmond |
5306 |
for jar in $@ ; do
|
| 236 |
fourmond |
5895 |
if locate_jar $jar; then
|
| 237 |
fourmond |
5338 |
JAVA_CLASSPATH=$JAVA_CLASSPATH:$found_jar
|
| 238 |
fourmond |
5306 |
return 0
|
| 239 |
|
|
fi
|
| 240 |
|
|
done
|
| 241 |
fourmond |
5338 |
java_warning "Could fine none of $@ in $JAVA_JARPATH"
|
| 242 |
fourmond |
5306 |
return 1
|
| 243 |
|
|
}
|
| 244 |
|
|
|
| 245 |
fourmond |
5267 |
# Runs the program !
|
| 246 |
|
|
run_java() {
|
| 247 |
fourmond |
5313 |
if [ -z "$JAVA_CMD" ]; then
|
| 248 |
fourmond |
5338 |
java_warning "No JAVA_CMD set for run_java, falling back to JAVA_CMD = java"
|
| 249 |
fourmond |
5313 |
JAVA_CMD=java
|
| 250 |
fourmond |
5306 |
fi
|
| 251 |
fourmond |
5381 |
# We try to conjure up a JAVA_HOME from JAVA_CMD, if the former
|
| 252 |
|
|
# is absent. Idea coming from bug #404728.
|
| 253 |
|
|
if [ -z "$JAVA_HOME" ]; then
|
| 254 |
|
|
full_cmd_path="$(readlink -f `which $JAVA_CMD`)"
|
| 255 |
|
|
JAVA_HOME="${full_cmd_path:bin/*}"
|
| 256 |
|
|
java_debug "Using JAVA_CMD to find JAVA_HOME = '$JAVA_HOME'"
|
| 257 |
|
|
fi
|
| 258 |
fourmond |
5313 |
if [ "$FORCE_CLASSPATH" ]; then
|
| 259 |
fourmond |
5338 |
java_debug "Using imposed classpath : FORCE_CLASSPATH = '$FORCE_CLASSPATH'";
|
| 260 |
fourmond |
5313 |
cp="-classpath $FORCE_CLASSPATH";
|
| 261 |
|
|
elif [ "$JAVA_CLASSPATH" ]; then
|
| 262 |
|
|
cp="-classpath $JAVA_CLASSPATH";
|
| 263 |
fourmond |
5267 |
else
|
| 264 |
|
|
cp="";
|
| 265 |
|
|
fi
|
| 266 |
fourmond |
5381 |
# Exporting JAVA_HOME, I guess it can't hurt much, can it ?
|
| 267 |
|
|
export JAVA_HOME
|
| 268 |
fourmond |
5338 |
java_debug "Environment variable CLASSPATH is '$CLASSPATH'"
|
| 269 |
fourmond |
5313 |
java_debug "Runnning $JAVA_CMD $JAVA_ARGS $cp $@"
|
| 270 |
|
|
exec $JAVA_CMD $JAVA_ARGS $cp "$@"
|
| 271 |
|
|
}
|
| 272 |
|
|
|
| 273 |
|
|
# Runs a java jar.
|
| 274 |
fourmond |
5381 |
# You don't have to use this function to run a jar, but you might find
|
| 275 |
|
|
# it useful, though.
|
| 276 |
fourmond |
5313 |
run_jar() {
|
| 277 |
fourmond |
5895 |
if [ "$looked_for_jars" ]; then
|
| 278 |
fourmond |
5313 |
java_warning "It is most likely useless to use find_jar when running"
|
| 279 |
|
|
java_warning "a class with run_jar (-classpath is ignored)"
|
| 280 |
|
|
fi
|
| 281 |
fourmond |
5338 |
if locate_jar $1; then
|
| 282 |
|
|
shift
|
| 283 |
|
|
run_java -jar "$@"
|
| 284 |
|
|
else
|
| 285 |
|
|
java_fail "Unable to find jar $1 in $JAVA_JARPATH"
|
| 286 |
|
|
fi
|
| 287 |
fourmond |
5267 |
}
|