📄 directorypoller.java
字号:
f=new File(directory, DEFAULT_AUTOMOVE_DIRECTORY);
setAutoMoveDirectory0(directory, f);
}
return f;
}
/**
* Associate a directory to one of the controlled directories, for
* the autoMove mode (see class description).
* <p>
* This method can be called only if the poller has not started yet.
*
* @param directory the controlled directory
* @param autoMoveDirectory the directory associated to the controlled directory
* @exception IllegalArgumentException if <tt>directory</tt> is not a controlled
* directory
* @exception IllegalStateException
*/
public void setAutoMoveDirectory(File directory,
File autoMoveDirectory)
throws IllegalArgumentException, IllegalStateException {
if (isAlive())
throw new IllegalStateException("auto-move directories cannot be set once the poller has started");
setAutoMoveDirectory0(directory, autoMoveDirectory);
}
// This version is called internally from run(), when the thread is alive
private void setAutoMoveDirectory0(File directory, File autoMoveDirectory)
throws IllegalArgumentException {
directory=PathNormalizer.normalize(directory);
checkIfManaged(directory);
autoMoveDirs.put(directory, directory=PathNormalizer.normalize(autoMoveDirectory));
}
// Throw an IllegalArgumentException if the given directory is not
// managed by the poller
protected void checkIfManaged(File directory) {
for(int i=0;i<dirs.length;i++)
if (PathNormalizer.normalize(dirs[i]).equals(
directory)
) return;
throw new IllegalArgumentException(
"The directory "+directory+
" is not under control of the directory poller");
}
/**
* Set the verbose level. Verbosity is mainly for debugging/tracing purposes,
* since the poller delivers events to any listener, which can therefore perform the
* tracing.
*
* @param v if true, the poller logs to system out.
*/
public void setVerbose(boolean v) { this.verbose=v; }
/**
* Return the verbose level. See {@link DirectoryPoller#setVerbose(boolean)}.
* @return the verbose level
*/
public boolean isVerbose() { return verbose; }
/**
* Sets the usage of time-based filtering (besides the normal filtering).
* It can be called only if the poller thread hasn't started yet.
*
* @param v if <b>true</b> the poller will use time-based filtering
* @exception IllegalStateException if the poller has already started.
*/
public void setTimeBased(boolean v) {
if (v) {
if (filter != null && isTimeBased()) return;
this.filter=new TimeFilter(originalFilter);
} else {
if ( filter != null && ! isTimeBased()) return;
this.filter=originalFilter;
}
}
/**
* Return <b>true</b> if the poller is using time-based filtering (see class comment).
* @return <b>true</b> if the poller is using time-based filtering (see class comment)
*/
public boolean isTimeBased() {
return (filter instanceof TimeFilter);
}
/**
* Reset the base time for the given directory.
* <p>
* It's irrelevant if time-based filtering is not enabled (see class comment).
* @param the directory for which to set the base time
* @param time the new base time
* @exception IllegalArgumentException if the given directory is not under control of the poller
*/
public void setBaseTime(File directory, long time) {
for(int i=0;i<dirs.length;i++)
if (dirs[i].getAbsolutePath().equals(directory.getAbsolutePath())) {
baseTime[i]=time;
return;
}
throw new IllegalArgumentException("'"+directory+"' is not under control of the poller");
}
/**
* Reset the base time for the all the directories under control of the poller.
* <p>
* It's irrelevant if time-based filtering is not enabled (see class comment).
*
* @param time the new base time
*/
public void setBaseTime(long time) {
for(int i=0;i<dirs.length;i++)
setBaseTime(dirs[i], time);
}
/**
* Return the current base time for the given directory.
* <p>
* The returned value is unpredictable if time-based filtering is not enabled (see class comment).
* @param the directory for which to get the base time
* @exception IllegalArgumentException if the given directory is not under control of the poller
* @return the current base time, if time-based filtering is enabled
*/
public long getBaseTime(File directory) {
for(int i=0;i<dirs.length;i++) {
if (dirs[i].getAbsolutePath().equals(directory.getAbsolutePath())) return baseTime[i];
}
throw new IllegalArgumentException("'"+directory+"' is not under control of the poller");
}
/**
* Return <b>true</b> if the poller is time based, and uses last-polling time
* as a basis for the lookup (see class comment)
*/
public boolean isPollingTimeBased() {
return isTimeBased() && timeBasedOnLastLookup;
}
/**
* Sets the subtype of time-based filtering used by the poller.
* <p>
* For the call to have any meaning, the poller must be in time-based mode, that is,
* {@link DirectoryPoller#setTimeBased(boolean) setTimeBased()} must have been
* called with <b>true</b> as parameter (se class comment): if the parameter
* is <b>true</b>, the poller will select only files older than the last
* polling time (besides applying any user-defined filter); if the parameter
* is <b>false</b>, the poller will select only files <i>whose last modification
* time is higher than the higher last modification time found in the last
* polling cycle</i>.
*
* @param v determines the time-based filtering subtype
*/
public void setPollingTimeBased(boolean v) {
timeBasedOnLastLookup=v;
}
/**
* Instruct the poller whether to notify per-file events to the listeners
* or not (see class comment).
* @param v if <b>true</b>, the poller will notify each file to the listeners
*/
public void setSendSingleFileEvent(boolean v) { sendSingleFileEvent=v; }
/**
* Return <b>true</b> if the poller is currently instructed to send per-file
* events to the listeners (see class comment).
*
* @return <b>true</b> if the poller is currently instructed to send per-file
* events to the listeners
*/
public boolean isSendSingleFileEvent() { return sendSingleFileEvent; }
/**
* Return the current poll interval. See class comments for notes.
* @return the current poll interval
*/
public long getPollInterval() { return pollInterval; }
/**
* Set the poll interval. The poller sleeps for <tt>pollInterval</tt> milliseconds
* and then performs a lookup in the bound directories. See class comments for notes.
*
* @param pollInterfval the poll interval
*/
public void setPollInterval(long pollInterval) { this.pollInterval=pollInterval; }
/**
* Instruct the poller whether to start by sweeping the controlled directories,
* or by going immediatly to sleep.
* @param v if <b>true</b> the poller starts by going immediatly to sleep
*/
public void setStartBySleeping(boolean v) { startBySleeping=v; }
/**
* Return if the poller starts by sweeping the controlled directories,
* or going immediatly to sleep.
* @return if the poller starts by sweeping the controlled directories,
* or going immediatly to sleep.
*/
public boolean isStartBySleeping() { return startBySleeping; }
/**
* Adds on {@link PollManager PollManager} to the poller,
* which will be notified on polling events.
* <p>
* You may add many poll managers, but there is no support for
* inter-poll manager coordination, so if a PollManager deletes/moves
* a polled file, the others will still receive the associated
* event, but may not be able to perform proper processing.
*/
public void addPollManager(PollManager pm) {
pollManagersList.add(pm);
addListener(new DefaultListener(this, pm));
}
/**
* Request the poller to shut down.
* <p>
* If a notification is in course, the notification is completed before
* shutting down. Therefore, the event handler may want to check whether a
* shutdown has been requested before reacting to an event.
*/
public void shutdown() {
shutdownRequested=true;
this.interrupt();
if (verbose)
System.out.println("Polling shutdown requested");
}
/**
* Return true if a shutdown has been requested.
* @return true if a shutdown has been requested.
*/
public boolean isShuttingDown() { return shutdownRequested; }
/**
* Invoked when the thread is started.
*
* Performs a polling, notifying the registered listeners of
* related events. After each nofication, a check is done
* of whether a shutdown has been requested or not.
*/
public synchronized void run() {
System.out.println("********************************************************");
System.out.println("* DirectoryPoller 1.5.2 (C) Cristiano Sadun under LGPL *");
System.out.println("********************************************************");
shutdownRequested=false;
if (dirs==null) throw new IllegalStateException("Programming error: no directories to poll specified");
if (verbose)
System.out.println("Polling started, interval is "+pollInterval+"ms");
if (autoMove) {
// Try to create the automove dirs
for(int j=0;j<dirs.length;j++) {
File automoveDir = PathNormalizer.normalize(getAutoMoveDirectory(dirs[j]));
if (!automoveDir.exists()) {
if (verbose)
System.out.println("Automove directory "+automoveDir+" does not exist, attempting to create.");
if (! automoveDir.mkdirs()) throw new RuntimeException("Could not create the directory "+automoveDir.getAbsolutePath());
if (verbose)
System.out.println("Automove directory "+automoveDir+" created successfully.");
}
}
}
// Main loop
do {
// Immediatly go to sleep if startBySleeping==true...
if (startBySleeping) {
startBySleeping=false; // ..and reset it so we dont *only* sleep.
} else {
runCycle();
}
// Go to sleep
if (!shutdownRequested)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -