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 echo "JVM must be 1.8.0 or greater"
258 setupDebugOptions() {
259 if [ "x$JAVA_OPTS" = "x" ]; then
260 JAVA_OPTS="$DEFAULT_JAVA_OPTS"
264 if [ "x$EXTRA_JAVA_OPTS" != "x" ]; then
265 JAVA_OPTS="$JAVA_OPTS $EXTRA_JAVA_OPTS"
268 # Set Debug options if enabled
269 if [ "x$KARAF_DEBUG" != "x" ]; then
270 # Ignore DEBUG in case of stop, client, or status mode
271 if [ "x$MODE" = "xstop" ]; then
274 if [ "x$MODE" = "xclient" ]; then
277 if [ "x$MODE" = "xstatus" ]; then
280 # Use the defaults if JAVA_DEBUG_OPTS was not set
281 if [ "x$JAVA_DEBUG_OPTS" = "x" ]; then
282 JAVA_DEBUG_OPTS="$DEFAULT_JAVA_DEBUG_OPTS"
285 JAVA_OPTS="$JAVA_DEBUG_OPTS $JAVA_OPTS"
286 warn "Enabling Java debug options: $JAVA_DEBUG_OPTS"
291 DEFAULT_JAVA_OPTS="-Xms$JAVA_MIN_MEM -Xmx$JAVA_MAX_MEM -XX:+UnlockDiagnosticVMOptions -XX:+UnsyncloadClass -XX:+HeapDumpOnOutOfMemoryError"
293 #Set the JVM_VENDOR specific JVM flags
294 if [ "$JVM_VENDOR" = "SUN" ]; then
295 # permgen was removed in Java 8
296 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'`
297 if [ "$VERSION" -lt "80" ]; then
298 # Check some easily accessible MIN/MAX params for JVM mem usage
299 if [ "x$JAVA_PERM_MEM" != "x" ]; then
300 DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS -XX:PermSize=$JAVA_PERM_MEM"
302 if [ "x$JAVA_MAX_PERM_MEM" != "x" ]; then
303 DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS -XX:MaxPermSize=$JAVA_MAX_PERM_MEM"
306 DEFAULT_JAVA_OPTS="-server $DEFAULT_JAVA_OPTS -Dcom.sun.management.jmxremote"
307 elif [ "$JVM_VENDOR" = "IBM" ]; then
309 DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS"
311 DEFAULT_JAVA_OPTS="-Xverify:none -Xdump:heap -Xlp $DEFAULT_JAVA_OPTS"
313 DEFAULT_JAVA_OPTS="-Xverify:none $DEFAULT_JAVA_OPTS"
317 # Add default security file option
318 if [ "x$ODL_JAVA_SECURITY_PROPERTIES" != "x" ]; then
319 DEFAULT_JAVA_OPTS="-Djava.security.properties="${ODL_JAVA_SECURITY_PROPERTIES}" $DEFAULT_JAVA_OPTS"
321 DEFAULT_JAVA_OPTS="-Djava.security.properties="${KARAF_ETC}/odl.java.security" $DEFAULT_JAVA_OPTS"
324 # Add the jars in the lib dir
325 for file in "$KARAF_HOME"/lib/karaf*.jar
327 if [ -z "$CLASSPATH" ]; then
330 CLASSPATH="$CLASSPATH:$file"
334 DEFAULT_JAVA_DEBUG_PORT="5005"
335 if [ "x$JAVA_DEBUG_PORT" = "x" ]; then
336 JAVA_DEBUG_PORT="$DEFAULT_JAVA_DEBUG_PORT"
338 DEFAULT_JAVA_DEBUG_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$JAVA_DEBUG_PORT"
341 ## TODO: Move to conf/profiler/yourkit.{sh|cmd}
343 # Uncomment to enable YourKit profiling
344 #DEFAULT_JAVA_DEBUG_OPTS="-Xrunyjpagent"
348 # Determine if there is special OS handling we must perform
351 # Unlimit the number of file descriptors if possible
354 # Locate the Karaf home directory
357 # Locate the Karaf base directory
360 # Locate the Karaf data directory
363 # Locate the Karaf etc directory
366 # Setup the native library path
369 # Locate the Java VM to execute
372 # Determine the JVM vendor
375 # Determine the JVM version >= 1.8
378 # Setup default options
381 # Install debug options
387 OPTS="-Dkaraf.startLocalConsole=true -Dkaraf.startRemoteShell=true"
388 MAIN=org.apache.karaf.main.Main
389 while [ "$1" != "" ]; do
396 if [ "x$JAVA_DEBUG_OPTS" = "x" ]; then
397 JAVA_DEBUG_OPTS="$DEFAULT_JAVA_DEBUG_OPTS"
399 JAVA_OPTS="$JAVA_DEBUG_OPTS $JAVA_OPTS"
403 MAIN=org.apache.karaf.main.Status
407 MAIN=org.apache.karaf.main.Stop
414 OPTS="-Dkaraf.startLocalConsole=false -Dkaraf.startRemoteShell=true"
418 OPTS="-Dkaraf.startLocalConsole=true -Dkaraf.startRemoteShell=false"
427 JAVA_ENDORSED_DIRS="$JAVA_HOME/jre/lib/endorsed:$JAVA_HOME/lib/endorsed:${KARAF_HOME}/lib/endorsed"
428 JAVA_EXT_DIRS="$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/ext:${KARAF_HOME}/lib/ext"
430 KARAF_HOME=`cygpath --path --windows "$KARAF_HOME"`
431 KARAF_BASE=`cygpath --path --windows "$KARAF_BASE"`
432 KARAF_DATA=`cygpath --path --windows "$KARAF_DATA"`
433 KARAF_ETC=`cygpath --path --windows "$KARAF_ETC"`
434 if [ ! -z "$CLASSPATH" ]; then
435 CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
437 JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
438 JAVA_ENDORSED_DIRS=`cygpath --path --windows "$JAVA_ENDORSED_DIRS"`
439 JAVA_EXT_DIRS=`cygpath --path --windows "$JAVA_EXT_DIRS"`
443 # Use /dev/urandom to avoid blocking on /dev/random
444 # See http://www.2uo.de/myths-about-urandom/ to understand why this is safe (as long as your VM provisioning seeds
446 # The /dev/./urandom workaround is necessary because of https://bugs.openjdk.java.net/browse/JDK-6202721
448 [ -c /dev/urandom -a -r /dev/urandom ] && NON_BLOCKING_PRNG=-Djava.security.egd=file:/dev/./urandom
450 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 "$@"