/* Copyright (c) 2004 Joseph Gleason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Current versions of this and other code can be downloaded at: http://gleason.cc/ */ package cc.glsn; import java.io.File; import java.util.Map; import java.util.HashMap; import java.util.IdentityHashMap; import java.util.Iterator; import com.sleepycat.je.Cursor; import com.sleepycat.je.Database; import com.sleepycat.je.DatabaseConfig; import com.sleepycat.je.DatabaseEntry; import com.sleepycat.je.Environment; import com.sleepycat.je.EnvironmentConfig; import com.sleepycat.je.LockMode; import com.sleepycat.je.OperationStatus; public class DirUpdateDetector { public static final boolean Debug=false; File StartLoc; long BetweenCheckPause; long MinAge; int StartLocPathLen; HashMap FileInfoMap; IdentityHashMap Listeners; IdentityHashMap Filters; long PassNumber; boolean Persistant; boolean HaltRequested; //DB stuff only used with persitance Environment DBEnviron; Database PersistantDB; boolean SaveDeleteData; long SaveDeleteTime; public DirUpdateDetector(File startloc, long betweencheckpause, File RootDBDir, long minage) { this("",startloc,betweencheckpause,RootDBDir,minage); } public DirUpdateDetector(File startloc, long betweencheckpause, File RootDBDir) { this(startloc,betweencheckpause,RootDBDir,0); } public DirUpdateDetector(File startloc, long betweencheckpause) { this(startloc,betweencheckpause,null,0); } public DirUpdateDetector(String UniqStr, File startloc, long betweencheckpause, File RootDBDir, long minage) { HaltRequested=false; SaveDeleteData=false; SaveDeleteTime=0; MinAge=minage; StartLoc=startloc; StartLocPathLen=StartLoc.getAbsolutePath().length()+1; //System.out.println(startloc.getAbsolutePath()); BetweenCheckPause=betweencheckpause; FileInfoMap=new HashMap(16,0.75f); Listeners=new IdentityHashMap(16); Filters=new IdentityHashMap(16); PassNumber=0; if (RootDBDir!=null) { Persistant=true; EnvironmentConfig ec=new EnvironmentConfig(); ec.setAllowCreate(true); ec.setReadOnly(false); String DBPath=RootDBDir.getAbsolutePath() + '/' + UniqStr + startloc.getAbsolutePath().replace('/','_').replace('\\','_').replace(':','_'); new File(DBPath).mkdir(); try { DatabaseConfig DC=new DatabaseConfig(); DC.setAllowCreate(true); DC.setTransactional(false); DBEnviron=new Environment(new File(DBPath),ec); PersistantDB=DBEnviron.openDatabase(null,startloc.getAbsolutePath(),DC); readDB(); } catch(com.sleepycat.je.DatabaseException de) { de.printStackTrace(); } } } /** Stop processing after next iteration. * Can be restarted with start() */ public void halt() { HaltRequested=true; } public boolean getSaveDeleteData() { return SaveDeleteData; } public long getSaveDeleteTime() { return SaveDeleteTime; } public void setSaveDeleteData(boolean saveDeleteData) { SaveDeleteData = saveDeleteData; } public void setSaveDeleteTime(long saveDeleteTime) { SaveDeleteTime = saveDeleteTime; } public void start() { HaltRequested=false; new DUDThread().start(); } public void addListener(DirUpdateDetectorListener L) { synchronized(Listeners) { Listeners.put(L,L); } } public void addFilter(DirUpdateDetectorFilter F) { synchronized(Filters) { Filters.put(F,F); } } private void readDB() throws com.sleepycat.je.DatabaseException { Cursor c=PersistantDB.openCursor(null,null); DatabaseEntry Key=new DatabaseEntry(); DatabaseEntry Data=new DatabaseEntry(); while(c.getNext(Key,Data,LockMode.DEFAULT).equals(OperationStatus.SUCCESS)) { DirUpdateInfoEntry E; E=new DirUpdateInfoEntry(Data); if (E.ScanPass > PassNumber) PassNumber=E.ScanPass; FileInfoMap.put(E.RelPath,E); } c.close(); PassNumber++; } private void recursiveCheck(File currLoc) { if (!currLoc.exists()) return; String RelPath="."; if (currLoc.getAbsolutePath().length() > StartLocPathLen) RelPath=currLoc.getAbsolutePath().substring(StartLocPathLen); //MinAge is not set //or file is outside age range if ((MinAge==0) || (Math.abs(System.currentTimeMillis() - currLoc.lastModified()) >= MinAge)) { DirUpdateInfoEntry Info=(DirUpdateInfoEntry)FileInfoMap.get(RelPath); if (Info==null) { Info=new DirUpdateInfoEntry(RelPath,currLoc); FileInfoMap.put(Info.RelPath,Info); synchronized(Listeners) { for(Iterator I=Listeners.keySet().iterator(); I.hasNext(); ) { DirUpdateDetectorListener L=(DirUpdateDetectorListener)I.next(); L.newFile(Info); L.changeFile(Info); } } dbUpdate(Info); } else { if (Info.update(currLoc)) { synchronized(Listeners) { for(Iterator I=Listeners.keySet().iterator(); I.hasNext(); ) { DirUpdateDetectorListener L=(DirUpdateDetectorListener)I.next(); L.modifyFile(Info); L.changeFile(Info); } } dbUpdate(Info); } } Info.ScanPass=PassNumber; } else { //This is so files inside the time range limit (minage) we //dont consider those files deleted or changed in any way //until the time is up DirUpdateInfoEntry Info=(DirUpdateInfoEntry)FileInfoMap.get(RelPath); if (Info!=null) Info.ScanPass=PassNumber; } if (currLoc.isDirectory()) { synchronized(Filters) { for(Iterator I=Filters.keySet().iterator(); I.hasNext(); ) { DirUpdateDetectorFilter F=(DirUpdateDetectorFilter)I.next(); if (!F.careAboutThisDirectory(RelPath)) return; } } File[] FileList=currLoc.listFiles(); if (FileList!=null) for(int i=0; i