home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-01-09 | 11.5 KB | 429 lines |
- /*
- -----------------------------------------------------------------------------------------------------
- Description: ReadWeather
- Author: F. Salazar
- Created: June, 1997
-
- Copyright (c)1992-1997 Lotus Development Corp. All Rights Reserved.
- -----------------------------------------------------------------------------------------------------
- */
- /*******************************************************************************
- Change History:
-
- $Log: //reebok/xyzL/JavaComp/webpack/samples/weatherchart/ReadWeather.java
-
- Rev 1.5.2.1 09 Jan 1998 10:52:12 jdonohue
- Change package name to samples.weatherchart
-
- Rev 1.5 13 Oct 1997 11:19:24 fsalazar
- Correct precision on floating pt values.
-
- Rev 1.4 05 Aug 1997 10:15:42 fsalazar
- Use webpack.samples.weatherchart package name.
-
- Rev 1.3 31 Jul 1997 15:25:48 fsalazar
- Missed a deprecated readline().
-
- *******************************************************************************/
-
- package samples.weatherchart;
-
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import java.text.*;
-
-
-
- /* ------------------------------------------------------------------------------------------------------
-
- ReadWeather is a supporting application for the Lotus Kona Webpack Samples.
- Its purpose is to update dataset files which record weather information -- temperature,
- pressure and humidity -- for various cities. ReadWeather should be scheduled to run once
- an hour; every 24 hours, its data files should be "reset", so that the datasets do not grow
- beyond a reasonable size.
-
- Syntax for invoking ReadWeather is as follows:
-
- (jvm) ReadWeather config [proxy:port] [/tzone]
- (jvm) Java virtual machine which loads the application; either java or jview.
- ReadWeather Name of class.
- config Name of configuration file (see below).
- [proxy:port] Optional. If specified, ReadWeather will issue HTTP requests through a proxy server.
- [/t] Optional. Specifies a timezone code, eg EST. If not specified, default will be used.
-
- Example command-lines:
-
- jview ReadWeather readweather.dat
- jview ReadWeather readweather.dat proxysrv.com:8080 /tEST
-
- ------------------------------------------------------------------------------------------------------- */
-
- /////////////////////////////////////////////////////////////////////////////////////////////////////
-
- class ReadWeather
- {
- private static String m_sConfigFile;
- private static boolean m_bUseProxy = false;
- private static String m_sProxy;
- private static int m_iPort = 0;
- private static String m_sTimeZone;
-
-
- /////////////////////////////////////////////////////////////////////////////////////////////////////
-
- private static boolean parseArgs( String args[] )
- {
- String portstr, arg;
- int i, idx;
- char c;
-
- // get config file
- try
- {
- m_sConfigFile = args[0];
- }
- catch( IndexOutOfBoundsException e )
- {
- System.out.print( "Syntax error: No configuration file specified.\r\n" +
- " Usage: ReadWeather (config. file) [proxy:port]\r\n");
- return false;
- }
-
-
- // go through optional args
- try
- {
- i = 1;
- while ( true )
- {
- arg = args[i++];
- try
- {
- if ( arg.charAt( 0 ) == '/' )
- {
- c = arg.charAt( 1 );
- c = Character.toUpperCase(c);
- switch( c )
- {
- case 'T':
- m_sTimeZone = arg.substring( 2, arg.length() );
- break;
-
- default:
- break;
- }
- }
- else
- {
- // get optional proxy/port
- idx = arg.lastIndexOf( ':' );
- if ( idx < 1 )
- {
- System.out.print( "Syntax error: Invalid proxy:port expression.\r\n" +
- " Usage: ReadWeather (config. file) [proxy:port]\r\n");
- return false;
- }
-
- m_sProxy = arg.substring( 0, idx );
- portstr = arg.substring( idx+1, arg.length() );
- m_iPort = Integer.parseInt( portstr );
-
- m_bUseProxy = true;
- }
- } // try
- catch( StringIndexOutOfBoundsException e )
- {
- }
- } // while
- } // try
- catch( IndexOutOfBoundsException e )
- {
- // all done going through args array
- }
- catch( NumberFormatException e )
- {
- System.out.print( "Syntax error: Invalid proxy:port expression.\r\n" +
- " Usage: ReadWeather (config. file) [proxy:port]\r\n");
- return false;
- }
-
- return true;
- } // parseArgs
-
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Update 3 dataset files. The filenames are constructed using 'setfile' as the base
- // name, and then appending the extensions .TEM, .HUM, .PRE.
- // Method for each file:
- // - delete temp file (if present)
- // - create/open temp file
- // - open dataset file
- // - copy 1st line (caption) from dataset file to temp file.
- // - read 2nd line (column labels). append [tab] and current hour. write to temp file.
- // - read 3rd line (data values).
- // - append new temperature, pressure or humidity value to line, write to temp file
- // - close files.
- // - rename dataset to .BAK
- // - rename temp file to dataset file.
- // - delete .BAK file
-
- private static boolean updateDataset( String cityname, String setfile, GetWData reader )
- {
- int ctr, idx, itOffset, iHour, iAmPm;
- String dataName, tmpName, bakName;
- String line, value, ampm;
- File tmpFile, dataFile, bakFile;
-
- Date now;
- TimeZone tz = null;
- GregorianCalendar cal;
- SimpleDateFormat fmt;
-
-
- FileInputStream fis;
- FileOutputStream fos;
- DataInputStream dis;
- DataOutputStream dos;
- BufferedReader bdis;
-
- String ext[] = new String[3];
- ext[0] = new String( ".TEM" );
- ext[1] = new String( ".HUM" );
- ext[2] = new String( ".BAR" );
-
-
- try
- {
- // make local time, in hours
- if ( null != m_sTimeZone )
- tz = TimeZone.getTimeZone( m_sTimeZone );
- if ( null == tz )
- tz = TimeZone.getDefault();
- cal = new GregorianCalendar( tz );
- iHour = cal.get( Calendar.HOUR );
- if (0 == iHour )
- iHour = 12;
- iAmPm = cal.get( Calendar.AM_PM );
- if ( 0 == iAmPm )
- ampm = new String( "a" );
- else
- ampm = new String( "p" );
-
-
- // make name for temp. file
- tmpName = new String( setfile );
- idx = tmpName.lastIndexOf( '.' );
- if ( idx > 1 )
- tmpName = tmpName.substring( 0, idx );
- tmpName = tmpName + ".tmp";
-
- // make name for backup file
- bakName = new String( setfile );
- idx = bakName.lastIndexOf( '.' );
- if ( idx > 1 )
- bakName = bakName.substring( 0, idx );
- bakName = bakName + ".bak";
-
- // loop through set of 3 data files
- ctr = 0;
- while ( ctr < 3 )
- {
- // make name of dataset file
- dataName = new String( setfile );
- idx = dataName.lastIndexOf( '.' );
- if ( idx > 1 )
- dataName = dataName.substring( 0, idx );
- dataName = dataName + ext[ ctr ];
-
- // create files
- tmpFile = new File( tmpName );
- dataFile = new File( dataName );
-
- // delete temp. file
- if ( tmpFile.exists() )
- tmpFile.delete();
-
- // create some streams
- fis = new FileInputStream( dataFile );
- fos = new FileOutputStream( tmpFile );
- dis = new DataInputStream( fis );
- dos = new DataOutputStream( fos );
-
- bdis = new BufferedReader( new InputStreamReader( dis ) );
-
-
- // copy 1st line from input to output
- line = bdis.readLine();
- if ( line == null )
- return false;
- line += "\r\n";
- dos.writeBytes( line );
-
-
- // copy hours caption line, add new hour value
- // note that we preface the time with 'a' or 'p'
- // for am or pm.
- line = bdis.readLine();
- if ( line == null )
- return false;
- dos.writeBytes( line );
- value = String.valueOf( iHour );
- dos.writeBytes( "\t" + ampm + value + "\r\n" );
-
-
- //copy data line
- line = bdis.readLine();
- if ( line == null )
- return false;
- dos.writeBytes( line );
- dos.writeBytes( " " );
- switch( ctr )
- {
- case 0: value = reader.getTemperatureString(); break;
- case 1: value = reader.getHumidityString(); break;
- case 2: value = reader.getPressureString(); break;
- }
- dos.writeBytes( value + "\r\n" );
-
- // close files.
- bdis.close();
- dos.close();
-
- // rename dataset to .BAK
- bakFile = new File( bakName );
- dataFile.renameTo( bakFile );
-
- // rename temp file to dataset file.
- dataFile = new File( dataName );
- tmpFile.renameTo( dataFile );
-
- // delete .BAK file
- bakFile.delete();
-
- // bump counter
- ctr++;
- } // while ctr<3
-
- return true;
- } // try
- catch( Exception e )
- {
- System.out.print( "Exception in updateDataset() : " + e.getMessage() + "\r\n");
-
- return false;
- }
- } // updateDataset
-
-
-
-
- public static void main(String args[])
- {
- GetWData reader;
-
-
- // parse arguments
- if ( !parseArgs( args ) )
- return;
-
- // open config file
- FileInputStream fin;
- DataInputStream din;
- BufferedReader bdin;
-
- try
- {
- fin = new FileInputStream( m_sConfigFile );
- }
- catch( FileNotFoundException e )
- {
- System.out.print( "Syntax error: Config. file [" + m_sConfigFile + "] not found.\r\n" +
- " Usage: ReadWeather (config. file) [proxy:port]\r\n");
- return;
- }
-
- // the config. file has lines with format:
- // (city name)|(dataset filename)|URL
- // For each line read, update the specified dataset file based on the URL
-
- int cityidx, fileidx, iSetsUpdated = 0;
- String dataline, setfile, seturl, cityname;
-
- try
- {
- din = new DataInputStream( fin );
- bdin = new BufferedReader( new InputStreamReader( din ) );
-
- while ( true )
- {
- // dataline = din.readLine();
- dataline = bdin.readLine();
- if ( null == dataline )
- break;
-
- cityidx = dataline.indexOf( '|' );
- if ( cityidx < 1 )
- {
- System.out.print( "ERROR: Invalid config. file entry [" + dataline + "]\r");
- continue;
- }
- cityname = dataline.substring( 0, cityidx );
-
- fileidx = dataline.indexOf( '|', cityidx+1 );
- if ( fileidx < 1 )
- {
- System.out.print( "ERROR: Invalid config. file entry [" + dataline + "]\r");
- continue;
- }
- setfile = dataline.substring( cityidx+1, fileidx );
- seturl = dataline.substring( fileidx+1, dataline.length() );
- setfile = setfile.trim();
- seturl = seturl.trim();
-
- if ( m_bUseProxy )
- reader = new GetWDataProxy( seturl, m_sProxy, m_iPort );
- else
- reader = new GetWDataDirect( seturl );
-
- try
- {
- reader.read();
- updateDataset( cityname, setfile, reader );
-
- System.out.print( cityname + "\t\t" +
- "Temperature: " + reader.getTemperatureString() +
- "\tHumidity: " + reader.getHumidityString() +
- "\tPressure: " + reader.getPressureString() + "\r\n" );
-
- iSetsUpdated++;
- }
- catch( MalformedURLException e )
- {
- System.out.print( "ERROR: " + e.getMessage() +
- "\r\nProcessing line [" + dataline + "]\r\n" );
- }
- catch( IOException e )
- {
- System.out.print( "ERROR: " + e.getMessage() +
- "\r\nProcessing line [" + dataline + "]\r\n" );
- }
- } // while
-
- System.out.print( "\r\nComplete.\r\n" );
- System.out.print( iSetsUpdated + " date-set file(s) updated.\r\n\r\n" );
- } // try
- catch( IOException e )
- {
- System.out.print( "ERROR: " + e.getMessage() + "]\r\n" );
- }
-
- } // main
-
- } // ReadWeather
-
-
-
- // end of ReadWeather.java
- //////////////////////////////
-