Skip to content

Commit 7ed013b

Browse files
committed
Update the dispatcher scripts.
1 parent 984dc9f commit 7ed013b

File tree

2 files changed

+236
-8
lines changed

2 files changed

+236
-8
lines changed
Lines changed: 196 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
#!/usr/bin/env sh
22

3-
# Dispatcher script (copied from project root)
3+
# ***************************************************************************
4+
# Copyright (c) 2025 James Mover Zhou
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
# ***************************************************************************
18+
419
ROOT="$(pwd)"
5-
VERSION="${tinystructVersion}"
20+
VERSION="1.7.11"
621
cd "$(dirname "$0")" || exit
722
cd "../"
823
# Navigate to the root directory
924
cd "$ROOT" || exit
1025

26+
# Java options initialization
1127
JAVA_OPTS=""
28+
29+
# Arguments initialization
1230
args=""
1331

32+
# Loop through each argument
1433
for arg; do
34+
# Extract the first two characters of the argument
35+
# str=${arg:0:2}
1536
str=$(echo "$arg" | awk '{ string=substr($0, 0, 2); print string; }' )
37+
38+
# Check if it starts with '-D' or '-X'
1639
if [ "$str" = "-D" ] || [ "$str" = "-X" ]; then
1740
JAVA_OPTS="$JAVA_OPTS $arg"
1841
else
@@ -21,11 +44,181 @@ for arg; do
2144
done
2245

2346
JAR_FILE=""
47+
48+
# Check if additional JAR files exist and add them to the classpath
49+
# shellcheck disable=SC2043
2450
for jar_file in "$ROOT"/lib/tinystruct-"$VERSION".jar; do
2551
[ -f "$jar_file" ] && JAR_FILE="$JAR_FILE$jar_file:"
2652
done
2753

