First working version.

This commit is contained in:
giberta1 2002-04-26 16:45:37 +00:00
parent 25b02619ef
commit f4a2691324

View File

@ -1,7 +1,7 @@
// $RCSfile: TSLaunch.java,v $ // $RCSfile: TSLaunch.java,v $
// $Revision: 1.1 $ // $Revision: 1.2 $
// $Name: $ // $Name: $
// $Date: 2002/04/26 16:44:46 $ // $Date: 2002/04/26 16:45:37 $
// $Author: giberta1 $ // $Author: giberta1 $
/* /*
@ -35,6 +35,70 @@ import java.text.*;
public class TSLaunch 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();
}
}
//------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------
@ -50,12 +114,227 @@ public class TSLaunch
//-------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------
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;
}
}
//------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------
// //
//------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------
public TSLaunch( String args[]) 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();
}
} }
} }