Merge "Remove sonar.profile since it's deprecated in Sonar 4.5"
[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     local 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     local bas=`basename "${dst}"`
176     local 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)"
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" : '.*is \(.*\)$'`
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     # Set Debug options if enabled
238     if [ "x$KARAF_DEBUG" != "x" ]; then
239         # Use the defaults if JAVA_DEBUG_OPTS was not set
240         if [ "x$JAVA_DEBUG_OPTS" = "x" ]; then
241             JAVA_DEBUG_OPTS="$DEFAULT_JAVA_DEBUG_OPTS"
242         fi
243
244         JAVA_OPTS="$JAVA_DEBUG_OPTS $JAVA_OPTS"
245         warn "Enabling Java debug options: $JAVA_DEBUG_OPTS"
246     fi
247 }
248
249 setupDefaults() {
250     DEFAULT_JAVA_OPTS="-Xms$JAVA_MIN_MEM -Xmx$JAVA_MAX_MEM "
251
252     #Set the JVM_VENDOR specific JVM flags
253     if [ "$JVM_VENDOR" = "SUN" ]; then
254         #
255         # Check some easily accessible MIN/MAX params for JVM mem usage
256         #
257         if [ "x$JAVA_PERM_MEM" != "x" ]; then
258             DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS -XX:PermSize=$JAVA_PERM_MEM"
259         fi
260         if [ "x$JAVA_MAX_PERM_MEM" != "x" ]; then
261             DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS -XX:MaxPermSize=$JAVA_MAX_PERM_MEM"
262         fi
263         DEFAULT_JAVA_OPTS="-server $DEFAULT_JAVA_OPTS -Dcom.sun.management.jmxremote"
264     elif [ "$JVM_VENDOR" = "IBM" ]; then
265         if $os400; then
266             DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS"
267         elif $aix; then
268             DEFAULT_JAVA_OPTS="-Xverify:none -Xdump:heap -Xlp $DEFAULT_JAVA_OPTS"
269         else
270             DEFAULT_JAVA_OPTS="-Xverify:none $DEFAULT_JAVA_OPTS"
271         fi
272     fi
273
274     # Add the jars in the lib dir
275     for file in "$KARAF_HOME"/lib/*.jar
276     do
277         if [ -z "$CLASSPATH" ]; then
278             CLASSPATH="$file"
279         else
280             CLASSPATH="$CLASSPATH:$file"
281         fi
282     done
283     DEFAULT_JAVA_DEBUG_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
284
285     ##
286     ## TODO: Move to conf/profiler/yourkit.{sh|cmd}
287     ##
288     # Uncomment to enable YourKit profiling
289     #DEFAULT_JAVA_DEBUG_OPTS="-Xrunyjpagent"
290 }
291
292 init() {
293     # Determine if there is special OS handling we must perform
294     detectOS
295
296     # Unlimit the number of file descriptors if possible
297     unlimitFD
298
299     # Locate the Karaf home directory
300     locateHome
301
302     # Locate the Karaf base directory
303     locateBase
304
305     # Locate the Karaf data directory
306     locateData
307
308     # Locate the Karaf etc directory
309     locateEtc
310
311     # Setup the native library path
312     setupNativePath
313
314     # Locate the Java VM to execute
315     locateJava
316
317     # Determine the JVM vendor
318     detectJVM
319
320     # Setup default options
321     setupDefaults
322
323     # Install debug options
324     setupDebugOptions
325
326 }
327
328 run() {
329
330     CLASSPATH="${KARAF_HOME}/system/org/apache/karaf/instance/org.apache.karaf.instance.command/3.0.1/org.apache.karaf.instance.command-3.0.1.jar:${KARAF_HOME}/system/org/apache/karaf/instance/org.apache.karaf.instance.core/3.0.1/org.apache.karaf.instance.core-3.0.1.jar:${KARAF_HOME}/system/org/apache/karaf/shell/org.apache.karaf.shell.console/3.0.1/org.apache.karaf.shell.console-3.0.1.jar:${KARAF_HOME}/system/org/apache/karaf/shell/org.apache.karaf.shell.table/3.0.1/org.apache.karaf.shell.table-3.0.1.jar:${KARAF_HOME}/system/org/apache/aries/blueprint/org.apache.aries.blueprint.api/1.0.0/org.apache.aries.blueprint.api-1.0.0.jar:${KARAF_HOME}/system/org/apache/aries/blueprint/org.apache.aries.blueprint.core/1.4.0/org.apache.aries.blueprint.core-1.4.0.jar:${KARAF_HOME}/system/org/apache/aries/blueprint/org.apache.aries.blueprint.cm/1.0.3/org.apache.aries.blueprint.cm-1.0.3.jar:${KARAF_HOME}/system/org/ops4j/pax/logging/pax-logging-api/1.7.2/pax-logging-api-1.7.2.jar:${KARAF_HOME}/system/org/apache/felix/org.apache.felix.framework/4.2.1/org.apache.felix.framework-4.2.1.jar:${KARAF_HOME}/system/jline/jline/2.11/jline-2.11.jar:$CLASSPATH"
331
332     if $cygwin; then
333         KARAF_HOME=`cygpath --path --windows "$KARAF_HOME"`
334         KARAF_BASE=`cygpath --path --windows "$KARAF_BASE"`
335         KARAF_DATA=`cygpath --path --windows "$KARAF_DATA"`
336         KARAF_ETC=`cygpath --path --windows "$KARAF_ETC"`
337         CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
338     fi
339
340     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 "$@"
341 }
342
343 main() {
344     init
345     run "$@"
346 }
347
348 main "$@"
349