Author: davsclaus
Date: Mon Nov 16 16:50:08 2009
New Revision: 880842

URL: http://svn.apache.org/viewvc?rev=880842&view=rev
Log:
CAMEL-2165: Improved preWriteCheck on ftp to send a NOOP to check if connection 
works. And forces a reconnect if it dont.

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/RemoteFileConsumer.java
    
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileOperations.java
    
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.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=880842&r1=880841&r2=880842&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 Nov 16 16:50:08 2009
@@ -424,6 +424,14 @@
         }
     }
 
+    public boolean sendNoop() throws GenericFileOperationFailedException {
+        try {
+            return client.sendNoOp();
+        } catch (IOException e) {
+            throw new 
GenericFileOperationFailedException(client.getReplyCode(), 
client.getReplyString(), e.getMessage(), e);
+        }
+    }
+
     private boolean buildDirectoryChunks(String dirName) throws IOException {
         final StringBuilder sb = new StringBuilder(dirName.length());
         final String[] dirs = dirName.split("/|\\\\");

Modified: 
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java?rev=880842&r1=880841&r2=880842&view=diff
==============================================================================
--- 
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
 (original)
+++ 
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConsumer.java
 Mon Nov 16 16:50:08 2009
@@ -33,6 +33,10 @@
         this.setPollStrategy(new RemoteFilePollingConsumerPollStrategy());
     }
 
+    protected RemoteFileOperations getOperations() {
+        return (RemoteFileOperations) operations;
+    }
+
     protected boolean prePollCheck() throws Exception {
         connectIfNecessary();
         if (!loggedIn) {
@@ -52,12 +56,12 @@
     protected void disconnect() {
         // disconnect when stopping
         try {
-            if (((RemoteFileOperations) operations).isConnected()) {
+            if (getOperations().isConnected()) {
                 loggedIn = false;
                 if (log.isDebugEnabled()) {
                     log.debug("Disconnecting from: " + remoteServer());
                 }
-                ((RemoteFileOperations) operations).disconnect();
+                getOperations().disconnect();
             }
         } catch (GenericFileOperationFailedException e) {
             // ignore just log a warning
@@ -66,11 +70,11 @@
     }
 
     protected void connectIfNecessary() throws IOException {
-        if (!((RemoteFileOperations) operations).isConnected() || !loggedIn) {
+        if (!loggedIn) {
             if (log.isDebugEnabled()) {
                 log.debug("Not connected/logged in, connecting to: " + 
remoteServer());
             }
-            loggedIn = ((RemoteFileOperations) 
operations).connect((RemoteFileConfiguration) endpoint.getConfiguration());
+            loggedIn = getOperations().connect((RemoteFileConfiguration) 
endpoint.getConfiguration());
             if (loggedIn) {
                 log.info("Connected and logged in to: " + remoteServer());
             }

Modified: 
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileOperations.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileOperations.java?rev=880842&r1=880841&r2=880842&view=diff
==============================================================================
--- 
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileOperations.java
 (original)
+++ 
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileOperations.java
 Mon Nov 16 16:50:08 2009
@@ -19,7 +19,6 @@
 import org.apache.camel.component.file.GenericFileOperationFailedException;
 import org.apache.camel.component.file.GenericFileOperations;
 
-
 /**
  * Remote file operations based on some backing framework
  */
@@ -28,7 +27,7 @@
     /**
      * Connects to the remote server
      *
-     * @param configuration configuraiton
+     * @param configuration configuration
      * @return true if connected
      * @throws GenericFileOperationFailedException can be thrown
      */
@@ -43,10 +42,18 @@
     boolean isConnected() throws GenericFileOperationFailedException;
 
     /**
-     * Discconects from the remote server
+     * Disconnects from the remote server
      *
      * @throws GenericFileOperationFailedException can be thrown
      */
     void disconnect() throws GenericFileOperationFailedException;
 
+    /**
+     * Sends a noop command to the remote server
+     *
+     * @throws GenericFileOperationFailedException can be thrown
+     * @return <tt>true</tt> if the noop was a success, <tt>false</tt> 
otherwise
+     */
+    boolean sendNoop() throws GenericFileOperationFailedException;
+
 }

Modified: 
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java?rev=880842&r1=880841&r2=880842&view=diff
==============================================================================
--- 
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
 (original)
+++ 
camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileProducer.java
 Mon Nov 16 16:50:08 2009
@@ -90,6 +90,33 @@
 
     @Override
     protected void preWriteCheck() throws Exception {
+        // before writing send a noop to see if the connection is alive and 
works
+        boolean noop = false;
+        try {
+            connectIfNecessary();
+            if (loggedIn) {
+                noop = getOperations().sendNoop();
+            }
+        } catch (Exception e) {
+            log.error(e);
+            noop = false;
+        }
+        if (log.isDebugEnabled()) {
+            log.debug("preWriteCheck send noop success: " + noop);
+        }
+
+        // if not alive then force a disconnect so we reconnect again
+        if (!noop) {
+            try {
+                if (log.isDebugEnabled()) {
+                    log.debug("preWriteCheck forcing a disconnect as noop 
failed");
+                }
+                disconnect();
+            } catch (IOException e) {
+                // ignore for now
+            }
+        }
+
         connectIfNecessary();
         if (!loggedIn) {
             // must be logged in to be able to upload the file
@@ -117,7 +144,7 @@
     }
 
     protected void connectIfNecessary() throws IOException {
-        if (!(getOperations()).isConnected() || !loggedIn) {
+        if (!loggedIn) {
             if (log.isDebugEnabled()) {
                 log.debug("Not already connected/logged in. Connecting to: " + 
getEndpoint());
             }

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=880842&r1=880841&r2=880842&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 Nov 16 16:50:08 2009
@@ -461,6 +461,10 @@
         } catch (SftpException e) {
             throw new GenericFileOperationFailedException(e.getMessage(), e);
         }
-    }   
+    }
 
+    public boolean sendNoop() throws GenericFileOperationFailedException {
+        // is not implemented
+        return true;
+    }
 }
\ No newline at end of file


Reply via email to