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