nyla.solutions.global.io.FileMonitor Maven / Gradle / Ivy
Show all versions of nyla.solutions.global Show documentation
package nyla.solutions.global.io;
import java.util.*;
import java.io.*;
import nyla.solutions.global.io.FileEvent;
import nyla.solutions.global.io.FileMonitor;
import nyla.solutions.global.io.IO;
import nyla.solutions.global.io.WildCardFilter;
import nyla.solutions.global.util.*;
//import solutions.global.io.FileMonitorNotifier;
/**
*/
public class FileMonitor extends Observable
{
public FileMonitor()
{
this(1000);
}//------------------------------------------------------------------
/**
* Create a file monitor instance with specified polling interval.
*
* @param pollingInterval
* Polling interval in milli seconds.
*/
public FileMonitor(long pollingInterval)
{
this.pollingInterval = pollingInterval;
timer = new Timer(true);
}//------------------------------------------------------------------
/**
* Stop the file monitor polling.
*/
public synchronized void stop()
{
timer.cancel();
}//------------------------------------------------------------------
/**
* Used to wait for transferred file's content length to stop changes for
* 5 seconds
*
* @param aFile
* @throws InterruptedException
*/
public static void waitFor(File aFile)
{
if(aFile == null)
return;
String path =aFile.getAbsolutePath();
long previousSize = IO.getFileSize(path);
long currentSize = previousSize;
while(true)
{
try
{
Thread.sleep(Config.getPropertyLong("file.monitor.file.wait.time",1000*5).longValue());
}
catch(InterruptedException e){}
currentSize = IO.getFileSize(path);
if(currentSize == previousSize )
return;
previousSize = currentSize;
}
}//------------------------------------------------------------------
/**
* Add file to observer for. File may be any java.io.File (including a
* directory) and may well be a non-existing file in the case where the
* creating of the file is to be trepped.
*
* More than one file can be listened for. When the specified file is
* created, modified or deleted, listeners are notified.
*
* @param file
* File to listen for.
*/
public synchronized void monitor(String aDirectory, String aFileNameFilter, boolean aProcessCurrentFiles)
{
timer.schedule(
new FileMonitorNotifier(this, aDirectory, aFileNameFilter, aProcessCurrentFiles)
, 0,
pollingInterval);
}//-------------------------------------------
/**
* Notify observers that the file has changed
*
* @param file
* the file that changes
*/
protected synchronized void notifyChange(File file)
{
this.setChanged();
System.out.println("Notify change file="+file.getAbsolutePath());
this.notifyObservers(FileEvent.createChangedEvent(file));
}//---------------------------------------------------
/**
*
* @param file file that was added
*/
protected synchronized void notifyAdd(File file)
{
this.setChanged();
System.out.println("Notify added file="+file.getAbsolutePath());
this.notifyObservers(FileEvent.createAddedEvent(file));
}//---------------------------------------------------
/**
* This is the timer thread which is executed every n milliseconds according
* to the setting of the file monitor. It investigates the file in question
* and notify listeners if changed.
*/
private static class FileMonitorNotifier extends TimerTask
{
/**
* Constructor
*
* @param aFileMonitor
* the file monitor
* @param aDirectory
* the directory to monitor
* @param aFileFilter
* the file filter example *.exe"
*/
FileMonitorNotifier(FileMonitor aFileMonitor, String aDirectory,
String aFileFilter, boolean aPreviousCurrentFile)
{
if (aDirectory == null || aDirectory.length() == 0)
throw new IllegalArgumentException(
"Directory to minotor has not provided.");
this.directory = new File(aDirectory);
if (!this.directory.isDirectory())
throw new IllegalArgumentException("Invalid directory \""
+ aDirectory + "\" provided!");
if (aFileMonitor == null)
throw new IllegalArgumentException("FileMonitor not provided");
this.fileMonitor = aFileMonitor;
//----------------------------------------------
this.filter = new WildCardFilter(aFileFilter);
if(aPreviousCurrentFile)
init();
}//--------------------------------------
private void init()
{
File[] files = this.directory.listFiles(this.filter);
if(files == null)
return;
File file = null;
for(int i = 0; i fileLastModifyTimeMap = new Hashtable(); // File -> Long
private FilenameFilter filter = null;
private File directory = null;
/**
*
* @uml.property name="fileMonitor"
* @uml.associationEnd multiplicity="(0 1)"
*/
private FileMonitor fileMonitor = null;
}//---------------------------------------------------------------
/**
* Test this class.
*
* @param args Not used.
*/
public static void main(String args[])
{
try
{
// Create the monitor
//FileMonitor fm = FileMonitor.createFileMonitor(args);
FileMonitor.createFileMonitor(args);
}
catch(Exception e)
{
System.err.println("Unable monitor specified input error="+e);
System.exit(-1);
}
// Avoid program exit
while (!false)
{
try
{
Thread.sleep(1000);
}
catch(Exception e){e.printStackTrace();}
}
}//-------------------------------------------
private static FileMonitor createFileMonitor(String [] args)
throws Exception
{
String usage= " Usage: [-pollingInterval ]? [-observer ]* [-monitor ::]*";
if(args == null || args.length < 2 )
throw new IllegalArgumentException(usage);
FileMonitor fileMonitor =new FileMonitor();
String [] arrayString = null;
for(int i=0; i < args.length;i++)
{
if("-monitor".equals(args[i]))
{
if(i+1 >= args.length)
{
throw new IllegalArgumentException("directory:filter specified -monitor argument "+usage);
}
arrayString = args[i+1].split(":");
if(arrayString.length != 3)
throw new IllegalArgumentException(args[i+1]+usage );
fileMonitor.monitor(arrayString[0],arrayString[1],Boolean.valueOf(arrayString[2]).booleanValue());
i = i+1;
}
else if("-pollingInterval".equals(args[i]))
{
if(i+1 >= args.length)
{
throw new IllegalArgumentException("no time specified -pollingInterval argument "+usage);
}
fileMonitor.setPollingInterval(Long.parseLong(args[i+1]));
i = i+1;
}
else if("-observer".equals(args[i]))
{
if(i+1 >= args.length)
{
throw new IllegalArgumentException("no observer specific in -observer argument "+usage);
}
fileMonitor.addObserver((Observer)Class.forName(args[i+1]).newInstance());
i = i+1;
}
}//end for
return fileMonitor;
}//---------------------------------------------
/**
* @return Returns the pollingInterval.
*
* @uml.property name="pollingInterval"
*/
public long getPollingInterval() {
return pollingInterval;
}
/**
* @param pollingInterval The pollingInterval to set.
*
* @uml.property name="pollingInterval"
*/
public void setPollingInterval(long pollingInterval) {
this.pollingInterval = pollingInterval;
}
private Timer timer;
private long pollingInterval = 1000;
}