client 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. #!/bin/sh
  2. ###############################################################################
  3. # WebSphere Application Client liberty launch script
  4. #
  5. # Copyright IBM Corp. 2011, 2014
  6. # The source code for this program is not published or other-
  7. # wise divested of its trade secrets, irrespective of what has
  8. # been deposited with the U.S. Copyright Office.
  9. #
  10. # ----------------------------------------------------------------------------
  11. #
  12. # To customize the use of this script (for example with /etc/init.d system
  13. # service managers), use the following environment variables:
  14. #
  15. # JAVA_HOME - The java executable is found in ${JAVA_HOME}/bin
  16. #
  17. # JVM_ARGS - A list of JVM command line options,
  18. # e.g. system properties or -X parameters
  19. # The value will be expanded by the shell (use quotes for spaces)
  20. #
  21. # LOG_DIR - The log file directory
  22. # The default value is ${WLP_CLIENT_OUTPUT_DIR}/clientName/logs
  23. #
  24. # LOG_FILE - The log file name
  25. # This log file is only used if the client is run in the
  26. # background via the start action. This is not supported in client.
  27. # The default value is console.log
  28. #
  29. # WLP_USER_DIR - The user/custom configuration directory used to store
  30. # shared and client-specific configuration.
  31. # See README.TXT for details about shared resource locations.
  32. # A client's configuration is at ${WLP_USER_DIR}/clients/clientName
  33. # The default value is the usr directory in the install directory.
  34. #
  35. # WLP_CLIENT_OUTPUT_DIR - The directory containing output files for defined clients.
  36. # This directory must have both read and write permissions for
  37. # the user or users that start clients.
  38. # By default, a client's output logs and workarea are stored
  39. # in the ${WLP_USER_DIR}/clients/clientName directory
  40. # (alongside configuration and applications).
  41. # If this variable is set, the output logs and workarea
  42. # would be stored in ${WLP_CLIENT_OUTPUT_DIR}/clientName.
  43. #
  44. # WLP_DEBUG_ADDRESS - The port to use when running the client in debug mode.
  45. # The default value is 7778.
  46. #
  47. # WLP_SKIP_UMASK - Skip setting the umask value to allow the default value
  48. # to be used.
  49. ###############################################################################
  50. ##
  51. ## Determine the platform and absolute path of the installation directory.
  52. ##
  53. case $OSTYPE in
  54. cygwin)
  55. uname=CYGWIN_NT
  56. # Determine the installation directory without forking if possible. Use
  57. # eval to hide ${var//find/replace}, ${var%suffix}, and ${var:first:length}
  58. # syntaxes from shells that can't parse them.
  59. eval '
  60. # cd to the install directory.
  61. savePWD=$PWD
  62. script=${0//\\/\/}
  63. unset CDPATH; cd "${script%/*}"/..
  64. # Convert the install (current working) directory to a Windows path.
  65. case $PWD in
  66. /cygdrive/?/*)
  67. # Use ${var:first:length} to avoid forking for cygpath.
  68. WLP_INSTALL_DIR=${PWD:10:1}:${PWD:11}
  69. ;;
  70. *)
  71. WLP_INSTALL_DIR=`cygpath -ma .`
  72. esac
  73. cd "$savePWD"
  74. '
  75. ;;
  76. *)
  77. uname=`uname`
  78. case $uname in
  79. CYGWIN_*)
  80. WLP_INSTALL_DIR=`cygpath -ma "${0}"/../..`
  81. ;;
  82. *)
  83. dirname=`dirname "$0"`
  84. WLP_INSTALL_DIR=`unset CDPATH; cd "$dirname/.." && pwd`
  85. esac
  86. esac
  87. ##
  88. ## Platform specific setup
  89. ##
  90. UMASK_O='umask o='
  91. tryShellExtensions=true
  92. shareclassesCacheDirPerm=true
  93. defaultFileEncoding=
  94. newline='
  95. '
  96. nativeEBCDIC=false
  97. case ${uname} in
  98. CYGWIN_*)
  99. # java.exe is a non-Cygwin process, so we need to pass -W.
  100. shareclassesCacheDirPerm=false
  101. ;;
  102. OS/390)
  103. defaultFileEncoding=iso8859-1
  104. nativeEBCDIC=true # Auto-convert client.env/client.jvm.options from ASCII-to-EBCDIC, if necessary.
  105. _BPXK_WLM_PROPAGATE=NO # Prevent WLM enclave propagation to spawned processes
  106. _EDC_PTHREAD_YIELD=-2 # Disable sleeps inside of pthread_yield
  107. JAVA_PROPAGATE=NO # Prevent WLM enclave propagation to new threads
  108. export _BPXK_WLM_PROPAGATE _EDC_PTHREAD_YIELD JAVA_PROPAGATE
  109. WLP_NLS_PATH="${WLP_INSTALL_DIR}/lib/native/zos/s390x/nls/%N.cat"
  110. if [ ${NLSPATH} ]
  111. then
  112. NLSPATH="${WLP_NLS_PATH}:${NLSPATH}"
  113. else
  114. NLSPATH="${WLP_NLS_PATH}"
  115. fi
  116. ;;
  117. esac
  118. ##
  119. ## safeEcho: Portable echo that can handle backslashes and leading dashes.
  120. safeEcho()
  121. {
  122. cat <<EOF
  123. $*
  124. EOF
  125. }
  126. # Define escapeForEval functions using ${var//find/replace} and ${var#suffix}
  127. # if possible since those constructs are significantly faster than safeEcho+sed
  128. # since they avoid forks. Use eval (to hide the syntax from shells that don't
  129. # support them) in a subshell (to avoid exiting the shell process on error) to
  130. # test if the shell has support.
  131. if ${tryShellExtensions} && (eval 'true ${1//b/c} ${1#*=}') 2> /dev/null; then
  132. # The shell has support. Define the functions using eval, again to hide the
  133. # syntax from shells that don't support it.
  134. eval "
  135. escapeForEval()
  136. {
  137. escapeForEvalResult=\\'\${1//\\'/\\'\\\"\\'\\\"\\'}\\'
  138. }
  139. extractValueAndEscapeForEval()
  140. {
  141. escapeForEval \"\${1#*=}\"
  142. }
  143. substitutePrefixVar()
  144. {
  145. case \$1 in
  146. @\$2@*) substitutePrefixVarResult=\$3\${1#@\$2@};;
  147. *) substitutePrefixVarResult=\$1
  148. esac
  149. }
  150. "
  151. else
  152. ##
  153. ## escapeForEval: Escape the first parameter to be safe for use in eval,
  154. ## and set escapeForEvalResult with the result.
  155. ##
  156. escapeForEval()
  157. {
  158. escapeForEvalResult=\'`safeEcho "$1" | sed s/\'/\'\"\'\"\'/g`\'
  159. }
  160. ##
  161. ## extractValueAndEscapeForEval: Extract the value of a var=value string,
  162. ## and set escapeForEvalResult with the result.
  163. ##
  164. extractValueAndEscapeForEval()
  165. {
  166. escapeForEvalResult=\'`safeEcho "$1" | sed -e 's/[^=]*=//' -e s/\'/\'\"\'\"\'/g`\'
  167. }
  168. ##
  169. ## substitutePrefixVar: If $1 has a prefix @$2@, set substitutePrefixVarResult
  170. ## to $1 with the prefix replaced by $3. Otherwise, set
  171. ## to $1.
  172. substitutePrefixVar()
  173. {
  174. case $1 in
  175. @$2@*) substitutePrefixVarResult=$3`safeEcho "$1" | sed -e "s/^@$2@//"`;;
  176. *) substitutePrefixVarResult=$1
  177. esac
  178. }
  179. fi
  180. ##
  181. ## Quote ${WLP_INSTALL_DIR} for eval.
  182. ##
  183. escapeForEval "${WLP_INSTALL_DIR}"
  184. WLP_INSTALL_DIR_QUOTED=${escapeForEvalResult}
  185. READ_ETC=1
  186. ##
  187. ## Consume script parameters:
  188. ## action is required/positional,
  189. ## clientName is optional, --options following
  190. ##
  191. if [ $# -lt 1 ]
  192. then
  193. ACTION=help:usage
  194. else
  195. ACTION=$1
  196. shift #consume
  197. if [ $# -ge 1 ]
  198. then
  199. # Only use if it isn't something that looks like an option
  200. case $1 in
  201. -*);;
  202. *)
  203. CLIENT_NAME=$1
  204. shift #consume
  205. esac
  206. fi
  207. fi
  208. ##
  209. ## Set client name and directory
  210. ##
  211. if [ -z "$CLIENT_NAME" ]
  212. then
  213. CLIENT_NAME=defaultClient
  214. fi
  215. ##
  216. ## Set JAVA_AGENT_QUOTED if WLP_SKIP_BOOTSTRAP_AGENT is unset.
  217. ##
  218. JAVA_AGENT_QUOTED=-javaagent:${WLP_INSTALL_DIR_QUOTED}/bin/tools/ws-javaagent.jar
  219. if [ -n "${WLP_SKIP_BOOTSTRAP_AGENT}" ]; then
  220. JAVA_AGENT_QUOTED=
  221. fi
  222. ##
  223. ## createClient: Function to launch client create
  224. ##
  225. createClient()
  226. {
  227. javaCmd "${CLIENT_NAME}" --create "$@"
  228. rc=$?
  229. return $rc
  230. }
  231. ##
  232. ## mkdirs: Create a directory and all parent directories, if necessary.
  233. ##
  234. mkdirs()
  235. {
  236. if [ ! -d "$1" ]; then
  237. mkdir -p "$1"
  238. fi
  239. }
  240. ##
  241. ## checkClient: Check for created/existing client. If it does not, issue an
  242. ## error message if the first argument is true, and then exit the
  243. ## script regardless.
  244. checkClient()
  245. {
  246. if [ -d "${CLIENT_CONFIG_DIR}" ] || [ "defaultClient" = "${CLIENT_NAME}" ]
  247. then
  248. return 0
  249. fi
  250. if $1
  251. then
  252. javaCmd --message:info.clientNotExist "${CLIENT_NAME}"
  253. rc=$?
  254. if [ $rc = 0 ]
  255. then
  256. rc=1
  257. fi
  258. else
  259. rc=1
  260. fi
  261. return $rc
  262. }
  263. ##
  264. ## installEnv: Set variables for a non-client or nonexistent client command.
  265. installEnv()
  266. {
  267. readClientEnv "${WLP_INSTALL_DIR}/etc/client.env"
  268. installEnvDefaults
  269. clientEnvDefaults
  270. }
  271. ##
  272. ## installEnvDefaults: Set variable defaults for a non-client or nonexistent
  273. ## client command.
  274. installEnvDefaults()
  275. {
  276. readClientEnv "${WLP_INSTALL_DIR}/java/java.env"
  277. readClientEnv "${WLP_INSTALL_DIR}/etc/default.env"
  278. if [ -z "${WLP_USER_DIR}" ]
  279. then
  280. if [ -z "${WLP_DEFAULT_USER_DIR}" ]
  281. then
  282. WLP_DEFAULT_USER_DIR=${WLP_INSTALL_DIR}/usr
  283. fi
  284. WLP_USER_DIR=${WLP_DEFAULT_USER_DIR}
  285. fi
  286. if [ -z "${WLP_CLIENT_OUTPUT_DIR}" ]
  287. then
  288. if [ -z "${WLP_DEFAULT_OUTPUT_DIR}" ]
  289. then
  290. WLP_DEFAULT_OUTPUT_DIR=${WLP_USER_DIR}/clients
  291. fi
  292. WLP_CLIENT_OUTPUT_DIR=${WLP_DEFAULT_OUTPUT_DIR}
  293. fi
  294. CLIENT_CONFIG_DIR=${WLP_USER_DIR}/clients/${CLIENT_NAME}
  295. export WLP_USER_DIR
  296. export WLP_CLIENT_OUTPUT_DIR
  297. }
  298. ##
  299. ## clientEnvDefaults: Set defaults for client variables.
  300. clientEnvDefaults()
  301. {
  302. CLIENT_OUTPUT_DIR=${WLP_CLIENT_OUTPUT_DIR}/${CLIENT_NAME}
  303. # Unset set/tested variables to prevent collisions with nested process invocations
  304. if [ -z "$LOG_DIR" ]
  305. then
  306. X_LOG_DIR=${CLIENT_OUTPUT_DIR}/logs
  307. else
  308. X_LOG_DIR=${LOG_DIR}
  309. unset LOG_DIR
  310. fi
  311. export X_LOG_DIR
  312. if [ -z "$LOG_FILE" ]
  313. then
  314. X_LOG_FILE=console.log
  315. else
  316. X_LOG_FILE=${LOG_FILE}
  317. unset LOG_FILE
  318. fi
  319. export X_LOG_FILE
  320. if [ -z "${JAVA_HOME}" ]
  321. then
  322. if [ -z "${JRE_HOME}" ]
  323. then
  324. if [ -z "${WLP_DEFAULT_JAVA_HOME}" ]
  325. then
  326. # Use whatever java is on the path
  327. JAVA_CMD=java
  328. else
  329. substitutePrefixVar "${WLP_DEFAULT_JAVA_HOME}" WLP_INSTALL_DIR "${WLP_INSTALL_DIR}"
  330. JAVA_HOME=${substitutePrefixVarResult}
  331. JAVA_CMD=${JAVA_HOME}/bin/java
  332. fi
  333. else
  334. JAVA_HOME=${JRE_HOME}
  335. JAVA_CMD=${JRE_HOME}/bin/java
  336. fi
  337. else
  338. if [ -f "${JAVA_HOME}/jre/bin/java" ]
  339. then
  340. JAVA_CMD=${JAVA_HOME}/jre/bin/java
  341. else
  342. JAVA_CMD=${JAVA_HOME}/bin/java
  343. fi
  344. fi
  345. # Set a default file encoding if needed
  346. if [ -n "$defaultFileEncoding" ]; then
  347. if ! expr "${JVM_OPTIONS_QUOTED} ${JVM_ARGS}" : '.*\(-Dfile\.encoding\=[^[:space:]]\)' > /dev/null; then
  348. JVM_ARGS="${JVM_ARGS} -Dfile.encoding=$defaultFileEncoding"
  349. fi
  350. fi
  351. clientUmask
  352. }
  353. ##
  354. ## clientEnv: Set variables for an existing client.
  355. clientEnv()
  356. {
  357. readClientEnv "${WLP_INSTALL_DIR}/etc/client.env"
  358. installEnvDefaults
  359. readClientEnv "${CLIENT_CONFIG_DIR}/client.env"
  360. clientEnvDefaults
  361. }
  362. ##
  363. ## clientEnvAndJVMOptions: Read client.env files and set environment variables.
  364. ## Read client.jvm.options file into ${JVM_OPTIONS_QUOTED}
  365. clientEnvAndJVMOptions()
  366. {
  367. clientEnv
  368. # Allow Headless mode for running AWT application.
  369. JVM_OPTIONS_QUOTED=-Djava.awt.headless=true
  370. rc=0
  371. mergeJVMOptions "${CLIENT_CONFIG_DIR}/client.jvm.options"
  372. if [ $rc -ne 0 ]; then
  373. return $rc
  374. fi
  375. # If ${CLIENT_CONFIG_DIR}/client.jvm.options did not exist, then
  376. # try to read ${WLP_INSTALL_DIR}/etc/client.jvm.options.
  377. if [ $READ_ETC -eq 1 ]; then
  378. mergeJVMOptions "${WLP_INSTALL_DIR}/etc/client.jvm.options"
  379. fi
  380. JPMS_MODULE_FILE_LOCATION="${JAVA_HOME}/lib/modules"
  381. if [ -z "${JAVA_HOME}" ]; then
  382. JPMS_MODULE_FILE_LOCATION=$(dirname $(dirname $(which java)))/lib/modules
  383. fi
  384. # If we are running on Java 9, apply Liberty's built-in java 9 options
  385. if [ -f "${JPMS_MODULE_FILE_LOCATION}" ]; then
  386. mergeJVMOptions "${WLP_INSTALL_DIR}/lib/platform/java/java9.options"
  387. fi
  388. return $rc
  389. }
  390. mergeJVMOptions()
  391. {
  392. jvmOptions=$1
  393. if [ -f "$jvmOptions" ]; then
  394. READ_ETC=0
  395. saveIFS=$IFS
  396. IFS=$newline
  397. for option in `readNativeFile "$jvmOptions" '[#-]' | tr -d '\r'`; do
  398. if [ -n "$option" ]; then
  399. case $option in
  400. \#*);;
  401. *)
  402. escapeForEval "${option}"
  403. JVM_OPTIONS_QUOTED="${JVM_OPTIONS_QUOTED} ${escapeForEvalResult}"
  404. ;;
  405. esac
  406. fi
  407. done
  408. IFS=$saveIFS
  409. fi
  410. }
  411. ##
  412. ## readClientEnv: Read client.env file and export environment variables.
  413. readClientEnv()
  414. {
  415. if [ -f "$1" ]; then
  416. saveIFS=$IFS
  417. IFS=$newline
  418. for line in `readNativeFile "$1" '[#_A-Za-z=]' | tr -d '\r'`; do
  419. case $line in
  420. \#*);;
  421. *=*)
  422. # Only accept alphanumeric variable names to avoid eval complexities.
  423. if var=`safeEcho "$line" | sed -e 's/^\([a-zA-Z0-9_][a-zA-Z0-9_]*\)=.*/\1/'`; then
  424. extractValueAndEscapeForEval "${line}"
  425. eval "${var}=${escapeForEvalResult}; export ${var}"
  426. fi
  427. esac
  428. done
  429. IFS=$saveIFS
  430. fi
  431. }
  432. ##
  433. ## Detects the code page of the file and converts to EBCDIC,
  434. ## if necessary, before cat'ing.
  435. ##
  436. ## Only applicable if running in a native EBCDIC environment (z/OS).
  437. ##
  438. ## $1 the file name
  439. ## $2 pattern denoting the expected first char of file
  440. readNativeFile() {
  441. if ${nativeEBCDIC}; then
  442. # ASCII 'm' overlaps with EBCDIC '_', so strip it out before detecting the codepage.
  443. # Note: cat used here to handle ASCII-tagged files.
  444. filecontents=`cat "$1" | iconv -f ISO8859-1 -t IBM-1047 | tr -d 'm\r\n'`
  445. case $filecontents in
  446. $2*) iconv -f ISO8859-1 -t IBM-1047 "$1" ;; # ASCII file.
  447. *) cat "$1" # EBCDIC file or ASCII-tagged file.
  448. esac
  449. else
  450. cat "$1"
  451. fi
  452. }
  453. ##
  454. ## clientWorkingDirectory: Change the working directory to ${CLIENT_OUTPUT_DIR}
  455. clientWorkingDirectory()
  456. {
  457. # Change the working directory to ${CLIENT_OUTPUT_DIR}.
  458. mkdirs "${CLIENT_OUTPUT_DIR}"
  459. cd "${CLIENT_OUTPUT_DIR}"
  460. }
  461. ## set the umask value to remove all permissions for the Other category
  462. ## but leave existing values for User and Group unaltered
  463. ##
  464. ## If the WLP_SKIP_UMASK variable is set then do not set umask
  465. clientUmask()
  466. {
  467. if [ -z "${WLP_SKIP_UMASK}" ]; then
  468. $UMASK_O
  469. fi
  470. }
  471. ##
  472. ## javaCmd: Execute a java-based command. Arguments are:
  473. ##
  474. ## "$@"
  475. ## All command-line arguments to be passed to the command
  476. ##
  477. ## The following variables must be set:
  478. ## ${JAVA_CMD} - the java command
  479. ## ${JAVA_DEBUG} - JVM options to enable debug, or empty
  480. ## ${JAVA_AGENT_QUOTED} - The -javaagent argument quoted for eval
  481. ## ${JVM_OPTIONS_QUOTED} - jvm.options quoted for eval
  482. ## ${WLP_INSTALL_DIR_QUOTED} - ${WLP_INSTALL_DIR} quoted for eval
  483. javaCmd()
  484. {
  485. # Set all the parameters for the java command. We use eval so that each line
  486. # in client.jvm.options is treated as a distinct argument.
  487. eval "set -- ${JAVA_AGENT_QUOTED} ${JVM_OPTIONS_QUOTED} ${JAVA_DEBUG} ${JVM_ARGS} -jar ${WLP_INSTALL_DIR_QUOTED}/bin/tools/ws-client.jar "'"$@"'
  488. ARGS="$@"
  489. # Do not add extra logic after the commands without preserving $?
  490. "${JAVA_CMD}" "$@"
  491. }
  492. ##
  493. ## clientCmd: Launch a client process.
  494. ##
  495. ##
  496. clientCmd()
  497. {
  498. X_CMD="${JAVA_CMD} ${ARGS}"
  499. export X_CMD
  500. javaCmd "${CLIENT_NAME}" "$@"
  501. rc=$?
  502. return $rc
  503. }
  504. JAVA_DEBUG=
  505. JVM_OPTIONS_QUOTED=
  506. INVOKED="$0"
  507. export INVOKED
  508. case "$ACTION" in
  509. # Start the client in the foreground
  510. run | debug)
  511. if clientEnvAndJVMOptions
  512. then
  513. :
  514. else
  515. exit $?
  516. fi
  517. if checkClient true
  518. then
  519. if [ "${ACTION}" = "debug" ]
  520. then
  521. if [ -z "${WLP_DEBUG_ADDRESS}" ]
  522. then
  523. WLP_DEBUG_ADDRESS=7778
  524. fi
  525. JAVA_DEBUG="-Dwas.debug.mode=true -Dcom.ibm.websphere.ras.inject.at.transform=true -Dsun.reflect.noInflation=true -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=${WLP_DEBUG_ADDRESS}"
  526. fi
  527. clientWorkingDirectory
  528. clientCmd "$@"
  529. exit $?
  530. else
  531. exit 2
  532. fi
  533. ;;
  534. create)
  535. installEnv
  536. createClient "$@"
  537. exit $rc
  538. ;;
  539. # Package up the runtime and target client configuration
  540. package)
  541. clientEnv
  542. # Check to see if the client exists
  543. if checkClient true
  544. then
  545. javaCmd "${CLIENT_NAME}" --package "$@"
  546. exit $?
  547. else
  548. exit 2
  549. fi
  550. ;;
  551. help)
  552. installEnv
  553. javaCmd --help "${CLIENT_NAME}"
  554. ;;
  555. help:usage)
  556. installEnv
  557. javaCmd --help:usage
  558. ;;
  559. *)
  560. installEnv
  561. javaCmd --help:actions:${ACTION}
  562. ;;
  563. esac
  564. exit 0