Author: ningjiang
Date: Mon Aug 29 04:49:33 2011
New Revision: 1162647
URL: http://svn.apache.org/viewvc?rev=1162647&view=rev
Log:
CAMEL-4356 add an option fastExist to test the file exist in the FTP server
Added:
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileFastExistFailTest.java
(with props)
Modified:
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
Modified:
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java?rev=1162647&r1=1162646&r2=1162647&view=diff
==============================================================================
---
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
(original)
+++
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/FtpOperations.java
Mon Aug 29 04:49:33 2011
@@ -507,6 +507,38 @@ public class FtpOperations implements Re
public boolean existsFile(String name) throws
GenericFileOperationFailedException {
log.trace("existsFile({})", name);
+ if (endpoint.isFastExist()) {
+ return fastExistsFile(name);
+ }
+ // check whether a file already exists
+ String directory = FileUtil.onlyPath(name);
+ String onlyName = FileUtil.stripPath(name);
+ try {
+ String[] names;
+ if (directory != null) {
+ names = client.listNames(directory);
+ } else {
+ names = client.listNames();
+ }
+ // can return either null or an empty list depending on FTP servers
+ if (names == null) {
+ return false;
+ }
+ for (String existing : names) {
+ log.trace("Existing file: {}, target file: {}", existing,
name);
+ existing = FileUtil.stripPath(existing);
+ if (existing != null && existing.equals(onlyName)) {
+ return true;
+ }
+ }
+ return false;
+ } catch (IOException e) {
+ throw new
GenericFileOperationFailedException(client.getReplyCode(),
client.getReplyString(), e.getMessage(), e);
+ }
+ }
+
+ protected boolean fastExistsFile(String name) throws
GenericFileOperationFailedException {
+ log.trace("fastexistsFile({})", name);
try {
String[] names = client.listNames(name);
if (names == null) {
Modified:
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java?rev=1162647&r1=1162646&r2=1162647&view=diff
==============================================================================
---
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
(original)
+++
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileEndpoint.java
Mon Aug 29 04:49:33 2011
@@ -33,6 +33,7 @@ public abstract class RemoteFileEndpoint
private int maximumReconnectAttempts = 3;
private long reconnectDelay = 1000;
private boolean disconnect;
+ private boolean fastExist;
public RemoteFileEndpoint() {
// no args constructor for spring bean endpoint configuration
@@ -170,4 +171,13 @@ public abstract class RemoteFileEndpoint
public void setDisconnect(boolean disconnect) {
this.disconnect = disconnect;
}
+
+ public boolean isFastExist() {
+ return fastExist;
+ }
+
+ public void setFastExist(boolean fastExist) {
+ this.fastExist = fastExist;
+ }
+
}
Modified:
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java?rev=1162647&r1=1162646&r2=1162647&view=diff
==============================================================================
---
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
(original)
+++
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
Mon Aug 29 04:49:33 2011
@@ -678,6 +678,45 @@ public class SftpOperations implements R
public boolean existsFile(String name) throws
GenericFileOperationFailedException {
LOG.trace("existsFile({})", name);
+ if (endpoint.isFastExist()) {
+ return fastExistsFile(name);
+ }
+ // check whether a file already exists
+ String directory = FileUtil.onlyPath(name);
+ if (directory == null) {
+ // assume current dir if no path could be extracted
+ directory = ".";
+ }
+ String onlyName = FileUtil.stripPath(name);
+
+ try {
+ Vector files = channel.ls(directory);
+ // can return either null or an empty list depending on FTP servers
+ if (files == null) {
+ return false;
+ }
+ for (Object file : files) {
+ ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) file;
+ String existing = entry.getFilename();
+ LOG.trace("Existing file: {}, target file: {}", existing,
name);
+ existing = FileUtil.stripPath(existing);
+ if (existing != null && existing.equals(onlyName)) {
+ return true;
+ }
+ }
+ return false;
+ } catch (SftpException e) {
+ // or an exception can be thrown with id 2 which means file does
not exists
+ if (ChannelSftp.SSH_FX_NO_SUCH_FILE == e.id) {
+ return false;
+ }
+ // otherwise its a more serious error so rethrow
+ throw new GenericFileOperationFailedException(e.getMessage(), e);
+ }
+ }
+
+ protected boolean fastExistsFile(String name) throws
GenericFileOperationFailedException {
+ LOG.trace("fastExistsFile({})", name);
try {
Vector files = channel.ls(name);
if (files == null) {
Added:
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileFastExistFailTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileFastExistFailTest.java?rev=1162647&view=auto
==============================================================================
---
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileFastExistFailTest.java
(added)
+++
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileFastExistFailTest.java
Mon Aug 29 04:49:33 2011
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.file.remote;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @version
+ */
+public class FtpProducerFileFastExistFailTest extends
FtpProducerFileExistFailTest {
+
+ private String getFtpUrl() {
+ return "ftp://admin@localhost:" + getPort() +
"/exist?fastExist=true&password=admin&delay=2000&noop=true&fileExist=Fail";
+ }
+
+}
\ No newline at end of file
Propchange:
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileFastExistFailTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpProducerFileFastExistFailTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date