Modified the setting of JAVA_HOME for linux to be more generic.
[controller.git] / opendaylight / distribution / opendaylight / src / main / resources / run.sh
1 #!/bin/bash
2
3 platform='unknown'
4 unamestr=`uname`
5 if [[ "$unamestr" == 'Linux' ]]; then
6    platform='linux'
7 elif [[ "$unamestr" == 'Darwin' ]]; then
8    platform='osx'
9 fi
10
11 if [[ $platform == 'linux' ]]; then
12    fullpath=`readlink -f $0`
13
14    if [[ -z ${JAVA_HOME} ]]; then
15       # Find the actual location of the Java launcher:
16       java_launcher=`which java`
17       java_launcher=`readlink -f "${java_launcher}"`
18
19       # Compute the Java home from the location of the Java launcher:
20       export JAVA_HOME="${java_launcher%/bin/java}"
21     fi
22 elif [[ $platform == 'osx' ]]; then
23    TARGET_FILE=$0
24    cd `dirname $TARGET_FILE`
25    TARGET_FILE=`basename $TARGET_FILE`
26
27    # Iterate down a (possible) chain of symlinks
28    while [ -L "$TARGET_FILE" ]
29    do
30        TARGET_FILE=`readlink $TARGET_FILE`
31        cd `dirname $TARGET_FILE`
32        TARGET_FILE=`basename $TARGET_FILE`
33    done
34
35    # Compute the canonicalized name by finding the physical path
36    # for the directory we're in and appending the target file.
37    PHYS_DIR=`pwd -P`
38    RESULT=$PHYS_DIR/$TARGET_FILE
39    fullpath=$RESULT
40
41    [[ -z ${JAVA_HOME} ]] && [[ -x "/usr/libexec/java_home" ]] && export JAVA_HOME=`/usr/libexec/java_home -v 1.7`;
42
43 fi
44
45 [[ -z ${JAVA_HOME} ]] && echo "Need to set JAVA_HOME environment variable" && exit -1;
46 [[ ! -x ${JAVA_HOME}/bin/java ]] && echo "Cannot find an executable \
47 JVM at path ${JAVA_HOME}/bin/java check your JAVA_HOME" && exit -1;
48
49 basedir=`dirname ${fullpath}`
50
51 function usage {
52     echo "Usage: $0 [-jmx] [-jmxport <num>] [-debug] [-debugsuspend] [-debugport <num>] [-start [<console port>]] [-stop] [-status] [-console] [-help] [<other args will automatically be used for the JVM>]"
53     exit 1
54 }
55
56 if [ -z ${TMP} ]; then
57     pidfile="/tmp/opendaylight.PID"
58 else
59     pidfile="${TMP}/opendaylight.PID"
60 fi
61 debug=0
62 debugsuspend=0
63 debugport=8000
64 debugportread=""
65 startdaemon=0
66 daemonport=2400
67 daemonportread=""
68 jmxport=1088
69 jmxportread=""
70 startjmx=0
71 stopdaemon=0
72 statusdaemon=0
73 consolestart=1
74 dohelp=0
75 extraJVMOpts=""
76 unknown_option=0
77 while true ; do
78     case "$1" in
79         -debug) debug=1; shift ;;
80         -jmx) startjmx=1; shift ;;
81         -debugsuspend) debugsuspend=1; shift ;;
82         -debugport) shift; debugportread="$1"; if [[ "${debugportread}" =~ ^[0-9]+$ ]] ; then debugport=${debugportread}; shift; else echo "-debugport expects a number but was not found"; exit -1; fi;;
83         -jmxport) shift; jmxportread="$1"; if [[ "${jmxportread}" =~ ^[0-9]+$ ]] ; then jmxport=${jmxportread}; shift; else echo "-jmxport expects a number but was not found"; exit -1; fi;;
84         -start) startdaemon=1; shift; daemonportread="$1"; if [[ "${daemonportread}" =~ ^[0-9]+$ ]] ; then daemonport=${daemonportread}; shift; fi;;
85         -stop) stopdaemon=1; shift ;;
86         -status) statusdaemon=1; shift ;;
87         -console) shift ;;
88         -help) dohelp=1; shift;;
89         -D*) extraJVMOpts="${extraJVMOpts} $1"; shift;;
90         -X*) extraJVMOpts="${extraJVMOpts} $1"; shift;;
91         "") break ;;
92         *) echo "Unknown option $1"; unknown_option=1; shift ;;
93     esac
94 done
95
96 # Unknown Options and help
97 if [ "${unknown_option}" -eq 1 ]; then
98     usage
99 fi
100
101 if [ "${dohelp}" -eq 1 ]; then
102     usage
103 fi
104
105 # Validate debug port
106 if [[ "${debugport}" -lt 1024 ]] || [[ "${debugport}" -gt 65535 ]]; then
107     echo "Debug Port not in the range [1024,65535] ${debugport}"
108     exit -1
109 fi
110
111 # Validate daemon console port
112 if [[ "${daemonport}" -lt 1024 ]] || [[ "${daemonport}" -gt 65535 ]]; then
113     echo "Daemon console Port not in the range [1024,65535] value is ${daemonport}"
114     exit -1
115 fi
116
117 # Validate jmx port
118 if [[ "${jmxport}" -lt 1024 ]] || [[ "${jmxport}" -gt 65535 ]]; then
119     echo "JMX Port not in the range [1024,65535] value is ${jmxport}"
120     exit -1
121 fi
122
123 # Debug options
124 if [ "${debugsuspend}" -eq 1 ]; then
125     extraJVMOpts="${extraJVMOpts} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=${debugport}"
126 elif [ "${debug}" -eq 1 ]; then
127     extraJVMOpts="${extraJVMOpts} -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${debugport}"
128 fi
129
130 # Add JMX support
131 if [ "${startjmx}" -eq 1 ]; then
132     extraJVMOpts="${extraJVMOpts} -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=${jmxport} -Dcom.sun.management.jmxremote"
133 fi
134
135 ########################################
136 # Now add to classpath the OSGi JAR
137 ########################################
138 CLASSPATH=${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar
139 FWCLASSPATH=file:${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar
140
141 ########################################
142 # Now add the extensions
143 ########################################
144
145 # Extension 1: this is used to be able to convert all the
146 # bundleresouce: URL in file: so packages that are not OSGi ready can
147 # still work. Notably this is the case for spring classes
148 CLASSPATH=${CLASSPATH}:${basedir}/lib/org.eclipse.virgo.kernel.equinox.extensions-3.6.0.RELEASE.jar
149 FWCLASSPATH=${FWCLASSPATH},file:${basedir}/lib/org.eclipse.virgo.kernel.equinox.extensions-3.6.0.RELEASE.jar
150
151 ########################################
152 # Now add the launcher
153 ########################################
154 CLASSPATH=${CLASSPATH}:${basedir}/lib/org.eclipse.equinox.launcher-1.3.0.v20120522-1813.jar
155 FWCLASSPATH=${FWCLASSPATH},file:${basedir}/lib/org.eclipse.equinox.launcher-1.3.0.v20120522-1813.jar
156
157 if [ "${stopdaemon}" -eq 1 ]; then
158     if [ -e "${pidfile}" ]; then
159         daemonpid=`cat "${pidfile}"`
160         kill "${daemonpid}"
161         rm -f "${pidfile}"
162         echo "Controller with PID: ${daemonpid} -- Stopped!"
163         exit 0
164     else
165         echo "Doesn't seem any Controller daemon is currently running"
166         exit -1
167     fi
168 fi
169
170 if [ "${statusdaemon}" -eq 1 ]; then
171     if [ -e "${pidfile}" ]; then
172         daemonpid=`cat "${pidfile}"`
173         ps -p ${daemonpid} > /dev/null
174         daemonexists=$?
175         if [ "${daemonexists}" -eq 0 ]; then
176             echo "Controller with PID: ${daemonpid} -- Running!"
177             exit 0
178         else
179             echo "Controller with PID: ${daemonpid} -- Doesn't seem to exist"
180             rm -f "${pidfile}"
181             exit 0
182         fi
183     else
184         echo "Doesn't seem any Controller daemon is currently running, at least no PID file has been found"
185         exit -1
186     fi
187 fi
188
189 if [ "${startdaemon}" -eq 1 ]; then
190     if [ -e "${pidfile}" ]; then
191         echo "Another instance of controller running, check with $0 -status"
192         exit -1
193     fi
194     $JAVA_HOME/bin/java ${extraJVMOpts} \
195         -Djava.io.tmpdir=${basedir}/work/tmp \
196         -Dosgi.install.area=${basedir} \
197         -Dosgi.configuration.area=${basedir}/configuration \
198         -Dosgi.frameworkClassPath=${FWCLASSPATH} \
199         -Dosgi.framework=file:${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar \
200         -classpath ${CLASSPATH} \
201         org.eclipse.equinox.launcher.Main \
202         -console ${daemonport} \
203         -consoleLog &
204     daemonpid=$!
205     echo ${daemonpid} > ${pidfile}
206 elif [ "${consolestart}" -eq 1 ]; then
207     if [ -e "${pidfile}" ]; then
208         echo "Another instance of controller running, check with $0 -status"
209         exit -1
210     fi
211     $JAVA_HOME/bin/java ${extraJVMOpts} \
212         -Djava.io.tmpdir=${basedir}/work/tmp \
213         -Dosgi.install.area=${basedir} \
214         -Dosgi.configuration.area=${basedir}/configuration \
215         -Dosgi.frameworkClassPath=${FWCLASSPATH} \
216         -Dosgi.framework=file:${basedir}/lib/org.eclipse.osgi-3.8.1.v20120830-144521.jar \
217         -classpath ${CLASSPATH} \
218         org.eclipse.equinox.launcher.Main \
219         -console \
220         -consoleLog
221 fi