From: paspes <pa...@us...> - 2007-12-21 12:06:26
|
Update of /cvsroot/babeldoc/babeldoc/modules/scanner/src/com/babeldoc/scanner/worker In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv11806/modules/scanner/src/com/babeldoc/scanner/worker Modified Files: VFSScanner.java Log Message: Index: VFSScanner.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/scanner/src/com/babeldoc/scanner/worker/VFSScanner.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** VFSScanner.java 17 Dec 2007 16:00:03 -0000 1.4 --- VFSScanner.java 21 Dec 2007 12:05:33 -0000 1.5 *************** *** 67,70 **** --- 67,71 ---- import java.io.ByteArrayOutputStream; + import java.io.File; import java.io.InputStream; import java.util.ArrayList; *************** *** 72,79 **** import java.util.Date; import org.apache.commons.vfs.FileContent; ! import org.apache.commons.vfs.FileFilterSelector; import org.apache.commons.vfs.FileObject; ! import org.apache.commons.vfs.FileSelectInfo; import org.apache.commons.vfs.FileSystemException; import org.apache.commons.vfs.FileSystemManager; --- 73,81 ---- import java.util.Date; + import org.apache.commons.vfs.FileChangeEvent; import org.apache.commons.vfs.FileContent; ! 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; *************** *** 81,88 **** import org.apache.commons.vfs.FileType; import org.apache.commons.vfs.FileTypeSelector; - import org.apache.commons.vfs.CacheStrategy; import org.apache.commons.vfs.VFS; import org.apache.commons.vfs.auth.StaticUserAuthenticator; import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder; import com.babeldoc.core.I18n; --- 83,90 ---- import org.apache.commons.vfs.FileType; import org.apache.commons.vfs.FileTypeSelector; import org.apache.commons.vfs.VFS; 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; *************** *** 91,95 **** import com.babeldoc.core.option.ConfigOption; import com.babeldoc.core.option.IConfigOptionType; - import com.babeldoc.core.option.ValueListConfigOptionType; import com.babeldoc.core.pipeline.PipelineDocument; import com.babeldoc.scanner.ScannerConfigurationException; --- 93,96 ---- *************** *** 113,120 **** public static final String FILTER_FILENAME = "filter"; public static final String MINIMUM_FILE_AGE = "minimumFileAge"; ! public static final String SCAN_NEW = "scanNew"; ! ! public static final String SCAN_NEW_DELETE = "delete"; ! public static final String SCAN_NEW_FOLDER = "doneFolder"; private LogService log = LogService.getInstance(this.getClass().getName()); --- 114,125 ---- 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"; private LogService log = LogService.getInstance(this.getClass().getName()); *************** *** 131,135 **** private FileSystemManager fsManager; ! private FileSystemOptions opts; /** URI to scan */ --- 136,144 ---- private FileSystemManager fsManager; ! private FileSystemOptions optsIn; ! private FileSystemOptions optsOut; ! ! private FileSystem fs; ! private TestListener listener; /** URI to scan */ *************** *** 141,144 **** --- 150,156 ---- /** flag to include sub directories */ private boolean includeSubDirs = false; + + /** flag to delete procesed files */ + private boolean delete = true; /** *************** *** 172,182 **** public void initialize() throws ScannerConfigurationException { try { - StaticUserAuthenticator auth = new StaticUserAuthenticator("username", "password", null); - opts = new FileSystemOptions(); - DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth); - //FileObject fo = VFS.getManager().resolveFile("smb://host/anyshare/dir", opts); fsManager = VFS.getManager(); ! // new File((String) this.getInfo().getOption(IN_DIR).getValue())); setInDirectory(this.getInfo().getStrValue(IN_DIR)); --- 184,200 ---- public void initialize() throws ScannerConfigurationException { 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)); *************** *** 187,200 **** getInDirectory()), null); } setDoneDirectory(this.getInfo().getStrValue(DONE_DIR)); if (getDoneDirectory() != null && !"".equals(getDoneDirectory())) { - /* - * // if the done directory does not end with '/' then add one. - * if (!getDoneDirectory().endsWith(File.pathSeparator)) { - * setDoneDirectory(getDoneDirectory() + File.separator); } - */ - // Check of the donedirectory actually is a directory if (doneDirectory.getType() != FileType.FOLDER) { --- 205,220 ---- 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)); if (getDoneDirectory() != null && !"".equals(getDoneDirectory())) { // Check of the donedirectory actually is a directory if (doneDirectory.getType() != FileType.FOLDER) { *************** *** 204,222 **** } } else { ! getLog() ! .logInfo( ! "Done directory not specified. All processed file will be deleted!!!"); setDoneDirectory(null); } ! setIncludeSubDirs("true".equals(this.getInfo().getStrValue( ! INCLUDE_SUB_DIRS))); ! ! setMinimumFileAge(this.getInfo().getIntValue(MINIMUM_FILE_AGE)); if (getMinimumFileAge() > 0) { ! LogService.getInstance().logInfo( ! "Minimum file age: " + getMinimumFileAge() + " ms"); } --- 224,240 ---- } } 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"); } *************** *** 254,260 **** if (getDoneDirectory() != null) { ! ! String path = file.getName().getPath(); ! FileObject outDir = fsManager.resolveFile(getDoneDirectory()); // create out dir if not exist --- 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 *************** *** 264,270 **** // create file with the same name (path) in out dir ! FileObject outfile = fsManager.resolveFile(outDir.getName() ! .getURI() ! + "/" + file.getName().getBaseName()); // delete file it is already exists --- 286,291 ---- // create file with the same name (path) in out dir ! FileObject outfile = fsManager.resolveFile(outDir.getName().getURI() ! + File.separatorChar + file.getName().getBaseName()); // delete file it is already exists *************** *** 289,311 **** .get("scanner.VFSScanner.error.finishing"), e); } ! ! if (this.getInfo().getStrValue(SCAN_NEW) == VFSScanner.SCAN_NEW_DELETE) { if (!file.delete()) { ! throw new ScannerException(I18n ! .get("scanner.VFSScanner.error.finishing"), null); ! // Exceptio no es podra saber si es nou pq no ha borrat!!! } return; } - if (this.getInfo().getStrValue(SCAN_NEW) == VFSScanner.SCAN_NEW_FOLDER) { - if (getDoneDirectory() == null) - throw new ScannerException(I18n - .get("scanner.VFSScanner.error.finishing"), null); - // Exceptio no es podra saber si es nou pq no a mogut!!! - return; - - } - } --- 310,321 ---- .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; } } *************** *** 345,352 **** for (int i = 0; i < files.length; i++) { if (files[i].getType() == FileType.FOLDER) { ! // add files from this folder ! processVFS(files[i]); } else { ! if(acceptFile(files[i])){ // create scan document and queue it! try { --- 355,364 ---- 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 { *************** *** 416,421 **** */ private boolean acceptFile(FileObject file) throws FileSystemException { - // Afegir if ( this.getInfo().getStrValue(SCAN_NEW) == - // VFSScanner.SCAN_NEW_DELETE ){ i FOLDER // Check name filter first, and then age. --- 428,431 ---- *************** *** 433,436 **** --- 443,447 ---- return false; } + /** *************** *** 452,457 **** public void setInDirectory(String inDirectory) throws FileSystemException { ! this.inDirectory = fsManager.resolveFile(this.getInfo().getStrValue( ! IN_DIR),opts); } --- 463,468 ---- public void setInDirectory(String inDirectory) throws FileSystemException { ! //FtpFileSystemConfigBuilder.getInstance().setPassiveMode(optsIn, true); ! this.inDirectory = fsManager.resolveFile(inDirectory,optsIn); } *************** *** 462,467 **** public void setDoneDirectory(String doneDirectory) throws FileSystemException { ! this.doneDirectory = fsManager.resolveFile(this.getInfo().getStrValue( ! DONE_DIR)); } --- 473,477 ---- public void setDoneDirectory(String doneDirectory) throws FileSystemException { ! this.doneDirectory = fsManager.resolveFile(doneDirectory,optsOut); } *************** *** 473,476 **** --- 483,496 ---- this.includeSubDirs = includeSubDirs; } + + public boolean getDeleteProcesedFiles() { + return delete; + } + + public void setDeleteProcesedFiles(boolean delete) { + this.delete = delete; + } + + public long getMinimumFileAge() { *************** *** 534,545 **** .get("scanner.VFSScannerInfo.option.minimumFileAge"))); ! String[] scanNewTypes = { VFSScanner.SCAN_NEW_DELETE, ! VFSScanner.SCAN_NEW_FOLDER }; ! options.add(new ConfigOption(VFSScanner.SCAN_NEW, ! new ValueListConfigOptionType(scanNewTypes), ! VFSScanner.SCAN_NEW_DELETE, false, I18n ! .get("scanner.VfsScannerInfo.option.scanNew"))); ! return options; } } --- 554,676 ---- .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 |