28-
$JAVA_HOME/bin/java \
54+
# OS specific support. $var _must_ be set to either true or false.
55+
cygwin=false;
56+
darwin=false;
57+
mingw=false
58+
case "`uname`" in
59+
CYGWIN*) cygwin=true ;;
60+
MINGW*) mingw=true;;
61+
Darwin*) darwin=true
62+
# Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
63+
# See https://developer.apple.com/library/mac/qa/qa1170/_index.html
64+
if [ -z "$JAVA_HOME" ]; then
65+
if [ -x "/usr/libexec/java_home" ]; then
66+
export JAVA_HOME="`/usr/libexec/java_home`"
67+
else
68+
export JAVA_HOME="/Library/Java/Home"
69+
fi
70+
fi
71+
;;
72+
esac
73+
74+
if [ -z "$JAVA_HOME" ] ; then
75+
if [ -r /etc/gentoo-release ] ; then
76+
JAVA_HOME=`java-config --jre-home`
77+
fi
78+
fi
79+
80+
if [ -z "$M2_HOME" ] ; then
81+
## resolve links - $0 may be a link to maven's home
82+
PRG="$0"
83+
84+
# need this for relative symlinks
85+
while [ -h "$PRG" ] ; do
86+
ls=`ls -ld "$PRG"`
87+
link=`expr "$ls" : '.*-> \(.*\)$'`
88+
if expr "$link" : '/.*' > /dev/null; then
89+
PRG="$link"
90+
else
91+
PRG="`dirname "$PRG"`/$link"
92+
fi
93+
done
94+
95+
saveddir=`pwd`
96+
97+
M2_HOME=`dirname "$PRG"`/..
98+
99+
# make it fully qualified
100+
M2_HOME=`cd "$M2_HOME" && pwd`
101+
102+
cd "$saveddir"
103+
# echo Using m2 at $M2_HOME
104+
fi
105+
106+
# For Cygwin, ensure paths are in UNIX format before anything is touched
107+
if $cygwin ; then
108+
[ -n "$M2_HOME" ] &&
109+
M2_HOME=`cygpath --unix "$M2_HOME"`
110+
[ -n "$JAVA_HOME" ] &&
111+
JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
112+
[ -n "$CLASSPATH" ] &&
113+
CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
114+
fi
115+
116+
# For Migwn, ensure paths are in UNIX format before anything is touched
117+
if $mingw ; then
118+
[ -n "$M2_HOME" ] &&
119+
M2_HOME="`(cd "$M2_HOME"; pwd)`"
120+
[ -n "$JAVA_HOME" ] &&
121+
JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
122+
# TODO classpath?
123+
fi
124+
125+
if [ -z "$JAVA_HOME" ]; then
126+
javaExecutable="`which javac`"
127+
if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
128+
# readlink(1) is not available as standard on Solaris 10.
129+
readLink=`which readlink`
130+
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
131+
if $darwin ; then
132+
javaHome="`dirname \"$javaExecutable\"`"
133+
javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
134+
else
135+
javaExecutable="`readlink -f \"$javaExecutable\"`"
136+
fi
137+
javaHome="`dirname \"$javaExecutable\"`"
138+
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
139+
JAVA_HOME="$javaHome"
140+
export JAVA_HOME
141+
fi
142+
fi
143+
fi
144+
145+
if [ -z "$JAVACMD" ] ; then
146+
if [ -n "$JAVA_HOME" ] ; then
147+
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
148+
# IBM's JDK on AIX uses strange locations for the executables
149+
JAVACMD="$JAVA_HOME/jre/sh/java"
150+
else
151+
JAVACMD="$JAVA_HOME/bin/java"
152+
fi
153+
else
154+
JAVACMD="`which java`"
155+
fi
156+
fi
157+
158+
if [ ! -x "$JAVACMD" ] ; then
159+
echo "Error: JAVA_HOME is not defined correctly." >&2
160+
echo " We cannot execute $JAVACMD" >&2
161+
exit 1
162+
fi
163+
164+
if [ -z "$JAVA_HOME" ] ; then
165+
echo "Warning: JAVA_HOME environment variable is not set."
166+
fi
167+
168+
# For Cygwin, switch paths to Windows format before running java
169+
if $cygwin; then
170+
[ -n "$M2_HOME" ] &&
171+
M2_HOME=`cygpath --path --windows "$M2_HOME"`
172+
[ -n "$JAVA_HOME" ] &&
173+
JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
174+
[ -n "$CLASSPATH" ] &&
175+
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
176+
[ -n "$MAVEN_PROJECTBASEDIR" ] &&
177+
MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
178+
fi
179+
180+
# Check if M2_HOME is not set or is equal to the current project path
181+
if [ -z "$M2_HOME" ] || [ "$M2_HOME" = "$(pwd)" ]; then
182+
# Set M2_HOME to the .m2 folder under the user's home directory
183+
M2_HOME="$HOME/.m2"
184+
fi
185+
186+
# Add all JAR files under the lib folder to the classpath
187+
for jar_file in "$ROOT"/lib/*.jar; do
188+
CLASSPATH="$CLASSPATH:$jar_file"
189+
done
190+
191+
# Check if the JAR file exists in .m2
192+
DEFAULT_JAR_FILE="$M2_HOME/repository/org/tinystruct/tinystruct/$VERSION/tinystruct-$VERSION.jar"
193+
[ -f "$DEFAULT_JAR_FILE" ] && JAR_FILE="$DEFAULT_JAR_FILE"
194+
195+
# Check if the JAR file exists in .m2
196+
DEFAULT_JAR_FILE_WITH_DEPENDENCIES="$M2_HOME/repository/org/tinystruct/tinystruct/$VERSION/tinystruct-$VERSION-jar-with-dependencies.jar"
197+
[ -f "$DEFAULT_JAR_FILE_WITH_DEPENDENCIES" ] && JAR_FILE="$DEFAULT_JAR_FILE_WITH_DEPENDENCIES"
198+
199+
# Check if the Maven Wrapper is already available
200+
if [ ! -f "mvnw" ]; then
201+
echo "Maven Wrapper not found. Extracting from JAR..."
202+
203+
# Run Java code to extract the ZIP file from the JAR
204+
$JAVACMD -cp "$JAR_FILE" org.tinystruct.system.Dispatcher maven-wrapper --jar-file-path "$JAR_FILE" --destination-dir "$ROOT"
205+
206+
if [ -f "$ROOT/maven-wrapper.zip" ]; then
207+
# Now unzip the Maven Wrapper files
208+
unzip -q "$ROOT/maven-wrapper.zip" -d "$ROOT"
209+
# Delete the ZIP file after extraction
210+
rm -f "$ROOT/maven-wrapper.zip"
211+
chmod +x ./mvnw
212+
echo "Maven wrapper setup completed."
213+
else
214+
echo "Error: Maven wrapper ZIP file not found in JAR."
215+
exit 1
216+
fi
217+
fi
218+
219+
220+
# Java execution
221+
$JAVACMD \
29222
$JAVA_OPTS \
30223
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/ \
31224
-cp "$ROOT/target/classes:$CLASSPATH:$ROOT/WEB-INF/lib/*:$ROOT/WEB-INF/classes:$JAR_FILE" org.tinystruct.system.Dispatcher "$@"
Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,64 @@
1+
@rem ***************************************************************************
2+
@rem Copyright (c) 2025 James M. Zhou
3+
@rem
4+
@rem Licensed under the Apache License, Version 2.0 (the "License");
5+
@rem you may not use this file except in compliance with the License.
6+
@rem You may obtain a copy of the License at
7+
@rem
8+
@rem http://www.apache.org/licenses/LICENSE-2.0
9+
@rem
10+
@rem Unless required by applicable law or agreed to in writing, software
11+
@rem distributed under the License is distributed on an "AS IS" BASIS,
12+
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
@rem See the License for the specific language governing permissions and
14+
@rem limitations under the License.
15+
@rem ***************************************************************************
116
@echo off
2-
REM Dispatcher Windows script (copied from project root)
3-
set "ROOT=%~dp0.."
4-
set "VERSION=${tinystructVersion}"
517

18+
@REM Set the local Maven repository path for tinystruct.jar
619
set "MAVEN_REPO=%USERPROFILE%\.m2\repository\org\tinystruct\tinystruct"
20+
@REM Consolidate classpath entries, initialize ROOT and VERSION
21+
set "ROOT=%~dp0.."
22+
set "VERSION=1.7.11"
23+
24+
@REM Define the paths for tinystruct jars in the Maven repository
725
set "DEFAULT_JAR_FILE=%MAVEN_REPO%\%VERSION%\tinystruct-%VERSION%.jar"
826
set "DEFAULT_JAR_FILE_WITH_DEPENDENCIES=%MAVEN_REPO%\%VERSION%\tinystruct-%VERSION%-jar-with-dependencies.jar"
927

28+
REM Check which jar to use for extracting Maven Wrapper
1029
if exist "%DEFAULT_JAR_FILE_WITH_DEPENDENCIES%" (
1130
set "JAR_PATH=%DEFAULT_JAR_FILE_WITH_DEPENDENCIES%"
1231
) else (
1332
set "JAR_PATH=%DEFAULT_JAR_FILE%"
1433
)
1534

16-
if "%JAVA_HOME%"=="" (
35+
@REM Check if JAVA_HOME is set and valid
36+
if "%JAVA_HOME%" == "" (
1737
echo Error: JAVA_HOME not found in your environment. >&2
38+
echo Please set the JAVA_HOME variable in your environment to match the location of your Java installation. >&2
39+
exit /B 1
40+
)
41+
42+
if not exist "%JAVA_HOME%\bin\java.exe" (
43+
echo Error: JAVA_HOME is set to an invalid directory. >&2
44+
echo JAVA_HOME = "%JAVA_HOME%" >&2
45+
echo Please set the JAVA_HOME variable in your environment to match the location of your Java installation. >&2
1846
exit /B 1
1947
)
2048

2149
set "JAVA_CMD=%JAVA_HOME%\bin\java.exe"
2250

51+
@REM Check if the Maven Wrapper is already available
2352
if not exist "mvnw" (
2453
echo Maven Wrapper not found. Extracting from JAR...
54+
55+
@REM Run Java code to extract the ZIP file from the JAR
2556
%JAVA_CMD% -cp "%JAR_PATH%" org.tinystruct.system.Dispatcher maven-wrapper --jar-file-path "%JAR_PATH%" --destination-dir "%ROOT%"
57+
2658
if exist "%ROOT%\maven-wrapper.zip" (
27-
powershell -Command "Expand-Archive -Path '%ROOT%\\maven-wrapper.zip' -DestinationPath '%ROOT%'"
59+
@REM Now unzip the Maven Wrapper files
60+
powershell -Command "Expand-Archive -Path '%ROOT%\maven-wrapper.zip' -DestinationPath '%ROOT%'"
61+
@REM Delete the ZIP file after extraction
2862
del /F /Q "%ROOT%\maven-wrapper.zip"
2963
echo Maven wrapper setup completed.
3064
) else (
@@ -35,4 +69,5 @@ if not exist "mvnw" (
3569

3670
set "classpath=%ROOT%\target\classes;%ROOT%\lib\tinystruct-%VERSION%-jar-with-dependencies.jar;%ROOT%\lib\tinystruct-%VERSION%.jar;%ROOT%\lib\*;%ROOT%\WEB-INF\lib\*;%ROOT%\WEB-INF\classes;%USERPROFILE%\.m2\repository\org\tinystruct\tinystruct\%VERSION%\tinystruct-%VERSION%-jar-with-dependencies.jar;%USERPROFILE%\.m2\repository\org\tinystruct\tinystruct\%VERSION%\tinystruct-%VERSION%.jar"
3771

72+
@REM Run Java application
3873
%JAVA_CMD% -cp "%classpath%" org.tinystruct.system.Dispatcher %*

0 commit comments

Comments
 (0)