001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/util/FileMonitor.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    
037    package org.deegree.io.util;
038    
039    import java.io.File;
040    import java.io.FileNotFoundException;
041    import java.net.URL;
042    import java.util.Hashtable;
043    import java.util.Timer;
044    import java.util.TimerTask;
045    
046    /**
047     * Replaces inner class Reloader in AbstractOGCServlet.
048     *
049     * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </A>
050     * @author last edited by: $Author: mschneider $
051     *
052     * @version 2.0, $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
053     *
054     * @see org.deegree.enterprise.AbstractOGCServlet
055     * @see java.io.File#lastModified
056     * @see <a href="http://www.javaworld.com/javaworld/javatips/jw-javatip125.html">Java Tip 125 </a>
057     *
058     * @since 2.0
059     */
060    
061    public class FileMonitor {
062    
063        private static final FileMonitor instance = new FileMonitor();
064    
065        private Timer timer;
066    
067        private Hashtable<String, FileMonitorTask> timerEntries;
068    
069        /**
070         * Factory method to get singleton instance.
071         *
072         * @return the instance
073         *
074         */
075        public static FileMonitor getInstance() {
076            return instance;
077        }
078    
079        protected FileMonitor() {
080            // Create timer, run timer thread as daemon.
081            timer = new Timer( true );
082            timerEntries = new Hashtable<String, FileMonitorTask>();
083        }
084    
085        /**
086         * Add a monitored file with a FileChangeListener.
087         *
088         * @param listener
089         *            listener to notify when the file changed.
090         * @param fileName
091         *            name of the file to monitor.
092         * @param period
093         *            polling period in milliseconds.
094         * @throws FileNotFoundException
095         */
096        public void addFileChangeListener( FileChangeListener listener, String fileName, long period )
097                                throws FileNotFoundException {
098            removeFileChangeListener( listener, fileName );
099            FileMonitorTask task = new FileMonitorTask( listener, fileName );
100            timerEntries.put( fileName + listener.hashCode(), task );
101            timer.schedule( task, period, period );
102        }
103    
104        /**
105         * Remove the listener from the notification list.
106         *
107         * @param listener
108         *            the listener to be removed.
109         * @param fileName
110         */
111        public void removeFileChangeListener( FileChangeListener listener, String fileName ) {
112            FileMonitorTask task = timerEntries.remove( fileName + listener.hashCode() );
113            if ( task != null ) {
114                task.cancel();
115            }
116        }
117    
118        protected void fireFileChangeEvent( FileChangeListener listener, String fileName ) {
119            listener.fileChanged( fileName );
120        }
121    
122        class FileMonitorTask extends TimerTask {
123    
124            FileChangeListener listener;
125    
126            String fileName;
127    
128            File monitoredFile;
129    
130            long lastModified;
131    
132            /**
133             * @param listener
134             * @param fileName
135             * @throws FileNotFoundException
136             */
137            public FileMonitorTask( FileChangeListener listener, String fileName ) throws FileNotFoundException {
138                this.listener = listener;
139                this.fileName = fileName;
140                this.lastModified = 0;
141    
142                monitoredFile = new File( fileName );
143                if ( !monitoredFile.exists() ) { // but is it on CLASSPATH?
144                    URL fileURL = listener.getClass().getClassLoader().getResource( fileName );
145                    if ( fileURL != null ) {
146                        monitoredFile = new File( fileURL.getFile() );
147                    } else {
148                        throw new FileNotFoundException( "File Not Found: " + fileName );
149                    }
150                }
151                this.lastModified = monitoredFile.lastModified();
152            }
153    
154            @Override
155            public void run() {
156                long lastModified = monitoredFile.lastModified();
157                if ( lastModified != this.lastModified ) {
158                    this.lastModified = lastModified;
159                    fireFileChangeEvent( this.listener, this.fileName );
160                }
161            }
162        }
163    }