You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

karaf 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. # Use in priority xpg4 awk or nawk on SunOS as standard awk is outdated
  20. AWK=awk
  21. if ${solaris}; then
  22. if [ -x /usr/xpg4/bin/awk ]; then
  23. AWK=/usr/xpg4/bin/awk
  24. elif [ -x /usr/bin/nawk ]; then
  25. AWK=/usr/bin/nawk
  26. fi
  27. fi
  28. READLINK_EXISTS=$(command -v readlink &> /dev/null)
  29. if [ -z "$READLINK_EXISTS" ]; then
  30. OURPWD=${PWD}
  31. cd "$(dirname "${1}")" || exit 2
  32. LINK=$(ls -l "$(basename "${1}")" | ${AWK} -F"-> " '{print $2}')
  33. while [ "${LINK}" ]; do
  34. echo "link: ${LINK}" >&2
  35. cd "$(dirname "${LINK}")" || exit 2
  36. LINK=$(ls -l "$(basename "${1}")" | ${AWK} -F"-> " '{print $2}')
  37. done
  38. REALPATH="${PWD}/$(basename "${1}")"
  39. cd "${OURPWD}" || exit 2
  40. echo "${REALPATH}"
  41. else
  42. OURPWD=${PWD}
  43. cd "$(dirname "${1}")" || exit 2
  44. LINK=$(readlink "$(basename "${1}")")
  45. while [ "${LINK}" ]; do
  46. echo "link: ${LINK}" >&2
  47. cd "$(dirname "${LINK}")" || exit 2
  48. LINK=$(readlink "$(basename "${1}")")
  49. done
  50. REALPATH="${PWD}/$(basename "${1}")"
  51. cd "${OURPWD}" || exit 2
  52. echo "${REALPATH}"
  53. fi
  54. }
  55. REALNAME=$(realpath "$0")
  56. DIRNAME=$(dirname "${REALNAME}")
  57. PROGNAME=$(basename "${REALNAME}")
  58. LOCAL_CLASSPATH=$CLASSPATH
  59. #
  60. # Load common functions
  61. #
  62. . "${DIRNAME}/inc"
  63. #
  64. # Sourcing environment settings for karaf similar to tomcats setenv
  65. #
  66. KARAF_SCRIPT="${PROGNAME}"
  67. export KARAF_SCRIPT
  68. if [ -f "${DIRNAME}/setenv" ]; then
  69. . "${DIRNAME}/setenv"
  70. fi
  71. forceNoRoot() {
  72. # If configured, prevent execution as root
  73. if [ "${KARAF_NOROOT}" ] && [ "$(id -u)" -eq 0 ]; then
  74. die "Do not run as root!"
  75. fi
  76. }
  77. setupClassPath() {
  78. # Add the jars in the lib dir
  79. for file in "${KARAF_HOME}"/lib/boot/*.jar
  80. do
  81. if [ -z "${CLASSPATH}" ]; then
  82. CLASSPATH="${file}"
  83. else
  84. CLASSPATH="${CLASSPATH}:${file}"
  85. fi
  86. done
  87. }
  88. checkRootInstance() {
  89. ROOT_INSTANCE_RUNNING=false
  90. if [ -f "${KARAF_HOME}/instances/instance.properties" ];
  91. then
  92. ROOT_INSTANCE_PID=$(sed -n -e '/item.0.pid/ s/.*\= *//p' "${KARAF_HOME}/instances/instance.properties")
  93. ROOT_INSTANCE_NAME=$(sed -n -e '/item.0.name/ s/.*\= *//p' "${KARAF_HOME}/instances/instance.properties")
  94. if [ "${ROOT_INSTANCE_PID}" -ne "0" ]; then
  95. if ps -p "${ROOT_INSTANCE_PID}" > /dev/null
  96. then
  97. MAIN=org.apache.karaf.main.Main
  98. PID_COMMAND=$("${PS_PREFIX}"ps -p "${ROOT_INSTANCE_PID}" -o args | sed 1d)
  99. if [ "${PID_COMMAND#*$MAIN}" != "$PID_COMMAND" ]; then
  100. ROOT_INSTANCE_RUNNING=true
  101. fi
  102. fi
  103. fi
  104. fi
  105. }
  106. init() {
  107. # Prevent root execution if configured
  108. forceNoRoot
  109. # Determine if there is special OS handling we must perform
  110. detectOS
  111. # Unlimit the number of file descriptors if possible
  112. unlimitFD
  113. # Locate the Karaf home directory
  114. locateHome
  115. # Locate the Karaf base directory
  116. locateBase
  117. # Locate the Karaf data directory
  118. locateData
  119. # Locate the Karaf etc directory
  120. locateEtc
  121. # Setup the native library path
  122. setupNativePath
  123. # Locate the Java VM to execute
  124. locateJava
  125. # Determine the JVM vendor
  126. detectJVM
  127. # Determine the JVM version >= 1.6
  128. checkJvmVersion
  129. # Check if a root instance is already running
  130. checkRootInstance
  131. # Setup default options
  132. setupDefaults
  133. # Setup classpath
  134. setupClassPath
  135. # Install debug options
  136. setupDebugOptions
  137. }
  138. run() {
  139. OPTS="-Dkaraf.startLocalConsole=true -Dkaraf.startRemoteShell=true"
  140. MAIN=org.apache.karaf.main.Main
  141. if [ "x$CHECK_ROOT_INSTANCE_RUNNING" = "x" ]; then
  142. CHECK_ROOT_INSTANCE_RUNNING=true
  143. fi
  144. JAVA_ENDORSED_DIRS="${JAVA_HOME}/jre/lib/endorsed:${JAVA_HOME}/lib/endorsed:${KARAF_HOME}/lib/endorsed"
  145. JAVA_EXT_DIRS="${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext:${KARAF_HOME}/lib/ext"
  146. if ${cygwin}; then
  147. JAVA_HOME=$(cygpath --path --windows "${JAVA_HOME}")
  148. JAVA_ENDORSED_DIRS=$(cygpath --path --windows "${JAVA_ENDORSED_DIRS}")
  149. JAVA_EXT_DIRS=$(cygpath --path --windows "${JAVA_EXT_DIRS}")
  150. fi
  151. convertPaths
  152. cd "${KARAF_BASE}" || exit 2
  153. if [ -z "${KARAF_EXEC}" ]; then
  154. KARAF_EXEC=""
  155. fi
  156. debug=false
  157. debugs=false
  158. nodebug=false
  159. while [ "${1}" != "" ]; do
  160. case "${1}" in
  161. 'clean')
  162. rm -rf "${KARAF_DATA:?}"
  163. shift
  164. ;;
  165. 'debug')
  166. debug=true
  167. shift
  168. ;;
  169. 'debugs')
  170. debug=true
  171. debugs=true
  172. shift
  173. ;;
  174. 'status')
  175. MAIN=org.apache.karaf.main.Status
  176. CHECK_ROOT_INSTANCE_RUNNING=false
  177. nodebug=true
  178. shift
  179. ;;
  180. 'stop')
  181. MAIN=org.apache.karaf.main.Stop
  182. CHECK_ROOT_INSTANCE_RUNNING=false
  183. nodebug=true
  184. shift
  185. ;;
  186. 'console')
  187. CHECK_ROOT_INSTANCE_RUNNING=false
  188. shift
  189. ;;
  190. 'server')
  191. OPTS="-Dkaraf.startLocalConsole=false -Dkaraf.startRemoteShell=true"
  192. shift
  193. ;;
  194. 'run')
  195. OPTS="-Dkaraf.startLocalConsole=false -Dkaraf.startRemoteShell=true -Dkaraf.log.console=ALL"
  196. shift
  197. ;;
  198. 'daemon')
  199. OPTS="-Dkaraf.startLocalConsole=false -Dkaraf.startRemoteShell=true"
  200. KARAF_DAEMON="true"
  201. KARAF_EXEC="exec"
  202. shift
  203. ;;
  204. 'client')
  205. OPTS="-Dkaraf.startLocalConsole=true -Dkaraf.startRemoteShell=false"
  206. CHECK_ROOT_INSTANCE_RUNNING=false
  207. nodebug=true
  208. shift
  209. ;;
  210. 'classpath')
  211. echo "Classpath: ${CLASSPATH}"
  212. shift
  213. ;;
  214. *)
  215. break
  216. ;;
  217. esac
  218. done
  219. if ${nodebug}; then
  220. debug=false
  221. fi
  222. if ${debug}; then
  223. if [ "x${JAVA_DEBUG_OPTS}" = "x" ]; then
  224. if ${debugs}; then
  225. JAVA_DEBUG_OPTS="${DEFAULT_JAVA_DEBUGS_OPTS}"
  226. else
  227. JAVA_DEBUG_OPTS="${DEFAULT_JAVA_DEBUG_OPTS}"
  228. fi
  229. fi
  230. JAVA_OPTS="${JAVA_DEBUG_OPTS} ${JAVA_OPTS}"
  231. fi
  232. while true; do
  233. # When users want to update the lib version of, they just need to create
  234. # a lib.next directory and on the new restart, it will replace the current lib directory.
  235. if [ -d "${KARAF_HOME:?}/lib.next" ] ; then
  236. echo "Updating libs..."
  237. rm -rf "${KARAF_HOME:?}/lib"
  238. mv -f "${KARAF_HOME:?}/lib.next" "${KARAF_HOME}/lib"
  239. echo "Updating classpath..."
  240. CLASSPATH=$LOCAL_CLASSPATH
  241. setupClassPath
  242. fi
  243. # Ensure the log directory exists
  244. # We may need to have a place to redirect stdout/stderr
  245. if [ ! -d "${KARAF_DATA}/log" ]; then
  246. mkdir -p "${KARAF_DATA}/log"
  247. fi
  248. if [ ! -d "${KARAF_DATA}/tmp" ]; then
  249. mkdir -p "${KARAF_DATA}/tmp"
  250. fi
  251. if [ "${ROOT_INSTANCE_RUNNING}" = "false" ] || [ "${CHECK_ROOT_INSTANCE_RUNNING}" = "false" ] ; then
  252. if [ "${VERSION}" -gt "8" ]; then
  253. ${KARAF_EXEC} "${JAVA}" ${JAVA_OPTS} \
  254. --add-reads=java.xml=java.logging \
  255. --patch-module java.base=lib/endorsed/org.apache.karaf.specs.locator-4.2.1.jar \
  256. --patch-module java.xml=lib/endorsed/org.apache.karaf.specs.java.xml-4.2.1.jar \
  257. --add-opens java.base/java.security=ALL-UNNAMED \
  258. --add-opens java.base/java.net=ALL-UNNAMED \
  259. --add-opens java.base/java.lang=ALL-UNNAMED \
  260. --add-opens java.base/java.util=ALL-UNNAMED \
  261. --add-opens java.naming/javax.naming.spi=ALL-UNNAMED \
  262. --add-opens java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED \
  263. --add-exports=java.base/sun.net.www.protocol.http=ALL-UNNAMED \
  264. --add-exports=java.base/sun.net.www.protocol.https=ALL-UNNAMED \
  265. --add-exports=java.base/sun.net.www.protocol.jar=ALL-UNNAMED \
  266. --add-exports=jdk.xml.dom/org.w3c.dom.html=ALL-UNNAMED \
  267. --add-exports=jdk.naming.rmi/com.sun.jndi.url.rmi=ALL-UNNAMED \
  268. -Dkaraf.instances="${KARAF_HOME}/instances" \
  269. -Dkaraf.home="${KARAF_HOME}" \
  270. -Dkaraf.base="${KARAF_BASE}" \
  271. -Dkaraf.data="${KARAF_DATA}" \
  272. -Dkaraf.etc="${KARAF_ETC}" \
  273. -Dkaraf.restart.jvm.supported=true \
  274. -Djava.io.tmpdir="${KARAF_DATA}/tmp" \
  275. -Djava.util.logging.config.file="${KARAF_BASE}/etc/java.util.logging.properties" \
  276. ${KARAF_SYSTEM_OPTS} \
  277. ${KARAF_OPTS} \
  278. ${OPTS} \
  279. -classpath "${CLASSPATH}" \
  280. ${MAIN} "$@"
  281. else
  282. ${KARAF_EXEC} "${JAVA}" ${JAVA_OPTS} \
  283. -Djava.endorsed.dirs="${JAVA_ENDORSED_DIRS}" \
  284. -Djava.ext.dirs="${JAVA_EXT_DIRS}" \
  285. -Dkaraf.instances="${KARAF_HOME}/instances" \
  286. -Dkaraf.home="${KARAF_HOME}" \
  287. -Dkaraf.base="${KARAF_BASE}" \
  288. -Dkaraf.data="${KARAF_DATA}" \
  289. -Dkaraf.etc="${KARAF_ETC}" \
  290. -Dkaraf.restart.jvm.supported=true \
  291. -Djava.io.tmpdir="${KARAF_DATA}/tmp" \
  292. -Djava.util.logging.config.file="${KARAF_BASE}/etc/java.util.logging.properties" \
  293. ${KARAF_SYSTEM_OPTS} \
  294. ${KARAF_OPTS} \
  295. ${OPTS} \
  296. -classpath "${CLASSPATH}" \
  297. ${MAIN} "$@"
  298. fi
  299. else
  300. die "There is a Root instance already running with name ${ROOT_INSTANCE_NAME} and pid ${ROOT_INSTANCE_PID}. If you know what you are doing and want to force the run anyway, export CHECK_ROOT_INSTANCE_RUNNING=false and re run the command."
  301. fi
  302. KARAF_RC=$?
  303. if [ ${KARAF_DAEMON} ] ; then
  304. exit ${KARAF_RC}
  305. else
  306. if [ "${KARAF_RC}" -eq 10 ]; then
  307. echo "Restarting JVM..."
  308. else
  309. exit ${KARAF_RC}
  310. fi
  311. fi
  312. done
  313. }
  314. nothing() {
  315. # nothing to do here
  316. a=a
  317. }
  318. main() {
  319. init
  320. trap 'nothing' TSTP
  321. run "$@"
  322. }
  323. main "$@"