|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
FileWatchdog.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* Joey and its relative products are published under the terms
|
|
3 |
* of the Apache Software License.
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Created on 2003/11/04
|
|
7 |
*/
|
|
8 |
package org.asyrinx.brownie.core.io;
|
|
9 |
|
|
10 |
import java.io.File;
|
|
11 |
|
|
12 |
/**
|
|
13 |
* ファイルの変更を監視するスレッドです。
|
|
14 |
*
|
|
15 |
* @author akima
|
|
16 |
*/
|
|
17 |
public abstract class FileWatchdog extends Thread { |
|
18 |
|
|
19 |
/**
|
|
20 |
* The default delay between every file modification check, set to 60
|
|
21 |
* seconds.
|
|
22 |
*/
|
|
23 |
static final public long DEFAULT_DELAY = 60000; |
|
24 |
|
|
25 |
/**
|
|
26 |
* The delay to observe between every check. By default set {@link
|
|
27 |
* #DEFAULT_DELAY}.
|
|
28 |
*/
|
|
29 |
protected long delay = DEFAULT_DELAY; |
|
30 |
|
|
31 |
private final File file;
|
|
32 |
|
|
33 |
private long lastModified = 0; |
|
34 |
|
|
35 |
private boolean warnedAlready = false; |
|
36 |
|
|
37 |
private boolean interrupted = false; |
|
38 |
|
|
39 | 0 |
protected FileWatchdog(String filename) {
|
40 | 0 |
this(filename, DEFAULT_DELAY);
|
41 |
} |
|
42 |
|
|
43 | 0 |
protected FileWatchdog(String filename, long delay_msec) { |
44 | 0 |
this.file = new File(filename); |
45 | 0 |
this.delay = delay_msec;
|
46 | 0 |
setDaemon(true);
|
47 |
//checkAndConfigure();
|
|
48 |
} |
|
49 |
|
|
50 |
/**
|
|
51 |
* Set the delay to observe between each check of the file changes.
|
|
52 |
*/
|
|
53 | 0 |
public void setDelay(long delay) { |
54 | 0 |
this.delay = delay;
|
55 |
} |
|
56 |
|
|
57 |
abstract protected void doOnChange(); |
|
58 |
|
|
59 | 0 |
public void checkAndConfigure() { |
60 | 0 |
final boolean fileExists;
|
61 | 0 |
try {
|
62 | 0 |
fileExists = file.exists(); |
63 |
} catch (SecurityException e) {
|
|
64 | 0 |
System.err |
65 |
.println("Was not allowed to read check file existance, file:["
|
|
66 |
+ file.getAbsolutePath() + "].");
|
|
67 | 0 |
interrupted = true; // there is no point in continuing |
68 | 0 |
return;
|
69 |
} |
|
70 |
|
|
71 | 0 |
if (fileExists) {
|
72 | 0 |
long l = file.lastModified();
|
73 |
// this can also throw a SecurityException
|
|
74 | 0 |
if (l > lastModified) { // however, if we reached this point this |
75 | 0 |
lastModified = l; // is very unlikely.
|
76 | 0 |
doOnChange(); |
77 | 0 |
warnedAlready = false;
|
78 |
} |
|
79 |
} else {
|
|
80 | 0 |
if (!warnedAlready) {
|
81 | 0 |
System.err.println("[" + file.getAbsolutePath()
|
82 |
+ "] does not exist.");
|
|
83 | 0 |
warnedAlready = true;
|
84 |
} |
|
85 |
} |
|
86 |
} |
|
87 |
|
|
88 | 0 |
public void run() { |
89 | 0 |
while (!interrupted) {
|
90 | 0 |
try {
|
91 | 0 |
Thread.sleep(delay); |
92 |
} catch (InterruptedException e) {
|
|
93 |
// no interruption expected
|
|
94 |
} |
|
95 | 0 |
checkAndConfigure(); |
96 |
} |
|
97 |
} |
|
98 |
} |
|