Clover coverage report - Alipes Project - 0.1
Coverage timestamp: Sat Sep 10 2005 21:25:58 BST
file stats: LOC: 186   Methods: 7
NCLOC: 77   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
URLMonitor.java 0% 0% 0% 0%
coverage
 1    /*
 2    * $Id: URLMonitor.java,v 1.1 2004/03/17 21:18:02 johndavidtaylor Exp $
 3    * Created
 4    * on Feb 19, 2004 by John Taylor jdt@roe.ac.uk .
 5    *
 6    */
 7    package org.abraracourcix.alipes.monitors.url;
 8    import java.io.IOException;
 9    import java.net.URL;
 10    import java.util.HashMap;
 11    import java.util.Iterator;
 12    import java.util.Map;
 13    import java.util.Set;
 14    import org.abraracourcix.alipes.listeners.Listener;
 15    import org.abraracourcix.alipes.listeners.ListenerException;
 16    import org.abraracourcix.alipes.monitors.PollingMonitor;
 17   
 18    /**
 19    * Concrete implementation of a PollingMonitor. Given a URL or a
 20    * collection of URLs it checks their status.
 21    * @author jdt
 22    */
 23    public class URLMonitor extends PollingMonitor {
 24    /**
 25    * Commons-logging logger
 26    */
 27    private static org.apache.commons.logging.Log log =
 28    org.apache.commons.logging.LogFactory.getLog(URLMonitor.class);
 29    /**
 30    * Collection of URLs we're interested in
 31    */
 32    private Map urls = new HashMap();
 33    /**
 34    * Constructor
 35    *
 36    * @param listener
 37    * who's listening
 38    * @param pollingFrequency
 39    * in seconds
 40    */
 41  0 public URLMonitor(final Listener listener, final long pollingFrequency) {
 42  0 super(listener, pollingFrequency);
 43    }
 44    /**
 45    * Constructor
 46    *
 47    * @param listener
 48    * who's listening
 49    * @param pollingFrequency
 50    * in seconds
 51    * @param url
 52    * the url to poll
 53    */
 54  0 public URLMonitor(
 55    final Listener listener,
 56    final long pollingFrequency,
 57    final URL url) {
 58  0 this(listener, pollingFrequency);
 59  0 addNewURL(url);
 60    }
 61    /**
 62    * Add a new URL to the list we monitor
 63    *
 64    * @param url
 65    * url to add
 66    */
 67  0 public final void addNewURL(final URL url) {
 68  0 log.info("addNewURL: url=" + url);
 69  0 urls.put(url, Status.UNKNOWN);
 70    }
 71    /**
 72    * Constructor defaults to polling every 60 seconds
 73    *
 74    * @param listener
 75    * who's listening
 76    * @param url
 77    * the url to poll
 78    */
 79  0 public URLMonitor(final Listener listener, final URL url) {
 80  0 this(listener, 60, url);
 81    }
 82    /**
 83    * Pole the URLs
 84    * @TODO deal with exceptions
 85    * @see org.abraracourcix.alipes.monitors.AbstractMonitor#pole()
 86    */
 87  0 public final void pole() {
 88  0 final Set keys = urls.keySet();
 89  0 final Iterator it = keys.iterator();
 90  0 while (it.hasNext()) {
 91  0 final URL url = (URL) it.next();
 92  0 final Status oldStatus = (Status) urls.get(url);
 93  0 log.debug("Checking status of " + url);
 94  0 log.debug("Previous status: " + oldStatus);
 95  0 Status newStatus;
 96  0 try {
 97  0 url.openStream().close();
 98  0 newStatus = Status.UP;
 99    } catch (IOException io) {
 100  0 newStatus = Status.DOWN;
 101    }
 102  0 log.debug("Current status: " + newStatus);
 103  0 try {
 104  0 if (oldStatus == Status.DOWN && newStatus == Status.UP) {
 105  0 signalListener(URLEvent.GONE_UP, url);
 106    }
 107  0 if (oldStatus == Status.UP && newStatus == Status.DOWN) {
 108  0 signalListener(URLEvent.GONE_DOWN, url);
 109    }
 110  0 if (newStatus == Status.UP) {
 111  0 signalListener(URLEvent.UP, url);
 112    } else {
 113  0 signalListener(URLEvent.DOWN, url);
 114    }
 115    } catch (ListenerException le) {
 116   
 117    }
 118  0 urls.put(url, newStatus);
 119    }
 120    }
 121   
 122    /**
 123    * The status of a URL: up, down or unknown.
 124    * @author jdt
 125    */
 126    private static final class Status {
 127    /**
 128    * Java enum value
 129    */
 130    public static final Status UP = new Status("up");
 131    /**
 132    * Java enum value
 133    */
 134    public static final Status DOWN = new Status("down");
 135    /**
 136    * Java enum value
 137    */
 138    public static final Status UNKNOWN = new Status("unknown");
 139    /**
 140    *
 141    * Constructor
 142    * @param string human readable representation of this status
 143    */
 144  0 private Status(final String string) {
 145  0 this.string = string;
 146    }
 147    /**
 148    * description
 149    */
 150    private String string;
 151    /**
 152    * return description
 153    * @return description
 154    */
 155  0 public String toString() {
 156  0 return string;
 157    }
 158    };
 159    }
 160    /*
 161    * $Log: URLMonitor.java,v $
 162    * Revision 1.1 2004/03/17 21:18:02 johndavidtaylor
 163    * Copied across from incubation in beanpeeler
 164    *
 165    * Revision 1.6 2004/03/14 18:49:14 johndavidtaylor
 166    * corrected comments
 167    *
 168    * Revision 1.5 2004/03/14 00:44:22 johndavidtaylor
 169    * Added ListenerException
 170    *
 171    * Revision 1.4 2004/03/11 15:46:35 johndavidtaylor
 172    * Applied coding standards
 173    * Revision 1.3 2004/03/11 11:33:45 johndavidtaylor
 174    * Refactored the way that the PollingMonitor worked. Previously it got started
 175    * in its ctor, but this allowed access to subclass resources that hadn't yet
 176    * been initialised. Bad. Now needs to be started explicitly post-construction.
 177    *
 178    * Revision 1.2 2004/03/08 20:16:45 johndavidtaylor refactored to new package
 179    * name
 180    *
 181    * Revision 1.1 2004/03/08 19:58:25 johndavidtaylor Initial commit -transfer
 182    * from previous repository
 183    *
 184    * Revision 1.1 2004/03/02 23:04:37 jdt new
 185    *
 186    */