From: paspes <pa...@us...> - 2008-01-08 17:04:29
|
Update of /cvsroot/babeldoc/babeldoc/modules/scanner/src/com/babeldoc/scanner/worker In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv15342/modules/scanner/src/com/babeldoc/scanner/worker Modified Files: VFSScanner.java Log Message: VFS Scanner Event monitor active Index: VFSScanner.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/scanner/src/com/babeldoc/scanner/worker/VFSScanner.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** VFSScanner.java 21 Dec 2007 12:05:33 -0000 1.5 --- VFSScanner.java 8 Jan 2008 17:01:57 -0000 1.6 *************** *** 72,75 **** --- 72,77 ---- import java.util.Collection; import java.util.Date; + import java.util.GregorianCalendar; + import java.util.Iterator; import org.apache.commons.vfs.FileChangeEvent; *************** *** 77,81 **** import org.apache.commons.vfs.FileListener; import org.apache.commons.vfs.FileObject; - import org.apache.commons.vfs.FileSystem; import org.apache.commons.vfs.FileSystemException; import org.apache.commons.vfs.FileSystemManager; --- 79,82 ---- *************** *** 86,90 **** import org.apache.commons.vfs.auth.StaticUserAuthenticator; import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder; ! import org.apache.commons.vfs.provider.ftp.FtpFileSystemConfigBuilder; import com.babeldoc.core.I18n; --- 87,91 ---- import org.apache.commons.vfs.auth.StaticUserAuthenticator; import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder; ! import org.apache.commons.vfs.impl.DefaultFileMonitor; import com.babeldoc.core.I18n; *************** *** 109,124 **** /** Configuration options */ public static final String IN_DIR = "inDirectory"; public static final String DONE_DIR = "doneDirectory"; public static final String BUFFER_LEN = "bufferLen"; public static final String INCLUDE_SUB_DIRS = "includeSubfolders"; public static final String FILTER_FILENAME = "filter"; public static final String MINIMUM_FILE_AGE = "minimumFileAge"; - public static final String DELETE = "deleteIn"; public static final String IN_DOMAIN = "inDomain"; public static final String IN_USERNAME = "inUsername"; public static final String IN_PASSWORD = "inPassword"; public static final String OUT_DOMAIN = "outDomain"; public static final String OUT_USERNAME = "outUsername"; public static final String OUT_PASSWORD = "outPassword"; --- 110,138 ---- /** Configuration options */ public static final String IN_DIR = "inDirectory"; + public static final String DONE_DIR = "doneDirectory"; + public static final String BUFFER_LEN = "bufferLen"; + public static final String INCLUDE_SUB_DIRS = "includeSubfolders"; + public static final String FILTER_FILENAME = "filter"; + public static final String MINIMUM_FILE_AGE = "minimumFileAge"; + public static final String MINIMUM_FILE_DATE = "minimumFileDate"; + + public static final String DELETE = "deleteIn"; + public static final String IN_DOMAIN = "inDomain"; + public static final String IN_USERNAME = "inUsername"; + public static final String IN_PASSWORD = "inPassword"; + public static final String OUT_DOMAIN = "outDomain"; + public static final String OUT_USERNAME = "outUsername"; + public static final String OUT_PASSWORD = "outPassword"; *************** *** 136,144 **** private FileSystemManager fsManager; private FileSystemOptions optsIn; private FileSystemOptions optsOut; ! ! private FileSystem fs; ! private TestListener listener; /** URI to scan */ --- 150,163 ---- private FileSystemManager fsManager; + private FileSystemOptions optsIn; + private FileSystemOptions optsOut; ! ! /* Monitor that add FileObjects to Queue */ ! private DefaultFileMonitor fm; ! ! /* Queue of FileObjects to process */ ! private VFSQueue queue; /** URI to scan */ *************** *** 148,154 **** private FileObject doneDirectory; - /** flag to include sub directories */ - private boolean includeSubDirs = false; - /** flag to delete procesed files */ private boolean delete = true; --- 167,170 ---- *************** *** 159,162 **** --- 175,184 ---- */ private long minimumFileAge = 0; + + /** + * Minimum time in ms since file was last modified. Attempts to guard + * against incomplete reads when the writer of the file is "slow". + */ + private Date minimumFileDate = null; /** *************** *** 169,173 **** public void doScan() throws ScannerException { try { ! processVFS(this.getInDirectory()); } catch (FileSystemException e) { // TODO Auto-generated catch block --- 191,195 ---- public void doScan() throws ScannerException { try { ! processVFS(queue.getQueue()); } catch (FileSystemException e) { // TODO Auto-generated catch block *************** *** 185,217 **** try { fsManager = VFS.getManager(); ! ! // Create connection options (autentication) ! StaticUserAuthenticator authIn = new StaticUserAuthenticator(this.getInfo().getStrValue(IN_DOMAIN),this.getInfo().getStrValue(IN_USERNAME), this.getInfo().getStrValue(IN_PASSWORD)); ! optsIn = new FileSystemOptions(); ! DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(optsIn, authIn); ! ! StaticUserAuthenticator authOut = new StaticUserAuthenticator(this.getInfo().getStrValue(OUT_DOMAIN),this.getInfo().getStrValue(OUT_USERNAME), this.getInfo().getStrValue(OUT_PASSWORD)); ! optsOut = new FileSystemOptions(); ! DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(optsOut, authOut); ! ! ! //Create in directory setInDirectory(this.getInfo().getStrValue(IN_DIR)); if (!getInDirectory().exists() || !getInDirectory().isReadable() || getInDirectory().getType() != FileType.FOLDER) { ! throw new ScannerConfigurationException(I18n.get( ! "scanner.VFSScanner.error.notDir", IN_DIR, ! getInDirectory()), null); } - - fs= getInDirectory().getParent().getFileSystem(); - listener = new TestListener(getInDirectory()); - fs.addListener(getInDirectory(), listener); - listener.addCreateEvent(); - listener.addChangeEvent(); - ! //Create out directory setDoneDirectory(this.getInfo().getStrValue(DONE_DIR)); --- 207,239 ---- try { fsManager = VFS.getManager(); ! ! // Create connection options (autentication) ! StaticUserAuthenticator authIn = new StaticUserAuthenticator(this ! .getInfo().getStrValue(IN_DOMAIN), this.getInfo() ! .getStrValue(IN_USERNAME), this.getInfo().getStrValue( ! IN_PASSWORD)); ! optsIn = new FileSystemOptions(); ! DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator( ! optsIn, authIn); ! ! StaticUserAuthenticator authOut = new StaticUserAuthenticator(this ! .getInfo().getStrValue(OUT_DOMAIN), this.getInfo() ! .getStrValue(OUT_USERNAME), this.getInfo().getStrValue( ! OUT_PASSWORD)); ! optsOut = new FileSystemOptions(); ! DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator( ! optsOut, authOut); ! ! // Create in directory setInDirectory(this.getInfo().getStrValue(IN_DIR)); if (!getInDirectory().exists() || !getInDirectory().isReadable() || getInDirectory().getType() != FileType.FOLDER) { ! String name = I18n.get("scanner.VFSScanner.error.notDir", ! IN_DIR, getInDirectory().toString()); ! throw new ScannerConfigurationException(name, null); } ! // Create out directory setDoneDirectory(this.getInfo().getStrValue(DONE_DIR)); *************** *** 224,244 **** } } else { ! getLog().logInfo("Done directory not specified. All processed file will be deleted!!!"); setDoneDirectory(null); } ! //Other configuration options ! setIncludeSubDirs("true".equals(this.getInfo().getStrValue(INCLUDE_SUB_DIRS))); ! ! setDeleteProcesedFiles("true".equals(this.getInfo().getStrValue(DELETE))); ! setMinimumFileAge(this.getInfo().getIntValue(MINIMUM_FILE_AGE)); - if (getMinimumFileAge() > 0) { ! LogService.getInstance().logInfo("Minimum file age: " + getMinimumFileAge() + " ms"); } ! // Add filename filter if exist addFilter(FILTER_FILENAME); } catch (FileSystemException e1) { // TODO Auto-generated catch block --- 246,286 ---- } } else { ! getLog() ! .logInfo( ! "Done directory not specified. All processed file will be deleted!!!"); setDoneDirectory(null); } ! // Other configuration options ! setDeleteProcesedFiles("true".equals(this.getInfo().getStrValue( ! DELETE))); setMinimumFileAge(this.getInfo().getIntValue(MINIMUM_FILE_AGE)); if (getMinimumFileAge() > 0) { ! LogService.getInstance().logInfo( ! "Minimum file age: " + getMinimumFileAge() + " ms"); } ! ! try{ ! setMinimumFileDate(this.getInfo().getStrValue(MINIMUM_FILE_DATE)); ! if (getMinimumFileDate() !=null) { ! LogService.getInstance().logInfo("Minimum file date: " + getMinimumFileDate()); ! } ! }catch (Exception e){ ! getLog().logInfo("Invalid "+MINIMUM_FILE_DATE+":" + this.getInfo().getStrValue(MINIMUM_FILE_DATE)); ! } ! // Add filename filter if exist addFilter(FILTER_FILENAME); + + // Monitor event + queue = new VFSQueue(); + initializeQueue(getInDirectory(),"true".equals(this.getInfo().getStrValue(INCLUDE_SUB_DIRS))); + + fm = new DefaultFileMonitor(new CustomFileListener(queue, log)); + fm.setRecursive("true".equals(this.getInfo().getStrValue( + INCLUDE_SUB_DIRS))); + fm.addFile(getInDirectory()); + fm.start(); + } catch (FileSystemException e1) { // TODO Auto-generated catch block *************** *** 246,249 **** --- 288,312 ---- } } + + /** + * Load into queue existing files + * @param recursive + */ + private void initializeQueue(FileObject dir ,boolean recursive) throws FileSystemException{ + dir.refresh(); + FileObject[] files = getInDirectory().getChildren(); + + for (int i = 0; i < files.length; i++) { + if (files[i].getType() == FileType.FOLDER && recursive) { + initializeQueue(files[i],recursive); + } + else { + if (acceptFile(files[i])) { // File filter + queue.addQueue(files[i]); + } + } + } + + } /** *************** *** 272,282 **** if (getDoneDirectory() != null) { ! String path = file.getParent().getURL().toString(); int index = path.length() + 1; ! //String name = path.substring(index);// ! ! FileObject outDir = fsManager.resolveFile(getDoneDirectory()+ ! path.substring(getInDirectory().getURL().toString().length(),index - 1)); // create out dir if not exist --- 335,345 ---- if (getDoneDirectory() != null) { ! String path = file.getParent().getURL().toString(); int index = path.length() + 1; ! ! FileObject outDir = fsManager.resolveFile(getDoneDirectory() ! + path.substring(getInDirectory().getURL().toString() ! .length(), index - 1)); // create out dir if not exist *************** *** 286,290 **** // create file with the same name (path) in out dir ! FileObject outfile = fsManager.resolveFile(outDir.getName().getURI() + File.separatorChar + file.getName().getBaseName()); --- 349,354 ---- // create file with the same name (path) in out dir ! FileObject outfile = fsManager.resolveFile(outDir.getName() ! .getURI() + File.separatorChar + file.getName().getBaseName()); *************** *** 293,307 **** outfile.delete(); } ! ! FileTypeSelector filter = new FileTypeSelector(FileType.FILE); outfile.createFile(); outfile.copyFrom(file, filter); outfile.close(); - // rename file - /* - * if(!file.canRenameTo(outfile)) throw new - * ScannerException(I18n.get("scanner.VFSScanner.error.finishing")); - * - */ } --- 357,365 ---- outfile.delete(); } ! ! FileTypeSelector filter = new FileTypeSelector(FileType.FILE); outfile.createFile(); outfile.copyFrom(file, filter); outfile.close(); } *************** *** 310,317 **** .get("scanner.VFSScanner.error.finishing"), e); } ! ! if(getDeleteProcesedFiles()){ if (!file.delete()) { ! throw new ScannerException(I18n.get("scanner.VFSScanner.error.finishing")+" can't delete procesed file " + file.getName().getBaseName(), null); } return; --- 368,378 ---- .get("scanner.VFSScanner.error.finishing"), e); } ! ! if (getDeleteProcesedFiles()) { if (!file.delete()) { ! throw new ScannerException(I18n ! .get("scanner.VFSScanner.error.finishing") ! + " can't delete procesed file " ! + file.getName().getBaseName(), null); } return; *************** *** 319,323 **** } ! /** * Process all files in this directory --- 380,384 ---- } ! /** * Process all files in this directory *************** *** 327,372 **** * @throws FileSystemException */ ! private void processVFS(FileObject dir) throws FileSystemException { ! dir.refresh(); ! // inner class that implements FileFilter interface ! /*org.apache.commons.vfs.FileFilter filter = new org.apache.commons.vfs.FileFilter() { ! public boolean accept(FileSelectInfo currentInfo) { ! FileObject currentFile = currentInfo.getFile(); try { ! if (currentFile.getType() == FileType.FOLDER) { ! // if iclude subDirs if it is scpecified ! return isIncludeSubDirs(); ! } else { ! // if file fulfils configured criteria ! return acceptFile(currentFile); ! } ! } catch (FileSystemException e) { ! // TODO Auto-generated catch block ! e.printStackTrace(); ! return false; ! } ! } ! }; ! */ ! ! ! FileObject[] files = dir.getChildren(); ! ! for (int i = 0; i < files.length; i++) { ! if (files[i].getType() == FileType.FOLDER) { ! if(isIncludeSubDirs()){ //Directory filter ! // add files from this folder ! processVFS(files[i]); ! } ! } else { ! if(acceptFile(files[i])){ //File filter ! // create scan document and queue it! ! try { ! processFile(files[i]); ! moveFile(files[i]); ! } catch (Exception ex) { ! log.logError( ! "Error getting document from file " + files[i], ex); ! } } } --- 388,403 ---- * @throws FileSystemException */ ! private void processVFS(ArrayList alQueue) throws FileSystemException { ! Iterator it = alQueue.iterator(); ! while (it.hasNext()) { ! FileObject file = (FileObject) it.next(); ! if (acceptFile(file)) { // File filter ! // create scan document and queue it! try { ! processFile(file); ! moveFile(file); ! } catch (Exception ex) { ! log.logError("Error getting document from file " ! + file.getName().toString(), ex); } } *************** *** 431,441 **** // Check name filter first, and then age. if (acceptEntry(FILTER_FILENAME, file.getName().toString())) { ! if (getMinimumFileAge() <= getFileAge(file)) { ! return true; ! } else { ! if (log.isDebugEnabled()) ! log.logDebug("Ignoring " + file.getName().getPathDecoded() ! + " (age " + getFileAge(file) + " < " ! + getMinimumFileAge() + ")"); } // Potentially add additional checks here. --- 462,476 ---- // Check name filter first, and then age. if (acceptEntry(FILTER_FILENAME, file.getName().toString())) { ! long modificationDate = file.getContent().getLastModifiedTime(); ! if(getMinimumFileDate().getTime() >0 && getMinimumFileDate().getTime() <= modificationDate){ ! if (getMinimumFileAge() <= getFileAge(file)) { ! return true; ! } ! } ! ! if (log.isInfoEnabled()){ ! log.logInfo("Ignoring " + file.getName().getPathDecoded() ! + " (age " + getFileAge(file) + " < "+ getMinimumFileAge() + ") or" ! + " (modification date " + new Date(modificationDate).toString() + " < " + getMinimumFileDate().toString() +")" ); } // Potentially add additional checks here. *************** *** 443,447 **** return false; } - /** --- 478,481 ---- *************** *** 463,468 **** public void setInDirectory(String inDirectory) throws FileSystemException { ! //FtpFileSystemConfigBuilder.getInstance().setPassiveMode(optsIn, true); ! this.inDirectory = fsManager.resolveFile(inDirectory,optsIn); } --- 497,503 ---- public void setInDirectory(String inDirectory) throws FileSystemException { ! // FtpFileSystemConfigBuilder.getInstance().setPassiveMode(optsIn, ! // true); ! this.inDirectory = fsManager.resolveFile(inDirectory, optsIn); } *************** *** 473,487 **** public void setDoneDirectory(String doneDirectory) throws FileSystemException { ! this.doneDirectory = fsManager.resolveFile(doneDirectory,optsOut); ! } ! ! public boolean isIncludeSubDirs() { ! return includeSubDirs; } - public void setIncludeSubDirs(boolean includeSubDirs) { - this.includeSubDirs = includeSubDirs; - } - public boolean getDeleteProcesedFiles() { return delete; --- 508,514 ---- public void setDoneDirectory(String doneDirectory) throws FileSystemException { ! this.doneDirectory = fsManager.resolveFile(doneDirectory, optsOut); } public boolean getDeleteProcesedFiles() { return delete; *************** *** 491,496 **** this.delete = delete; } - - public long getMinimumFileAge() { --- 518,521 ---- *************** *** 503,506 **** --- 528,553 ---- this.minimumFileAge = minimumFileAge; } + + public Date getMinimumFileDate() { + return minimumFileDate; + } + + public void setMinimumFileDate(String tmpDate) throws Exception{ + if(tmpDate!=null){ + int hour = 0; + int min = 0; + int yyyy = new Integer(tmpDate.substring(0,4)).intValue(); + int mm = new Integer(tmpDate.substring(5, 7)).intValue()-1; + int dd = new Integer(tmpDate.substring(8, 10)).intValue(); + + if(tmpDate.length()>10){ + hour = new Integer(tmpDate.substring(11, 13)).intValue(); + min = new Integer(tmpDate.substring(14)).intValue(); + } + GregorianCalendar c = new GregorianCalendar(); + c.set(yyyy, mm, dd,hour,min); + minimumFileDate = c.getTime(); + } + } } *************** *** 553,676 **** IConfigOptionType.INTEGER, null, false, I18n .get("scanner.VFSScannerInfo.option.minimumFileAge"))); options.add(new ConfigOption(VFSScanner.DELETE, IConfigOptionType.BOOLEAN, "true", false, I18n .get("scanner.VFSScannerInfo.option.delete"))); ! options.add(new ConfigOption(VFSScanner.IN_DOMAIN, ! IConfigOptionType.STRING, null, false, I18n ! .get("scanner.VFSScannerInfo.option.domain",VFSScanner.IN_DIR))); ! options.add(new ConfigOption(VFSScanner.IN_USERNAME, ! IConfigOptionType.STRING, null, false, I18n ! .get("scanner.VFSScannerInfo.option.username",VFSScanner.IN_DIR))); ! options.add(new ConfigOption(VFSScanner.IN_PASSWORD, ! IConfigOptionType.STRING, null, false, I18n ! .get("scanner.VFSScannerInfo.option.password",VFSScanner.IN_DIR))); ! options.add(new ConfigOption(VFSScanner.OUT_DOMAIN, ! IConfigOptionType.STRING, null, false, I18n ! .get("scanner.VFSScannerInfo.option.domain",VFSScanner.DONE_DIR))); ! options.add(new ConfigOption(VFSScanner.OUT_USERNAME, ! IConfigOptionType.STRING, null, false, I18n ! .get("scanner.VFSScannerInfo.option.username",VFSScanner.DONE_DIR))); ! options.add(new ConfigOption(VFSScanner.OUT_PASSWORD, ! IConfigOptionType.STRING, null, false, I18n ! .get("scanner.VFSScannerInfo.option.password",VFSScanner.DONE_DIR))); ! ! return options; } } ! ! /*** ! * A test listener. ! */ ! class TestListener implements FileListener ! { ! private final FileObject file; ! private final ArrayList events = new ArrayList(); ! private static final Object CREATE = "create"; ! private static final Object DELETE = "delete"; ! private static final Object CHANGED = "changed"; ! ! public TestListener(final FileObject file) ! { ! this.file = file; ! } ! ! /*** ! * Called when a file is created. ! */ ! public void fileCreated(final FileChangeEvent event) ! { ! System.out.println("fileCreated"); ! /* assertTrue("Unexpected create event", events.size() > 0); ! assertSame("Expecting a create event", CREATE, events.remove(0)); ! assertSame(file, event.getFile()); ! try ! { ! assertTrue(file.exists()); ! } ! catch (FileSystemException e) ! { ! fail(); ! } ! */ ! } ! ! /*** ! * Called when a file is deleted. ! */ ! public void fileDeleted(final FileChangeEvent event) ! { ! System.out.println("fileDeleted"); ! /* assertTrue("Unexpected delete event", events.size() > 0); ! assertSame("Expecting a delete event", DELETE, events.remove(0)); ! assertSame(file, event.getFile()); ! try ! { ! assertTrue(!file.exists()); ! } ! catch (FileSystemException e) ! { ! fail(); ! } ! */ ! } ! ! public void fileChanged(FileChangeEvent event) throws Exception ! { ! System.out.println("fileChanged"); ! /*assertTrue("Unexpected changed event", events.size() > 0); ! assertSame("Expecting a changed event", CHANGED, events.remove(0)); ! assertSame(file, event.getFile()); ! try ! { ! assertTrue(!file.exists()); ! } ! catch (FileSystemException e) ! { ! fail(); ! } ! */ ! } ! ! public void addCreateEvent() ! { ! events.add(CREATE); ! } ! ! public void addChangeEvent() ! { ! events.add(CHANGED); ! } ! ! public void assertFinished() ! { ! //assertEquals("Missing event", 0, events.size()); ! } } \ No newline at end of file --- 600,794 ---- IConfigOptionType.INTEGER, null, false, I18n .get("scanner.VFSScannerInfo.option.minimumFileAge"))); + + //Default minium date (is creating date) + GregorianCalendar g = new GregorianCalendar(); + int min = g.get(GregorianCalendar.MINUTE); + int hor = g.get(GregorianCalendar.HOUR_OF_DAY); + int d = g.get(GregorianCalendar.DATE); + int m = g.get(GregorianCalendar.MONTH)+1; + int y = g.get(GregorianCalendar.YEAR); + String defaultDate = ""+y; + + if(m<10)defaultDate += "/0"+m; + else defaultDate += "/"+m; + + if(d<10) defaultDate = "/0"+d; + else defaultDate = "/"+d; + + if(hor<10) defaultDate += " 0"+hor; + else defaultDate += " "+hor; + + if(min<10) defaultDate += ":0"+min; + else defaultDate += ":"+min; + + g.toString(); + options.add(new ConfigOption(VFSScanner.MINIMUM_FILE_DATE, + IConfigOptionType.STRING,defaultDate, false, I18n + .get("scanner.VFSScannerInfo.option.minimumFileDate"))); options.add(new ConfigOption(VFSScanner.DELETE, IConfigOptionType.BOOLEAN, "true", false, I18n .get("scanner.VFSScannerInfo.option.delete"))); ! options.add(new ConfigOption(VFSScanner.IN_DOMAIN, ! IConfigOptionType.STRING, null, false, I18n.get( ! "scanner.VFSScannerInfo.option.domain", ! VFSScanner.IN_DIR))); ! options.add(new ConfigOption(VFSScanner.IN_USERNAME, ! IConfigOptionType.STRING, null, false, I18n.get( ! "scanner.VFSScannerInfo.option.username", ! VFSScanner.IN_DIR))); ! options.add(new ConfigOption(VFSScanner.IN_PASSWORD, ! IConfigOptionType.STRING, null, false, I18n.get( ! "scanner.VFSScannerInfo.option.password", ! VFSScanner.IN_DIR))); ! options.add(new ConfigOption(VFSScanner.OUT_DOMAIN, ! IConfigOptionType.STRING, null, false, I18n.get( ! "scanner.VFSScannerInfo.option.domain", ! VFSScanner.DONE_DIR))); ! options.add(new ConfigOption(VFSScanner.OUT_USERNAME, ! IConfigOptionType.STRING, null, false, I18n.get( ! "scanner.VFSScannerInfo.option.username", ! VFSScanner.DONE_DIR))); ! options.add(new ConfigOption(VFSScanner.OUT_PASSWORD, ! IConfigOptionType.STRING, null, false, I18n.get( ! "scanner.VFSScannerInfo.option.password", ! VFSScanner.DONE_DIR))); ! return options; } } ! ! /** ! * VFSQueue is a synchronized queue ! * ! * @author paspes ! * ! */ ! class VFSQueue { ! private static int OPERATION_ADD = 1; ! ! private static int OPERATION_GET = 2; ! ! private ArrayList alQueue; ! ! public VFSQueue() { ! alQueue = new ArrayList(); ! } ! ! /** ! * Get FileObject queue ! * ! * @return Arraylist with FileObjects ! */ ! public ArrayList getQueue() { ! return queue(OPERATION_GET, null); ! } ! ! /** ! * Add FileObject to queue ! * ! * @param file ! * FileObject to add ! */ ! public void addQueue(FileObject file) { ! queue(OPERATION_ADD, file); ! } ! ! /** ! * Method to add and remove FileObjects to queue ! * ! * @param operation ! * (add fileObject or get queue) ! * @param fileObject ! * to add ! * @return queue of FileObjects ! */ ! private synchronized ArrayList queue(int operation, FileObject file) { ! if (operation == OPERATION_ADD) { ! alQueue.add(file); ! return alQueue; ! } ! if (operation == OPERATION_GET) { ! ArrayList alReturn = (ArrayList) alQueue.clone(); ! alQueue.clear(); ! return alReturn; ! } ! return null; ! ! } ! } ! ! /** ! * CustomFileListener send FileObjects to VFSScanner ! * ! * @author paspes ! * ! */ ! class CustomFileListener implements FileListener { ! ! private VFSQueue queue; ! ! private LogService log; ! ! public CustomFileListener(VFSQueue queue, LogService log) { ! this.queue = queue; ! this.log = log; ! } ! ! /** ! * Called when a file is created. ! */ ! public void fileCreated(final FileChangeEvent event) { ! if (log.isDebugEnabled()) { ! log.logDebug("fileCreated into VSFScanner: " ! + event.getFile().getName()); ! } ! queue.addQueue(event.getFile()); ! /* ! * assertTrue("Unexpected create event", events.size() > 0); ! * assertSame("Expecting a create event", CREATE, events.remove(0)); ! * assertSame(file, event.getFile()); try { assertTrue(file.exists()); } ! * catch (FileSystemException e) { fail(); } ! */ ! } ! ! /** ! * Called when a file is deleted. ! */ ! public void fileDeleted(final FileChangeEvent event) { ! if (log.isDebugEnabled()) { ! log.logDebug("fileDeleted into VSFScanner: " ! + event.getFile().getName()); ! } ! /* ! * assertTrue("Unexpected delete event", events.size() > 0); ! * assertSame("Expecting a delete event", DELETE, events.remove(0)); ! * assertSame(file, event.getFile()); try { assertTrue(!file.exists()); } ! * catch (FileSystemException e) { fail(); } ! */ ! } ! ! /** ! * Called when a file is changed. ! */ ! public void fileChanged(FileChangeEvent event) throws Exception { ! if (log.isDebugEnabled()) { ! log.logDebug("fileChanged into VSFScanner: " ! + event.getFile().getName()); ! } ! queue.addQueue(event.getFile()); ! /* ! * assertTrue("Unexpected changed event", events.size() > 0); ! * assertSame("Expecting a changed event", CHANGED, events.remove(0)); ! * assertSame(file, event.getFile()); try { assertTrue(!file.exists()); } ! * catch (FileSystemException e) { fail(); } ! */ ! } ! } \ No newline at end of file |