From f4a26913245e441d307063b5698c932fd43177c4 Mon Sep 17 00:00:00 2001 From: giberta1 Date: Fri, 26 Apr 2002 16:45:37 +0000 Subject: [PATCH] First working version. --- TSLaunch.java | 287 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 283 insertions(+), 4 deletions(-) diff --git a/TSLaunch.java b/TSLaunch.java index 0bf6458..91f7f57 100644 --- a/TSLaunch.java +++ b/TSLaunch.java @@ -1,7 +1,7 @@ // $RCSfile: TSLaunch.java,v $ -// $Revision: 1.1 $ +// $Revision: 1.2 $ // $Name: $ -// $Date: 2002/04/26 16:44:46 $ +// $Date: 2002/04/26 16:45:37 $ // $Author: giberta1 $ /* @@ -35,6 +35,70 @@ import java.text.*; public class TSLaunch { + private String TS_Key = "TIME-STAMP"; + private String Sub_Args[]; + + + + + + //------------------------------------------------------------------------------------------------------------------------- + // + //------------------------------------------------------------------------------------------------------------------------- + + public class Stream_Gobbler extends Thread + { + BufferedInputStream BIS; + PrintStream PS; + + + + + + //----------------------------------------------------------------------------------------------------------------- + // + //----------------------------------------------------------------------------------------------------------------- + + public void run() + { + int out; + + + try + { + while ( ( out = BIS.read() ) != -1) + { + PS.print( (char)out); + } + } + + catch( Exception Expt) + { + System.out.println("Exception: (" + Expt + ") !"); + Expt.printStackTrace(); + } + + } + + + + + + //----------------------------------------------------------------------------------------------------------------- + // + //----------------------------------------------------------------------------------------------------------------- + + public Stream_Gobbler( InputStream IS, PrintStream PS) + { + this.BIS = new BufferedInputStream( IS); + this.PS = PS; + + this.start(); + } + } + + + //------------------------------------------------------------------------------------------------------------------------- @@ -45,10 +109,174 @@ public class TSLaunch { new TSLaunch( args); } - + + + + + //------------------------------------------------------------------------------------------------------------------------- + // + //------------------------------------------------------------------------------------------------------------------------- + + private void Arg_Parse( String Args[]) throws Exception + { + int argc = 0; + + + try + { + while( argc < Args.length) + { + if ( Args[argc].equals( "-k")) + { + if( argc < ( Args.length + 1)) + { + TS_Key = Args[++argc]; + } + else + { + System.out.println( "Invalid number of command line options..."); + throw new Exception(); + } + + argc++; + } + else + { + if( argc < ( Args.length + 2)) + { + Sub_Args = new String[Args.length - argc]; + System.arraycopy( Args, argc, Sub_Args, 0, ( Args.length - argc)); + + break; + } + else + { + System.out.println( "Invalid number of command line options..."); + throw new Exception(); + } + } + } + } + + catch( Exception Expt) + { + throw Expt; + } + } + + + + + //------------------------------------------------------------------------------------------------------------------------- + // + //------------------------------------------------------------------------------------------------------------------------- + + private String Array_Dump( String Args[], int offset) throws Exception + { + int argc = offset; + String Dump = ""; + + + try + { + while( argc < Args.length) + { + if( argc > offset) + { + Dump += " "; + } + + Dump += "\"" + Args[ argc++] + "\""; + } + + return( Dump); + } + + catch( Exception Expt) + { + throw Expt; + } + } + + + + + //------------------------------------------------------------------------------------------------------------------------- + // + //------------------------------------------------------------------------------------------------------------------------- + + private String TimeStamp_Get() throws Exception + { + Calendar now = Calendar.getInstance(); + NumberFormat nf2 = NumberFormat.getInstance(); + NumberFormat nf3 = NumberFormat.getInstance(); + + + try + { + nf2.setMinimumIntegerDigits(2); + nf2.setMaximumIntegerDigits(2); + nf3.setMinimumIntegerDigits(3); + nf3.setMaximumIntegerDigits(3); + + return( "" + now.get( Calendar.YEAR) + nf2.format( now.get( Calendar.MONTH)) + nf2.format( now.get( Calendar.DAY_OF_MONTH)) + + nf2.format( now.get( Calendar.HOUR_OF_DAY)) + nf2.format( now.get( Calendar.MINUTE)) + nf2.format( now.get( Calendar.SECOND)) + + nf3.format( now.get( Calendar.MILLISECOND))); + } + + catch( Exception Expt) + { + throw Expt; + } + } + + + + + + //------------------------------------------------------------------------------------------------------------------------- + // + //------------------------------------------------------------------------------------------------------------------------- + + private void TS_Replace( String Args[], String TS_Key, String TimeStamp) throws Exception + { + int argc, idx; + String Dump = ""; + + + try + { + for( argc = 1; argc < Args.length; argc++) + { + idx = 0; + + while( idx != -1) + { +// System.out.println( "Argc: (" + argc + ") Idx: (" + idx + ")"); + + idx = Args[argc].indexOf( TS_Key, 0); + + if( idx != -1) + { +// System.out.println( "Found: (" + idx + ") End: (" + (idx + TS_Key.length()) + ")"); + Args[argc] = Args[argc].substring( 0, idx) + TimeStamp + Args[argc].substring( idx + TS_Key.length()); + } + } + } + } + + catch( Exception Expt) + { + throw Expt; + } + } + + + + //------------------------------------------------------------------------------------------------------------------------- // @@ -56,7 +284,58 @@ public class TSLaunch public TSLaunch( String args[]) { - } + String TimeStamp = ""; + long time_begin, time_end; + double time_elapsed, speed; + Process subproc; + Stream_Gobbler sgout, sgerr; + + + try + { + if( args.length < 2) + { + System.out.println( "Usage: TSLaunch [-k \"TimeStamp_Key\"] Command_to_Run [...Args...]TimeStamp_Key[...Args...]"); + System.out.println( " Default: : TimeStamp_Key: (" + TS_Key + ")"); + } + else + { + System.out.println( "TS Lauch Starting..."); + + Arg_Parse( args); + + System.out.println( "Initial: Command_to_Run: (" + Sub_Args[0] + ") Args: (" + Array_Dump( Sub_Args, 1) + ") TimeStamp_Key: (" + TS_Key + ")"); + TimeStamp = TimeStamp_Get(); + TS_Replace( Sub_Args, TS_Key, TimeStamp); + + System.out.println( "Final: Command_to_Run: (" + Sub_Args[0] + ") Args: (" + Array_Dump( Sub_Args, 1) + ") TimeStamp: (" + TimeStamp + ")"); + + subproc = Runtime.getRuntime().exec( Sub_Args); + + sgout = new Stream_Gobbler( subproc.getInputStream(), System.out); + sgerr = new Stream_Gobbler( subproc.getErrorStream(), System.out); + + System.out.println( "Wainting..."); + + subproc.waitFor(); + sgout.join(); + sgerr.join(); + + System.out.println( "Exit_Value: (" + subproc.exitValue() + ")"); + + System.out.println( "TS Lauch Completed !"); + + System.exit( subproc.exitValue()); + } + } + + catch( Exception Expt) + { + System.out.println("Exception: (" + Expt + ") !"); + Expt.printStackTrace(); + } + } + }