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