HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Writing Unix Filters in Java"
Vol. 2, Issue 12, p. 36

	

Listing 1: Java Filter Front-End. 

import Java.util.*; 
class MyApp { 

 public static void main(String args[]) { 

int verboseLevel = 0 ; 
boolean helpFlag = false; 
String usage = "-v[erbose] -h[elp]"; 
int ArgSpec= 1; // minimum number of expected arguments 
int switchCount= 0; // number of switches found 
boolean CLIError = false; // user invocation error 

for (int i=0; i < args.length; i++) { 
 if (args[i].equals("-verbose") || args[i].equals("-v")) { 
verboseLevel++; 
switchCount++; 

 } else if (args[i].equals("-help") || args[i].equals("-h")) { 
helpFlag = true; 
switchCount++; 

 } else if (args[i].startsWith("-", 0)) { 
System.err.println("Illegal switch: " + args[i]); 
CLIError = true; 
switchCount++; 
 }  
} 

if (helpFlag) { 
 System.out.println("" + 
 " -v[erbose] Increase verboseness level\n" + 
 " -h[elp]Display this on-line help message\n"); 
 System.err.println("Usage: " + usage); 
 System.exit(0); 
}  

int totalArgs = args.length - switchCount; 
int firstArgOff= switchCount; 
if (ArgSpec != totalArgs) { 
 System.err.println(totalArgs + " arguments found " +  
ArgSpec + " expected\n"); 
 System.err.println("Usage: " + usage); 
 System.exit(1); 
} else if (CLIError) { 
 System.err.println("Invalid/illegal switches encountered"); 
 System.err.println("Usage: " + usage); 
 System.exit(2); 
} 

 /****************************************/ 
** Place your application code here**/ 
 /****************************************/  
 } 
} 

Listing 2: Wrapper Script 

1 #!/sbin/ksh  # magic cookie 
2 BASENAME=$(basename $0)  # eg: MyApp 
3 ME=$(whence $0 )  # eg: /usr/local/bin/MyApp 
4 CLASS=${BASENAME}.class  # eg: MyApp.class 
5  
6 PATH2ME=$(dirname $ME )  # eg: /usr/local/bin 
7  
8 cd $PATH2ME  # go to the bin directory 
9  
10 if [[ ! -a $CLASS ]]; then  
11echo "$BASENAME: unable to locate target: $CLASS" >&2  
12exit 1  
13 fi  
14  
15 export CLASSPATH=$PATH2ME:$CLASSPATH  
16  
17 Java $BASENAME [email protected]  # invoke the application 

Listing 3: Java Compiler (JCC). 

#!/usr/bin/ksh 

# initialize shell overhead variables 
BASENAME=$(basename $0) 
USAGE="USAGE: $BASENAME [-c] [-d] [-s] [-o string] [-p CLASSPATH] [-m] \ 
 [-v [-v]] [-h] filename[.Java]" 
(( ARG_COUNT = 1 )) # This nubmer must reflect true argument count 
(( OPT_FLAG = 0 )) # Command line mistake flag 
(( OPT_COUNT = 0 )) # Number of options on the command line 
(( HELP_FLAG = 0 )) # Default: no help required 
(( WARNING = 0 )) # General purpose no fail warning flag 
(( JAVAC = 1 )) # call the compiler 
(( JAVADOC = 1 )) # call Javadoc 
(( SCRIPT= 1 )) # generate the script 
(( PATH_OPT= 0 )) # extend the CLASSPATH switch 
JAVAOPTS=""  # default Java compile options 
PATH_OPT_VAL=""  # default extended CLASSPATH 

if [[ $VERBOSE_FLAG = "" ]]; then 
 (( VERBOSE_FLAG= 0 )) # Default: no verbose 
fi 

# Parse command line options 
while getopts :p:cdso:hv arguments  
do 
 case $arguments in 
p)(( PATH_OPT = 1 ))  # extend the CLASSPATH 
 PATH_OPT_VAL=$OPTARG;; 
s)(( SCRIPT= 0 ));;  # generate script (& man page) 
c)(( JAVAC = 0 ));;  # compile the Java program 
d)(( JAVADOC = 0 ));;  # create the Java Documentation 
o)JAVAOPTS=$OPTARG;;  # read compile switches 
v)(( VERBOSE_FLAG = VERBOSE_FLAG + 1 )) 
  if (( VERBOSE_FLAG > 1 )); then 
 vbs=" -verbose "  
  fi 
 ;; 
h)(( HELP_FLAG = 1 ));;  # display help 
\?) echo "Illegal switch: $OPTARG" # flag illegal switch 
 (( OPT_FLAG = 1 ));; 
 esac 
done 
(( OPT_COUNT = OPTIND - 1 )) 
shift $OPT_COUNT 
  

