Author: davsclaus
Date: Mon Jun 11 07:40:13 2012
New Revision: 1348732

URL: http://svn.apache.org/viewvc?rev=1348732&view=rev
Log:
CAMEL-5122: rmi consumer now rethrows exception tdefined from the method 
signature. And unmatched exception is rethrown as javax.rmi.RemoteException.

Added:
    
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/DamnException.java
    
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiDamnExceptionTest.java
      - copied, changed from r1348391, 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiConcurrencyTest.java
    
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiIllegalArgumentExceptionTest.java
Modified:
    
camel/trunk/components/camel-rmi/src/main/java/org/apache/camel/component/rmi/RmiConsumer.java
    
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/EchoService.java
    
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/IEcho.java
    
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiConcurrencyTest.java

Modified: 
camel/trunk/components/camel-rmi/src/main/java/org/apache/camel/component/rmi/RmiConsumer.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-rmi/src/main/java/org/apache/camel/component/rmi/RmiConsumer.java?rev=1348732&r1=1348731&r2=1348732&view=diff
==============================================================================
--- 
camel/trunk/components/camel-rmi/src/main/java/org/apache/camel/component/rmi/RmiConsumer.java
 (original)
+++ 
camel/trunk/components/camel-rmi/src/main/java/org/apache/camel/component/rmi/RmiConsumer.java
 Mon Jun 11 07:40:13 2012
@@ -17,10 +17,10 @@
 package org.apache.camel.component.rmi;
 
 import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.rmi.Remote;
+import java.rmi.RemoteException;
 import java.rmi.registry.Registry;
 import java.rmi.server.UnicastRemoteObject;
 
@@ -89,10 +89,31 @@ public class RmiConsumer extends Default
         BeanInvocation invocation = new BeanInvocation(method, args);
         Exchange exchange = getEndpoint().createExchange();
         exchange.getIn().setBody(invocation);
-        getProcessor().process(exchange);
+        try {
+            log.debug("Invoking {} with args {}", method, args);
+            getProcessor().process(exchange);
+        } catch (Exception e) {
+            exchange.setException(e);
+        }
+
+        // is there a matching exception from the signature, then throw that
+        // or fallback and ensure the exception is thrown as a RemoteException
         Throwable fault = exchange.getException();
         if (fault != null) {
-            throw new InvocationTargetException(fault);
+            Object match = null;
+            for (Class<?> type : method.getExceptionTypes()) {
+                Object found = exchange.getException(type);
+                if (found != null) {
+                    match = found;
+                    break;
+                }
+            }
+            if (match != null && match instanceof Throwable) {
+                // we have a match
+                throw (Throwable) match;
+            } else {
+                throw new RemoteException("Error invoking " + method, fault);
+            }
         }
         return exchange.getOut().getBody();
     }

Added: 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/DamnException.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/DamnException.java?rev=1348732&view=auto
==============================================================================
--- 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/DamnException.java
 (added)
+++ 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/DamnException.java
 Mon Jun 11 07:40:13 2012
@@ -0,0 +1,27 @@
+/**
+ * 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.rmi;
+
+/**
+ *
+ */
+public class DamnException extends Exception {
+
+    public DamnException(String s) {
+        super(s);
+    }
+}

Modified: 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/EchoService.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/EchoService.java?rev=1348732&r1=1348731&r2=1348732&view=diff
==============================================================================
--- 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/EchoService.java
 (original)
+++ 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/EchoService.java
 Mon Jun 11 07:40:13 2012
@@ -26,4 +26,12 @@ public class EchoService implements IEch
     public String echo(String s) throws RemoteException {
         return "Echo " + s;
     }
+
+    public String damn(String s) throws DamnException, RemoteException {
+        throw new DamnException("Damn this did not work");
+    }
+
+    public String foo(String s) throws RemoteException {
+        throw new IllegalArgumentException("Illegal");
+    }
 }

Modified: 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/IEcho.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/IEcho.java?rev=1348732&r1=1348731&r2=1348732&view=diff
==============================================================================
--- 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/IEcho.java
 (original)
+++ 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/IEcho.java
 Mon Jun 11 07:40:13 2012
@@ -23,4 +23,8 @@ public interface IEcho extends Remote {
 
     String echo(String s) throws RemoteException;
 
+    String damn(String s) throws RemoteException, DamnException;
+
+    String foo(String s) throws RemoteException;
+
 }
\ No newline at end of file

Modified: 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiConcurrencyTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiConcurrencyTest.java?rev=1348732&r1=1348731&r2=1348732&view=diff
==============================================================================
--- 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiConcurrencyTest.java
 (original)
+++ 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiConcurrencyTest.java
 Mon Jun 11 07:40:13 2012
@@ -100,7 +100,7 @@ public class RmiConcurrencyTest extends 
                 from(echo).to("bean:echo");
 
                 // and our route where we call the server
-                
from("direct:echo").to("rmi://localhost:37543/echo").to("mock:result");
+                
from("direct:echo").to("rmi://localhost:37543/echo?method=echo").to("mock:result");
             }
         };
     }

