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+
419ROOT=" $( pwd) "
5- VERSION=" ${tinystructVersion} "
20+ VERSION=" 1.7.11 "
621cd " $( dirname " $0 " ) " || exit
722cd " ../"
823# Navigate to the root directory
924cd " $ROOT " || exit
1025
26+ # Java options initialization
1127JAVA_OPTS=" "
28+
29+ # Arguments initialization
1230args=" "
1331
32+ # Loop through each argument
1433for 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
2144done
2245
2346JAR_FILE=" "
47+
48+ # Check if additional JAR files exist and add them to the classpath
49+ # shellcheck disable=SC2043
2450for jar_file in " $ROOT " /lib/tinystruct-" $VERSION " .jar; do
2551 [ -f " $jar_file " ] && JAR_FILE=" $JAR_FILE$jar_file :"
2652done
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 " $@ "
0 commit comments