aboutsummaryrefslogtreecommitdiffstats
path: root/java/javavmwrapper/src/javavmwrapper.sh
blob: 836ef04e87a014a33d5c45c4fe0847674296e4fa (plain) (blame)
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
#!/bin/sh
#
# javawrapper.sh
#
# Allows to install several Java Virtual Machines
# on the same system and use config file/or environment
# variable to select wichever to use
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp):
# Maxim Sobolev <sobomax@FreeBSD.org> wrote this file.  As long as you retain
# this notice you can do whatever you want with this stuff. If we meet some
# day, and you think this stuff is worth it, you can buy me a beer in return.
#
# Maxim Sobolev
# ----------------------------------------------------------------------------
#
# $FreeBSD$
#
# MAINTAINER= sobomax@FreeBSD.org

ARGS="${*}"
PREFIX="%%PREFIX%%"
CONF="${PREFIX}/etc/javavms"
IAM=`basename "${0}"`

tryrunVM () {
    if [ -x "${1}" ]; then
        exec "${1}" ${2}
    fi

    /bin/echo "${IAM}: warning: couldn't start specified JavaVM - \"${1}\"" >&2
}

registerVM () {
    if [ x"${1}" = x"" ]; then
       /bin/echo "Usage: ${IAM} path"
       exit
    fi

    if [ ! -e "${CONF}" ]; then
       /usr/bin/touch "${CONF}"
    fi

    VM=`/bin/echo "${1}" | sed 's|#.*||'`
    if [ ! -x ${VM} ]; then
        /bin/echo "${IAM}: warning: the specified JavaVM \"${VM}\" either not exists or not executable" >&2
    fi

    /bin/ed "${CONF}" >/dev/null <<EOF
0a
${1}
.
w
q
EOF
    exit 0
}

unregisterVM () {
    if [ x"${1}" = x"" ]; then
       /bin/echo "Usage: ${IAM} path"
       exit
    fi

    if [ ! -e "${CONF}" ]; then
       /bin/echo "${IAM}: error: can't find ${CONF} config file!" >&2
       exit 1
    fi

    if [ x"`grep ${1} ${CONF}`" = x"" ]; then
        /bin/echo "${IAM}: error: \"${1}\" JavaVM is not currently registered"
        exit 1
    fi

    /bin/ed "${CONF}" >/dev/null <<EOF
g|${1}|d
w
q
EOF

    # Remove config file if its size reached 0
    if [ ! -s "${CONF}" ]; then
        /bin/rm "${CONF}"
    fi

    exit 0
}

case "${IAM}" in
    registervm )
        registerVM "${1}"
        ;;
    unregistervm )
        unregisterVM "${1}"
        ;;
esac

# Main ()

# First check if JAVAVM environment variable is set
if [ x"${JAVAVM}" != x"" ]; then
    tryrunVM "${JAVAVM}" "${ARGS}"
fi

# Then try to make sure that ${CONF} exists
if [ ! -e "${CONF}" ]; then
    echo "${IAM}: error: can't find ${CONF} config file" >&2
    exit 1
fi

# Allow coment in the ${CONF}
VMS=`/usr/bin/sed 's|#.*||' < "${CONF}" | uniq`

# Finally try to run one of the ${VMS}
for JAVAVM in ${VMS}; do
    tryrunVM "${JAVAVM}" "${ARGS}";
done

echo "${IAM}: error: no suitable JavaVMs found" >&2
exit 1