This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jvmw
executable file
·620 lines (620 loc) · 20.5 KB
/
jvmw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#!/usr/bin/env bash
# Build date: 20190204_202420
#
# Hack for code verification
JVM_VERSION_MAJOR=${JVM_VERSION_MAJOR}
JVMW_DEBUG=${JVMW_DEBUG}
JVMW_HOME=${JVMW_HOME}
JVM_VERSION=${JVM_VERSION}
JVM_VENDOR=${JVM_VENDOR}
ARCHIVE_FILE=${ARCHIVE_FILE}
ARCHIVE_JVM_CHECKSUM=${ARCHIVE_JVM_CHECKSUM}
ARCHIVE_JVM_URL=${ARCHIVE_JVM_URL}
JVM_PAGE_URL=${JVM_PAGE_URL}
CLEAR_COOKIE=${CLEAR_COOKIE}
ORACLE_USER=${ORACLE_USER}
ORACLE_PASSWORD=${ORACLE_PASSWORD}
JVMW_ORACLE_KEYCHAIN=${JVMW_ORACLE_KEYCHAIN}
LAST_UPDATE_FILE=${LAST_UPDATE_FILE}
REQUIRED_UPDATE=${REQUIRED_UPDATE}
#
export JVMW=true
#JVMW_HOME=${HOME}/.jvm
if [[ -z "${JVMW_HOME}" ]]; then
JVMW_HOME="${HOME}/.jvm"
fi
mkdir -p "${JVMW_HOME}"
#
OTN_USER_AGENT='Mozilla/5.0 https://github.com/itbasis/jvm-wrapper'
#
ARCHIVE_EXT_ZIP="zip"
ARCHIVE_EXT_TAR_GZ="tar.gz"
ARCHIVE_EXT_DMG="dmg"
#
JVMW_PROPERTY_FILE="${JVMW_PROPERTY_FILE:-./jvmw.properties}"
OS=$(uname | tr '[:upper:]' '[:lower:]')
ARCH=$([[ "$(uname -m)" == "x86_64" ]] && echo "x64" || echo "i586")
ARCHIVE_EXT=$([[ "${OS}" == "darwin" ]] && echo ${ARCHIVE_EXT_DMG} || echo ${ARCHIVE_EXT_TAR_GZ})
#
REQUIRED_COMMANDS_CORE='awk tr head rm mv cd curl readlink dirname'
REQUIRED_COMMANDS_DARWIN='hdiutil xar cpio shasum'
REQUIRED_COMMANDS_LINUX='sha256sum tar'
function log_2() {
if [[ -n "$1" ]]; then
printf "[%s] [%s" "$1" "${BASH_SOURCE[1]}"
for i in "${!FUNCNAME[@]}"; do
printf ":%s(%s)" "${FUNCNAME[$i]}" "${BASH_LINENO[$i]}"
done
printf "] "
fi
printf "%s\\n" "${*:2}"
}
function print_debug_info() {
if [[ "${JVMW_DEBUG}" == "Y" ]]; then
printf ''
for key in OS ARCH JVMW_HOME ARCHIVE_EXT JVM_VERSION JVM_VERSION_MAJOR JVM_VERSION_UPDATE JDK_ROOT_DIR JDK_HOME_DIR JAVA_HOME_DIR JVM_PAGE_URL ARCHIVE_JVM_URL ARCHIVE_JVM_CHECKSUM ARCHIVE_FILE LAST_UPDATE_FILE REQUIRED_UPDATE JVMW_DEBUG USE_SYSTEM_JVM JVM_VENDOR ORACLE_USER; do
>&2 log_2 '' "${key}=${!key}";
done
fi
}
function debug() {
if [[ "${JVMW_DEBUG}" == "Y" ]]; then
>&2 log_2 'DEBUG' "$*"
fi
}
function die() {
JVMW_DEBUG=Y
print_debug_info
>&2 log_2 'ERROR' "$*"
exit 1
}
function check_checksum() {
if [[ ${JVM_VERSION_MAJOR} -lt 8 ]]; then
# checksum was added only from version 8
return 0
fi
local local_jdk_checksum
if [[ "${OS}" == "darwin" ]]; then
local_jdk_checksum=$(shasum -a 256 "${ARCHIVE_FILE}" | cut -d' ' -f 1)
else
local_jdk_checksum=$(sha256sum "${ARCHIVE_FILE}" | cut -d' ' -f 1)
fi
if [[ "${local_jdk_checksum}" != "${ARCHIVE_JVM_CHECKSUM}" ]]; then
debug "checksum of archive does not match: local_jdk_checksum=${local_jdk_checksum}, ARCHIVE_JVM_CHECKSUM=${ARCHIVE_JVM_CHECKSUM}"
return 1
fi
return 0
}
function write_last_update() {
local -r now=$(date +"%F %R")
if [[ -z "${ARCHIVE_JVM_URL}" ]]; then
printf "%s\\n%s" "${now}" "$(tail -n2 "${LAST_UPDATE_FILE}")" >"${LAST_UPDATE_FILE}"
else
printf "%s\\n%s\\n%s" "${now}" "${ARCHIVE_JVM_URL}" "${ARCHIVE_JVM_CHECKSUM}" >"${LAST_UPDATE_FILE}"
fi
}
function whereis() {
for path in ${PATH//:/ }; do
if [[ -f "${path}/${1}" ]]; then
echo "${path}/${1}"
return 0
fi
done
}
function system_check_program_exists() {
# shellcheck disable=SC2068
for cmd in $@; do
if [[ "$("$cmd" --version 2>&1)" == *"command not found"* ]]; then
die "command not found: ${cmd}"
fi
done
}
function properties_parser() {
while IFS='=' read -r key value || [[ -n "$key" ]]; do
[[ -n "${key}" ]] && [[ -z "${!key}" ]] && { debug "${key}='${value}'"; eval "${key}='${value}'"; }
done <<<"$1"
}
function properties_default() {
cat << EOF
JVM_VERSION=11
REQUIRED_UPDATE=Y
CLEAR_COOKIE=Y
JVMW_DEBUG=N
USE_SYSTEM_JVM=N
JVM_VENDOR=openjdk
JVMW_ORACLE_KEYCHAIN=JVM_WRAPPER_ORACLE
EOF
}
function properties_build() {
JVM_VERSION_MAJOR="${JVM_VERSION%_*}"
JVM_VERSION_MAJOR="${JVM_VERSION_MAJOR#1.*}"
JVM_VERSION_MAJOR="${JVM_VERSION_MAJOR%%.*}"
JVM_VERSION_MAJOR="${JVM_VERSION_MAJOR%%u*}"
if [[ "${JVM_VERSION}" != "${JVM_VERSION_MAJOR}" ]]; then
JVM_VERSION_UPDATE="${JVM_VERSION##*.}"
JVM_VERSION_UPDATE="${JVM_VERSION_UPDATE##*_}"
JVM_VERSION_UPDATE="${JVM_VERSION_UPDATE##*u}"
JVM_VERSION_UPDATE="${JVM_VERSION_UPDATE%%-b*}"
else
unset JVM_VERSION_UPDATE
fi
JVM_FULL_NAME="${JVM_VENDOR}-jdk-"
if [[ ${JVM_VERSION_MAJOR} -lt 9 ]]; then
JVM_FULL_NAME="${JVM_FULL_NAME}${JVM_VERSION_MAJOR}"
if [[ -n "${JVM_VERSION_UPDATE}" ]]; then
JVM_FULL_NAME="${JVM_FULL_NAME}u${JVM_VERSION_UPDATE}"
fi
else
JVM_FULL_NAME="${JVM_FULL_NAME}${JVM_VERSION}"
fi
JDK_ROOT_DIR=${JVMW_HOME}/${JVM_FULL_NAME}
ARCHIVE_FILE=${JDK_ROOT_DIR}.${ARCHIVE_EXT}
LAST_UPDATE_FILE=${JDK_ROOT_DIR}.last_update
JDK_HOME_DIR=${JDK_ROOT_DIR}/$([[ "${OS}" == 'darwin' ]] && echo 'Home/')
}
function download_jdk() {
eval "${JVM_VENDOR}_check_the_need_for_downloading"
debug "REQUIRED_UPDATE=${REQUIRED_UPDATE}"
if [[ "${REQUIRED_UPDATE}" != "Y" ]]; then
return 0
fi
if [[ -z "${ARCHIVE_JVM_URL}" ]]; then
die "empty ARCHIVE_JVM_URL. Use page '${JVM_PAGE_URL}'"
fi
#
if [[ -f "${LAST_UPDATE_FILE}" ]] && [[ "${ARCHIVE_JVM_URL}" == "$(head -2 <"${LAST_UPDATE_FILE}" | tail -1)" ]]; then
return 0
fi
#
if [[ -f "${ARCHIVE_FILE}" ]]; then
if [[ ${JVM_VERSION_MAJOR} -lt 8 ]]; then
# TODO
rm -f "${ARCHIVE_FILE}"
elif check_checksum; then
unpack
return 0
fi
fi
#
if [[ -d "${JDK_HOME_DIR}" ]]; then
if [[ "$("${JVM_HOME_DIR}/bin/java" -fullversion 2>&1)" == *"${JVM_VERSION}"* ]]; then
write_last_update
return 0
fi
fi
#
eval "${JVM_VENDOR}_download_jdk"
unpack
write_last_update
}
function unpack() {
if [[ "${ARCHIVE_EXT}" == "${ARCHIVE_EXT_TAR_GZ}" ]]; then
unpack_tar_gz
elif [[ "${ARCHIVE_EXT}" == "${ARCHIVE_EXT_DMG}" ]]; then
unpack_dmg
elif [[ "${ARCHIVE_EXT}" == "${ARCHIVE_EXT_ZIP}" ]]; then
unpack_zip
else
eval "${JVM_VENDOR}_unpack_${OS}"
fi
rm -f "${ARCHIVE_FILE}"
}
function unpack_tar_gz() {
local sub_dir
local -r tmp_dir=$(mktemp -d -t "${JVM_FULL_NAME}.XXXXXX")/
debug "tmp_dir=${tmp_dir}"
tar xf "${ARCHIVE_FILE}" -C "${tmp_dir}" || { rm -Rf "${tmp_dir}";
die 'error unpack archive...'; }
sub_dir=$(find "${tmp_dir}" -mindepth 1 -maxdepth 1 -type d)
mv "${sub_dir}" "${JDK_ROOT_DIR}" || { rm -Rf "${tmp_dir}";
die 'error move unpacked jdk...'; }
rm -Rf "${tmp_dir}"
if [[ -d "${JDK_ROOT_DIR}/Contents" ]]; then
mv "${JDK_ROOT_DIR}"/Contents/* "${JDK_ROOT_DIR}/"
rm -Rf "${JDK_ROOT_DIR}"/Contents/
fi
}
function unpack_dmg() {
hdiutil detach "/Volumes/${JVM_FULL_NAME}" -quiet -force 2> /dev/null
hdiutil attach "${ARCHIVE_FILE}" -mountpoint "/Volumes/${JVM_FULL_NAME}" 2>&1 || die
local -r PKG_FILE_NAME=$(find "/Volumes/${JVM_FULL_NAME}" -mindepth 1 -maxdepth 1 -name '*.pkg')
local -r tmp_dir=$(mktemp -d -t "${JVM_FULL_NAME}.XXXXXX")/
cd "${tmp_dir}/" || { rm -Rf "${tmp_dir}";
die "can't change directory: ${tmp_dir}/"; }
xar -xf "${PKG_FILE_NAME}" . &> /dev/null || die
for dir in ./*jdk*; do
cpio -i <"${dir}/Payload" || exit 1
done
rm -Rf "${JDK_ROOT_DIR}"
mv Contents "${JDK_ROOT_DIR}" 1> /dev/null 2>&1 || die
cd - || die
rm -Rf "${tmp_dir}"
#
hdiutil detach "/Volumes/${JVM_FULL_NAME}" -force || die
}
function unpack_zip() {
system_check_program_exists "unzip"
local sub_dir
local -r tmp_dir=$(mktemp -d -t "${JVM_FULL_NAME}.XXXXXX")
debug "tmp_dir=${tmp_dir}"
unzip "${ARCHIVE_FILE}" -d "${tmp_dir}" > /dev/null || { rm -Rf "${tmp_dir}";
die 'error unpack archive...'; }
sub_dir=$(find "${tmp_dir}" -mindepth 1 -maxdepth 1 -type d)
sub_dir=$(find "${sub_dir}" -mindepth 1 -maxdepth 1 -type d)
debug "sub_dir=${sub_dir}"
mv "${sub_dir}" "${JDK_ROOT_DIR}" || { rm -Rf "${tmp_dir}";
die 'error move unpacked jdk...'; }
rm -Rf "${tmp_dir}"
}
# Hack for code verification
OTN_HEADERS_FILE=${OTN_HEADERS_FILE}
OTN_FORM_ACTION=${OTN_FORM_ACTION}
OTN_COOKIE_FILE=${OTN_COOKIE_FILE}
OTN_FORM_DATA=${OTN_FORM_DATA}
OTN_CONTENT_FILE=${OTN_CONTENT_FILE}
OTN_HOST_LOGIN=${OTN_HOST_LOGIN}
#
function oracle_prepare_actions() {
return 0
}
function oracle_otn_curl_send_request() {
local -r url="$1"
debug "url='${url}'"
local -r CURL_OPTIONS="${*:2}"
# shellcheck disable=SC2086
curl -kLs -A "${OTN_USER_AGENT}" -D "${OTN_HEADERS_FILE}" -b "${OTN_COOKIE_FILE}" -c "${OTN_COOKIE_FILE}" ${CURL_OPTIONS} "${url}" >"${OTN_CONTENT_FILE}"
}
function oracle_otn_curl_post() {
oracle_otn_form_parser
oracle_otn_curl_send_request "${OTN_FORM_ACTION}" -X POST -d "${OTN_FORM_DATA}"
}
function ont_form_data_build() {
IFS='>'
local form_data=
for fld in $(echo "$1" | awk '{ if (match($0, "<input")) print }'); do
fld_name=$(echo "${fld}" | awk 'match($0, /name="([^"]+)/) { print substr($0, RSTART+6, RLENGTH-6) }')
fld_value=$(echo "${fld}" | awk 'match($0, /value="([^"]+)/) { print substr($0, RSTART+7, RLENGTH-7) }')
if [[ -n "${fld_name}" ]]; then
if [[ "${fld_name}" == "userid" ]]; then
fld_value=${ORACLE_USER}
elif [[ "${fld_name}" == "pass" ]]; then
fld_value=${ORACLE_PASSWORD}
fi
form_data="${form_data}&${fld_name}=${fld_value}"
fi
done
echo "${form_data}"
}
function oracle_otn_form_clean_env() {
unset oracle_otn_FORM_DATA oracle_otn_FORM_ACTION
}
function oracle_otn_form_parser() {
oracle_otn_form_clean_env
OTN_FORM_DATA=$(ont_form_data_build "$(cat "${OTN_CONTENT_FILE}")")
OTN_FORM_ACTION=$(awk 'match($0, /action="([^"]+)/) { print substr($0, RSTART+8, RLENGTH-8) }' <"${OTN_CONTENT_FILE}")
if [[ "${OTN_FORM_ACTION:0:1}" == "/" ]]; then
OTN_FORM_ACTION="${OTN_HOST_LOGIN}${OTN_FORM_ACTION}"
fi
}
function oracle_otn_curl_redirect() {
# shellcheck disable=SC2155
local host=$(awk '{ if (match($0, "http-equiv=\"refresh\".*")) print substr($0, RSTART, RLENGTH) }' <"${OTN_CONTENT_FILE}" | awk -F';' '{ if (match($0, "URL=[^\"]+")) print substr($0, RSTART+4, RLENGTH-4)}')
if [[ "${host:0:1}" == "/" ]]; then
host="${OTN_HOST_LOGIN}${host}"
fi
oracle_otn_curl_send_request "${host}"
}
function oracle_otn_login() {
OTN_COOKIE_FILE="${TMPDIR}/jvmwrapper.oracle_otn.cookie"
if [[ "${CLEAR_COOKIE}" == "Y" ]]; then
rm -f "${OTN_COOKIE_FILE}";
fi
if [[ -f "${OTN_COOKIE_FILE}" ]]; then
return 0
fi
if [[ -z "${ORACLE_USER}" ]] || [[ -z "${ORACLE_PASSWORD}" ]]; then
case "${OS}" in
darwin)
if [[ -n "${JVMW_ORACLE_KEYCHAIN}" ]]; then
ORACLE_USER="$(security 2>&1 find-generic-password -l "${JVMW_ORACLE_KEYCHAIN}" | awk 'match($0, /\"acct\"<blob>=\"([^\"]+)/) { print substr($0, RSTART+14, RLENGTH-14) }')"
fi
if [[ -n "${ORACLE_USER}" ]]; then
ORACLE_PASSWORD="$(security 2>&1 find-generic-password -g -a "${ORACLE_USER}" | awk 'match($0, /password: \"([^\"]+)/) { print substr($0, RSTART+11, RLENGTH-11) }')"
fi
;;
esac
fi
if [[ -z "${ORACLE_USER}" ]] || [[ -z "${ORACLE_PASSWORD}" ]]; then
die 'no values were found in ORACLE_USER and ORACLE_PASSWORD'
fi
debug "login OTN as '${ORACLE_USER}'"
OTN_HEADERS_FILE=$(mktemp -t oracle_otn_HEADERS_XXXXX.tmp)
OTN_CONTENT_FILE=$(mktemp -t oracle_otn_CONTENT_XXXXX.tmp)
OTN_URL_INDEX=https://www.oracle.com/index.html
OTN_HOST_LOGIN=https://login.oracle.com
OTN_URL_SIGNON="https://www.oracle.com/webapps/redirect/signon?nexturl=${OTN_URL_INDEX}"
oracle_otn_curl_send_request "${OTN_URL_SIGNON}"
oracle_otn_curl_post
oracle_otn_curl_post
oracle_otn_curl_redirect
oracle_otn_curl_post
oracle_otn_curl_send_request "${OTN_URL_SIGNON}"
rm -f "${OTN_HEADERS_FILE}" "${OTN_CONTENT_FILE}"
}
function oracle_download_jdk() {
if [[ "${ARCHIVE_JVM_URL}" != *'/otn-pub/'* ]]; then
oracle_otn_login
fi
debug "download ${ARCHIVE_JVM_URL}..."
curl -kL -A "${OTN_USER_AGENT}" -b "${OTN_COOKIE_FILE}" -o "${ARCHIVE_FILE}" --cookie "oraclelicense=accept-securebackup-cookie" "${ARCHIVE_JVM_URL}"
rm -f "${OTN_HEADERS_FILE}" "${OTN_CONTENT_FILE}"
if [[ "${CLEAR_COOKIE}" == "Y" ]]; then
rm -f "${OTN_COOKIE_FILE}";
fi
if [[ "${JVMW_DEBUG}" == "Y" ]]; then
ls -laFh "${ARCHIVE_FILE}"
fi
#
check_checksum
local status=$?
if [[ ${status} -ne 0 ]]; then
die
fi
}
function oracle_check_the_need_for_downloading() {
if [[ "${REQUIRED_UPDATE}" != "Y" ]]; then
return 0
fi
if [[ ! -f "${JVM_HOME_DIR}/bin/java" ]]; then
REQUIRED_UPDATE=Y
else
check_is_out_of_date
fi
if [[ "${REQUIRED_UPDATE}" != "Y" ]]; then
debug "not required update"
return 0
fi
oracle_otn_page_latest_version_jdk_page_parser
if [[ ${ARCHIVE_JVM_URL} == *"${JVM_VERSION}"* ]] && [[ "${REQUIRED_UPDATE}" != "Y" ]]; then
write_last_update
return 0
fi
}
function oracle_otn_page_latest_version_jdk_page_parser() {
JVM_PAGE_URL=https://www.oracle.com$(curl -s https://www.oracle.com/technetwork/java/javase/downloads/index.html | awk 'match($0, /(\/technetwork\/java\/javase\/downloads\/jdk'"${JVM_VERSION_MAJOR}"'-downloads-[^\"]+)/) { print substr($0, RSTART, RLENGTH) }' | head -1)
if [[ "${JVM_PAGE_URL}" != "https://www.oracle.com" ]]; then
oracle_otn_page_archive_jdk_parser
if [[ -n "${ARCHIVE_JVM_URL}" ]]; then
return 0
fi
fi
local -r url_archive=https://www.oracle.com$(curl -sS https://www.oracle.com/technetwork/java/javase/downloads/index.html | awk 'match($0, /(\/technetwork\/java\/javase\/archive-[^\"]+)/) { print substr($0, RSTART, RLENGTH) }' | head -1)
JVM_PAGE_URL=https://www.oracle.com$(curl -sS "${url_archive}" | awk 'match($0, /(\/technetwork\/java\/javase\/downloads\/java-archive-[^'"${JVM_VERSION_MAJOR}"']+'"${JVM_VERSION_MAJOR}"'-[^\"]+)/) { print substr($0, RSTART, RLENGTH) }' 2>&1 | head -1)
oracle_otn_page_archive_jdk_parser
}
function oracle_otn_page_archive_jdk_parser() {
debug "JVM_PAGE_URL='${JVM_PAGE_URL}'"
local -r content=$(curl -s "${JVM_PAGE_URL}")
local awk_mask
if [[ ${JVM_VERSION_MAJOR} -lt 9 ]]; then
awk_mask='^downloads.*?jdk-'${JVM_VERSION_MAJOR}'u'${JVM_VERSION_UPDATE}'.*?-'${ARCH}'.*?.'${ARCHIVE_EXT}
else
awk_mask='^downloads.*?jdk-'${JVM_VERSION}'.*?-'${ARCH}'.*?.'${ARCHIVE_EXT}
fi
debug "awk_mask=${awk_mask}"
# shellcheck disable=SC2034
local -r separator='":"|", "|"}'
# shellcheck disable=SC2086
local -r row=$(echo "${content}" | awk '{ if (match($0, "'${awk_mask}'")) print }')
IFS=',"' read -ra fields <<<"${row}"
for i in "${!fields[@]}"; do
case "${fields[$i]}" in
filepath)
ARCHIVE_JVM_URL="${fields[$i+2]}"
;;
SHA256)
ARCHIVE_JVM_CHECKSUM="${fields[$i+2]}"
;;
esac
done
debug "ARCHIVE_JVM_URL='${ARCHIVE_JVM_URL}'"
}
function openjdk_prepare_actions() {
if [[ "${OS}" == "darwin" ]] && [[ ${JVM_VERSION_MAJOR} -ge 9 ]]; then
ARCHIVE_EXT=${ARCHIVE_EXT_TAR_GZ}
ARCHIVE_FILE=${JDK_ROOT_DIR}.${ARCHIVE_EXT}
fi
}
RI_VERSION_MAX=10
function openjdk_check_the_need_for_downloading() {
if [[ "${REQUIRED_UPDATE}" != "Y" ]]; then
return 0
fi
if [[ ! -f "${JVM_HOME_DIR}/bin/java" ]]; then
REQUIRED_UPDATE=Y
else
check_is_out_of_date
fi
if [[ "${REQUIRED_UPDATE}" != "Y" ]]; then
debug "not required update"
return 0
fi
openjdk_latest_version_page_parser
if [[ ${ARCHIVE_JVM_URL} == *"${JVM_VERSION}"* ]] && [[ "${REQUIRED_UPDATE}" != "Y" ]]; then
write_last_update
return 0
fi
}
function openjdk_latest_version_page_parser() {
if [[ ${JVM_VERSION_MAJOR} -gt ${RI_VERSION_MAX} ]]; then
JVM_PAGE_URL="https://jdk.java.net/${JVM_VERSION_MAJOR}/"
else
JVM_PAGE_URL="https://jdk.java.net/java-se-ri/${JVM_VERSION_MAJOR}"
fi
openjdk_archive_parser
}
function openjdk_archive_parser() {
debug "JVM_PAGE_URL='${JVM_PAGE_URL}'"
local os=${OS}
if [[ "${os}" == "darwin" ]]; then
os=osx
fi
local arch=${ARCH}
if [[ "${OS}" == "darwin" ]] && [[ ${JVM_VERSION_MAJOR} -le 8 ]]; then
arch="x86_64"
fi
local -r content=$(curl -s "${JVM_PAGE_URL}")
local awk_mask
if [[ ${JVM_VERSION_MAJOR} -gt ${RI_VERSION_MAX} ]]; then
awk_mask='https://download.java.net/java/.*?jdk-'${JVM_VERSION_MAJOR}'.*?'${os}'.*?-'${arch}'.*?.'${ARCHIVE_EXT}''
else
if [[ ${JVM_VERSION_MAJOR} -eq '9' ]]; then
ARCHIVE_EXT=${ARCHIVE_EXT_ZIP}
fi
awk_mask='https://download.java.net/openjdk/jdk'${JVM_VERSION_MAJOR}'.*?/ri/.*?'${os}'.*?.'${ARCHIVE_EXT}''
fi
debug "awk_mask=${awk_mask}"
# shellcheck disable=SC2090,SC2086
ARCHIVE_JVM_URL=$(echo "${content}" | awk '{ if (match($0, "\"'${awk_mask}'\"")) print substr($0,RSTART+1, RLENGTH-2)}' | head -n 1)
debug "ARCHIVE_JVM_URL.. = ${ARCHIVE_JVM_URL}"
if [[ ${JVM_VERSION_MAJOR} -ge 9 ]]; then
# shellcheck disable=SC2086
local -r content_url=$(echo "${content}" | awk '{ if (match($0, "\"'${awk_mask}'.sha256\"")) print substr($0,RSTART+1, RLENGTH-2)}')
ARCHIVE_JVM_CHECKSUM=$(curl -s "${content_url}")
fi
}
function openjdk_download_jdk() {
debug "download ${ARCHIVE_JVM_URL}..."
curl -kL -A "${OTN_USER_AGENT}" -o "${ARCHIVE_FILE}" "${ARCHIVE_JVM_URL}"
if [[ "${JVMW_DEBUG}" == "Y" ]]; then
ls -laFh "${ARCHIVE_FILE}"
fi
# if [[ ! `check_checksum` ]]; then
# die
# fi
}
function main_properties() {
[[ -f "${JVMW_HOME}/${JVMW_PROPERTY_FILE}" ]] && { properties_parser "$(cat "${JVMW_HOME}/${JVMW_PROPERTY_FILE}")"; }
[[ -f "${JVMW_PROPERTY_FILE}" ]] && { properties_parser "$(cat "${JVMW_PROPERTY_FILE}")"; }
properties_parser "$(properties_default)"
properties_build
}
function check_is_out_of_date() {
REQUIRED_UPDATE=N
if [[ ! -f "${LAST_UPDATE_FILE}" ]]; then
debug "not found file: '${LAST_UPDATE_FILE}'"
REQUIRED_UPDATE=Y
else
local -r luf_date="$(head -1 <"${LAST_UPDATE_FILE}")"
# shellcheck disable=SC2005
local -r prev_date=$([[ "${OS}" == "darwin" ]] && echo "$(date -jf '%F %R' "${luf_date}" +%j)" || echo "$(date --date="${luf_date}" '+%j')")
local -r curr_date=$(date +%j)
debug "prev_date='${prev_date}', curr_date='${curr_date}'"
if [[ ${curr_date} > ${prev_date} ]]; then
REQUIRED_UPDATE=Y
fi
fi
return 0
}
function detect_system_jdk() {
local -r cmd="javac"
cmd_path=$(whereis "${cmd}")
if [[ -z "${cmd_path}" ]]; then
USE_SYSTEM_JVM=N
return 0
fi
ls_output="$(ls -l "${cmd_path}")"
while [[ "${ls_output}" == *"${cmd_path} "* ]]; do
cmd_path="$(readlink "${cmd_path}")"
ls_output="$(ls -l "${cmd_path}")"
done
jvm_dir=$(dirname "${cmd_path}")
if [[ -f "${jvm_dir}/java_home" ]]; then
jvm_dir=$("${jvm_dir}/java_home" 2>&1)
fi
if [[ -z "${jvm_dir}" ]] || [[ "${jvm_dir}" == *"Unable to find any JVMs matching version"* ]]; then
USE_SYSTEM_JVM=N
return 0
fi
local -r jdk_output="$(java -XshowSettings:properties -version 2>&1)"
if [[ "${jdk_output}" == *"command not found"* ]]; then
USE_SYSTEM_JVM=N
return 0
fi
if [[ "${jdk_output}" != *"java.runtime.version = ${JVM_VERSION_MAJOR}."* ]] && [[ "${jdk_output}" != *"java.runtime.version = 1.${JVM_VERSION_MAJOR}."* ]] && [[ "${jdk_output}" != *"java.runtime.version = ${JVM_VERSION_MAJOR}+"* ]]; then
USE_SYSTEM_JVM=N
return 0
fi
USE_SYSTEM_JVM=Y
eval "JDK_HOME_DIR='${jvm_dir}/'"
return 0
}
function detect_reuse_jdk() {
if [[ -z "${JVM_VERSION_UPDATE}" ]]; then
return 0;
fi
local -r jvm_root_dir=${JVMW_HOME}/${JVM_VENDOR}-jdk-${JVM_VERSION_MAJOR}/$([[ "${OS}" == 'darwin' ]] && echo 'Home/')
if [[ ! -d "${jvm_root_dir}" ]]; then
return 0;
fi
local -r jdk_output="$("${jvm_root_dir}/bin/java" -fullversion 2>&1)"
if [[ "${jdk_output}" != *"java full version \"${JVM_VERSION_MAJOR}.0.${JVM_VERSION_UPDATE}+"* ]] && [[ "${jdk_output}" != *"java full version \"1.${JVM_VERSION_MAJOR}.0_${JVM_VERSION_UPDATE}-"* ]] && [[ "${jdk_output}" != *"java full version \"${JVM_VERSION_MAJOR}+"* ]]; then
return 0
fi
JVM_FULL_NAME="${JVM_VENDOR}-jdk-${JVM_VERSION_MAJOR}"
JDK_ROOT_DIR=${JVMW_HOME}/${JVM_FULL_NAME}
JDK_HOME_DIR=${JDK_ROOT_DIR}/$([[ "${OS}" == 'darwin' ]] && echo 'Home/')
REQUIRED_UPDATE=N
return 0
}
if [[ "${OS}" == "darwin" ]]; then
system_check_program_exists "${REQUIRED_COMMANDS_CORE}" "${REQUIRED_COMMANDS_DARWIN}"
else
system_check_program_exists "${REQUIRED_COMMANDS_CORE}" "${REQUIRED_COMMANDS_LINUX}"
fi
if [[ "$1" == "upgrade" ]]; then
curl -sS https://raw.githubusercontent.com/itbasis/jvm-wrapper/master/jvmw >"$0" && chmod u+x "$0"
exit 0
fi
main_properties
detect_reuse_jdk
if [[ "${USE_SYSTEM_JVM}" == "Y" ]]; then
detect_system_jdk
fi
if [[ "${USE_SYSTEM_JVM}" -eq "Y" ]]; then
JVM_HOME_DIR="${JDK_HOME_DIR}"
fi
export JDK_HOME=${JDK_HOME_DIR%%/bin/*}
export JAVA_HOME=${JVM_HOME_DIR%%/bin/*}
if [[ -z "$1" ]] || [[ "$1" == "info" ]]; then
print_debug_info
echo "JDK_HOME=${JDK_HOME}"
echo "JAVA_HOME=${JAVA_HOME}"
else
eval "${JVM_VENDOR}_prepare_actions"
download_jdk
if [[ -z "${JVM_HOME_DIR}" ]]; then
die "can't JVM_HOME_DIR"
fi
print_debug_info
if [[ "$1" == "javac" ]] && [[ "${USE_SYSTEM_JVM}" != "Y" ]]; then
PATH=.:${JDK_HOME}/bin:$PATH
EXEC_PATH=${JDK_HOME}/bin/$1
elif [[ "$1" == "java" ]] && [[ "${USE_SYSTEM_JVM}" != "Y" ]]; then
PATH=.:${JAVA_HOME}/bin:$PATH
EXEC_PATH=${JDK_HOME}/bin/$1
else
PATH=.:$PATH
if [[ "${1:0:2}" == "./" ]]; then
EXEC_PATH="$1"
else
EXEC_PATH=$(whereis "$1")
fi
fi
if [[ -z "${EXEC_PATH}" ]]; then
die "No program found to execute: $1"
fi
# execute program
debug "command: ${EXEC_PATH}" "${@:2}"
eval "${EXEC_PATH}" "${@:2}"
fi