shell 10 KB

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