Bug 6027 - Can't start karaf using symbolic link
[controller.git] / karaf / opendaylight-karaf-resources / src / main / resources / bin / instance
1 #!/bin/bash
2 #
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
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
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.
17 #
18
19 REALNAME=`readlink -e "$0"`
20 if [ $? != 0 ]; then
21     REALNAME=$0
22 fi
23 DIRNAME=`dirname "$REALNAME"`
24 PROGNAME=`basename "$REALNAME"`
25
26 #
27 # Sourcing environment settings for karaf similar to tomcats setenv
28 #
29 KARAF_SCRIPT="instance"
30 export KARAF_SCRIPT
31 if [ -f "$DIRNAME/setenv" ]; then
32   . "$DIRNAME/setenv"
33 fi
34
35 #
36 # Check/Set up some easily accessible MIN/MAX params for JVM mem usage
37 #
38 if [ "x$JAVA_MIN_MEM" = "x" ]; then
39     JAVA_MIN_MEM=128M
40     export JAVA_MIN_MEM
41 fi
42 if [ "x$JAVA_MAX_MEM" = "x" ]; then
43     JAVA_MAX_MEM=512M
44     export JAVA_MAX_MEM
45 fi
46
47 warn() {
48     echo "${PROGNAME}: $*"
49 }
50
51 die() {
52     warn "$*"
53     exit 1
54 }
55
56 detectOS() {
57     # OS specific support (must be 'true' or 'false').
58     cygwin=false;
59     darwin=false;
60     aix=false;
61     os400=false;
62     case "`uname`" in
63         CYGWIN*)
64             cygwin=true
65             ;;
66         Darwin*)
67             darwin=true
68             ;;
69         AIX*)
70             aix=true
71             ;;
72         OS400*)
73             os400=true
74             ;;
75     esac
76     # For AIX, set an environment variable
77     if $aix; then
78          export LDR_CNTRL=MAXDATA=0xB0000000@DSA
79          echo $LDR_CNTRL
80     fi
81 }
82
83 unlimitFD() {
84     # Use the maximum available, or set MAX_FD != -1 to use that
85     if [ "x$MAX_FD" = "x" ]; then
86         MAX_FD="maximum"
87     fi
88
89     # Increase the maximum file descriptors if we can
90     if [ "$os400" = "false" ] && [ "$cygwin" = "false" ]; then
91         MAX_FD_LIMIT=`ulimit -H -n`
92         if [ "$MAX_FD_LIMIT" != 'unlimited' ]; then
93             if [ $? -eq 0 ]; then
94                 if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ]; then
95                     # use the system max
96                     MAX_FD="$MAX_FD_LIMIT"
97                 fi
98
99                 ulimit -n $MAX_FD > /dev/null
100                 # echo "ulimit -n" `ulimit -n`
101                 if [ $? -ne 0 ]; then
102                     warn "Could not set maximum file descriptor limit: $MAX_FD"
103                 fi
104             else
105                 warn "Could not query system maximum file descriptor limit: $MAX_FD_LIMIT"
106             fi
107         fi
108     fi
109 }
110
111 locateHome() {
112     if [ "x$KARAF_HOME" != "x" ]; then
113         warn "Ignoring predefined value for KARAF_HOME"
114     fi
115
116     # In POSIX shells, CDPATH may cause cd to write to stdout
117     (unset CDPATH) >/dev/null 2>&1 && unset CDPATH
118
119     KARAF_HOME=`cd "$DIRNAME/.."; pwd`
120     if [ ! -d "$KARAF_HOME" ]; then
121         die "KARAF_HOME is not valid: $KARAF_HOME"
122     fi
123 }
124
125 locateBase() {
126     if [ "x$KARAF_BASE" != "x" ]; then
127         if [ ! -d "$KARAF_BASE" ]; then
128             die "KARAF_BASE is not valid: $KARAF_BASE"
129         fi
130     else
131         KARAF_BASE=$KARAF_HOME
132     fi
133 }
134
135 locateData() {
136     if [ "x$KARAF_DATA" != "x" ]; then
137         if [ ! -d "$KARAF_DATA" ]; then
138             die "KARAF_DATA is not valid: $KARAF_DATA"
139         fi
140     else
141         KARAF_DATA=$KARAF_BASE/data
142     fi
143 }
144
145 locateEtc() {
146     if [ "x$KARAF_ETC" != "x" ]; then
147         if [ ! -d "$KARAF_ETC" ]; then
148             die "KARAF_ETC is not valid: $KARAF_ETC"
149         fi
150     else
151         KARAF_ETC=$KARAF_BASE/etc
152     fi
153 }
154
155 setupNativePath() {
156     # Support for loading native libraries
157     LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:$KARAF_BASE/lib:$KARAF_HOME/lib"
158
159     # For Cygwin, set PATH from LD_LIBRARY_PATH
160     if $cygwin; then
161         LD_LIBRARY_PATH=`cygpath --path --windows "$LD_LIBRARY_PATH"`
162         PATH="$PATH;$LD_LIBRARY_PATH"
163         export PATH
164     fi
165     export LD_LIBRARY_PATH
166 }
167
168 pathCanonical() {
169     dst="${1}"
170     while [ -h "${dst}" ] ; do
171         ls=`ls -ld "${dst}"`
172         link=`expr "$ls" : '.*-> \(.*\)$'`
173         if expr "$link" : '/.*' > /dev/null; then
174             dst="$link"
175         else
176             dst="`dirname "${dst}"`/$link"
177         fi
178     done
179     bas=`basename "${dst}"`
180     dir=`dirname "${dst}"`
181     if [ "$bas" != "$dir" ]; then
182         dst="`pathCanonical "$dir"`/$bas"
183     fi
184     echo "${dst}" | sed -e 's#//#/#g' -e 's#/./#/#g' -e 's#/[^/]*/../#/#g'
185 }
186
187 locateJava() {
188     # Setup the Java Virtual Machine
189     if $cygwin ; then
190         [ -n "$JAVA" ] && JAVA=`cygpath --unix "$JAVA"`
191         [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
192     fi
193
194     if [ "x$JAVA_HOME" = "x" ] && [ "$darwin" = "true" ]; then
195         JAVA_HOME="$(/usr/libexec/java_home -v 1.7)"
196     fi
197     if [ "x$JAVA" = "x" ] && [ -r /etc/gentoo-release ] ; then
198         JAVA_HOME=`java-config --jre-home`
199     fi
200     if [ "x$JAVA" = "x" ]; then
201         if [ "x$JAVA_HOME" != "x" ]; then
202             if [ ! -d "$JAVA_HOME" ]; then
203                 die "JAVA_HOME is not valid: $JAVA_HOME"
204             fi
205             JAVA="$JAVA_HOME/bin/java"
206         else
207             warn "JAVA_HOME not set; results may vary"
208             JAVA=`type java`
209             JAVA=`expr "$JAVA" : '.* \(/.*\)$'`
210             if [ "x$JAVA" = "x" ]; then
211                 die "java command not found"
212             fi
213         fi
214     fi
215     if [ "x$JAVA_HOME" = "x" ]; then
216         JAVA_HOME="$(dirname $(dirname $(pathCanonical "$JAVA")))"
217     fi
218 }
219
220 detectJVM() {
221    #echo "`$JAVA -version`"
222    # This service should call `java -version`,
223    # read stdout, and look for hints
224    if $JAVA -version 2>&1 | grep "^IBM" ; then
225        JVM_VENDOR="IBM"
226    # on OS/400, java -version does not contain IBM explicitly
227    elif $os400; then
228        JVM_VENDOR="IBM"
229    else
230        JVM_VENDOR="SUN"
231    fi
232    # echo "JVM vendor is $JVM_VENDOR"
233 }
234
235 setupDebugOptions() {
236     if [ "x$JAVA_OPTS" = "x" ]; then
237         JAVA_OPTS="$DEFAULT_JAVA_OPTS"
238     fi
239     export JAVA_OPTS
240
241     if [ "x$EXTRA_JAVA_OPTS" != "x" ]; then
242         JAVA_OPTS="$JAVA_OPTS $EXTRA_JAVA_OPTS"
243     fi
244
245     # Set Debug options if enabled
246     if [ "x$KARAF_DEBUG" != "x" ]; then
247         # Use the defaults if JAVA_DEBUG_OPTS was not set
248         if [ "x$JAVA_DEBUG_OPTS" = "x" ]; then
249             JAVA_DEBUG_OPTS="$DEFAULT_JAVA_DEBUG_OPTS"
250         fi
251
252         JAVA_OPTS="$JAVA_DEBUG_OPTS $JAVA_OPTS"
253         warn "Enabling Java debug options: $JAVA_DEBUG_OPTS"
254     fi
255 }
256
257 setupDefaults() {
258     DEFAULT_JAVA_OPTS="-Xms$JAVA_MIN_MEM -Xmx$JAVA_MAX_MEM "
259
260     #Set the JVM_VENDOR specific JVM flags
261     if [ "$JVM_VENDOR" = "SUN" ]; then
262         # permgen was removed in Java 8
263         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'`
264         if [ "$VERSION" -lt "80" ]; then
265             # Check some easily accessible MIN/MAX params for JVM mem usage
266             if [ "x$JAVA_PERM_MEM" != "x" ]; then
267                 DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS -XX:PermSize=$JAVA_PERM_MEM"
268             fi
269             if [ "x$JAVA_MAX_PERM_MEM" != "x" ]; then
270                 DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS -XX:MaxPermSize=$JAVA_MAX_PERM_MEM"
271             fi
272         fi
273         DEFAULT_JAVA_OPTS="-server $DEFAULT_JAVA_OPTS -Dcom.sun.management.jmxremote"
274     elif [ "$JVM_VENDOR" = "IBM" ]; then
275         if $os400; then
276             DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS"
277         elif $aix; then
278             DEFAULT_JAVA_OPTS="-Xverify:none -Xdump:heap -Xlp $DEFAULT_JAVA_OPTS"
279         else
280             DEFAULT_JAVA_OPTS="-Xverify:none $DEFAULT_JAVA_OPTS"
281         fi
282     fi
283
284     # Add default security file option
285     if [ "x$ODL_JAVA_SECURITY_PROPERTIES" != "x" ]; then
286         DEFAULT_JAVA_OPTS="-Djava.security.properties="${ODL_JAVA_SECURITY_PROPERTIES}" $DEFAULT_JAVA_OPTS"
287     else
288         DEFAULT_JAVA_OPTS="-Djava.security.properties="${KARAF_ETC}/odl.java.security" $DEFAULT_JAVA_OPTS"
289     fi
290
291     # Add the jars in the lib dir
292     for file in "$KARAF_HOME"/lib/*.jar
293     do
294         if [ -z "$CLASSPATH" ]; then
295             CLASSPATH="$file"
296         else
297             CLASSPATH="$CLASSPATH:$file"
298         fi
299     done
300
301     DEFAULT_JAVA_DEBUG_PORT="5005"
302     if [ "x$JAVA_DEBUG_PORT" = "x" ]; then
303         JAVA_DEBUG_PORT="$DEFAULT_JAVA_DEBUG_PORT"
304     fi
305     DEFAULT_JAVA_DEBUG_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$JAVA_DEBUG_PORT"
306
307     ##
308     ## TODO: Move to conf/profiler/yourkit.{sh|cmd}
309     ##
310     # Uncomment to enable YourKit profiling
311     #DEFAULT_JAVA_DEBUG_OPTS="-Xrunyjpagent"
312 }
313
314 init() {
315     # Determine if there is special OS handling we must perform
316     detectOS
317
318     # Unlimit the number of file descriptors if possible
319     unlimitFD
320
321     # Locate the Karaf home directory
322     locateHome
323
324     # Locate the Karaf base directory
325     locateBase
326
327     # Locate the Karaf data directory
328     locateData
329
330     # Locate the Karaf etc directory
331     locateEtc
332
333     # Setup the native library path
334     setupNativePath
335
336     # Locate the Java VM to execute
337     locateJava
338
339     # Determine the JVM vendor
340     detectJVM
341
342     # Setup default options
343     setupDefaults
344
345     # Install debug options
346     setupDebugOptions
347
348 }
349
350 run() {
351
352     CLASSPATH="${KARAF_HOME}/system/org/apache/karaf/instance/org.apache.karaf.instance.command/${karaf.version}/org.apache.karaf.instance.command-${karaf.version}.jar:${KARAF_HOME}/system/org/apache/karaf/instance/org.apache.karaf.instance.core/${karaf.version}/org.apache.karaf.instance.core-${karaf.version}.jar:${KARAF_HOME}/system/org/apache/karaf/shell/org.apache.karaf.shell.console/${karaf.version}/org.apache.karaf.shell.console-${karaf.version}.jar:${KARAF_HOME}/system/org/apache/karaf/shell/org.apache.karaf.shell.table/${karaf.version}/org.apache.karaf.shell.table-${karaf.version}.jar:${KARAF_HOME}/system/org/apache/aries/blueprint/org.apache.aries.blueprint.api/${aries.blueprint.api.version}/org.apache.aries.blueprint.api-${aries.blueprint.api.version}.jar:${KARAF_HOME}/system/org/apache/aries/blueprint/org.apache.aries.blueprint.core/${aries.blueprint.core.version}/org.apache.aries.blueprint.core-${aries.blueprint.core.version}.jar:${KARAF_HOME}/system/org/apache/aries/blueprint/org.apache.aries.blueprint.cm/${aries.blueprint.cm.version}/org.apache.aries.blueprint.cm-${aries.blueprint.cm.version}.jar:${KARAF_HOME}/system/org/ops4j/pax/logging/pax-logging-api/${pax.logging.version}/pax-logging-api-${pax.logging.version}.jar:${KARAF_HOME}/system/org/apache/felix/org.apache.felix.framework/${felix.framework.version}/org.apache.felix.framework-${felix.framework.version}.jar:${KARAF_HOME}/system/jline/jline/${jline.version}/jline-${jline.version}.jar:$CLASSPATH"
353
354     if $cygwin; then
355         KARAF_HOME=`cygpath --path --windows "$KARAF_HOME"`
356         KARAF_BASE=`cygpath --path --windows "$KARAF_BASE"`
357         KARAF_DATA=`cygpath --path --windows "$KARAF_DATA"`
358         KARAF_ETC=`cygpath --path --windows "$KARAF_ETC"`
359         if [ ! -z "$CLASSPATH" ]; then
360             CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
361         fi
362     fi
363
364     exec "$JAVA" $JAVA_OPTS -Dkaraf.instances="${KARAF_HOME}/instances" -Dkaraf.home="$KARAF_HOME" -Dkaraf.base="$KARAF_BASE" -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" org.apache.karaf.instance.main.Execute "$@"
365 }
366
367 main() {
368     init
369     run "$@"
370 }
371
372 main "$@"

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.