tslaunch/TSLaunch.java
giberta1 ac455ff994 Fix Calendar.MONTH strange bug (!),
Add version, date and author print out.
2002-05-21 17:11:52 +00:00

348 lines
11 KiB
Java

// $RCSfile: TSLaunch.java,v $
// $Revision: 1.3 $
// $Name: $
// $Date: 2002/05/21 17:11:52 $
// $Author: giberta1 $
/*
* TSLauch.java - Time Stamp args program laucher
* Copyright (C) 2002 Arnaud G. Gibert
* arnaud.gibert@misys.com
* www.misys.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
import java.util.*;
import java.io.*;
import java.text.*;
public class TSLaunch
{
private String TS_Key = "TIME-STAMP";
private String Sub_Args[];
private String TSLaunch_Revision = "$Revision: 1.3 $";
private String TSLaunch_Tag = "$Name: $";
private String TSLaunch_Date = "$Date: 2002/05/21 17:11:52 $";
private String TSLaunch_Author = "$Author: giberta1 $";
//-------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------
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();
}
}
//-------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------
public static void main( String args[])
{
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) + 1) + 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[])
{
String TimeStamp = "";
long time_begin, time_end;
double time_elapsed, speed;
Process subproc;
Stream_Gobbler sgout, sgerr;
try
{
System.out.println("MQSSave: " + TSLaunch_Tag + " / " + TSLaunch_Date + " / " + TSLaunch_Author);
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();
}
}
}