3 # Licensed to the Apache Software Foundation (ASF) under one or more
4 # contributor license agreements. See the NOTICE file distributed with
5 # this work for additional information regarding copyright ownership.
6 # The ASF licenses this file to You under the Apache License, Version 2.0
7 # (the "License"); you may not use this file except in compliance with
8 # the License. You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
22 LINK=$(readlink "$(basename "$1")")
24 cd "$(dirname "$LINK")"
25 LINK=$(readlink "$(basename "$1")")
27 REALPATH="$PWD/$(basename "$1")"
32 REALNAME=$(realpath "$0")
33 DIRNAME=$(dirname "$REALNAME")
34 PROGNAME=$(basename "$REALNAME")
37 # Sourcing environment settings for karaf similar to tomcats setenv
41 if [ -f "$DIRNAME/setenv" ]; then
46 # Set up some easily accessible MIN/MAX params for JVM mem usage
48 if [ "x$JAVA_MIN_MEM" = "x" ]; then
52 if [ "x$JAVA_MAX_MEM" = "x" ]; then
58 # Check the mode that initiated the script
60 if [ "x$1" != "x" ]; then
65 echo "${PROGNAME}: $*"
74 # OS specific support (must be 'true' or 'false').
93 # For AIX, set an environment variable
95 export LDR_CNTRL=MAXDATA=0xB0000000@DSA
101 # Use the maximum available, or set MAX_FD != -1 to use that
102 if [ "x$MAX_FD" = "x" ]; then
106 # Increase the maximum file descriptors if we can
107 if [ "$os400" = "false" ] && [ "$cygwin" = "false" ]; then
108 if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ]; then
109 MAX_FD_LIMIT=`ulimit -H -n`
110 if [ $? -eq 0 ]; then
112 MAX_FD="$MAX_FD_LIMIT"
114 warn "Could not query system maximum file descriptor limit: $MAX_FD_LIMIT"
117 if [ "$MAX_FD" != 'unlimited' ]; then
118 ulimit -n $MAX_FD > /dev/null
119 if [ $? -ne 0 ]; then
120 warn "Could not set maximum file descriptor limit: $MAX_FD"
127 if [ "x$KARAF_HOME" = "x" ]; then
128 # In POSIX shells, CDPATH may cause cd to write to stdout
129 (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
130 KARAF_HOME=`cd "$DIRNAME/.."; pwd`
133 if [ ! -d "$KARAF_HOME" ]; then
134 die "KARAF_HOME is not valid: $KARAF_HOME"
139 if [ "x$KARAF_BASE" != "x" ]; then
140 if [ ! -d "$KARAF_BASE" ]; then
141 die "KARAF_BASE is not valid: $KARAF_BASE"
144 KARAF_BASE=$KARAF_HOME
149 if [ "x$KARAF_DATA" != "x" ]; then
150 if [ ! -d "$KARAF_DATA" ]; then
151 die "KARAF_DATA is not valid: $KARAF_DATA"
154 KARAF_DATA=$KARAF_BASE/data
159 if [ "x$KARAF_ETC" != "x" ]; then
160 if [ ! -d "$KARAF_ETC" ]; then
161 die "KARAF_ETC is not valid: $KARAF_ETC"
164 KARAF_ETC=$KARAF_BASE/etc
169 # Support for loading native libraries
170 LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:$KARAF_BASE/lib:$KARAF_HOME/lib"
172 # For Cygwin, set PATH from LD_LIBRARY_PATH
174 LD_LIBRARY_PATH=`cygpath --path --windows "$LD_LIBRARY_PATH"`
175 PATH="$PATH;$LD_LIBRARY_PATH"
178 export LD_LIBRARY_PATH
183 while [ -h "${dst}" ] ; do
185 link=`expr "$ls" : '.*-> \(.*\)$'`
186 if expr "$link" : '/.*' > /dev/null; then
189 dst="`dirname "${dst}"`/$link"
192 bas=`basename "${dst}"`
193 dir=`dirname "${dst}"`
194 if [ "$bas" != "$dir" ]; then
195 dst="`pathCanonical "$dir"`/$bas"
197 echo "${dst}" | sed -e 's#//#/#g' -e 's#/./#/#g' -e 's#/[^/]*/../#/#g'
201 # Setup the Java Virtual Machine
203 [ -n "$JAVA" ] && JAVA=`cygpath --unix "$JAVA"`
204 [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
207 if [ "x$JAVA_HOME" = "x" ] && [ "$darwin" = "true" ]; then
208 JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
210 if [ "x$JAVA" = "x" ] && [ -r /etc/gentoo-release ] ; then
211 JAVA_HOME=`java-config --jre-home`
213 if [ "x$JAVA" = "x" ]; then
214 if [ "x$JAVA_HOME" != "x" ]; then
215 if [ ! -d "$JAVA_HOME" ]; then
216 die "JAVA_HOME is not valid: $JAVA_HOME"
218 JAVA="$JAVA_HOME/bin/java"
220 warn "JAVA_HOME not set; results may vary"
222 JAVA=`expr "$JAVA" : '.* \(/.*\)$'`
223 if [ "x$JAVA" = "x" ]; then
224 die "java command not found"
228 if [ "x$JAVA_HOME" = "x" ]; then
229 JAVA_HOME="$(dirname $(dirname $(pathCanonical "$JAVA")))"
234 #echo "`$JAVA -version`"
235 # This service should call `java -version`,
236 # read stdout, and look for hints
237 if $JAVA -version 2>&1 | grep "^IBM" ; then
239 # on OS/400, java -version does not contain IBM explicitly
245 # echo "JVM vendor is $JVM_VENDOR"
249 # echo "`$JAVA -version`"
250 VERSION=`"$JAVA" -version 2>&1 | egrep '"([0-9]+\.[0-9]+\..*[0-9]).*"' | awk '{print substr($3,2,length($3)-2)}' | awk '{print substr($1, 3, 3)}' | sed -e 's;\.;;g'`
252 if [ "$VERSION" -lt "80" ]; then
253 die "JVM must be 1.8.0 or greater"
257 setupDebugOptions() {
258 if [ "x$JAVA_OPTS" = "x" ]; then
259 JAVA_OPTS="$DEFAULT_JAVA_OPTS"
263 if [ "x$EXTRA_JAVA_OPTS" != "x" ]; then
264 JAVA_OPTS="$JAVA_OPTS $EXTRA_JAVA_OPTS"
267 # Set Debug options if enabled
268 if [ "x$KARAF_DEBUG" != "x" ]; then
269 # Ignore DEBUG in case of stop, client, or status mode
270 if [ "x$MODE" = "xstop" ]; then
273 if [ "x$MODE" = "xclient" ]; then
276 if [ "x$MODE" = "xstatus" ]; then
279 # Use the defaults if JAVA_DEBUG_OPTS was not set
280 if [ "x$JAVA_DEBUG_OPTS" = "x" ]; then
281 JAVA_DEBUG_OPTS="$DEFAULT_JAVA_DEBUG_OPTS"
284 JAVA_OPTS="$JAVA_DEBUG_OPTS $JAVA_OPTS"
285 warn "Enabling Java debug options: $JAVA_DEBUG_OPTS"
290 DEFAULT_JAVA_OPTS="-Xms$JAVA_MIN_MEM -Xmx$JAVA_MAX_MEM -XX:+UnlockDiagnosticVMOptions -XX:+UnsyncloadClass -XX:+HeapDumpOnOutOfMemoryError"
292 #Set the JVM_VENDOR specific JVM flags
293 if [ "$JVM_VENDOR" = "SUN" ]; then
294 DEFAULT_JAVA_OPTS="-server $DEFAULT_JAVA_OPTS -Dcom.sun.management.jmxremote"
295 elif [ "$JVM_VENDOR" = "IBM" ]; then
297 DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS"
299 DEFAULT_JAVA_OPTS="-Xverify:none -Xdump:heap -Xlp $DEFAULT_JAVA_OPTS"
301 DEFAULT_JAVA_OPTS="-Xverify:none $DEFAULT_JAVA_OPTS"
305 # Add default security file option
306 if [ "x$ODL_JAVA_SECURITY_PROPERTIES" != "x" ]; then
307 DEFAULT_JAVA_OPTS="-Djava.security.properties="${ODL_JAVA_SECURITY_PROPERTIES}" $DEFAULT_JAVA_OPTS"
309 DEFAULT_JAVA_OPTS="-Djava.security.properties="${KARAF_ETC}/odl.java.security" $DEFAULT_JAVA_OPTS"
312 # Add the jars in the lib dir
313 for file in "$KARAF_HOME"/lib/karaf*.jar
315 if [ -z "$CLASSPATH" ]; then
318 CLASSPATH="$CLASSPATH:$file"
322 DEFAULT_JAVA_DEBUG_PORT="5005"
323 if [ "x$JAVA_DEBUG_PORT" = "x" ]; then
324 JAVA_DEBUG_PORT="$DEFAULT_JAVA_DEBUG_PORT"
326 DEFAULT_JAVA_DEBUG_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$JAVA_DEBUG_PORT"
329 ## TODO: Move to conf/profiler/yourkit.{sh|cmd}
331 # Uncomment to enable YourKit profiling
332 #DEFAULT_JAVA_DEBUG_OPTS="-Xrunyjpagent"
336 # Determine if there is special OS handling we must perform
339 # Unlimit the number of file descriptors if possible
342 # Locate the Karaf home directory
345 # Locate the Karaf base directory
348 # Locate the Karaf data directory
351 # Locate the Karaf etc directory
354 # Setup the native library path
357 # Locate the Java VM to execute
360 # Determine the JVM vendor
363 # Determine the JVM version >= 1.8
366 # Setup default options
369 # Install debug options
375 OPTS="-Dkaraf.startLocalConsole=true -Dkaraf.startRemoteShell=true"
376 MAIN=org.apache.karaf.main.Main
377 while [ "$1" != "" ]; do
384 if [ "x$JAVA_DEBUG_OPTS" = "x" ]; then
385 JAVA_DEBUG_OPTS="$DEFAULT_JAVA_DEBUG_OPTS"
387 JAVA_OPTS="$JAVA_DEBUG_OPTS $JAVA_OPTS"
391 MAIN=org.apache.karaf.main.Status
395 MAIN=org.apache.karaf.main.Stop
402 OPTS="-Dkaraf.startLocalConsole=false -Dkaraf.startRemoteShell=true"
406 OPTS="-Dkaraf.startLocalConsole=true -Dkaraf.startRemoteShell=false"
415 JAVA_ENDORSED_DIRS="$JAVA_HOME/jre/lib/endorsed:$JAVA_HOME/lib/endorsed:${KARAF_HOME}/lib/endorsed"
416 JAVA_EXT_DIRS="$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/ext:${KARAF_HOME}/lib/ext"
418 KARAF_HOME=`cygpath --path --windows "$KARAF_HOME"`
419 KARAF_BASE=`cygpath --path --windows "$KARAF_BASE"`
420 KARAF_DATA=`cygpath --path --windows "$KARAF_DATA"`
421 KARAF_ETC=`cygpath --path --windows "$KARAF_ETC"`
422 if [ ! -z "$CLASSPATH" ]; then
423 CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
425 JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
426 JAVA_ENDORSED_DIRS=`cygpath --path --windows "$JAVA_ENDORSED_DIRS"`
427 JAVA_EXT_DIRS=`cygpath --path --windows "$JAVA_EXT_DIRS"`
431 # Use /dev/urandom to avoid blocking on /dev/random
432 # See http://www.2uo.de/myths-about-urandom/ to understand why this is safe (as long as your VM provisioning seeds
434 # The /dev/./urandom workaround is necessary because of https://bugs.openjdk.java.net/browse/JDK-6202721
436 [ -c /dev/urandom -a -r /dev/urandom ] && NON_BLOCKING_PRNG=-Djava.security.egd=file:/dev/./urandom
438 # Using command line arguments as java arguments (as opposed to arguments for $MAIN).
439 # FIXME: Document this in User Guide.
440 exec "$JAVA" $JAVA_OPTS "${NON_BLOCKING_PRNG}" -Djava.endorsed.dirs="${JAVA_ENDORSED_DIRS}" -Djava.ext.dirs="${JAVA_EXT_DIRS}" -Dkaraf.instances="${KARAF_HOME}/instances" -Dkaraf.home="$KARAF_HOME" -Dkaraf.base="$KARAF_BASE" -Dkaraf.data="$KARAF_DATA" -Dkaraf.etc="$KARAF_ETC" -Djava.io.tmpdir="$KARAF_DATA/tmp" -Djava.util.logging.config.file="$KARAF_BASE/etc/java.util.logging.properties" $KARAF_OPTS $OPTS "$@" -classpath "$CLASSPATH" $MAIN