client 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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="client"
  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="$(/usr/libexec/java_home -v 1.7)"
  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. setupDefaults() {
  223. DEFAULT_JAVA_OPTS="-Xms$JAVA_MIN_MEM -Xmx$JAVA_MAX_MEM "
  224. #Set the JVM_VENDOR specific JVM flags
  225. if [ "$JVM_VENDOR" = "SUN" ]; then
  226. # permgen was removed in Java 8
  227. 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'`
  228. if [ "$VERSION" -lt "80" ]; then
  229. # Check some easily accessible MIN/MAX params for JVM mem usage
  230. if [ "x$JAVA_PERM_MEM" != "x" ]; then
  231. DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS -XX:PermSize=$JAVA_PERM_MEM"
  232. fi
  233. if [ "x$JAVA_MAX_PERM_MEM" != "x" ]; then
  234. DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS -XX:MaxPermSize=$JAVA_MAX_PERM_MEM"
  235. fi
  236. fi
  237. DEFAULT_JAVA_OPTS="-server $DEFAULT_JAVA_OPTS -Dcom.sun.management.jmxremote"
  238. elif [ "$JVM_VENDOR" = "IBM" ]; then
  239. if $os400; then
  240. DEFAULT_JAVA_OPTS="$DEFAULT_JAVA_OPTS"
  241. elif $aix; then
  242. DEFAULT_JAVA_OPTS="-Xverify:none -Xdump:heap -Xlp $DEFAULT_JAVA_OPTS"
  243. else
  244. DEFAULT_JAVA_OPTS="-Xverify:none $DEFAULT_JAVA_OPTS"
  245. fi
  246. fi
  247. # Add the jars in the lib dir
  248. CLASSPATH="$KARAF_HOME/system/org/apache/karaf/org.apache.karaf.client/4.0.9/org.apache.karaf.client-4.0.9.jar"
  249. CLASSPATH="$CLASSPATH:$KARAF_HOME/system/org/apache/sshd/sshd-core/0.14.0/sshd-core-0.14.0.jar"
  250. CLASSPATH="$CLASSPATH:$KARAF_HOME/system/jline/jline/2.14.1/jline-2.14.1.jar"
  251. CLASSPATH="$CLASSPATH:$KARAF_HOME/system/org/slf4j/slf4j-api/1.7.12/slf4j-api-1.7.12.jar"
  252. }
  253. init() {
  254. # Determine if there is special OS handling we must perform
  255. detectOS
  256. # Unlimit the number of file descriptors if possible
  257. unlimitFD
  258. # Locate the Karaf home directory
  259. locateHome
  260. # Locate the Karaf base directory
  261. locateBase
  262. # Locate the Karaf data directory
  263. locateData
  264. # Locate the Karaf etc directory
  265. locateEtc
  266. # Setup the native library path
  267. setupNativePath
  268. # Locate the Java VM to execute
  269. locateJava
  270. # Determine the JVM vendor
  271. detectJVM
  272. # Setup default options
  273. setupDefaults
  274. }
  275. run() {
  276. if $cygwin; then
  277. KARAF_HOME=`cygpath --path --windows "$KARAF_HOME"`
  278. KARAF_BASE=`cygpath --path --windows "$KARAF_BASE"`
  279. KARAF_DATA=`cygpath --path --windows "$KARAF_DATA"`
  280. KARAF_ETC=`cygpath --path --windows "$KARAF_ETC"`
  281. if [ ! -z "$CLASSPATH" ]; then
  282. CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
  283. fi
  284. fi
  285. 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.client.Main "$@"
  286. }
  287. main() {
  288. init
  289. run "$@"
  290. }
  291. main "$@"