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
 

"The Future of Enterprise Java"
Vol. 2, Issue 9, p. 8

	

Listing 1: CGI Wrapper Script.
 
#!/sbin/ksh 
BASENAME=$(basename $0) # the name of this script 
JAVA=$(which java)  # the path to the VM 

if (( $# < 1 )); then 
   echo "$BASENAME: $# arguments found, at least 1 expected." >&2 
   echo "Usage: $BASENAME [-options] ClassFile GetString" >&2 
   exit -1 
fi 

JavaClass=$(basename $0) # assume the class name matches the script 
JavaDir=$(dirname $1)  # get the current directory name 
GetString=$2 

# build the system properties entries 
for Property in REMOTE_HOST HTTP_HOST HTTP_CONNECTION SERVER_SOFTWARE \ 
 DOCUMENT_NAME SERVER_URL SERVER_NAME \ 
 SERVER_PORT REMOTE_ADDR TZ DOCUMENT_URL 
do 
   # setup an option string that passes in all env variables 
   Temp=$(env | grep $Property) 
   Name=$(echo $Temp | cut -d= -f1) 
   Value=$(echo $Temp | cut -d= -f2) 
   if [[ $Name != "" ]]; then 
      SystemProperties="$SystemProperties -D\"$Temp\"" 
   fi 
done 

# add current dir to the class path 
if [[ $JavaDir != "" ]]; then 
   export CLASSPATH=$CLASSPATH:$JavaDir 
fi 
# invoke the VM on the target class file, pass the get string 
$JAVA $SystemProperties $JavaClass $GetString 

Listing 2: Java CGI Program.
 
import java.util.*; 

public class cgiParse extends Properties { 
  private Properties TVtable; 
  private StringTokenizer MetaToken; 

  public static void main (String args[]) { 
    cgiParse myTable = new cgiParse(); 
    // note the shell script wrapper checks for illegal args 
    myTable.parse(args[1]); 
  } 
  

  public void parse (String cli) { 

    TVtable = new Properties(); 
    MetaToken = new StringTokenizer(cli, "&"); 

    while (MetaToken.hasMoreTokens()) { 

      String NextToken = MetaToken.nextToken(); 
      StringTokenizer KeyValue = new StringTokenizer(NextToken, "="); 

      String Key   = KeyValue.nextToken();      // extract the Key part 
      String Value = KeyValue.nextToken();      // extract the Value art 

      // if the key is already in the table then append the new value 
      // else insert the new key/value pair 
      if (TVtable.containsKey(Key)) { 
        // key already exists, append the new value 
        String OldValue = TVtable.getProperty(Key); // get the old value 
        TVtable.remove(Key);                        // remove old entry 
        // append the new value and insert back into the table 
        TVtable.put(Key, OldValue+" "+Value); 
      } else { 
  # insert anew  
        TVtable.put(Key, Value); 
      } 
    } 
  } 
} 

Listing 3: Standalone Wrapper Script.
 
#!/sbin/ksh  # Eg Filename: MyApp 
BASENAME=$(basename $0)  # MyApp 
ME=$(whence $0 )  # /usr/local/javabin/MyApp 
CLASS=${BASENAME}.class  # MyApp.class 

PATH2ME=$(dirname $ME )  # eg: /usr/local/javabin 
PATH2CLASS=${PATH2ME}/${BASENAME}_bin # eg: /usr/local/javabin/MyApp_bin 
cd $PATH2CLASS # go to the dir w/ the class file 

if [[ ! -a $CLASS ]]; then  # if no class file, exit 
   echo "$BASENAME: unable to locate target: $CLASS" >&2  
   exit 1  
fi  

export CLASSPATH=$PATH2CLASS:$PATH2ME:$CLASSPATH  

java $BASENAME [email protected]  # java MyApp [args] 

Listing 4: Standalone Java Program.
 
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); 
      }      
   } 
}


 

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.