home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 October
/
PCWorld_1998-10_cd.bin
/
software
/
prehled
/
lotus
/
eSuite.exe
/
eSuiteDPP
/
samples
/
weatherchart
/
CopyWeather.java
< prev
next >
Wrap
Text File
|
1998-01-09
|
8KB
|
252 lines
/*
-----------------------------------------------------------------------------------------------------
Description: CopyWeather
Author: JDonohue
Created: June, 1997
Copyright (c)1992-1997 Lotus Development Corp. All Rights Reserved.
-----------------------------------------------------------------------------------------------------
*/
/*******************************************************************************
Change History:
$Log: //reebok/xyzL/JavaComp/webpack/samples/weatherchart/CopyWeather.java $
Rev 1.0.1.1 09 Jan 1998 10:52:02 jdonohue
Change package name to samples.weatherchart
Rev 1.0 05 Jan 1998 12:55:08 jdonohue
Initial Revision
*******************************************************************************/
package samples.weatherchart;
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import java.lang.Exception;
/* ------------------------------------------------------------------------------------------------------
CopyWeather 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. CopyWeather should be scheduled to run once
an hour;
Syntax for invoking CopyWeather is as follows:
(jvm) CopyWeather config [proxy:port] [/tzone]
(jvm) Java virtual machine which loads the application; either java or jview.
CopyWeather Name of class.
config Name of configuration file (see below).
[proxy:port] Optional. If specified, CopyWeather 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 CopyWeather copyweather.dat
jview CopyWeather copyweather.dat proxysrv.com:8080 /tEST
------------------------------------------------------------------------------------------------------- */
/////////////////////////////////////////////////////////////////////////////////////////////////////
class CopyWeather
{
private static String m_sConfigFile = null;
private static boolean m_bUseProxy = false;
private static String m_sProxy = null;
private static int m_iPort = 0;
private static String m_sTimeZone;
private static String m_sURL = null;
private static BufferedInputStream m_bistr = null;
private static DataOutputStream m_dostr = null;
/////////////////////////////////////////////////////////////////////////////////////////////////////
private static void close() throws IOException
{
if (m_bistr != null) m_bistr.close();
if (m_dostr != null) m_dostr.close();
m_bistr = null;
m_dostr = null;
}
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: CopyWeather (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: CopyWeather (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: CopyWeather (config. file) [proxy:port]\r\n");
return false;
}
return true;
} // parseArgs
/**
* Opens the weather data at URL 'sURL' and reads the data into input stream
* buffer 'm_bistr'
*/
private static void OpenWDataDirect( String sURL )
throws IOException, MalformedURLException
{
m_sURL = new String( sURL );
URL url;
URLConnection uconn;
url = new URL( m_sURL );
uconn = url.openConnection();
uconn.setDoOutput( true );
uconn.setDoInput(true);
uconn.connect();
m_bistr = new BufferedInputStream( uconn.getInputStream() );
}
/**
* Opens the weather data at URL 'sURL' through proxy 'sProxy' at port 'port'
* and reads the data into input stream buffer 'm_bistr'
*/
private static void OpenWDataProxy( String sURL, String sProxy, int port )
throws IOException, MalformedURLException
{
m_sURL = new String( sURL );
m_sProxy = new String( sProxy );
m_iPort = port;
String request = new String( "GET " + m_sURL + " HTTP/1.0\r\n\r\n" );
Socket sock = new Socket( m_sProxy, m_iPort );
m_bistr = new BufferedInputStream( sock.getInputStream() );
m_dostr = new DataOutputStream( sock.getOutputStream() );
m_dostr.writeBytes( request );
}
public static void main(String args[])
{
int BUFSIZE = 10000;
// parse arguments
if ( !parseArgs( args ) )
return;
// The config. file has lines containing URLs corresponding to the cities
// For each line read, update the specified dataset file based on the URL
int iSetsUpdated = 0;
String dataline, setfile, seturl;
try
{
FileInputStream fin = new FileInputStream(m_sConfigFile);
DataInputStream din = new DataInputStream( fin );
BufferedReader bdin = new BufferedReader(new InputStreamReader(din));
byte[] b = new byte[BUFSIZE];
while ( true )
{
dataline = bdin.readLine();
if ( null == dataline )
break;
// File name is curcond+city acronym, e.g., "curcond.lga"
setfile = dataline.substring(0, dataline.lastIndexOf("/") );
int ib = setfile.lastIndexOf("/")+1;
setfile = "currcond." + setfile.substring(ib);
seturl = dataline;
setfile = setfile.trim();
seturl = seturl.trim();
// Opens the URL, reads data into member 'm_bistr'
if ( m_bUseProxy )
OpenWDataProxy( seturl, m_sProxy, m_iPort );
else
OpenWDataDirect( seturl );
// Read the stream contents and write to the output stream
FileOutputStream fo = new FileOutputStream(setfile);
int i;
while ( (i = m_bistr.read(b, 0, BUFSIZE)) != -1)
{
if (i == BUFSIZE)
throw new IOException("Buffer size exceeded");
fo.write(b, 0, i);
}
fo.close();
// Close the member streams, if opened
close();
iSetsUpdated++;
} // 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
} // CopyWeather
// end of CopyWeather.java
//////////////////////////////