KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > util > FileChangeWatcher


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.util;
21
22 import java.io.*;
23 import java.util.*;
24
25
26 /**
27  * @author Uddhab Pant
28  *
29  * Abstract class that defines functionalities to detect file modification.
30  *
31  */

32 public abstract class FileChangeWatcher extends TimerTask {
33     private long lastModified;
34     private File file;
35
36     /**
37      * Constructor initializes file.
38      * @param file File
39      */

40     public FileChangeWatcher(File file) {
41         this.file = file;
42         this.lastModified = file.lastModified();
43     }
44
45     /**
46      * A thread is launched to check the lastModified value and compare it with the previous value.
47      */

48     public final void run() {
49         long lastModified = file.lastModified();
50
51         //If last modified values are not equal, onChange event is fired.
52
if (this.lastModified != lastModified) {
53             this.lastModified = lastModified;
54             onChange(file);
55         }
56     }
57
58     /**
59      * Fired when the file is modified.
60      * @param file File
61      */

62     protected abstract void onChange(File file);
63 }
64
Popular Tags