diff options
Diffstat (limited to 'libre/java-common')
-rw-r--r-- | libre/java-common/PKGBUILD | 42 | ||||
-rw-r--r-- | libre/java-common/bin_java-common-wrapper | 2 | ||||
-rw-r--r-- | libre/java-common/bin_parabola-java | 199 | ||||
-rw-r--r-- | libre/java-common/install_java-common.sh | 47 | ||||
-rw-r--r-- | libre/java-common/profile_jre.csh | 6 | ||||
-rw-r--r-- | libre/java-common/profile_jre.sh | 6 | ||||
-rw-r--r-- | libre/java-common/test_parabola-java | 305 |
7 files changed, 607 insertions, 0 deletions
diff --git a/libre/java-common/PKGBUILD b/libre/java-common/PKGBUILD new file mode 100644 index 000000000..8d29576e8 --- /dev/null +++ b/libre/java-common/PKGBUILD @@ -0,0 +1,42 @@ +# Maintainer (Arch): Guillaume ALAUX <guillaume@archlinux.org> +# Maintainer: André Silva <emulatorman@parabola.nu> +pkgname=java-common +pkgver=1 +pkgrel=7.parabola1 +pkgdesc='Common files for Java Environments (Parabola rebranded)' +arch=('any') +url='parabolagnulinux.org' +license=('GPL') +checkdepends=('wget') +backup=(etc/profile.d/jre.sh + etc/profile.d/jre.csh) +# TODO remove after a while +replaces=('java-runtime-headless-meta' 'java-runtime-meta' 'java-environment-meta') +install=install_java-common.sh +source=(profile_jre.csh + profile_jre.sh + bin_parabola-java + test_parabola-java + bin_java-common-wrapper + https://shunit2.googlecode.com/files/shunit2-2.1.6.tgz) + +sha256sums=('f3b7eb6ffa8bbc454250bfc3723f3fbcd1e19e314832524d0dbe5244752d3883' + '230214ef0d8cf8939abdcf0de01b763c76e9e711c46299b028c2c3c40930565e' + '284d7d18c4847485eaec693645afb2c0ad4bb7a4d0d42315a78b9ee3bcadb9a8' + 'e27e85eecbd8c8bd63738c1c332397d44cf44416d2ae9203945af1fc2a51dfed' + '16a4fde19bd34210ff2d06480085f681b4e41e85ec89853e3cafaa8377bcedfb' + '65a313a76fd5cc1c58c9e19fbc80fc0e418a4cbfbd46d54b35ed5b6e0025d4ee') + +check() { + cd "${srcdir}" + ./test_parabola-java +} + +build() { + cd "${srcdir}" + + install -D -m 644 profile_jre.sh "${pkgdir}/etc/profile.d/jre.sh" + install -D -m 644 profile_jre.csh "${pkgdir}/etc/profile.d/jre.csh" + install -D -m 755 bin_java-common-wrapper "${pkgdir}/usr/lib/java-common-wrapper" + install -D -m 755 bin_parabola-java "${pkgdir}/usr/bin/parabola-java" +} diff --git a/libre/java-common/bin_java-common-wrapper b/libre/java-common/bin_java-common-wrapper new file mode 100644 index 000000000..19d9edaa2 --- /dev/null +++ b/libre/java-common/bin_java-common-wrapper @@ -0,0 +1,2 @@ +#!/bin/bash +exec "${JAVA_HOME:-/usr/lib/jvm/default}/bin/${0##*/}" "$@" diff --git a/libre/java-common/bin_parabola-java b/libre/java-common/bin_parabola-java new file mode 100644 index 000000000..e9bcbb25f --- /dev/null +++ b/libre/java-common/bin_parabola-java @@ -0,0 +1,199 @@ +#! /bin/bash + +# Parabola GNU/linux-libre helper script to set/unset/check/fix the enabled Java environment +# This program may be freely redistributed under the terms of the GNU General Public License +# +# Author: Guillaume ALAUX <guillaume@archlinux.org> +# Parabola Rebranding Author: André Silva <emulatorman@parabola.nu> + +JVM_DIR=/usr/lib/jvm +DEFAULT_NAME=default +DEFAULT_PATH=${JVM_DIR}/${DEFAULT_NAME} +DEFAULT_NAME_JRE=default-runtime +DEFAULT_PATH_JRE=${JVM_DIR}/${DEFAULT_NAME_JRE} +BIN_PATH=/usr/bin +WRAPPER_PATH=/usr/lib/java-common-wrapper + + + +# Utility functions + +check_root() { + if [ $(id -u) -ne 0 ]; then + echo 'This script must be run as root' + exit 1 + fi +} + +# $1: parameter count given to this script for this option +# $2: expected parameter count for this option +check_param_count() { + if [ $1 -ne $2 ]; then + echo 'Wrong parameter count' + exit 2 + fi +} + +# Second level functions + +get_default_java() { + path=$(readlink -e ${DEFAULT_PATH}) + echo ${path/${JVM_DIR}\/} +} + +get_installed_javas() { + if [ -d ${JVM_DIR} ]; then + for dir in $(find ${JVM_DIR} -mindepth 1 -maxdepth 1 -type d | sort); do + if [ -x ${dir}/bin/javac ]; then + javas+=(${dir/${JVM_DIR}\/}) + else + if [ -x ${dir}/jre/bin/java ]; then + javas+=(${dir/${JVM_DIR}\/}/jre) + fi + fi + done + fi + echo ${javas[@]} +} + +# $1: Java environment name to test +is_java_valid() { + test "x$1" != "x${DEFAULT_NAME}" && test -x ${JVM_DIR}/$1/bin/java +} + +set_bin_links() { + find ${DEFAULT_PATH}/bin ! -type d -perm /a=x \ + | while read bpath; do + ln -sf ${WRAPPER_PATH} ${BIN_PATH}/$(basename ${bpath}) > /dev/null 2>&1 + done +} + +unset_bin_links() { + find -L ${BIN_PATH} -samefile ${WRAPPER_PATH} \ + | while read lpath; do + unlink ${lpath} + done +} + +# $1: Java environment name to set as default +set_default_link_to() { + new_default=$1 + unlink ${DEFAULT_PATH} 2>/dev/null + ln -sf ${new_default} ${DEFAULT_PATH} + + unlink ${DEFAULT_PATH_JRE} 2>/dev/null + if [[ ${new_default} == */jre ]]; then + ln -sf ${new_default} ${DEFAULT_PATH_JRE} + else + ln -sf ${new_default}/jre ${DEFAULT_PATH_JRE} + fi +} + +unset_default_link() { + unlink ${DEFAULT_PATH} 2>/dev/null + unlink ${DEFAULT_PATH_JRE} 2>/dev/null +} + +# First level functions + +do_status() { + installed_java=($(get_installed_javas)) + if [ ${#installed_java[@]} -eq 0 ]; then + echo 'No compatible Java environment installed' + else + default_java=$(get_default_java) + echo 'Available Java environments:' + for java in ${installed_java[@]}; do + if [ "${java}/jre" = "${default_java}" ]; then + echo -e " ${java} (${java}/jre default)" + elif [ ${java} = "${default_java}" ]; then + echo -e " ${java} (default)" + else + echo " ${java}" + fi + done + if [ -z ${default_java} ]; then + echo "No Java environment set as default" + fi + fi +} + +do_get() { + get_default_java +} + +# $1: Java environment name to set as default +do_set() { + if ! is_java_valid $1; then + echo "'${JVM_DIR}/$1' is not a valid Java environment path" + exit 1 + fi + default=$(get_default_java) + if [ "x$1" != "x${default}" ] || ! is_java_valid ${default}; then + unset_default_link + unset_bin_links + set_default_link_to $1 + fi + set_bin_links + + #parent_dir=$(dirname $1) + #if is_java_valid ${parent_dir}; then + # echo "Warning: '${parent_dir}' looks like a valid JDK whereas '$1' is set as default" + # echo "Fix this with 'parabola-java set ${parent_dir}'" + #fi +} + +# $1: Java environment name to unset +do_unset() { + unset_default_link + unset_bin_links +} + +do_fix() { + default=$(get_default_java) + if is_java_valid ${default}; then + if is_java_valid $(dirname ${default}); then + unset_default_link + set_default_link_to $(dirname ${default}) + fi + unset_bin_links + set_bin_links + else + prev=$(readlink ${DEFAULT_PATH}) + unset_default_link + unset_bin_links + prev_fix=("${prev/\/jre}" "${prev}/jre") + openjdk7=('java-7-openjdk' 'java-7-openjdk/jre') + to_check=(${prev_fix[@]} ${openjdk7[@]} $(get_installed_javas)) + for java in ${to_check[@]}; do + if ! is_java_valid $(get_default_java) && is_java_valid ${java}; then + set_default_link_to ${java} + set_bin_links + fi + done + fi + if ! is_java_valid $(get_default_java); then + echo 'No valid Java environment found' + fi +} + +usage() { + echo "$(basename $0) <COMMAND>" + echo -e "\nCOMMAND:" + echo -e '\tstatus\t\tList installed Java environments and enabled one' + echo -e '\tget\t\tReturn the short name of the Java environment set as default' + echo -e '\tset <JAVA_ENV>\tForce <JAVA_ENV> as default' + echo -e '\tunset\t\tUnset current default Java environment' + echo -e '\tfix\t\tFix an invalid/broken default Java environment configuration' +} + +## Main +case $1 in + 'status') do_status;; + 'get') do_get;; + 'set') check_root; check_param_count $# 2; do_set $2;; + 'unset') check_root; do_unset;; + 'fix') check_root; do_fix;; + 'help' | '--help' | '-h' | '') usage;; + *) echo "$(basename $0): unknown option '$@'"; exit 1;; +esac diff --git a/libre/java-common/install_java-common.sh b/libre/java-common/install_java-common.sh new file mode 100644 index 000000000..5ba935e2b --- /dev/null +++ b/libre/java-common/install_java-common.sh @@ -0,0 +1,47 @@ +JVM_DIR=/usr/lib/jvm +OLD_DEFAULT_NAME=java-default-runtime +OLD_DEFAULT_PATH=${JVM_DIR}/${OLD_DEFAULT_NAME} +DEFAULT_NAME=default +DEFAULT_PATH=${JVM_DIR}/${DEFAULT_NAME} +DEFAULT_NAME_JRE=default-runtime +DEFAULT_PATH_JRE=${JVM_DIR}/${DEFAULT_NAME_JRE} + +# $1: Java environment name to set as default +set_default_link_to() { + new_default=$1 + unlink ${DEFAULT_PATH} 2>/dev/null + ln -sf ${new_default} ${DEFAULT_PATH} + + unlink ${DEFAULT_PATH_JRE} 2>/dev/null + if [[ ${new_default} == */jre ]]; then + ln -sf ${new_default} ${DEFAULT_PATH_JRE} + else + ln -sf ${new_default}/jre ${DEFAULT_PATH_JRE} + fi +} + +pre_install() { + _curr_def=$(readlink ${OLD_DEFAULT_PATH} && true) + if [ -n "${_curr_def}" ]; then + unlink ${OLD_DEFAULT_PATH} > /dev/null 2>&1 + ln -sf ${DEFAULT_NAME} ${OLD_DEFAULT_PATH} + + set_default_link_to ${_curr_def} + fi +} + +pre_upgrade() { + pre_install "$@" +} + +post_install() { + echo 'Installing multiple non-conflicting Java environments is now supported' + echo 'Check the following wiki section for more info:' + echo ' https://wiki.parabolagnulinux.org/index.php/Java' + echo 'Please make sure JAVA_HOME is set to /usr/lib/jvm/default' + echo 'If not, you may have to logout and login again to set it according to /etc/profile.d/jre.sh' +} + +post_upgrade() { + post_install "$@" +} diff --git a/libre/java-common/profile_jre.csh b/libre/java-common/profile_jre.csh new file mode 100644 index 000000000..282ea964e --- /dev/null +++ b/libre/java-common/profile_jre.csh @@ -0,0 +1,6 @@ +# Do not change this unless you want to completely by-pass Parabola' way +# of handling Java versions and vendors. Instead, please use script `parabola-java` +setenv JAVA_HOME "/usr/lib/jvm/default" + +# Enable this for non-reparenting window managers +#setenv _JAVA_AWT_WM_NONREPARENTING 1 diff --git a/libre/java-common/profile_jre.sh b/libre/java-common/profile_jre.sh new file mode 100644 index 000000000..bd835f021 --- /dev/null +++ b/libre/java-common/profile_jre.sh @@ -0,0 +1,6 @@ +# Do not change this unless you want to completely by-pass Parabola' way +# of handling Java versions and vendors. Instead, please use script `parabola-java` +export JAVA_HOME=/usr/lib/jvm/default + +# Enable this for non-reparenting window managers +#export _JAVA_AWT_WM_NONREPARENTING=1 diff --git a/libre/java-common/test_parabola-java b/libre/java-common/test_parabola-java new file mode 100644 index 000000000..1051186d6 --- /dev/null +++ b/libre/java-common/test_parabola-java @@ -0,0 +1,305 @@ +#! /bin/sh +# +# To run this script, ensure shunit2 (https://code.google.com/p/shunit2/) +# is available on the local directory, then just run sh bin_parabola-java_tests +# +# inotifywait -r -m -e MODIFY . | while read l; do sh bin_parabola-java_tests; done + +declare test_dir + +. ./bin_parabola-java > /dev/null + +INIT_JVM_DIR=${JVM_DIR} +INIT_DEFAULT_PATH=${DEFAULT_PATH} +INIT_DEFAULT_PATH_JRE=${DEFAULT_PATH_JRE} +INIT_BIN_PATH=${BIN_PATH} +INIT_WRAPPER_PATH=${WRAPPER_PATH} + +# Redefining this so that tested script can be invoked without root privileges +check_root() { + true +} + + +get_rand_str() { + echo $(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1) +} + +setUp() { + test_dir=$(mktemp -d) + JVM_DIR=${test_dir}${INIT_JVM_DIR} + DEFAULT_PATH=${test_dir}${INIT_DEFAULT_PATH} + DEFAULT_PATH_JRE=${test_dir}${INIT_DEFAULT_PATH_JRE} + JAVA_NAME=$(get_rand_str) + BIN_PATH=${test_dir}${INIT_BIN_PATH} + mkdir -p ${BIN_PATH} + WRAPPER_PATH=${test_dir}${INIT_WRAPPER_PATH} + mkdir -p $(dirname ${WRAPPER_PATH}) + touch ${WRAPPER_PATH} + chmod +x ${WRAPPER_PATH} +} + +tearDown() { + rm -rf ${test_dir} +} + +# $1: path of Java environment to create +create_valid_jre_env() { + mkdir -p ${JVM_DIR}/$1/bin + for binary in java keytool orbd; do + touch ${JVM_DIR}/$1/bin/${binary} + chmod +x ${JVM_DIR}/$1/bin/${binary} + done +} + +# $1: path of Java environment to create +create_valid_jdk_env() { + create_valid_jre_env "$1" + for binary in javac javah jhat; do + touch ${JVM_DIR}/$1/bin/${binary} + chmod +x ${JVM_DIR}/$1/bin/${binary} + done + create_valid_jre_env "$1/jre" +} + +set_default_for_test() { + ln -s $1 ${DEFAULT_PATH} + for binary in java keytool orbd; do + ln -s ${WRAPPER_PATH} ${BIN_PATH}/${binary} + done + if [ -d ${DEFAULT_PATH}/jre ]; then + for binary in javac javah jhat; do + ln -s ${WRAPPER_PATH} ${BIN_PATH}/${binary} + done + ln -s $1/jre ${DEFAULT_PATH_JRE} + else + ln -s $1 ${DEFAULT_PATH_JRE} + fi +} + +assertDefaultNotSet() { + assertNull 'Default is set' "$(readlink ${DEFAULT_PATH})" + assertNull 'Default runtime is set' "$(readlink ${DEFAULT_PATH_JRE})" + assertNull "PATH has a java binary" "$(readlink ${BIN_PATH}/java)" + assertNull "PATH has a javac binary" "$(readlink ${BIN_PATH}/javac)" +} + +assertDefaultSetTo() { + new_java=$1 + assertEquals "Default points at wrong Java env" "${new_java}" "$(readlink ${DEFAULT_PATH})" + if [ ${new_java} == */jre ]; then + assertEquals "Default runtime points at wrong Java env" "${new_java}/jre" "$(readlink ${DEFAULT_PATH_JRE})" + fi + # For all links in /usr/bin that point at the wrapper + for lpath in $(find -L ${BIN_PATH} -samefile ${WRAPPER_PATH}); do + # There is a binary in the JVM bin dir + assertTrue 'Wrong /usr/bin link' "test -x ${JVM_DIR}/${new_java}/bin/$(basename ${lpath})" + done + # For all executable binaries provided by the Java env + for binary in $(find ${JVM_DIR}/${new_java}/bin -type f -perm /a=x); do + # There is a link in /usr/bin + bin_link=${BIN_PATH}/$(basename ${binary}) + assertEquals 'No link in /usr/bin' "${WRAPPER_PATH}" "$(readlink ${bin_link})" + done +} + +# do_status + +test_do_status() { + JAVA_NAME_1="a$(get_rand_str)" + JAVA_NAME_2="b$(get_rand_str)" + create_valid_jre_env "${JAVA_NAME_1}/jre" + set_default_for_test "${JAVA_NAME_1}/jre" + create_valid_jdk_env "${JAVA_NAME_2}" + expected=$(echo -e "Available Java environments:\n ${JAVA_NAME_1}/jre (default)\n ${JAVA_NAME_2}") + assertEquals 'Wrong status output' "${expected}" "$(do_status)" +} + +test_do_status_no_default() { + JAVA_NAME_1="a$(get_rand_str)" + JAVA_NAME_2="b$(get_rand_str)" + create_valid_jre_env "${JAVA_NAME_1}/jre" + create_valid_jdk_env "${JAVA_NAME_2}" + mess="Available Java environments:" + mess+="\n ${JAVA_NAME_1}/jre" + mess+="\n ${JAVA_NAME_2}" + mess+="\nNo Java environment set as default" + expected=$(echo -e "${mess}") + assertEquals 'Wrong status output' "${expected}" "$(do_status)" +} + +test_do_status_no_javas() { + assertEquals 'Wrong status output' 'No compatible Java environment installed' "$(do_status)" +} + +test_do_status_jre_but_jdk_exists() { + create_valid_jdk_env "${JAVA_NAME}" + set_default_for_test "${JAVA_NAME}/jre" + mess="Available Java environments:" + mess+="\n ${JAVA_NAME} (${JAVA_NAME}/jre default)" + expected=$(echo -e "${mess}") + assertEquals 'Wrong status output' "${expected}" "$(do_status)" +} + +# do_get + +test_do_get_with_default() { + create_valid_jdk_env "${JAVA_NAME}" + set_default_for_test "${JAVA_NAME}" + assertEquals 'Wrong result' "${JAVA_NAME}" "$(do_get)" + assertDefaultSetTo "${JAVA_NAME}" +} + +test_do_get_without_default() { + create_valid_jdk_env "${JAVA_NAME}" + assertNull 'Wrong result' "$(do_get)" +} + +# do_set + +test_do_set_new_invalid() { + create_valid_jdk_env "${JAVA_NAME}" + set_default_for_test "${JAVA_NAME}" + output=$(do_set "INVALID") + assertEquals 'Wrong status returned' 1 $? + assertEquals 'Wrong message' "'${JVM_DIR}/INVALID' is not a valid Java environment path" "${output}" + assertDefaultSetTo "${JAVA_NAME}" +} + +test_do_set_current_invalid() { + create_valid_jdk_env "${JAVA_NAME}" + set_default_for_test "INVALID" + $(do_set "${JAVA_NAME}") + assertEquals 'Wrong status returned' 0 $? + assertDefaultSetTo "${JAVA_NAME}" +} + +test_do_set_same_java_missing_bin_link() { + create_valid_jdk_env "${JAVA_NAME}" + set_default_for_test "${JAVA_NAME}" + unlink ${BIN_PATH}/java + $(do_set "${JAVA_NAME}") + assertEquals 'Wrong status returned' 0 $? + assertDefaultSetTo "${JAVA_NAME}" +} + +test_do_set_with_default_already_set() { + create_valid_jre_env "${JAVA_NAME}/jre" + set_default_for_test "${JAVA_NAME}/jre" + JAVA_NAME_2="$(get_rand_str)" + create_valid_jre_env "${JAVA_NAME_2}/jre" + $(do_set "${JAVA_NAME_2}/jre") + assertEquals 'Wrong status returned' 0 $? + assertDefaultSetTo "${JAVA_NAME_2}/jre" +} + +test_do_set_no_default() { + create_valid_jre_env "${JAVA_NAME}/jre" + $(do_set "${JAVA_NAME}/jre") + assertEquals 'Wrong status returned' 0 $? + assertDefaultSetTo "${JAVA_NAME}/jre" +} + +test_do_set_exists_jdk() { + create_valid_jdk_env "${JAVA_NAME}" + output=$(do_set "${JAVA_NAME}/jre") + assertEquals 'Wrong status returned' 0 $? + assertDefaultSetTo "${JAVA_NAME}/jre" + #mess="Warning: '${JAVA_NAME}' looks like a valid JDK whereas you only set '${JAVA_NAME}/jre' as default" + #mess+="\nFix this with 'parabola-java set ${JAVA_NAME}'" + #expected=$(echo -e "${mess}") + #assertEquals 'Wrong message' "${expected}" "${output}" +} + +test_do_set_cleanup_old_java_default() { + create_valid_jdk_env "${JAVA_NAME}" + set_default_for_test "${JAVA_NAME}" + output=$(do_set "${JAVA_NAME}/jre") + assertEquals 'Wrong status returned' 0 $? + assertDefaultSetTo "${JAVA_NAME}/jre" +} + +# do_unset + +test_do_unset_default_not_target() { + create_valid_jre_env "${JAVA_NAME}/jre" + set_default_for_test "${JAVA_NAME}/jre" + do_unset + assertDefaultNotSet +} + +test_do_unset_no_default_link() { + create_valid_jdk_env "${JAVA_NAME}" + set_default_for_test "${JAVA_NAME}" + unlink ${DEFAULT_PATH} + do_unset + assertDefaultNotSet +} + +# do_fix + +test_do_fix_default_valid() { + create_valid_jre_env "${JAVA_NAME}/jre" + set_default_for_test "${JAVA_NAME}/jre" + do_fix + assertDefaultSetTo "${JAVA_NAME}/jre" +} + +test_do_fix_no_default() { + create_valid_jre_env "${JAVA_NAME}/jre" + do_fix + assertDefaultSetTo "${JAVA_NAME}/jre" +} + +test_do_fix_default_invalid_openjdk7_jdk_installed() { + create_valid_jdk_env "java-7-openjdk" + create_valid_jdk_env "${JAVA_NAME}" + do_fix + assertDefaultSetTo "java-7-openjdk" +} + +test_do_fix_default_invalid_openjdk7_jre_installed() { + create_valid_jdk_env "${JAVA_NAME}" + create_valid_jre_env "java-7-openjdk/jre" + do_fix + assertDefaultSetTo "java-7-openjdk/jre" +} + +test_do_fix_default_invalid_random_java_installed() { + create_valid_jdk_env "${JAVA_NAME}" + do_fix + assertDefaultSetTo "${JAVA_NAME}" +} + +test_do_fix_default_invalid_no_java_installed() { + assertEquals 'Wrong message' "No valid Java environment found" "$(do_fix)" + assertDefaultNotSet +} + +test_do_fix_jre_but_jdk_exists() { + create_valid_jdk_env "${JAVA_NAME}" + set_default_for_test "${JAVA_NAME}/jre" + do_fix + assertDefaultSetTo "${JAVA_NAME}" +} + +test_do_fix_jdk_but_jre_exists() { + create_valid_jdk_env "java-7-openjdk" + create_valid_jre_env "${JAVA_NAME}/jre" + set_default_for_test "${JAVA_NAME}" + do_fix + assertDefaultSetTo "${JAVA_NAME}/jre" +} + +test_do_fix_cleanup_old_java_default() { + create_valid_jre_env "${JAVA_NAME}/jre" + set_default_for_test "${JAVA_NAME}/jre" + # Simulate installation of a jre over a jre-headless + ln -s ${WRAPPER_PATH} ${BIN_PATH}/policytool + output=$(do_fix "${JAVA_NAME}/jre") + assertEquals 'Wrong status returned' 0 $? + assertDefaultSetTo "${JAVA_NAME}/jre" +} + +# load shunit2 +. shunit2-2.1.6/src/shunit2 |