You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by jb...@apache.org on 2010/04/09 15:40:56 UTC

svn commit: r932410 - /servicemix/components/bindings/servicemix-rmi/trunk/src/main/java/org/apache/servicemix/rmi/RmiConsumerEndpoint.java

Author: jbonofre
Date: Fri Apr  9 13:40:56 2010
New Revision: 932410

URL: http://svn.apache.org/viewvc?rev=932410&view=rev
Log:
[SMXCOMP-734] RMI consumer endpoint raised an InvocationTargetException in place of the target one.

Modified:
    servicemix/components/bindings/servicemix-rmi/trunk/src/main/java/org/apache/servicemix/rmi/RmiConsumerEndpoint.java

Modified: servicemix/components/bindings/servicemix-rmi/trunk/src/main/java/org/apache/servicemix/rmi/RmiConsumerEndpoint.java
URL: http://svn.apache.org/viewvc/servicemix/components/bindings/servicemix-rmi/trunk/src/main/java/org/apache/servicemix/rmi/RmiConsumerEndpoint.java?rev=932410&r1=932409&r2=932410&view=diff
==============================================================================
--- servicemix/components/bindings/servicemix-rmi/trunk/src/main/java/org/apache/servicemix/rmi/RmiConsumerEndpoint.java (original)
+++ servicemix/components/bindings/servicemix-rmi/trunk/src/main/java/org/apache/servicemix/rmi/RmiConsumerEndpoint.java Fri Apr  9 13:40:56 2010
@@ -17,10 +17,10 @@
 package org.apache.servicemix.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.LocateRegistry;
 import java.rmi.registry.Registry;
 import java.rmi.server.UnicastRemoteObject;
@@ -57,13 +57,13 @@ public class RmiConsumerEndpoint extends
     private String name; // the endpoint name into the RMI registry
     private Object pojo; // delegate method call to a POJO
     private int port = Registry.REGISTRY_PORT; // the RMI registry port number
-    private boolean create = false; // try to create a new RMI registry
-    
     private Registry registry = null; // component RMI registry
     
     private Remote stub; // the remote stub
     private Remote proxy; // the remote proxy
     
+    private ClassLoader classLoader;
+    
     public Class getRemoteInterface() {
         return this.remoteInterface;
     }
@@ -124,21 +124,6 @@ public class RmiConsumerEndpoint extends
         this.port = port;
     }
     
-    public boolean isCreate() {
-        return this.create;
-    }
-    
-    /**
-     * <p>
-     * This attribute defines if a new RMI registry is created for the endpoint.
-     * </p>
-     * 
-     * @param create true if a new RMI registry is created for the endpoint, false else.
-     */
-    public void setCreate(boolean create) {
-        this.create = create;
-    }
-    
     /*
      * (non-Javadoc)
      * @see org.apache.servicemix.common.endpoints.ConsumerEndpoint#validate()
@@ -156,26 +141,32 @@ public class RmiConsumerEndpoint extends
             this.setTargetEndpoint(this.getEndpoint());
         }
 
+        // get the thread context class loader
+        classLoader = Thread.currentThread().getContextClassLoader();
+
         super.validate();
         
         if (remoteInterface == null) {
-            throw new DeploymentException("The remoteInterfaces property is mandatory.");
+            throw new DeploymentException("The remoteInterface property is mandatory.");
         }
         
         if (name == null || name.trim().length() < 1) {
             // if the user hasn't define the registry name, use the endpoint one
-            logger.debug("The user hasn't define the registry name, use the endpoint one.");
+            logger.debug("The user hasn't define the RMI registry name, use the endpoint one.");
             name = this.getEndpoint();
         }
         
-        // create or locate the RMI registry
-        try {
-            initRegistry();
-        } catch (RemoteException e) {
-            if (create) {
-                throw new DeploymentException("Can't create RMI registry.", e);
-            } else {
-                throw new DeploymentException("Can't locate RMI registry.", e);
+        // create the registry
+        if (registry == null) {
+            try {
+                registry = LocateRegistry.createRegistry(this.port);
+            } catch (Exception e) {
+                logger.warn("Can't create RMI registry, try to use an existing one.");
+                try {
+                    registry = LocateRegistry.getRegistry(this.port);
+                } catch (Exception exception) {
+                    throw new DeploymentException("Can't locate RMI registry.", exception);
+                }
             }
         }
 
@@ -226,7 +217,7 @@ public class RmiConsumerEndpoint extends
     @Override
     public synchronized void stop() throws Exception {
         // destroy the RMI registry if local
-        if (registry != null && create) {
+        if (registry != null) {
             Registry reg = registry;
             registry = null;
             UnicastRemoteObject.unexportObject(reg, true);
@@ -243,7 +234,13 @@ public class RmiConsumerEndpoint extends
         if (pojo != null) {
             // delegrate method call to the POJO
             // WARNING: using POJO you bypass NMR and it's a direct POJO call
-            return method.invoke(pojo, args);
+            try {
+                return method.invoke(pojo, args);
+            } catch (InvocationTargetException invocationTargetException) {
+                throw invocationTargetException.getTargetException();
+            } catch (Exception e) {
+                throw e;
+            }
         } else {
             // construct XML structure corresponding to the RMI call and send into the NMR
             // create in-out exchange
@@ -293,27 +290,4 @@ public class RmiConsumerEndpoint extends
         }
     }
     
-    /**
-     * <p>
-     * Initializes the RMI registry.
-     * </p>
-     * 
-     * @throws RemoteException in case of RMI registry creation failure.
-     */
-    private void initRegistry() throws RemoteException {
-        if (registry == null) {
-            if (create) {
-                registry = LocateRegistry.createRegistry(this.getPort());
-            } else {
-                try {
-                    Registry reg = LocateRegistry.getRegistry(this.getPort());
-                    reg.list();
-                    registry = reg;
-                } catch (RemoteException e) {
-                    // ignore
-                }
-            }
-        }
-    }
-    
 }