Source Code Cross Referenced for HTTPFileWatcher.java in  » Web-Services » xins » org » xins » common » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services » xins » org.xins.common.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 &lt; 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 &lt; 0 || urls.length &lt; 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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.