camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r979012 - in /camel/trunk/components/camel-ftp/src: main/java/org/apache/camel/component/file/remote/ test/java/org/apache/camel/component/file/remote/
Date Sun, 25 Jul 2010 09:45:13 GMT
Author: davsclaus
Date: Sun Jul 25 09:45:12 2010
New Revision: 979012

URL: http://svn.apache.org/viewvc?rev=979012&view=rev
Log:
CAMEL-2990: Added throwExceptionOnConnectFailed option to allow poll strategy to deal with
the thrown exception.

Added:
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerThrowExceptionOnLoginFailedTest.java
  (with props)
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpLoginNoRetryTest.java
      - copied, changed from r978996, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpLoginTest.java
    camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpThrowExceptionOnConnectionFailedTest.java
  (with props)
Modified:
    camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.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/RemoteFileProducer.java

Modified: camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java?rev=979012&r1=979011&r2=979012&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java
(original)
+++ camel/trunk/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/RemoteFileConfiguration.java
Sun Jul 25 09:45:12 2010
@@ -34,6 +34,7 @@ public abstract class RemoteFileConfigur
     private int connectTimeout = 10000;
     private int timeout = 30000;
     private int soTimeout;
+    private boolean throwExceptionOnConnectFailed;
 
     public RemoteFileConfiguration() {
     }
@@ -168,4 +169,19 @@ public abstract class RemoteFileConfigur
     public void setSoTimeout(int soTimeout) {
         this.soTimeout = soTimeout;
     }
+
+    public boolean isThrowExceptionOnConnectFailed() {
+        return throwExceptionOnConnectFailed;
+    }
+
+    /**
+     * Should an exception be thrown if connection failed (exhausted)
+     * <p/>
+     * By default exception is not thrown and a <tt>WARN</tt> is logged.
+     * You can use this to enable exception being thrown and handle the thrown exception
+     * from the {@link org.apache.camel.spi.PollingConsumerPollStrategy} rollback method.
+     */
+    public void setThrowExceptionOnConnectFailed(boolean throwExceptionOnConnectFailed) {
+        this.throwExceptionOnConnectFailed = throwExceptionOnConnectFailed;
+    }
 }

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=979012&r1=979011&r2=979012&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
Sun Jul 25 09:45:12 2010
@@ -53,13 +53,19 @@ public abstract class RemoteFileConsumer
             }
         } catch (Exception e) {
             loggedIn = false;
+
+            // login failed should we thrown exception
+            if (getEndpoint().getConfiguration().isThrowExceptionOnConnectFailed()) {
+                throw e;
+            }
         }
 
         if (!loggedIn) {
-            String message = "Could not connect/login to: " + remoteServer() + ". Will skip
this poll.";
+            String message = "Cannot connect/login to: " + remoteServer() + ". Will skip
this poll.";
             log.warn(message);
             return false;
         }
