Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2010 Tristan Tarrant Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package net.dataforte.commons.resources;
 
 import java.io.File;
 
 
 public class FileObserver extends Observable implements Runnable {
 	private static final Logger log = LoggerFactory.getLogger(FileObserver.class);
 
 	long lastModified;
 	public static final long DEFAULT_DELAY = 5000;
 	long delay;
 	boolean exitOnChange;
 
 	public FileObserver(String s) {
 		 = s;
 		start(null);
 	}
 
 	public FileObserver(String sThreadGroup group) {
 		 = s;
 		start(group);
 	}
 
 	public long getDelay() {
 		return ;
 	}
 
 	public void setDelay(long delay) {
 		this. = delay;
 	}
 
 	public boolean isExitOnChange() {
 		return ;
 	}
 
 	public void setExitOnChange(boolean exitOnChange) {
 		this. = exitOnChange;
 	}
 
 	public boolean check() {
 		File f = new File();
 		long actualLastModified = f.lastModified();
 		if (actualLastModified == 0l) {
 			// The file has disappeared, interrupt self
 			return false;
 		} else if ( != actualLastModified) {
 			 = actualLastModified;
 			return true;
 		} else {
 			return false;
 		}
 	}
 
 	public void start(ThreadGroup group) {
 		File f = new File();
 		if (group != null)
 			 = new Thread(groupthis"Observe: " + );
 		else
 			 = new Thread(this"Observe: " + );
 	}
 
 	public void run() {
 		try {
 			for (;;) {
 				Thread.sleep();
 				boolean res = check();
 				if ( && res) {
 					break;
 				}
 			}
 		} catch (InterruptedException e) {
 			// Do nothing
catch (Throwable t) {
			.error(""t);
		}
	}
New to GrepCode? Check out our FAQ X