home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / samples.z / URLList.java < prev    next >
Text File  |  1996-12-16  |  9KB  |  323 lines

  1. /*
  2.  * URLList.java
  3.  *
  4.  *
  5.  * Copyright 1996 Sybase, Inc. All rights reserved.
  6.  */
  7.  
  8.  
  9. import java.net.*;
  10. import java.util.*;
  11. import java.io.IOException;
  12.  
  13.  
  14. /**
  15.  * URLList stores a list of URLs on the server that will periodically
  16.  * be polled to see if their contents have been modified since the
  17.  * last saved modified date.  
  18.  */
  19.  
  20. public class URLList
  21. //******************
  22. {
  23.     
  24.     
  25.     //-------------------------------------------------------------
  26.     // Public Methods
  27.     //-------------------------------------------------------------
  28.  
  29.     /**
  30.      * Add a URL to the list
  31.      * @param URLStr URL to be added
  32.      */
  33.  
  34.     public void addURL( String URLStr )
  35.     //*********************************
  36.     {        
  37.         // Create a new URL and add it to the list
  38.         // NOTE: Use lower case URL as key for hashtable
  39.         try {            
  40.             URL aURL = new URL( URLStr );
  41.             String urlKey = aURL.toString().toLowerCase();
  42.             URLItem newURL = new URLItem( aURL, URLStatus.UNKNOWN );        
  43.             
  44.             synchronized( _list ) {
  45.                 _list.put( urlKey, newURL );                  
  46.             }
  47.         
  48.         } catch( MalformedURLException e ) {
  49.         } 
  50.     }
  51.     
  52.     /**
  53.      * Poll URLs that are currently stored in the list
  54.      */
  55.     
  56.     public void pollURLs()
  57.     //********************
  58.     {
  59.         try {
  60.             synchronized( _list ) {
  61.                 for( Enumeration e = getElements(); e.hasMoreElements(); ) { 
  62.                     URLItem oneItem         = ( URLItem ) e.nextElement();
  63.                     URL url                 = oneItem.getURL();
  64.                     String urlKey           = url.toString().toLowerCase();
  65.                     Date savedDate          = oneItem.getDate();
  66.                     URLConnection urlConn   = url.openConnection();                
  67.                     long lastModified       = urlConn.getLastModified();
  68.                     Date newDate            = new Date( lastModified );
  69.                     URLItem newItem         = null;
  70.                     
  71.                     // Check if lastModified dates are unknown.  If so, set the
  72.                     // status to UNKNOWN.  If lastModified dates are known, then
  73.                     // check if URL has been modified since the saved date or if
  74.                     // the dates of last modification are equal; then set the
  75.                     // status to NEW or UNCHANGED, respectively
  76.                     if( savedDate == null || lastModified == 0 ) {
  77.                         newItem = new URLItem( url, null, URLStatus.UNKNOWN );                  
  78.                     } else if( newDate.after( savedDate ) ) {                    
  79.                         newItem = new URLItem( url, newDate, URLStatus.NEW ); 
  80.                     } else if( newDate.equals( savedDate ) ) {
  81.                         newItem = new URLItem( url, newDate, URLStatus.UNCHANGED );                                
  82.                     } 
  83.                     
  84.                     // Replace the old URL
  85.                     _list.put( urlKey, newItem );                                        
  86.                 }                
  87.             }                       
  88.         } catch( IOException e ) {
  89.         }
  90.     }   
  91.     
  92.     /**
  93.      * Start the polling thread
  94.      */
  95.     
  96.     public void startPolling()
  97.     //************************
  98.     {
  99.         // Check if the polling thread is running;
  100.         // start it if it isn't
  101.         if( !_urlPoll.isAlive() ) {
  102.             _urlPoll.setURLList( this );
  103.             _urlPoll.start();
  104.         }
  105.     }
  106.     
  107.     
  108.     /*
  109.      * Stop the polling thread
  110.      */
  111.      
  112.     public void finalize()
  113.     //********************
  114.     {
  115.         if( _urlPoll.isAlive() ) {
  116.             _urlPoll.stop();
  117.         }
  118.     }
  119.             
  120.  
  121.     //-------------------------------------------------------------
  122.     // Properties
  123.     //-------------------------------------------------------------
  124.  
  125.     /**
  126.      * Returns the number of URLs currently stored in the list
  127.      */
  128.      
  129.     public int getSize() 
  130.     //******************
  131.     {       
  132.         return _list.size();
  133.     }
  134.  
  135.     /**
  136.      * Returns an Enumeration of the URLs stored in the list;
  137.      * includes the URLs, last modified date and status
  138.      */
  139.      
  140.     public Enumeration getElements()
  141.     //******************************
  142.     {
  143.         return _list.elements();    
  144.     }
  145.     
  146.     /**
  147.      * Returns an Enumeration of the URLs stored in the list;
  148.      * includes ONLY the URLs and their status
  149.      */
  150.       
  151.     public Enumeration getURLs()
  152.     //**************************
  153.     {
  154.         Vector statusList = new Vector();
  155.         synchronized( _list ) {
  156.             for( Enumeration e = getElements(); e.hasMoreElements(); ) { 
  157.                 URLStatus oneItem       = ( URLStatus ) e.nextElement();
  158.                 statusList.addElement( oneItem );
  159.             }
  160.         }
  161.         
  162.         return statusList.elements();         
  163.     }
  164.     
  165.     /**
  166.      * Returns the polling interval, i.e. time between polls
  167.      */
  168.     
  169.     public long getPollInterval()
  170.     //***************************
  171.     {
  172.         return _urlPoll.getSleepTime();
  173.     }
  174.         
  175.  
  176.     //-------------------------------------------------------------
  177.     // Data
  178.     //-------------------------------------------------------------
  179.     
  180.     static private Hashtable _list = new Hashtable();
  181.     static private URLPollingThread _urlPoll = new URLPollingThread();
  182. }
  183.  
  184.  
  185.  
  186. /*
  187.  * URLStatus stores a URL and its status since the last poll
  188.  */
  189.  
  190. class URLStatus
  191. //*************
  192. {
  193.     public static final int NEW       = 1;
  194.     public static final int UNCHANGED = 2;
  195.     public static final int UNKNOWN   = 3;
  196.     
  197.     //-------------------------------------------------------------
  198.     // Constructor
  199.     //-------------------------------------------------------------
  200.  
  201.     public URLStatus( URL aURL, int aStatus ) 
  202.     //***************************************
  203.     {
  204.        _url = aURL;
  205.        _status = aStatus;
  206.     }
  207.     
  208.     //-------------------------------------------------------------
  209.     // Properties
  210.     //-------------------------------------------------------------
  211.         
  212.     /*
  213.      * Returns the URL
  214.      */
  215.      
  216.     public URL getURL()
  217.     //*****************
  218.     {
  219.         return _url;
  220.     }
  221.     
  222.     
  223.     /* 
  224.      * Returns the status since the last poll of this URL
  225.      */
  226.      
  227.     public int getStatus()
  228.     //********************
  229.     {
  230.         return _status;
  231.     }
  232.     
  233.     /*
  234.      * Sets the status of this URL
  235.      */
  236.      
  237.     public void setStatus( int aStatus )
  238.     //**********************************
  239.     {
  240.         _status = aStatus;
  241.     }
  242.     
  243.     
  244.     //-------------------------------------------------------------
  245.     // Data
  246.     //-------------------------------------------------------------
  247.     
  248.     protected URL _url = null;
  249.     protected int _status = 0;
  250. }
  251.     
  252.  
  253.  
  254. /*
  255.  * URLItem stores a URL, its last modified date, and its status
  256.  * since the last poll
  257.  */
  258.  
  259. class URLItem extends URLStatus
  260. //*****************************
  261. {        
  262.     
  263.     //-------------------------------------------------------------
  264.     // Constructor
  265.     //-------------------------------------------------------------
  266.     
  267.     public URLItem( URL aURL, int aStatus )
  268.     //*************************************
  269.     {    
  270.         super( aURL, aStatus );
  271.         Date newDate = null;
  272.         try {
  273.             // Determine the last modified date of this URL
  274.             URLConnection urlConnection = aURL.openConnection();
  275.             long lastModified = urlConnection.getLastModified();
  276.             if( lastModified != 0 ) {
  277.                 newDate = new Date( lastModified );
  278.                 super.setStatus( URLStatus.UNCHANGED );
  279.             }            
  280.         } catch( IOException e ) {
  281.         }
  282.         _date = newDate;  
  283.     }
  284.     
  285.     public URLItem( URL aURL, Date aDate, int aStatus )
  286.     //*************************************************
  287.     {
  288.         super( aURL, aStatus );
  289.         _date = aDate;
  290.     }
  291.     
  292.     //-------------------------------------------------------------
  293.     // Properties
  294.     //-------------------------------------------------------------
  295.     
  296.     /*
  297.      * Returns the (stored) last modified date of this URL
  298.      */
  299.      
  300.     public Date getDate()
  301.     //*******************
  302.     {
  303.         return _date;
  304.     }        
  305.     
  306.     /*
  307.      * Sets the last modified date for this URL
  308.      */
  309.     
  310.     public void setDate( Date aDate )
  311.     //*******************************
  312.     {
  313.         _date = aDate;
  314.     }
  315.     
  316.     //-------------------------------------------------------------
  317.     // Data
  318.     //-------------------------------------------------------------
  319.     
  320.     protected Date _date = null;
  321. }
  322.  
  323.