Clover coverage report -
Coverage timestamp: Do Okt 21 2004 12:21:23 CEST
file stats: LOC: 254   Methods: 12
NCLOC: 162   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
URLFileWatcher.java 0% 0% 0% 0%
coverage
 1    package org.jconfig;
 2   
 3    import java.io.IOException;
 4    import java.io.InputStream;
 5    import java.net.MalformedURLException;
 6    import java.net.URL;
 7    import java.net.URLConnection;
 8    import java.util.Iterator;
 9    import java.util.List;
 10    import java.util.Properties;
 11    import java.util.Vector;
 12   
 13    import org.jconfig.event.FileListener;
 14    import org.jconfig.event.FileListenerEvent;
 15   
 16    /**
 17    * A class which implements an event dispatching mechanism to all
 18    * classes supporting the FileListener interface. This class will
 19    * notify all FileListeners when the configuration changes. Once
 20    * the URLFileWatcher has been shutdown, the class needs to be
 21    * reinstanciated and restarted.
 22    *
 23    * @author Steve Braun <steve.braun@cogeco.ca>
 24    * @see org.jconfig.FileWatcher
 25    */
 26    public class URLFileWatcher extends Thread {
 27   
 28    private List fileListenerList;
 29    private volatile Thread watcher;
 30    private int interval = 10000;
 31    private long lastmodified;
 32   
 33    private java.net.URL configURL = null;
 34    private String url;
 35    //private InputStream is = null;
 36    private URLConnection con = null;
 37   
 38    /**
 39    * Constructs a FileWatcher watching the specified File
 40    *
 41    * @param url The File to be watched
 42    */
 43  0 public URLFileWatcher( String url ) throws FileWatcherException
 44    {
 45  0 if( url == null ) {
 46  0 throw new NullPointerException("URL cannot be <null>");
 47    }
 48   
 49  0 this.url = url;
 50   
 51  0 try
 52    {
 53  0 ClassLoader cl = this.getClass().getClassLoader();
 54  0 InputStream jcf = cl.getResourceAsStream( "jconfig.properties" );
 55    // it is possible that the jconfig.properties does not exist, we get null
 56  0 if ( jcf != null )
 57    {
 58  0 Properties jcfProperties = new Properties();
 59  0 jcfProperties.load( jcf );
 60   
 61    // load what is set in system
 62  0 Properties prop = System.getProperties();
 63    // if we see http.proxyHost and/or http.proxyPort inside
 64    // the jconfig.properties, we can set the System.properties
 65    // for use by the URLConnection object
 66  0 if ( jcfProperties.getProperty( "http.proxyHost" ) != null )
 67  0 prop.put( "http.proxyHost", jcfProperties.getProperty( "http.proxyHost" ) );
 68  0 if ( jcfProperties.getProperty( "http.proxyPort" ) != null )
 69  0 prop.put( "http.proxyPort", jcfProperties.getProperty( "http.proxyPort" ) );
 70   
 71  0 if ( jcfProperties.getProperty( "watcher.interval" ) != null )
 72    {
 73  0 try
 74    {
 75  0 setInterval( Integer.parseInt( jcfProperties.getProperty( "watcher.interval" ) ) );
 76    }
 77    catch ( NumberFormatException nfe )
 78    {
 79  0 throw new FileWatcherException( nfe.getMessage() );
 80    }
 81    }
 82    }
 83    }
 84    catch ( IOException ioe )
 85    {
 86  0 throw new FileWatcherException( ioe.getMessage() );
 87    }
 88   
 89   
 90  0 try
 91    {
 92  0 this.configURL = new URL( url );
 93  0 this.con = this.configURL.openConnection();
 94  0 this.lastmodified = con.getLastModified();
 95  0 this.fileListenerList = new Vector();
 96    }
 97    catch ( MalformedURLException m )
 98    {
 99  0 throw new FileWatcherException( m.getMessage() );
 100    }
 101    catch ( IOException ioe )
 102    {
 103  0 throw new FileWatcherException( ioe.getMessage() );
 104    }
 105    }
 106   
 107    /**
 108    * Adds FileListener
 109    *
 110    * @param fileListener The FileListener
 111    */
 112  0 public void addFileListener( FileListener fileListener ) {
 113  0 fileListenerList.add( fileListener );
 114    }
 115   
 116    /**
 117    * Set the timer interval. The default is 10 seconds
 118    *
 119    * @param seconds The number of seconds to set the interval when
 120    * to check for the changes to the file.
 121    */
 122  0 public void setInterval( int seconds ) {
 123  0 this.interval = seconds*1000;
 124    }
 125   
 126    /**
 127    * Tell thread to stop watching. Currently once a Thread is started
 128    * and stopped, a new FileWatcher will be required.
 129    */
 130  0 public void stopWatching() {
 131  0 this.watcher = null;
 132    }
 133   
 134    /**
 135    * Start the Thread on its journey to scan for changes to the
 136    * file it is watching.
 137    */
 138  0 public void start() {
 139  0 watcher = new Thread( this );
 140  0 watcher.setDaemon( true );
 141  0 watcher.start();
 142    }
 143   
 144    /**
 145    * Start the thread to call checkFile()
 146    */
 147  0 public void run()
 148    {
 149   
 150  0 Thread thisThread = Thread.currentThread();
 151  0 while( thisThread == watcher )
 152    {
 153  0 try
 154    {
 155  0 Thread.sleep( interval );
 156    }
 157    catch ( InterruptedException e )
 158    {
 159    // can't do much from here with Exception
 160  0 watcher = null;
 161    }
 162   
 163  0 try
 164    {
 165  0 checkURLFile();
 166    }
 167    catch( Exception e )
 168    {
 169    //e.printStackTrace();
 170    // can't do much from here with Exception
 171  0 watcher = null;
 172    }
 173    }
 174    }
 175   
 176    /**
 177    * Retrieve an array of FileListeners.
 178    *
 179    * @return FileListeners as array of FileListener
 180    */
 181  0 public FileListener[] getFileListeners()
 182    {
 183  0 return ( FileListener[] ) fileListenerList.toArray();
 184    }
 185   
 186    /* allows us to update the File object, in case we need to. */
 187    /**
 188    * Sets a new File to be watched. This causes the FileWatcher to watch
 189    * the given File and disregard the File that was used during Construction.
 190    *
 191    * @param url The File to be watched
 192    */
 193  0 public void setURL( String url ) throws FileWatcherException
 194    {
 195  0 if ( url == null ) throw new NullPointerException( "url cannot be <null>" );
 196   
 197  0 this.url = url;
 198   
 199  0 try
 200    {
 201  0 this.configURL = new URL( url );
 202    }
 203    catch ( MalformedURLException e )
 204    {
 205  0 throw new FileWatcherException( e.getMessage() );
 206    }
 207    }
 208   
 209  0 public String getURLString()
 210    {
 211  0 return ( url );
 212    }
 213   
 214  0 public URL getURL()
 215    {
 216  0 return ( configURL );
 217    }
 218   
 219  0 public void setURL( URL configURL )
 220    {
 221  0 if ( configURL == null ) throw new NullPointerException( "URL cannot be <null>" );
 222   
 223  0 this.configURL = configURL;
 224    }
 225   
 226    /* looks at the internal FileListenerList and keeps track of changed info */
 227  0 private void checkURLFile() throws FileWatcherException
 228    {
 229  0 try
 230    {
 231  0 URL u = configURL;
 232  0 URLConnection uConn = u.openConnection();
 233   
 234  0 long mod = uConn.getLastModified();
 235   
 236  0 if( mod > lastmodified )
 237    {
 238  0 lastmodified = mod;
 239  0 Iterator iterator = fileListenerList.iterator();
 240  0 while( iterator.hasNext() )
 241    {
 242  0 FileListener listener = (FileListener)iterator.next();
 243    //listener.fileChanged( new FileListenerEvent( newFile ) );
 244  0 listener.fileChanged( new FileListenerEvent( null ) );
 245    }
 246    }
 247    }
 248    catch ( IOException ioe )
 249    {
 250  0 throw new FileWatcherException( ioe.getMessage() );
 251    }
 252    }
 253   
 254    }