Skip to content

Commit 10691f5

Browse files
committed
Extracting pathfinding logic out into a new module
1 parent 0812d30 commit 10691f5

File tree

6 files changed

+131
-90
lines changed

6 files changed

+131
-90
lines changed

modules/jlink/module.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ envs:
1515

1616
modules:
1717
install:
18-
- name: jboss.container.java.run
18+
- name: jboss.container.java.run
19+
- name: jboss.container.util.pathfinder

modules/run/artifacts/opt/jboss/container/java/run/run-java.sh

Lines changed: 5 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -22,102 +22,18 @@ check_error() {
2222
fi
2323
}
2424

25-
# detect Quarkus fast-jar package type (OPENJDK-631)
26-
is_quarkus_fast_jar() {
27-
if test -f quarkus-app/quarkus-run.jar; then
28-
log_info "quarkus fast-jar package type detected"
29-
echo quarkus-app/quarkus-run.jar
30-
return 0
31-
else
32-
return 1
33-
fi
34-
}
35-
36-
# Try hard to find a sane default jar-file
37-
auto_detect_jar_file() {
38-
local dir=$1
39-
40-
# Filter out temporary jars from the shade plugin which start with 'original-'
41-
local old_dir=$(pwd)
42-
cd ${dir}
43-
if [ $? = 0 ]; then
44-
45-
if quarkus="$(is_quarkus_fast_jar)"; then
46-
echo "$quarkus"
47-
return
48-
fi
49-
50-
local nr_jars=`ls *.jar 2>/dev/null | grep -v '^original-' | wc -l | tr -d '[[:space:]]'`
51-
if [ ${nr_jars} = 1 ]; then
52-
ls *.jar | grep -v '^original-'
53-
exit 0
54-
fi
55-
56-
log_error "Neither \$JAVA_MAIN_CLASS nor \$JAVA_APP_JAR is set and ${nr_jars} JARs found in ${dir} (1 expected)"
57-
cd ${old_dir}
58-
else
59-
log_error "No directory ${dir} found for auto detection"
60-
fi
61-
}
62-
63-
# Check directories (arg 2...n) for a jar file (arg 1)
64-
get_jar_file() {
65-
local jar=$1
66-
shift;
67-
68-
if [ "${jar:0:1}" = "/" ]; then
69-
if [ -f "${jar}" ]; then
70-
echo "${jar}"
71-
else
72-
log_error "No such file ${jar}"
73-
fi
74-
else
75-
for dir in $*; do
76-
if [ -f "${dir}/$jar" ]; then
77-
echo "${dir}/$jar"
78-
return
79-
fi
80-
done
81-
log_error "No ${jar} found in $*"
82-
fi
83-
}
84-
8525
load_env() {
8626
# Configuration stuff is read from this file
8727
local run_env_sh="run-env.sh"
88-
28+
29+
# Load JAVA_APP_JAR and JAVA_LIB_DIR
30+
if [ -f "${JBOSS_CONTAINER_UTIL_PATHFINDER_MODULE}/pathfinder.sh" ]; then
31+
source "$JBOSS_CONTAINER_UTIL_PATHFINDER_MODULE/pathfinder.sh"
32+
fi
8933
# Load default default config
9034
if [ -f "${JBOSS_CONTAINER_JAVA_RUN_MODULE}/${run_env_sh}" ]; then
9135
source "${JBOSS_CONTAINER_JAVA_RUN_MODULE}/${run_env_sh}"
9236
fi
93-
94-
# Check also $JAVA_APP_DIR. Overrides other defaults
95-
# It's valid to set the app dir in the default script
96-
if [ -z "${JAVA_APP_DIR}" ]; then
97-
# XXX: is this correct? This is defaulted above to /deployments. Should we
98-
# define a default to the old /opt/java-run?
99-
JAVA_APP_DIR="${JBOSS_CONTAINER_JAVA_RUN_MODULE}"
100-
else
101-
if [ -f "${JAVA_APP_DIR}/${run_env_sh}" ]; then
102-
source "${JAVA_APP_DIR}/${run_env_sh}"
103-
fi
104-
fi
105-
export JAVA_APP_DIR
106-
107-
# JAVA_LIB_DIR defaults to JAVA_APP_DIR
108-
export JAVA_LIB_DIR="${JAVA_LIB_DIR:-${JAVA_APP_DIR}}"
109-
if [ -z "${JAVA_MAIN_CLASS}" ] && [ -z "${JAVA_APP_JAR}" ]; then
110-
JAVA_APP_JAR="$(auto_detect_jar_file ${JAVA_APP_DIR})"
111-
check_error "${JAVA_APP_JAR}"
112-
fi
113-
114-
if [ "x${JAVA_APP_JAR}" != x ]; then
115-
local jar="$(get_jar_file ${JAVA_APP_JAR} ${JAVA_APP_DIR} ${JAVA_LIB_DIR})"
116-
check_error "${jar}"
117-
export JAVA_APP_JAR=${jar}
118-
else
119-
export JAVA_MAIN_CLASS
120-
fi
12137
}
12238

12339
# Combine all java options

modules/s2i/bash/artifacts/usr/local/s2i/assemble

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ source "${JBOSS_CONTAINER_JAVA_S2I_MODULE}/s2i-core-hooks"
1111
# inject our overridden maven_s2i_*() functions
1212
source "${JBOSS_CONTAINER_JAVA_S2I_MODULE}/maven-s2i-overrides"
1313

