List of usage examples for org.apache.commons.net.ftp FTPFile getName
public String getName()
From source file:org.apache.camel.component.file.remote.strategy.CachedFtpChangedExclusiveReadLockStrategy.java
@Override public boolean acquireExclusiveReadLock(GenericFileOperations<FTPFile> operations, GenericFile<FTPFile> file, Exchange exchange) throws Exception { boolean exclusive = false; FTPFile target = file.getFile(); FTPFile newStats = target;/* ww w . ja va 2 s .c o m*/ FTPFile oldStats = fileCache.get(target.getName()); if (oldStats != null) { // There is no "created time" available when using FTP. So we can't check the minAge. if (newStats.getSize() >= getMinLength() && (oldStats.getSize() == newStats.getSize() && oldStats.getTimestamp().equals(newStats.getTimestamp())) && ((System.currentTimeMillis() - newStats.getTimestamp().getTimeInMillis()) >= getCheckInterval())) { LOG.debug("File [{}] seems to have stopped changing. Attempting to grab a read lock...", target); exclusive = true; } else { LOG.debug("File [{}] is still changing. Will check it again on the next poll.", target); fileCache.put(target.getName(), newStats); exclusive = false; } } else { LOG.debug("File [{}] is not yet known. Will add it to the cache and check again on the next poll.", target); fileCache.put(target.getName(), newStats); exclusive = false; } if (exclusive) { LOG.debug("Got an exclusive lock for file [{}].", target); fileCache.remove(target.getName()); } return exclusive; }
From source file:org.apache.camel.component.file.remote.strategy.FtpChangedExclusiveReadLockStrategy.java
public boolean acquireExclusiveReadLock(GenericFileOperations<FTPFile> operations, GenericFile<FTPFile> file, Exchange exchange) throws Exception { boolean exclusive = false; LOG.trace("Waiting for exclusive read lock to file: " + file); long lastModified = Long.MIN_VALUE; long length = Long.MIN_VALUE; StopWatch watch = new StopWatch(); while (!exclusive) { // timeout check if (timeout > 0) { long delta = watch.taken(); if (delta > timeout) { CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file); // we could not get the lock within the timeout period, so return false return false; }/*from w w w .j a v a2 s . com*/ } long newLastModified = 0; long newLength = 0; List<FTPFile> files; if (fastExistsCheck) { // use the absolute file path to only pickup the file we want to check, this avoids expensive // list operations if we have a lot of files in the directory LOG.trace("Using fast exists to update file information for {}", file); files = operations.listFiles(file.getAbsoluteFilePath()); } else { LOG.trace( "Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file); // fast option not enabled, so list the directory and filter the file name files = operations.listFiles(file.getParent()); } LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size()); for (FTPFile f : files) { if (f.getName().equals(file.getFileNameOnly())) { newLastModified = f.getTimestamp().getTimeInMillis(); newLength = f.getSize(); } } LOG.trace("Previous last modified: " + lastModified + ", new last modified: " + newLastModified); LOG.trace("Previous length: " + length + ", new length: " + newLength); if (length >= minLength && (newLastModified == lastModified && newLength == length)) { LOG.trace("Read lock acquired."); exclusive = true; } else { // set new base file change information lastModified = newLastModified; length = newLength; boolean interrupted = sleep(); if (interrupted) { // we were interrupted while sleeping, we are likely being shutdown so return false return false; } } } return exclusive; }
From source file:org.apache.camel.component.file.remote.strategy.FtpChangedExclusiveReadLockStrategy.java
public boolean acquireExclusiveReadLock(GenericFileOperations<FTPFile> operations, GenericFile<FTPFile> file, Exchange exchange) throws Exception { boolean exclusive = false; LOG.trace("Waiting for exclusive read lock to file: " + file); long lastModified = Long.MIN_VALUE; long length = Long.MIN_VALUE; StopWatch watch = new StopWatch(); while (!exclusive) { // timeout check if (timeout > 0) { long delta = watch.taken(); if (delta > timeout) { CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file); // we could not get the lock within the timeout period, so return false return false; }//from w ww .j a va 2s .com } long newLastModified = 0; long newLength = 0; List<FTPFile> files; if (fastExistsCheck) { // use the absolute file path to only pickup the file we want to check, this avoids expensive // list operations if we have a lot of files in the directory LOG.trace("Using fast exists to update file information for {}", file); files = operations.listFiles(file.getAbsoluteFilePath()); } else { LOG.trace( "Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file); // fast option not enabled, so list the directory and filter the file name files = operations.listFiles(file.getParent()); } LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size()); for (FTPFile f : files) { if (f.getName().equals(file.getFileNameOnly())) { newLength = f.getSize(); newLastModified = f.getTimestamp().getTimeInMillis(); } } LOG.trace("Previous last modified: " + lastModified + ", new last modified: " + newLastModified); LOG.trace("Previous length: " + length + ", new length: " + newLength); if (length >= minLength && (newLastModified == lastModified && newLength == length)) { LOG.trace("Read lock acquired."); exclusive = true; } else { // set new base file change information lastModified = newLastModified; length = newLength; boolean interrupted = sleep(); if (interrupted) { // we were interrupted while sleeping, we are likely being shutdown so return false return false; } } } return exclusive; }
From source file:org.apache.camel.component.file.remote.strategy.FtpChangedExclusiveReadLockStrategy.java
public boolean acquireExclusiveReadLock(GenericFileOperations<FTPFile> operations, GenericFile<FTPFile> file, Exchange exchange) throws Exception { boolean exclusive = false; LOG.trace("Waiting for exclusive read lock to file: " + file); long lastModified = Long.MIN_VALUE; long length = Long.MIN_VALUE; StopWatch watch = new StopWatch(); while (!exclusive) { // timeout check if (timeout > 0) { long delta = watch.taken(); if (delta > timeout) { CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file); // we could not get the lock within the timeout period, so return false return false; }/*from w w w . ja v a2s . co m*/ } long newLastModified = 0; long newLength = 0; List<FTPFile> files; if (fastExistsCheck) { // use the absolute file path to only pickup the file we want to check, this avoids expensive // list operations if we have a lot of files in the directory LOG.trace("Using fast exists to update file information for {}", file); files = operations.listFiles(file.getAbsoluteFilePath()); } else { LOG.trace( "Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file); // fast option not enabled, so list the directory and filter the file name files = operations.listFiles(file.getParent()); } LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size()); for (FTPFile f : files) { if (f.getName().equals(file.getFileNameOnly())) { newLength = f.getSize(); if (f.getTimestamp() != null) { newLastModified = f.getTimestamp().getTimeInMillis(); } } } LOG.trace("Previous last modified: " + lastModified + ", new last modified: " + newLastModified); LOG.trace("Previous length: " + length + ", new length: " + newLength); if (length >= minLength && (newLastModified == lastModified && newLength == length)) { LOG.trace("Read lock acquired."); exclusive = true; } else { // set new base file change information lastModified = newLastModified; length = newLength; boolean interrupted = sleep(); if (interrupted) { // we were interrupted while sleeping, we are likely being shutdown so return false return false; } } } return exclusive; }
From source file:org.apache.flume.source.FTPSource.java
@SuppressWarnings("UnnecessaryContinue") public void discoverElements(FTPClient ftpClient, String parentDir, String currentDir, int level) throws IOException { String dirToList = parentDir; if (!currentDir.equals("")) { dirToList += "/" + currentDir; }//from w ww.j a v a 2 s .c o m FTPFile[] subFiles = ftpClient.listFiles(dirToList); if (subFiles != null && subFiles.length > 0) { for (FTPFile aFile : subFiles) { String currentFileName = aFile.getName(); if (currentFileName.equals(".") || currentFileName.equals("..")) { log.info("Skip parent directory and directory itself"); continue; } if (aFile.isDirectory()) { log.info("[" + aFile.getName() + "]"); ftpClient.changeWorkingDirectory(parentDir); discoverElements(ftpClient, dirToList, aFile.getName(), level + 1); continue; } else if (aFile.isFile()) { //aFile is a regular file ftpClient.changeWorkingDirectory(dirToList); existFileList.add(dirToList + "/" + aFile.getName()); //control of deleted files in server String fileName = aFile.getName(); if (!(sizeFileList.containsKey(dirToList + "/" + aFile.getName()))) { //new file ftpSourceCounter.incrementFilesCount(); InputStream inputStream = null; try { inputStream = ftpClient.retrieveFileStream(aFile.getName()); listener.fileStreamRetrieved(); readStream(inputStream, 0); boolean success = inputStream != null && ftpClient.completePendingCommand(); //mandatory if (success) { sizeFileList.put(dirToList + "/" + aFile.getName(), aFile.getSize()); saveMap(sizeFileList); ftpSourceCounter.incrementFilesProcCount(); log.info("discovered: " + fileName + " ," + sizeFileList.size()); } else { handleProcessError(fileName); } } catch (FTPConnectionClosedException e) { handleProcessError(fileName); log.error("Ftp server closed connection ", e); continue; } ftpClient.changeWorkingDirectory(dirToList); continue; } else { //known file long dif = aFile.getSize() - sizeFileList.get(dirToList + "/" + aFile.getName()); if (dif > 0) { //known and modified long prevSize = sizeFileList.get(dirToList + "/" + aFile.getName()); InputStream inputStream = null; try { inputStream = ftpClient.retrieveFileStream(aFile.getName()); listener.fileStreamRetrieved(); readStream(inputStream, prevSize); boolean success = inputStream != null && ftpClient.completePendingCommand(); //mandatory if (success) { sizeFileList.put(dirToList + "/" + aFile.getName(), aFile.getSize()); saveMap(sizeFileList); ftpSourceCounter.incrementCountModProc(); log.info("modified: " + fileName + " ," + sizeFileList.size()); } else { handleProcessError(fileName); } } catch (FTPConnectionClosedException e) { log.error("Ftp server closed connection ", e); handleProcessError(fileName); continue; } ftpClient.changeWorkingDirectory(dirToList); continue; } else if (dif < 0) { //known and full modified existFileList.remove(dirToList + "/" + aFile.getName()); //will be rediscovered as new file saveMap(sizeFileList); continue; } ftpClient.changeWorkingDirectory(parentDir); continue; } } else if (aFile.isSymbolicLink()) { log.info(aFile.getName() + " is a link of " + aFile.getLink() + " access denied"); ftpClient.changeWorkingDirectory(parentDir); continue; } else if (aFile.isUnknown()) { log.info(aFile.getName() + " unknown type of file"); ftpClient.changeWorkingDirectory(parentDir); continue; } else { ftpClient.changeWorkingDirectory(parentDir); continue; } } //fin de bucle } //el listado no es vaco }
From source file:org.apache.ftpserver.clienttests.ListTest.java
public void testListFilesInDir() throws Exception { TEST_DIR1.mkdirs();/* w ww .jav a2s .c o m*/ TEST_FILE_IN_DIR1.createNewFile(); TEST_DIR_IN_DIR1.mkdirs(); FTPFile[] files = client.listFiles(TEST_DIR1.getName()); assertEquals(2, files.length); FTPFile file = getFile(files, TEST_FILE_IN_DIR1.getName()); assertEquals(TEST_FILE_IN_DIR1.getName(), file.getName()); assertEquals(0, file.getSize()); assertEquals("group", file.getGroup()); assertEquals("user", file.getUser()); assertTrue(file.isFile()); assertFalse(file.isDirectory()); file = getFile(files, TEST_DIR_IN_DIR1.getName()); assertEquals(TEST_DIR_IN_DIR1.getName(), file.getName()); assertEquals(0, file.getSize()); assertEquals("group", file.getGroup()); assertEquals("user", file.getUser()); assertFalse(file.isFile()); assertTrue(file.isDirectory()); }
From source file:org.apache.ftpserver.clienttests.ListTest.java
public void testListFile() throws Exception { TEST_DIR1.mkdirs();//from w w w .ja v a 2s . c om TEST_FILE1.createNewFile(); TEST_FILE2.createNewFile(); FTPFile[] files = client.listFiles(TEST_FILE1.getName()); assertEquals(1, files.length); FTPFile file = getFile(files, TEST_FILE1.getName()); assertEquals(TEST_FILE1.getName(), file.getName()); assertEquals(0, file.getSize()); assertEquals("group", file.getGroup()); assertEquals("user", file.getUser()); assertTrue(file.isFile()); assertFalse(file.isDirectory()); Calendar expectedTimestamp = Calendar.getInstance(); expectedTimestamp.setTimeInMillis(TEST_FILE1.lastModified()); // server does not supply seconds and milliseconds expectedTimestamp.clear(Calendar.SECOND); expectedTimestamp.clear(Calendar.MILLISECOND); assertEquals(expectedTimestamp, file.getTimestamp()); }
From source file:org.apache.ftpserver.clienttests.ListTest.java
public void testListFileNoArgument() throws Exception { TEST_DIR1.mkdirs();/*from ww w. j av a2 s .c om*/ FTPFile[] files = client.listFiles(); assertEquals(1, files.length); FTPFile file = getFile(files, TEST_DIR1.getName()); assertEquals(TEST_DIR1.getName(), file.getName()); assertEquals(0, file.getSize()); assertEquals("group", file.getGroup()); assertEquals("user", file.getUser()); assertFalse(file.isFile()); assertTrue(file.isDirectory()); }
From source file:org.apache.ftpserver.clienttests.ListTest.java
public void testListFiles() throws Exception { TEST_FILE1.createNewFile();// w ww . j a v a 2 s . c o m TEST_FILE2.createNewFile(); TEST_DIR1.mkdirs(); TEST_DIR2.mkdirs(); TestUtil.writeDataToFile(TEST_FILE1, testData); FTPFile[] files = client.listFiles(); assertEquals(4, files.length); FTPFile file = getFile(files, TEST_FILE1.getName()); assertEquals(TEST_FILE1.getName(), file.getName()); assertEquals(testData.length, file.getSize()); assertEquals("group", file.getGroup()); assertEquals("user", file.getUser()); assertTrue(file.isFile()); assertFalse(file.isDirectory()); file = getFile(files, TEST_FILE2.getName()); assertEquals(TEST_FILE2.getName(), file.getName()); assertEquals(0, file.getSize()); assertEquals("group", file.getGroup()); assertEquals("user", file.getUser()); assertTrue(file.isFile()); assertFalse(file.isDirectory()); file = getFile(files, TEST_DIR1.getName()); assertEquals(TEST_DIR1.getName(), file.getName()); assertEquals(0, file.getSize()); assertEquals("group", file.getGroup()); assertEquals("user", file.getUser()); assertFalse(file.isFile()); assertTrue(file.isDirectory()); file = getFile(files, TEST_DIR2.getName()); assertEquals(TEST_DIR2.getName(), file.getName()); assertEquals(0, file.getSize()); assertEquals("group", file.getGroup()); assertEquals("user", file.getUser()); assertFalse(file.isFile()); assertTrue(file.isDirectory()); }
From source file:org.apache.ftpserver.clienttests.ListTest.java
private FTPFile getFile(FTPFile[] files, String name) { for (int i = 0; i < files.length; i++) { FTPFile file = files[i]; if (name.equals(file.getName())) { return file; }//ww w . j a v a 2s.c o m } return null; }