+
         return true;
     }
 

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=979012&r1=979011&r2=979012&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
Sun Jul 25 09:45:12 2010
@@ -94,36 +94,34 @@ public class RemoteFileProducer<T> exten
     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) {
+        if (loggedIn) {
+            try {
                 noop = getOperations().sendNoop();
+            } catch (Exception e) {
+                // ignore as we will try to recover connection
+                noop = false;
             }
-        } catch (Exception e) {
-            // ignore as we will try to recover connection
-            noop = false;
         }
-        if (log.isDebugEnabled()) {
-            log.debug("preWriteCheck send noop success: " + noop);
+
+        if (log.isTraceEnabled()) {
+            log.trace("preWriteCheck send noop success: " + noop);
         }
 
-        // if not alive then force a disconnect so we reconnect again
+        // if not alive then reconnect
         if (!noop) {
             try {
-                if (log.isDebugEnabled()) {
-                    log.debug("preWriteCheck forcing a disconnect as noop failed");
+                if (getEndpoint().getMaximumReconnectAttempts() > 0) {
+                    // only use recoverable if we are allowed any re-connect attempts
+                    recoverableConnectIfNecessary();
+                } else {
+                    connectIfNecessary();
                 }
-                disconnect();
             } catch (Exception e) {
-                // ignore for now as we will reconnect below
-            }
-        }
+                loggedIn = false;
 
-        recoverableConnectIfNecessary();
-        if (!loggedIn) {
-            // must be logged in to be able to upload the file
-            String message = "Cannot connect/login to: " + getEndpoint().remoteServerInformation();
-            throw new GenericFileOperationFailedException(message);
+                // must be logged in to be able to upload the file
+                throw e;
+            }
         }
     }
 
@@ -185,7 +183,7 @@ public class RemoteFileProducer<T> exten
             if (log.isDebugEnabled()) {
                 log.debug("Not already connected/logged in. Connecting to: " + getEndpoint());
             }
-            RemoteFileConfiguration config = (RemoteFileConfiguration) getEndpoint().getConfiguration();
+            RemoteFileConfiguration config = getEndpoint().getConfiguration();
             loggedIn = getOperations().connect(config);
             if (!loggedIn) {
                 return;

Added: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerThrowExceptionOnLoginFailedTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerThrowExceptionOnLoginFailedTest.java?rev=979012&view=auto
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerThrowExceptionOnLoginFailedTest.java
(added)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerThrowExceptionOnLoginFailedTest.java
Sun Jul 25 09:45:12 2010
@@ -0,0 +1,96 @@
+/**
+ * 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 java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Endpoint;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.impl.ServiceSupport;
+import org.apache.camel.spi.PollingConsumerPollStrategy;
+import org.junit.Test;
+
+/**
+ * Unit test for login failure due bad password and no re connect attempts allowed
+ */
+public class FtpConsumerThrowExceptionOnLoginFailedTest extends FtpServerTestSupport {
+
+    private CountDownLatch latch = new CountDownLatch(1);
+
+    private String getFtpUrl() {
+        return "ftp://dummy@localhost:" + getPort() + "/badlogin?password=cantremember"
+                + "&throwExceptionOnConnectFailed=true&maximumReconnectAttempts=0&pollStrategy=#myPoll";
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("myPoll", new MyPoll());
+        return jndi;
+    }
+
+    @Test
+    public void testBadLogin() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        latch.await(5, TimeUnit.SECONDS);
+
+        assertMockEndpointsSatisfied();
+
+        // consumer should be stopped
+        Thread.sleep(1000);
+
+        Consumer consumer = context.getRoute("foo").getConsumer();
+        assertTrue("Consumer should be stopped", ((ServiceSupport)consumer).isStopped());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from(getFtpUrl()).routeId("foo").to("mock:result");
+            }
+        };
+    }
+
+    private class MyPoll implements PollingConsumerPollStrategy {
+
+        public boolean begin(Consumer consumer, Endpoint endpoint) {
+            return true;
+        }
+
+        public void commit(Consumer consumer, Endpoint endpoint) {
+        }
+
+        public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception
cause) throws Exception {
+            GenericFileOperationFailedException e = assertIsInstanceOf(GenericFileOperationFailedException.class,
cause);
+            assertEquals(530, e.getCode());
+
+            // stop the consumer
+            consumer.stop();
+
+            latch.countDown();
+
+            return false;
+        }
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerThrowExceptionOnLoginFailedTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpConsumerThrowExceptionOnLoginFailedTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpLoginNoRetryTest.java
(from r978996, camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpLoginTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpLoginNoRetryTest.java?p2=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpLoginNoRetryTest.java&p1=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpLoginTest.java&r1=978996&r2=979012&rev=979012&view=diff
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpLoginTest.java
(original)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpLoginNoRetryTest.java
Sun Jul 25 09:45:12 2010
@@ -25,9 +25,9 @@ import org.apache.camel.component.file.G
 import org.junit.Test;
 
 /**
- * Unit test for login failure due bad password and login with accepted password
+ * Unit test for login failure due bad password and no re connect attempts
  */
-public class FtpLoginTest extends FtpServerTestSupport {
+public class FtpLoginNoRetryTest extends FtpServerTestSupport {
 
     @Test
     public void testBadLogin() throws Exception {
@@ -45,18 +45,8 @@ public class FtpLoginTest extends FtpSer
         assertFalse("The file should NOT exists", file.exists());
     }
 
-    @Test
-    public void testGoodLogin() throws Exception {
-        uploadFile("scott", "tiger");
-
-        // assert file created
-        File file = new File(FTP_ROOT_DIR + "login/report.txt");
-        file = file.getAbsoluteFile();
-        assertTrue("The file should exists", file.exists());
-    }
-
     private void uploadFile(String username, String password) throws Exception {
-        Endpoint endpoint = context.getEndpoint("ftp://" + username + "@localhost:" + getPort()
+ "/login?password=" + password);
+        Endpoint endpoint = context.getEndpoint("ftp://" + username + "@localhost:" + getPort()
+ "/login?password=" + password + "&maximumReconnectAttempts=0");
 
         Exchange exchange = endpoint.createExchange();
         exchange.getIn().setBody("Hello World from FTPServer");

Added: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpThrowExceptionOnConnectionFailedTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpThrowExceptionOnConnectionFailedTest.java?rev=979012&view=auto
==============================================================================
--- camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpThrowExceptionOnConnectionFailedTest.java
(added)
+++ camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpThrowExceptionOnConnectionFailedTest.java
Sun Jul 25 09:45:12 2010
@@ -0,0 +1,61 @@
+/**
+ * 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 java.io.File;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.junit.Test;
+
+/**
+ * Unit test for login failure due bad password and no re connect attempts allowed
+ */
+public class FtpThrowExceptionOnConnectionFailedTest extends FtpServerTestSupport {
+
+    @Test
+    public void testBadLogin() throws Exception {
+        try {
+            uploadFile("dummy", "cantremeber");
+            fail("Should have thrown a GenericFileOperationFailedException");
+        } catch (GenericFileOperationFailedException e) {
+            // expected
+            assertEquals(530, e.getCode());
+        }
+
+        // assert file NOT created
+        File file = new File(FTP_ROOT_DIR + "login/report.txt");
+        file = file.getAbsoluteFile();
+        assertFalse("The file should NOT exists", file.exists());
+    }
+
+    private void uploadFile(String username, String password) throws Exception {
+        Endpoint endpoint = context.getEndpoint("ftp://" + username + "@localhost:" + getPort()
+ "/login?password="
+                + password + "&maximumReconnectAttempts=0&throwExceptionOnConnectFailed=true");
+
+        Exchange exchange = endpoint.createExchange();
+        exchange.getIn().setBody("Hello World from FTPServer");
+        exchange.getIn().setHeader(Exchange.FILE_NAME, "report.txt");
+        Producer producer = endpoint.createProducer();
+        producer.start();
+        producer.process(exchange);
+        producer.stop();
+    }
+
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpThrowExceptionOnConnectionFailedTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FtpThrowExceptionOnConnectionFailedTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message