14+
# run the pathfinder scripts to define JAVA_APP_JAR and JAVA_LIB_DIR
15+
source "${JBOSS_CONTAINER_UTIL_PATHFINDER_MODULE}/pathfinder.sh"
16+
1417
# invoke the build
1518
maven_s2i_build
1619

modules/s2i/bash/module.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ modules:
1616
- name: jboss.container.java.run
1717
- name: jboss.container.util.logging
1818
- name: jboss.container.java.jlink
19+
- name: jboss.container.util.pathfinder
1920

2021
packages:
2122
install:
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
3+
# detect Quarkus fast-jar package type (OPENJDK-631)
4+
is_quarkus_fast_jar() {
5+
if test -f quarkus-app/quarkus-run.jar; then
6+
log_info "quarkus fast-jar package type detected"
7+
echo quarkus-app/quarkus-run.jar
8+
return 0
9+
else
10+
return 1
11+
fi
12+
}
13+
14+
# Try hard to find a sane default jar-file
15+
auto_detect_jar_file() {
16+
local dir=$1
17+
18+
# Filter out temporary jars from the shade plugin which start with 'original-'
19+
local old_dir=$(pwd)
20+
cd ${dir}
21+
if [ $? = 0 ]; then
22+
if quarkus="$(is_quarkus_fast_jar)"; then
23+
echo "$quarkus"
24+
return
25+
fi
26+
local nr_jars=`ls *.jar 2>/dev/null | grep -v '^original-' | wc -l | tr -d '[[:space:]]'`
27+
if [ ${nr_jars} = 1 ]; then
28+
ls *.jar | grep -v '^original-'
29+
exit 0
30+
fi
31+
32+
log_error "Neither \$JAVA_MAIN_CLASS nor \$JAVA_APP_JAR is set and ${nr_jars} JARs found in ${dir} (1 expected)"
33+
cd ${old_dir}
34+
else
35+
log_error "No directory ${dir} found for auto detection"
36+
fi
37+
}
38+
39+
# Check directories (arg 2...n) for a jar file (arg 1)
40+
get_jar_file() {
41+
local jar=$1
42+
shift;
43+
44+
if [ "${jar:0:1}" = "/" ]; then
45+
if [ -f "${jar}" ]; then
46+
echo "${jar}"
47+
else
48+
log_error "No such file ${jar}"
49+
fi
50+
else
51+
for dir in $*; do
52+
if [ -f "${dir}/$jar" ]; then
53+
echo "${dir}/$jar"
54+
return
55+
fi
56+
done
57+
log_error "No ${jar} found in $*"
58+
fi
59+
}
60+
61+
setup_java_app_and_lib() {
62+
# Check also $JAVA_APP_DIR. Overrides other defaults
63+
# It's valid to set the app dir in the default script
64+
if [ -z "${JAVA_APP_DIR}" ]; then
65+
# XXX: is this correct? This is defaulted above to /deployments. Should we
66+
# define a default to the old /opt/java-run?
67+
JAVA_APP_DIR="${JBOSS_CONTAINER_JAVA_RUN_MODULE}"
68+
else
69+
if [ -f "${JAVA_APP_DIR}/${run_env_sh}" ]; then
70+
source "${JAVA_APP_DIR}/${run_env_sh}"
71+
fi
72+
fi
73+
export JAVA_APP_DIR
74+
75+
# JAVA_LIB_DIR defaults to JAVA_APP_DIR
76+
export JAVA_LIB_DIR="${JAVA_LIB_DIR:-${JAVA_APP_DIR}}"
77+
if [ -z "${JAVA_MAIN_CLASS}" ] && [ -z "${JAVA_APP_JAR}" ]; then
78+
JAVA_APP_JAR="$(auto_detect_jar_file ${JAVA_APP_DIR})"
79+
check_error "${JAVA_APP_JAR}"
80+
fi
81+
82+
if [ "x${JAVA_APP_JAR}" != x ]; then
83+
local jar="$(get_jar_file ${JAVA_APP_JAR} ${JAVA_APP_DIR} ${JAVA_LIB_DIR})"
84+
check_error "${jar}"
85+
export JAVA_APP_JAR=${jar}
86+
else
87+
export JAVA_MAIN_CLASS
88+
fi
89+
}

modules/util/pathfinder/module.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
schema_version: 1
2+
3+
name: "jboss.container.util.pathfinder"
4+
version: "1.0"
5+
description: ^
6+
"Provides support for determining Java application jar locations
7+
as well as library directories"
8+
9+
envs:
10+
- name: JBOSS_CONTAINER_JAVA_S2I_MODULE
11+
value: /opt/jboss/container/util/pathfinder
12+
13+
- name: JAVA_APP_DIR
14+
description: ^
15+
The directory where the application resides. All paths in your application
16+
are relative to this directory.
17+
example: "myapplication/"
18+
19+
- name: JAVA_MAIN_CLASS
20+
description: ^
21+
A main class to use as argument for `java`. When this environment variable
22+
is given, all jar files in **JAVA_APP_DIR** are added to the classpath as
23+
well as **JAVA_LIB_DIR**.
24+
example: "com.example.MainClass"
25+
26+
- name: JAVA_LIB_DIR
27+
description: ^
28+
Directory holding the Java jar files as well an optional `classpath` file
29+
which holds the classpath. Either as a single line classpath (colon
30+
separated) or with jar files listed line-by-line. If not set
31+
**JAVA_LIB_DIR** is the same as **JAVA_APP_DIR**.

0 commit comments

Comments
 (0)