001: /*
002: * $Id: HTTPFileWatcher.java,v 1.3 2007/09/18 11:21:09 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.common.io;
008:
009: import java.io.IOException;
010: import java.net.HttpURLConnection;
011: import java.net.MalformedURLException;
012: import java.net.URL;
013:
014: import org.xins.common.Log;
015: import org.xins.common.Utils;
016:
017: /**
018: * File watcher thread. This thread checks if a URL or a set of URLs
019: * changed and if it has, it notifies the listener.
020: * The check is performed every <em>n</em> seconds, where <em>n</em> can be configured.
021: *
022: * <p>Initially this thread will be a daemon thread. This can be changed by
023: * calling {@link #setDaemon(boolean)}.
024: *
025: * @version $Revision: 1.3 $ $Date: 2007/09/18 11:21:09 $
026: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
027: *
028: * @since XINS 2.1
029: */
030: public class HTTPFileWatcher extends FileWatcher {
031:
032: /**
033: * The URLs to watch. Not <code>null</code>.
034: */
035: private URL[] _urls;
036:
037: /**
038: * Creates a new <code>HTTPFileWatcher</code> for the specified URL.
039: *
040: * <p>The interval must be set before the thread can be started.
041: *
042: * @param url
043: * the name of the URL to watch, cannot be <code>null</code>.
044: *
045: * @param listener
046: * the object to notify on events, cannot be <code>null</code>.
047: *
048: * @throws IllegalArgumentException
049: * if <code>url == null || listener == null</code>
050: */
051: public HTTPFileWatcher(String url, Listener listener)
052: throws IllegalArgumentException {
053: this (url, 0, listener);
054: }
055:
056: /**
057: * Creates a new <code>HTTPFileWatcher</code> for the specified URL, with the
058: * specified interval.
059: *
060: * @param url
061: * the name of the URL to watch, cannot be <code>null</code>.
062: *
063: * @param interval
064: * the interval in seconds, must be greater than or equal to 0.
065: * if the interval is 0 the interval must be set before the thread can
066: * be started.
067: *
068: * @param listener
069: * the object to notify on events, cannot be <code>null</code>.
070: *
071: * @throws IllegalArgumentException
072: * if <code>url == null || listener == null || interval < 0</code>
073: */
074: public HTTPFileWatcher(String url, int interval, Listener listener)
075: throws IllegalArgumentException {
076: this (new String[] { url }, interval, listener);
077: }
078:
079: /**
080: * Creates a new <code>HTTPFileWatcher</code> for the specified set of URLs,
081: * with the specified interval.
082: *
083: * @param urls
084: * the name of the URLs to watch, cannot be <code>null</code>.
085: * It should also have at least one URL and none of the URLs should be <code>null</code>.
086: *
087: * @param interval
088: * the interval in seconds, must be greater than or equal to 0.
089: * if the interval is 0 the interval must be set before the thread can
090: * be started.
091: *
092: * @param listener
093: * the object to notify on events, cannot be <code>null</code>.
094: *
095: * @throws IllegalArgumentException
096: * if <code>urls == null || listener == null || interval < 0 || urls.length < 1</code>
097: * or if one of the URL is <code>null</code>.
098: */
099: public HTTPFileWatcher(String[] urls, int interval,
100: Listener listener) throws IllegalArgumentException {
101:
102: super (urls, interval, listener);
103: }
104:
105: protected void storeFiles(String[] files) {
106: try {
107: _urls = new URL[files.length];
108: _urls[0] = new URL(files[0]);
109: _filePaths = files[0];
110: for (int i = 1; i < files.length; i++) {
111: _urls[i] = new URL(_urls[0], files[i]);
112: _filePaths += ";" + _urls[i].getPath();
113: }
114: } catch (MalformedURLException murlex) {
115: Log.log_1204(murlex);
116: }
117: }
118:
119: protected void firstCheck() {
120:
121: for (int i = 0; i < _urls.length; i++) {
122: URL url = _urls[i];
123: try {
124: HttpURLConnection connection = (HttpURLConnection) url
125: .openConnection();
126: connection.connect();
127: if (connection.getResponseCode() < 400) {
128: _lastModified = Math.max(_lastModified, connection
129: .getHeaderFieldDate("Last-Modified", 0L));
130: }
131:
132: // Ignore a IOException
133: } catch (IOException exception) {
134: Log.log_1204(exception);
135:
136: // Ignore a SecurityException
137: } catch (SecurityException exception) {
138: Utils.logIgnoredException(exception);
139: }
140: }
141: }
142:
143: protected long getLastModified() throws SecurityException {
144: long lastModified = 0L;
145: for (int i = 0; i < _urls.length; i++) {
146: URL url = _urls[i];
147: try {
148: HttpURLConnection connection = (HttpURLConnection) url
149: .openConnection();
150: connection.setIfModifiedSince(_lastModified);
151: connection.connect();
152: if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
153: } else if (connection.getResponseCode() < 400) {
154: lastModified = Math.max(lastModified, connection
155: .getHeaderFieldDate("Last-Modified", 0L));
156: } else {
157: return -1L;
158: }
159: } catch (IOException ioe) {
160: Log.log_1204(ioe);
161: }
162: }
163: if (lastModified == 0L) {
164: return _lastModified;
165: } else {
166: return lastModified;
167: }
168: }
169: }
|