# check for help 
if (( $HELP_FLAG == 1 )); then 
 echo " Usage: $USAGE" 
 echo " -c  Do not compile the target file" 
 echo " -d  No not create Java Documents" 
 echo " -h  Display this help message" 
 echo " -o string Javac compiler options" 
 echo " -p string Extend the CLASSPATH" 
 echo " -s  No not create the front-end script" 
 echo " -v  Display status information during execution" 
 exit 0 
fi 

# check for illegal switches 
if (( $OPT_FLAG == 1 )); then 
 echo "$BASENAME: Illegal or invalid switche(s) encountered" 
 echo "Usage: $USAGE" 
 exit -1 
elif (( $# != $ARG_COUNT )); then 
 echo "$BASENAME: $# arguments found, at least $ARG_COUNT expected." 
 echo "Usage: $USAGE" 
 exit -1 
fi 

# reset umask to all files are rw-able 
umask 000 

dir=$(echo $1 | dirname ) 
target_basename=$(basename $1) 
target=$(echo $target_basename | cut -d\. -f1) 
ext=$(echo $target_basename | cut -d\. -f2) 

if [[ $ext = $target ]]; then 
 # assume .Java 
 ext="Java" 
elif [[ $ext != "Java" ]]; then 
 echo "$BASENAME: invalid extension" 
 exit 1 
fi 

source=${target}.Java  # a.k.a MyApp.Java 
class=${target}.class  # a.k.a MyApp.class 
html=${target}.html  # a.k.a MyApp.html 
script=${target}  # a.k.a MyApp 
manpage=${target}.1  # a.k.a MyApp.1 
  

# compile program to create target.class from target.Java 
if (( JAVAC )); then 
 if (( VERBOSE_FLAG )); then 
echo "$BASENAME: Javac $JAVAOPTS $vbs ${source}" 
 fi 
 Javac $JAVAOPTS $vbs ${source} 
 status=$? 
 if (( status != 0 )); then 
echo "$BASENAME: Java compiler error: $status" 
exit $status 
 fi 
fi 

# create the target.html file 
if (( JAVADOC )); then 
 if (( VERBOSE_FLAG )); then 
echo "$BASENAME: Javadoc $vbs ${source}" 
 fi 
 Javadoc $vbs ${source} 
fi 

# if the script section is not selected then terminate 
if (( ! SCRIPT )); then 
 exit 0 
fi 
# otherwise create the shell script that will invoke the .class file 

if (( VERBOSE_FLAG )); then 
 echo "$BASENAME: creating $script" 
fi 

# remove the old "executable" version and man page 
rm -f $script $manpage 
  
# insert magic cookie and set debug flags (if required) 
ShellPath="$(which ksh)" 
jar=$? 
if (( jar != 0 )); then 
 echo $COOKIE 
 echo "$BASENAME: Error: Unable to determine magic cookie, aborting..." 
 exit 1 
fi 

# create the execut script 
COOKIE="$ShellPath $StartFlag" 
echo "#!$COOKIE${SHDEBUG}"     > $script 
echo "BASENAME=\$(basename \$0) "    >> $script 
echo "ME=\$(whence \$0 ) "     >> $script 
echo "TARGET=\${BASENAME}.class "    >> $script 
echo ""       >> $script 
echo "PATH2ME=\$(dirname \$ME ) "    >> $script 
echo ""       >> $script 
echo "cd \$PATH2ME "      >> $script 
echo ""       >> $script 
echo "if [[ ! -a \$TARGET ]]; then "    >> $script 
echo " echo \"\$BASENAME: unable to locate target: \$TARGET\" >&2 " >> $script 
echo " exit 1 "      >> $script 
echo "fi "       >> $script 
echo " "       >> $script 
echo "export CLASSPATH=\$PATH2ME:\$CLASSPATH "   >> $script 
if (( PATH_OPT )); then 
 echo "export CLASSPATH=$PATH_OPT_VAL:\$CLASSPATH " >> $script 
fi 
echo ""       >> $script 
echo ""       >> $script 
echo "Java \$BASENAME \[email protected] "     >> $script 
  

# create a dummy man page file 
if (( VERBOSE_FLAG )); then 
 echo "$BASENAME: creating $manpage" 
fi 
echo "" >> $manpage 
echo "${target} is a front-end to the Java application ${target}.class" \ 
 >> $manpage 
echo "please see ${target}.html for more details."\ 
 >> $manpage 
echo "" >> $manpage 
echo "For details regarding Java script wrappers please see $BASENAME" \ 
 >> $manpage 
echo "" >> $manpage 

# make the script executable 
chmod a+rx $script 

Listing 4: Example Makefile. 

BIN=/usr/local/Javabin 
MANBIN=/usr/share/man/cat1/ 
HTMLBIN=/usr/people/kkranz/www/htdocs/ManPages 

MyApp.class: MyApp.Java 
  jcc -v MyApp 

install: 
  cp -p MyApp $(BIN) 
  cp -p MyApp.class $(BIN) 
  cp -p MyApp.1 $(MANBIN) 
  cp -p MyApp.html $(HTMLBIN) 


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.