From: Albert A. <ap...@em...> - 2005-11-04 15:22:14
|
New diff against v1.24. Fixes problem with FTP server timeouts, directory tree iteration (see previous msg) ciao apa *** FtpScanner.java Mon Oct 31 11:27:07 2005 --- modules/scanner/src/com/babeldoc/scanner/worker/FtpScanner.java Fri Nov= 4 10:29:31 2005 *************** *** 81,84 **** --- 81,85 ---- import org.apache.commons.net.ftp.FTPFileListParser; import org.apache.commons.net.ftp.FTPReply; + import org.apache.commons.net.ftp.FTPConnectionClosedException; =20=20 import com.babeldoc.core.I18n; *************** *** 110,113 **** --- 111,115 ---- public static final String SCAN_SUBFOLDERS =3D "includeSubfolders"; public static final String FILTER_FILENAME =3D "filter"; + public static final String MAX_DEPTH =3D "maxDepth"; =20=20 //private final static int rertyCount =3D 3; *************** *** 118,122 **** private String ftpPassword =3D ""; private String ftpUsername =3D ""; ! private int ftpFileType =3D FTPClient.ASCII_FILE_TYPE; private String localBackupFolder =3D ""; private boolean scanSubfolders =3D false; --- 120,126 ---- private String ftpPassword =3D ""; private String ftpUsername =3D ""; ! private int ftpFileType =3D FTPClient.ASCII_FILE_TYPE; ! private int maxDepth =3D 0; ! private int curDepth =3D 0; private String localBackupFolder =3D ""; private boolean scanSubfolders =3D false; *************** *** 166,176 **** */ public void doScan() throws ScannerException { try { ! processFtpFolder(ftpFolder); } catch (Exception e) { ! throw new ScannerException( I18n.get("scanner.FtpScanner.error.general"), e); ! } } =20=20 --- 170,199 ---- */ public void doScan() throws ScannerException { + boolean err =3D false; try { ! processFtpFolder(ftpFolder); ! } catch (FTPConnectionClosedException cl){ ! err =3D true; ! if(log.isDebugEnabled()) ! log.logDebug("Connection to FTP server was terminated"); } catch (Exception e) { ! err =3D true; ! throw new ScannerException( I18n.get("scanner.FtpScanner.error.general"), e); ! } finally { ! try { ! if (err){ ! if(log.isDebugEnabled()) ! log.logDebug("Need to reconnect before scanning ..."); !=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20 ! ftpClient.disconnect(); ! connectToHost(); ! processFtpFolder(ftpFolder); ! } ! }catch (Exception re){ ! throw new ScannerException("Reconnection to FTPSe= rver failed"); ! } ! } } =20=20 *************** *** 191,194 **** --- 214,218 ---- .equalsIgnoreCase(this.getInfo().getStrValue(SCAN_SUBFOLDERS)))) { scanSubfolders =3D true; + maxDepth =3D Integer.parseInt(this.getInfo().getStrValue(MAX_DEPTH)); } =20=20 *************** *** 238,249 **** if (ftpClient.login(ftpUsername, ftpPassword)) { if (ftpClient.changeWorkingDirectory(ftpFolder)) { ! if(ftpClient.setFileType(ftpFileType)) { ! // ! } else { ! ftpClient.disconnect(); ! throw new ScannerException( ! I18n.get("scanner.FtpScanner.error.filetype")); ! } ! } else { ftpClient.disconnect(); throw new ScannerException( --- 262,273 ---- if (ftpClient.login(ftpUsername, ftpPassword)) { if (ftpClient.changeWorkingDirectory(ftpFolder)) { ! if(ftpClient.setFileType(ftpFileType)) { ! // ! } else { ! ftpClient.disconnect(); ! throw new ScannerException( ! I18n.get("scanner.FtpScanner.error.filetype")); ! } ! } else { ftpClient.disconnect(); throw new ScannerException( *************** *** 347,352 **** =20=20 if ((ftpOutFolder !=3D null) && (!ftpOutFolder.equals(""))) { ! String ftpOutPath =3D ! ftpOutFolder + fileName.substring(ftpFolder.length()); ftpClient.rename(fileName, ftpOutPath); } --- 371,387 ---- =20=20 if ((ftpOutFolder !=3D null) && (!ftpOutFolder.equals(""))) { ! StringBuffer sbOutPath =3D new StringBuff= er(); ! if (ftpOutFolder.endsWith("/")){ ! sbOutPath.append( ! ftpOutFolder + fileName.substring(fileName.indexOf(scanFileName)) ! ); ! }else{ ! sbOutPath.append( ! ftpOutFolder + "/"=20 ! + fileName.substring(fileName.ind= exOf(scanFileName)) ! );=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 ! } ! String ftpOutPath =3D sbOutPath.toString(= ); !=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 ftpClient.rename(fileName, ftpOutPath); } *************** *** 375,379 **** * @throws ScannerException */ ! private void processFtpFolder(String folder) throws ScannerException { //This is inner class that implements FileListParser interface //It is used for parsing files returned from MS ftp server --- 410,414 ---- * @throws ScannerException */ ! private void processFtpFolder(String folder) throws ScannerException, FT= PConnectionClosedException { //This is inner class that implements FileListParser interface //It is used for parsing files returned from MS ftp server *************** *** 462,474 **** log.logDebug("Scanning " + ftpClient.printWorkingDirectory()); } !=20 ! if ((ftpClient.getReplyCode() =3D=3D FTPReply.CLOSING_DATA_CONNECTION) ! || (ftpClient.getReplyCode() =3D=3D FTPReply.SERVICE_NOT_AVAILABLE)) { ! ftpClient.disconnect(); ! connectToHost(); ! } !=20 //first change working dir! ftpClient.changeWorkingDirectory(folder); =20=20 FTPFile[] ftpFiles =3D null; --- 497,504 ---- log.logDebug("Scanning " + ftpClient.printWorkingDirectory()); } !=20=09=09=09 //first change working dir! ftpClient.changeWorkingDirectory(folder); + curDepth++; =20=20 FTPFile[] ftpFiles =3D null; *************** *** 499,502 **** --- 529,535 ---- =20=20 if (ftpFiles =3D=3D null) { + // no files found in this dir - so return to parent + ftpClient.changeToParentDirectory(); + curDepth--; return; } *************** *** 515,521 **** } else if (ftpFiles[i].isDirectory() && scanSubfolders) { //Get files from new current diretory by executing this method recur= s. ! processFtpFolder(ftpFiles[i].getName()); } } } catch (Exception e) { throw new ScannerException( --- 548,564 ---- } else if (ftpFiles[i].isDirectory() && scanSubfolders) { //Get files from new current diretory by executing this method recur= s. ! if (curDepth <=3D maxDepth){ ! //Walk down the tree until max depth limit is reached ! processFtpFolder(ftpFiles[i].getName()); ! } } } + // return to parent, otherwise you get stuck at the end of the first b= ranch + // when walking down subdirs + ftpClient.changeToParentDirectory(); + curDepth--; + } catch (FTPConnectionClosedException ce) { + // The FTP Server has probably terminated the con= nection due to some timeout + throw new FTPConnectionClosedException("Oops, disconnected!"); } catch (Exception e) { throw new ScannerException( *************** *** 618,622 **** --- 661,674 ---- I18n.get("scanner.FtpScannerInfo.option.filter"))); =20=20 + options.add( + new ConfigOption( + FtpScanner.MAX_DEPTH, + IConfigOptionType.STRING, + "0", + false, + "scanner.FtpScannerInfo.option.maxDepth")); +=20 return options; } } +=20 --=20 ___________________________________________________ Play 100s of games for FREE! http://games.mail.com/ |