Copied: 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiDamnExceptionTest.java
 (from r1348391, 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiConcurrencyTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiDamnExceptionTest.java?p2=camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiDamnExceptionTest.java&p1=camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiConcurrencyTest.java&r1=1348391&r2=1348732&rev=1348732&view=diff
==============================================================================
--- 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiConcurrencyTest.java
 (original)
+++ 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiDamnExceptionTest.java
 Mon Jun 11 07:40:13 2012
@@ -19,10 +19,8 @@ package org.apache.camel.component.rmi;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.rmi.registry.LocateRegistry;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
 
+import org.apache.camel.CamelExecutionException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.test.junit4.CamelTestSupport;
@@ -31,12 +29,12 @@ import org.junit.Test;
 /**
  * @version 
  */
-public class RmiConcurrencyTest extends CamelTestSupport {
+public class RmiDamnExceptionTest extends CamelTestSupport {
 
     private static boolean created;
 
     protected int getPort() {
-        return 37543;
+        return 37544;
     }
 
     @Override
@@ -56,37 +54,22 @@ public class RmiConcurrencyTest extends 
     }
 
     @Test
-    public void testNoConcurrentProducers() throws Exception {
-        doSendMessages(1, 1);
-    }
-
-    @Test
-    public void testConcurrentProducers() throws Exception {
-        doSendMessages(10, 5);
-    }
-
-    private void doSendMessages(int files, int poolSize) throws Exception {
+    public void tesDamn() throws Exception {
         if (classPathHasSpaces()) {
             return;
         }
 
-        getMockEndpoint("mock:result").expectedMessageCount(files);
-        getMockEndpoint("mock:result").assertNoDuplicates(body());
-        getMockEndpoint("mock:result").message(0).body().contains("Echo");
-
-        ExecutorService executor = Executors.newFixedThreadPool(poolSize);
-        for (int i = 0; i < files; i++) {
-            final int index = i;
-            executor.submit(new Callable<Object>() {
-                public Object call() throws Exception {
-                    template.sendBody("direct:echo", "" + index);
-                    return null;
-                }
-            });
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        try {
+            template.sendBody("direct:echo", "Hello World");
+            fail("Should have thrown an exception");
+        } catch (CamelExecutionException e) {
+            assertIsInstanceOf(DamnException.class, e.getCause());
+            assertEquals("Damn this did not work", e.getCause().getMessage());
         }
 
         assertMockEndpointsSatisfied();
-        executor.shutdownNow();
     }
 
     @Override
@@ -95,12 +78,12 @@ public class RmiConcurrencyTest extends 
             @Override
             public void configure() throws Exception {
                 // setup the jmi server endpoint
-                RmiEndpoint echo = 
(RmiEndpoint)endpoint("rmi://localhost:37543/echo");
+                RmiEndpoint echo = 
(RmiEndpoint)endpoint("rmi://localhost:37544/echo");
                 echo.setRemoteInterfaces(IEcho.class);
                 from(echo).to("bean:echo");
 
                 // and our route where we call the server
-                
from("direct:echo").to("rmi://localhost:37543/echo").to("mock:result");
+                
from("direct:echo").to("rmi://localhost:37544/echo?method=damn").to("mock:result");
             }
         };
     }

Added: 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiIllegalArgumentExceptionTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiIllegalArgumentExceptionTest.java?rev=1348732&view=auto
==============================================================================
--- 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiIllegalArgumentExceptionTest.java
 (added)
+++ 
camel/trunk/components/camel-rmi/src/test/java/org/apache/camel/component/rmi/RmiIllegalArgumentExceptionTest.java
 Mon Jun 11 07:40:13 2012
@@ -0,0 +1,113 @@
+/**
+ * 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.rmi;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.rmi.RemoteException;
+import java.rmi.registry.LocateRegistry;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * @version 
+ */
+public class RmiIllegalArgumentExceptionTest extends CamelTestSupport {
+
+    private static boolean created;
+
+    protected int getPort() {
+        return 37545;
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        if (classPathHasSpaces()) {
+            return null;
+        }
+
+        if (!created) {
+            LocateRegistry.createRegistry(getPort());
+            created = true;
+        }
+
+        JndiRegistry context = super.createRegistry();
+        context.bind("echo", new EchoService());
+        return context;
+    }
+
+    @Test
+    public void tesIllegal() throws Exception {
+        if (classPathHasSpaces()) {
+            return;
+        }
+
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        try {
+            template.sendBody("direct:echo", "Hello World");
+            fail("Should have thrown an exception");
+        } catch (CamelExecutionException e) {
+            assertIsInstanceOf(RemoteException.class, e.getCause());
+            // wrapped far down
+            IllegalArgumentException iae = 
assertIsInstanceOf(IllegalArgumentException.class, 
e.getCause().getCause().getCause().getCause());
+            assertEquals("Illegal", iae.getMessage());
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // setup the jmi server endpoint
+                RmiEndpoint echo = 
(RmiEndpoint)endpoint("rmi://localhost:37545/echo");
+                echo.setRemoteInterfaces(IEcho.class);
+                from(echo).to("bean:echo");
+
+                // and our route where we call the server
+                
from("direct:echo").to("rmi://localhost:37545/echo?method=foo").to("mock:result");
+            }
+        };
+    }
+
+    private boolean classPathHasSpaces() {
+        ClassLoader cl = getClass().getClassLoader();
+        if (cl instanceof URLClassLoader) {
+            URLClassLoader ucl = (URLClassLoader)cl;
+            URL[] urls = ucl.getURLs();
+            for (URL url : urls) {
+                if (url.getPath().contains(" ")) {
+                    
System.err.println("=======================================================================");
+                    System.err.println(" TEST Skipped: " + 
this.getClass().getName());
+                    System.err.println("   Your probably on windows.  We 
detected that the classpath");
+                    System.err.println("   has a space in it.  Try running 
maven with the following option: ");
+                    System.err.println("   
-Dmaven.repo.local=C:\\DOCUME~1\\userid\\.m2\\repository");
+                    
System.err.println("=======================================================================");
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+}
\ No newline at end of file


Reply via email to