instance 11 KB

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