home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1997 October
/
Chip_1997-10_cd.bin
/
tema
/
sybase
/
powerj
/
samples.z
/
URLList.java
< prev
next >
Wrap
Text File
|
1996-12-16
|
9KB
|
323 lines
/*
* URLList.java
*
*
* Copyright 1996 Sybase, Inc. All rights reserved.
*/
import java.net.*;
import java.util.*;
import java.io.IOException;
/**
* URLList stores a list of URLs on the server that will periodically
* be polled to see if their contents have been modified since the
* last saved modified date.
*/
public class URLList
//******************
{
//-------------------------------------------------------------
// Public Methods
//-------------------------------------------------------------
/**
* Add a URL to the list
* @param URLStr URL to be added
*/
public void addURL( String URLStr )
//*********************************
{
// Create a new URL and add it to the list
// NOTE: Use lower case URL as key for hashtable
try {
URL aURL = new URL( URLStr );
String urlKey = aURL.toString().toLowerCase();
URLItem newURL = new URLItem( aURL, URLStatus.UNKNOWN );
synchronized( _list ) {
_list.put( urlKey, newURL );
}
} catch( MalformedURLException e ) {
}
}
/**
* Poll URLs that are currently stored in the list
*/
public void pollURLs()
//********************
{
try {
synchronized( _list ) {
for( Enumeration e = getElements(); e.hasMoreElements(); ) {
URLItem oneItem = ( URLItem ) e.nextElement();
URL url = oneItem.getURL();
String urlKey = url.toString().toLowerCase();
Date savedDate = oneItem.getDate();
URLConnection urlConn = url.openConnection();
long lastModified = urlConn.getLastModified();
Date newDate = new Date( lastModified );
URLItem newItem = null;
// Check if lastModified dates are unknown. If so, set the
// status to UNKNOWN. If lastModified dates are known, then
// check if URL has been modified since the saved date or if
// the dates of last modification are equal; then set the
// status to NEW or UNCHANGED, respectively
if( savedDate == null || lastModified == 0 ) {
newItem = new URLItem( url, null, URLStatus.UNKNOWN );
} else if( newDate.after( savedDate ) ) {
newItem = new URLItem( url, newDate, URLStatus.NEW );
} else if( newDate.equals( savedDate ) ) {
newItem = new URLItem( url, newDate, URLStatus.UNCHANGED );
}
// Replace the old URL
_list.put( urlKey, newItem );
}
}
} catch( IOException e ) {
}
}
/**
* Start the polling thread
*/
public void startPolling()
//************************
{
// Check if the polling thread is running;
// start it if it isn't
if( !_urlPoll.isAlive() ) {
_urlPoll.setURLList( this );
_urlPoll.start();
}
}
/*
* Stop the polling thread
*/
public void finalize()
//********************
{
if( _urlPoll.isAlive() ) {
_urlPoll.stop();
}
}
//-------------------------------------------------------------
// Properties
//-------------------------------------------------------------
/**
* Returns the number of URLs currently stored in the list
*/
public int getSize()
//******************
{
return _list.size();
}
/**
* Returns an Enumeration of the URLs stored in the list;
* includes the URLs, last modified date and status
*/
public Enumeration getElements()
//******************************
{
return _list.elements();
}
/**
* Returns an Enumeration of the URLs stored in the list;
* includes ONLY the URLs and their status
*/
public Enumeration getURLs()
//**************************
{
Vector statusList = new Vector();
synchronized( _list ) {
for( Enumeration e = getElements(); e.hasMoreElements(); ) {
URLStatus oneItem = ( URLStatus ) e.nextElement();
statusList.addElement( oneItem );
}
}
return statusList.elements();
}
/**
* Returns the polling interval, i.e. time between polls
*/
public long getPollInterval()
//***************************
{
return _urlPoll.getSleepTime();
}
//-------------------------------------------------------------
// Data
//-------------------------------------------------------------
static private Hashtable _list = new Hashtable();
static private URLPollingThread _urlPoll = new URLPollingThread();
}
/*
* URLStatus stores a URL and its status since the last poll
*/
class URLStatus
//*************
{
public static final int NEW = 1;
public static final int UNCHANGED = 2;
public static final int UNKNOWN = 3;
//-------------------------------------------------------------
// Constructor
//-------------------------------------------------------------
public URLStatus( URL aURL, int aStatus )
//***************************************
{
_url = aURL;
_status = aStatus;
}
//-------------------------------------------------------------
// Properties
//-------------------------------------------------------------
/*
* Returns the URL
*/
public URL getURL()
//*****************
{
return _url;
}
/*
* Returns the status since the last poll of this URL
*/
public int getStatus()
//********************
{
return _status;
}
/*
* Sets the status of this URL
*/
public void setStatus( int aStatus )
//**********************************
{
_status = aStatus;
}
//-------------------------------------------------------------
// Data
//-------------------------------------------------------------
protected URL _url = null;
protected int _status = 0;
}
/*
* URLItem stores a URL, its last modified date, and its status
* since the last poll
*/
class URLItem extends URLStatus
//*****************************
{
//-------------------------------------------------------------
// Constructor
//-------------------------------------------------------------
public URLItem( URL aURL, int aStatus )
//*************************************
{
super( aURL, aStatus );
Date newDate = null;
try {
// Determine the last modified date of this URL
URLConnection urlConnection = aURL.openConnection();
long lastModified = urlConnection.getLastModified();
if( lastModified != 0 ) {
newDate = new Date( lastModified );
super.setStatus( URLStatus.UNCHANGED );
}
} catch( IOException e ) {
}
_date = newDate;
}
public URLItem( URL aURL, Date aDate, int aStatus )
//*************************************************
{
super( aURL, aStatus );
_date = aDate;
}
//-------------------------------------------------------------
// Properties
//-------------------------------------------------------------
/*
* Returns the (stored) last modified date of this URL
*/
public Date getDate()
//*******************
{
return _date;
}
/*
* Sets the last modified date for this URL
*/
public void setDate( Date aDate )
//*******************************
{
_date = aDate;
}
//-------------------------------------------------------------
// Data
//-------------------------------------------------------------
protected Date _date = null;
}