[jboss-remoting-commits] JBoss Remoting SVN: r4398 - in remoting3/trunk: api/src/main/java/org/jboss/cx/remoting and 9 other directories.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Fri Jul 18 20:04:33 EDT 2008


Author: david.lloyd at jboss.com
Date: 2008-07-18 20:04:32 -0400 (Fri, 18 Jul 2008)
New Revision: 4398

Modified:
   remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/Endpoint.java
   remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/AbstractAutoCloseable.java
   remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/AbstractCloseable.java
   remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/SpiUtils.java
   remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/remote/RemoteClientEndpoint.java
   remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/remote/RemoteServiceEndpoint.java
   remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/wrapper/EndpointWrapper.java
   remoting3/trunk/api/src/test/java/org/jboss/cx/remoting/spi/CloseableTestCase.java
   remoting3/trunk/build.xml
   remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/ClientSourceImpl.java
   remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/EndpointImpl.java
   remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/RemoteServiceEndpointLocalImpl.java
   remoting3/trunk/core/src/test/java/org/jboss/cx/remoting/core/EndpointTestCase.java
   remoting3/trunk/protocol/basic/src/main/java/org/jboss/cx/remoting/protocol/basic/BasicHandler.java
   remoting3/trunk/protocol/basic/src/main/java/org/jboss/cx/remoting/protocol/basic/BasicProtocol.java
   remoting3/trunk/protocol/basic/src/main/java/org/jboss/cx/remoting/protocol/basic/ServiceRegistry.java
   remoting3/trunk/protocol/basic/src/test/java/org/jboss/cx/remoting/protocol/basic/ConnectionTestCase.java
   remoting3/trunk/standalone/src/main/java/org/jboss/cx/remoting/Remoting.java
Log:
Remove auto-closing; replace with a root handle notion

Modified: remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/Endpoint.java
===================================================================
--- remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/Endpoint.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/Endpoint.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -3,6 +3,7 @@
 import java.util.concurrent.ConcurrentMap;
 import org.jboss.cx.remoting.spi.remote.RemoteClientEndpoint;
 import org.jboss.cx.remoting.spi.remote.RemoteServiceEndpoint;
+import org.jboss.cx.remoting.spi.remote.Handle;
 
 /**
  * A potential participant in a JBoss Remoting communications relationship.
@@ -34,10 +35,10 @@
      * @param <I> the request type
      * @param <O> the reply type
      * @param requestListener the request listener
-     * @return the client
+     * @return a handle for the client
      * @throws RemotingException if an error occurs
      */
-    <I, O> RemoteClientEndpoint createClientEndpoint(RequestListener<I, O> requestListener) throws RemotingException;
+    <I, O> Handle<RemoteClientEndpoint> createClientEndpoint(RequestListener<I, O> requestListener) throws RemotingException;
 
     /**
      * Create a client source that can be used to acquire clients associated with a request listener on this endpoint.
@@ -49,10 +50,10 @@
      * @param <I> the request type
      * @param <O> the reply type
      * @param requestListener the request listener
-     * @return the context source
+     * @return a handle for the client source
      * @throws RemotingException if an error occurs
      */
-    <I, O> RemoteServiceEndpoint createServiceEndpoint(RequestListener<I, O> requestListener) throws RemotingException;
+    <I, O> Handle<RemoteServiceEndpoint> createServiceEndpoint(RequestListener<I, O> requestListener) throws RemotingException;
 
     /**
      * Create a client from a remote client endpoint.

Modified: remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/AbstractAutoCloseable.java
===================================================================
--- remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/AbstractAutoCloseable.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/AbstractAutoCloseable.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -26,6 +26,7 @@
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import org.jboss.cx.remoting.RemotingException;
+import org.jboss.cx.remoting.CloseHandler;
 import org.jboss.cx.remoting.spi.remote.Handle;
 import org.jboss.xnio.log.Logger;
 
@@ -34,8 +35,7 @@
  */
 public abstract class AbstractAutoCloseable<T> extends AbstractCloseable<T> {
 
-    private final AtomicBoolean autoClose = new AtomicBoolean();
-    private final AtomicInteger refcount = new AtomicInteger(1);
+    private final AtomicInteger refcount = new AtomicInteger(0);
     private final Executor executor;
 
     private static final Logger log = Logger.getLogger(AbstractAutoCloseable.class);
@@ -82,24 +82,17 @@
         }
     }
 
-    public void autoClose() throws RemotingException {
-        if (! autoClose.getAndSet(true)) {
-            dec();
-        }
-    }
-
     public Handle<T> getHandle() throws RemotingException {
         return new HandleImpl();
     }
 
     private final class HandleImpl extends AbstractCloseable<Handle<T>> implements Handle<T> {
-
         private HandleImpl() throws RemotingException {
             super(AbstractAutoCloseable.this.executor);
             inc();
         }
 
-        public void close() throws RemotingException {
+        protected void closeAction() throws RemotingException {
             dec();
         }
 

Modified: remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/AbstractCloseable.java
===================================================================
--- remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/AbstractCloseable.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/AbstractCloseable.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -34,7 +34,8 @@
 import java.util.concurrent.atomic.AtomicBoolean;
 
 /**
- *
+ * A basic implementation of a closeable resource.  Use as a convenient base class for your closeable resources.
+ * Ensures that the {@code close()} method is idempotent; implements the registry of close handlers.
  */
 public abstract class AbstractCloseable<T> implements Closeable<T> {
 
@@ -45,6 +46,11 @@
     private final AtomicBoolean closed = new AtomicBoolean();
     private Set<CloseHandler<? super T>> closeHandlers;
 
+    /**
+     * Basic constructor.
+     *
+     * @param executor the executor used to execute the close notification handlers
+     */
     protected AbstractCloseable(final Executor executor) {
         if (executor == null) {
             throw new NullPointerException("executor is null");
@@ -52,11 +58,27 @@
         this.executor = executor;
     }
 
+    /**
+     * Read the status of this resource.  This is just a snapshot in time; there is no guarantee that the resource
+     * will remain open for any amount of time, even if this method returns {@code true}.
+     *
+     * @return {@code true} if the resource is still open
+     */
     protected boolean isOpen() {
         return ! closed.get();
     }
 
-    public void close() throws RemotingException {
+    /**
+     * Called exactly once when the {@code close()} method is invoked; the actual close operation should take place here.
+     *
+     * @throws RemotingException if the close failed
+     */
+    protected void closeAction() throws RemotingException {}
+
+    /**
+     * {@inheritDoc}
+     */
+    public final void close() throws RemotingException {
         if (! closed.getAndSet(true)) {
             log.trace("Closed %s", this);
             synchronized (closeLock) {
@@ -72,9 +94,13 @@
                     closeHandlers = null;
                 }
             }
+            closeAction();
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     public void addCloseHandler(final CloseHandler<? super T> handler) {
         synchronized (closeLock) {
             if (closeHandlers == null) {
@@ -84,10 +110,18 @@
         }
     }
 
+    /**
+     * Get the executor to use for handler invocation.
+     *
+     * @return the executor
+     */
     protected Executor getExecutor() {
         return executor;
     }
 
+    /**
+     * Finalize this closeable instance.  If the instance hasn't been closed, it is closed and a warning is logged.
+     */
     protected void finalize() throws Throwable {
         try {
             super.finalize();

Modified: remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/SpiUtils.java
===================================================================
--- remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/SpiUtils.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/SpiUtils.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -136,22 +136,6 @@
 
     private static final RemoteRequestContext BLANK_REMOTE_REQUEST_CONTEXT = new BlankRemoteRequestContext();
 
-    public static void safeAutoClose(final RemoteClientEndpoint remoteClientEndpoint) {
-        try {
-            remoteClientEndpoint.autoClose();
-        } catch (Throwable t) {
-            log.error("Failed to set autoClose on %s: %s", remoteClientEndpoint, t);
-        }
-    }
-
-    public static void safeAutoClose(final RemoteServiceEndpoint remoteServiceEndpoint) {
-        try {
-            remoteServiceEndpoint.autoClose();
-        } catch (Throwable t) {
-            log.error("Failed to set autoClose on %s: %s", remoteServiceEndpoint, t);
-        }
-    }
-
     private static final class BlankRemoteRequestContext implements RemoteRequestContext {
         public void cancel(final boolean mayInterrupt) {
         }

Modified: remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/remote/RemoteClientEndpoint.java
===================================================================
--- remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/remote/RemoteClientEndpoint.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/remote/RemoteClientEndpoint.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -64,11 +64,6 @@
     Handle<RemoteClientEndpoint> getHandle() throws RemotingException;
 
     /**
-     * Automatically close this client endpoint when all handles and local client instances are closed.
-     */
-    void autoClose() throws RemotingException;
-
-    /**
      * Close this client endpoint.  The outcome of any outstanding requests is not defined, though implementations
      * should make an effort to cancel any outstanding requests.
      *

Modified: remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/remote/RemoteServiceEndpoint.java
===================================================================
--- remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/remote/RemoteServiceEndpoint.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/remote/RemoteServiceEndpoint.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -40,7 +40,7 @@
      * @return a client endpoint
      * @throws RemotingException if a client could not be opened
      */
-    RemoteClientEndpoint createClientEndpoint() throws RemotingException;
+    Handle<RemoteClientEndpoint> createClientEndpoint() throws RemotingException;
 
     /**
      * Get a handle to this service endpoint.  The service endpoint will not auto-close as long as there is at least
@@ -54,12 +54,6 @@
     Handle<RemoteServiceEndpoint> getHandle() throws RemotingException;
 
     /**
-     * Automatically close this service endpoint when all handles and local client source instances
-     * are closed.
-     */
-    void autoClose() throws RemotingException;
-
-    /**
      * Close this service endpoint immediately.
      */
     void close() throws RemotingException;

Modified: remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/wrapper/EndpointWrapper.java
===================================================================
--- remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/wrapper/EndpointWrapper.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/api/src/main/java/org/jboss/cx/remoting/spi/wrapper/EndpointWrapper.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -8,6 +8,7 @@
 import org.jboss.cx.remoting.ClientSource;
 import org.jboss.cx.remoting.spi.remote.RemoteClientEndpoint;
 import org.jboss.cx.remoting.spi.remote.RemoteServiceEndpoint;
+import org.jboss.cx.remoting.spi.remote.Handle;
 
 /**
  * A simple delegating wrapper for endpoints.
@@ -41,14 +42,14 @@
     /**
      * {@inheritDoc}  This implementation calls the same method on the delegate object.
      */
-    public <I, O> RemoteClientEndpoint createClientEndpoint(final RequestListener<I, O> requestListener) throws RemotingException {
+    public <I, O> Handle<RemoteClientEndpoint> createClientEndpoint(final RequestListener<I, O> requestListener) throws RemotingException {
         return delegate.createClientEndpoint(requestListener);
     }
 
     /**
      * {@inheritDoc}  This implementation calls the same method on the delegate object.
      */
-    public <I, O> RemoteServiceEndpoint createServiceEndpoint(final RequestListener<I, O> requestListener) throws RemotingException {
+    public <I, O> Handle<RemoteServiceEndpoint> createServiceEndpoint(final RequestListener<I, O> requestListener) throws RemotingException {
         return delegate.createServiceEndpoint(requestListener);
     }
 

Modified: remoting3/trunk/api/src/test/java/org/jboss/cx/remoting/spi/CloseableTestCase.java
===================================================================
--- remoting3/trunk/api/src/test/java/org/jboss/cx/remoting/spi/CloseableTestCase.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/api/src/test/java/org/jboss/cx/remoting/spi/CloseableTestCase.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -78,6 +78,7 @@
             final AbstractAutoCloseable<Object> closeable = new AbstractAutoCloseable<Object>(executorService) {
                 // empty
             };
+            final Handle<Object> rootHandle = closeable.getHandle();
             try {
                 closeable.addCloseHandler(new CloseHandler<Object>() {
                     public void handleClose(final Object x) {
@@ -87,7 +88,7 @@
                 });
                 assertTrue(closeable.isOpen());
                 assertFalse(closed.get());
-                closeable.autoClose();
+                rootHandle.close();
                 assertTrue(latch.await(500L, TimeUnit.MILLISECONDS));
                 assertFalse(closeable.isOpen());
                 assertTrue(closed.get());
@@ -107,6 +108,7 @@
             final AbstractAutoCloseable<Object> closeable = new AbstractAutoCloseable<Object>(executorService) {
                 // empty
             };
+            final Handle<Object> rootHandle = closeable.getHandle();
             try {
                 closeable.addCloseHandler(new CloseHandler<Object>() {
                     public void handleClose(final Object x) {
@@ -119,7 +121,7 @@
                 final Handle<Object> h1 = closeable.getHandle();
                 assertTrue(closeable.isOpen());
                 assertFalse(closed.get());
-                closeable.autoClose();
+                rootHandle.close();
                 assertTrue(closeable.isOpen());
                 assertFalse(closed.get());
                 h1.close();
@@ -142,6 +144,7 @@
             final AbstractAutoCloseable<Object> closeable = new AbstractAutoCloseable<Object>(executorService) {
                 // empty
             };
+            final Handle<Object> rootHandle = closeable.getHandle();
             try {
                 closeable.addCloseHandler(new CloseHandler<Object>() {
                     public void handleClose(final Object x) {
@@ -156,7 +159,7 @@
                 final Handle<Object> h3 = closeable.getHandle();
                 assertTrue(closeable.isOpen());
                 assertFalse(closed.get());
-                closeable.autoClose();
+                rootHandle.close();
                 assertTrue(closeable.isOpen());
                 assertFalse(closed.get());
                 h1.close();

Modified: remoting3/trunk/build.xml
===================================================================
--- remoting3/trunk/build.xml	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/build.xml	2008-07-19 00:04:32 UTC (rev 4398)
@@ -849,6 +849,7 @@
                 <path refid="api.classpath"/>
                 <path refid="core.classpath"/>
                 <path refid="util.classpath"/>
+                <pathelement location="${lib.xnio-api.local}"/>
             </classpath>
         </javac>
         <touch file="standalone/target/main/.lastcompile" verbose="false"/>
@@ -858,7 +859,7 @@
         <delete dir="standalone/target"/>
     </target>
 
-    <target name="standalone" description="Build the standalone module" depends="api,core,util,standalone.compile">
+    <target name="standalone" description="Build the standalone module" depends="lib.xnio-api,api,core,util,standalone.compile">
         <path id="standalone.classpath">
             <pathelement location="standalone/target/main/classes"/>
         </path>

Modified: remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/ClientSourceImpl.java
===================================================================
--- remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/ClientSourceImpl.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/ClientSourceImpl.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -28,7 +28,9 @@
 import org.jboss.cx.remoting.Endpoint;
 import org.jboss.cx.remoting.spi.remote.RemoteClientEndpoint;
 import org.jboss.cx.remoting.spi.remote.RemoteServiceEndpoint;
+import org.jboss.cx.remoting.spi.remote.Handle;
 import org.jboss.cx.remoting.spi.AbstractCloseable;
+import org.jboss.xnio.IoUtils;
 
 /**
  *
@@ -48,9 +50,12 @@
         if (! isOpen()) {
             throw new RemotingException("Client source is not open");
         }
-        final RemoteClientEndpoint clientEndpoint = serviceEndpoint.createClientEndpoint();
-        final Client<I, O> client = endpoint.createClient(clientEndpoint);
-        clientEndpoint.autoClose();
-        return client;
+        final Handle<RemoteClientEndpoint> handle = serviceEndpoint.createClientEndpoint();
+        try {
+            final Client<I, O> client = endpoint.createClient(handle.getResource());
+            return client;
+        } finally {
+            IoUtils.safeClose(handle);
+        }
     }
 }

Modified: remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/EndpointImpl.java
===================================================================
--- remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/EndpointImpl.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/EndpointImpl.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -121,18 +121,18 @@
         return endpointMap;
     }
 
-    public <I, O> RemoteClientEndpoint createClientEndpoint(final RequestListener<I, O> requestListener) throws RemotingException {
+    public <I, O> Handle<RemoteClientEndpoint> createClientEndpoint(final RequestListener<I, O> requestListener) throws RemotingException {
         final RemoteClientEndpointLocalImpl<I, O> clientEndpoint = new RemoteClientEndpointLocalImpl<I, O>(executor, requestListener);
         clientEndpoint.addCloseHandler(remover);
         clientEndpoint.open();
-        return clientEndpoint;
+        return clientEndpoint.getHandle();
     }
 
-    public <I, O> RemoteServiceEndpoint createServiceEndpoint(final RequestListener<I, O> requestListener) throws RemotingException {
+    public <I, O> Handle<RemoteServiceEndpoint> createServiceEndpoint(final RequestListener<I, O> requestListener) throws RemotingException {
         final RemoteServiceEndpointLocalImpl<I, O> serviceEndpoint = new RemoteServiceEndpointLocalImpl<I, O>(executor, requestListener);
         serviceEndpoint.addCloseHandler(remover);
         serviceEndpoint.open();
-        return serviceEndpoint;
+        return serviceEndpoint.getHandle();
     }
 
     public <I, O> Client<I, O> createClient(final RemoteClientEndpoint endpoint) throws RemotingException {

Modified: remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/RemoteServiceEndpointLocalImpl.java
===================================================================
--- remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/RemoteServiceEndpointLocalImpl.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/core/src/main/java/org/jboss/cx/remoting/core/RemoteServiceEndpointLocalImpl.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -24,6 +24,7 @@
 
 import org.jboss.cx.remoting.spi.remote.RemoteServiceEndpoint;
 import org.jboss.cx.remoting.spi.remote.RemoteClientEndpoint;
+import org.jboss.cx.remoting.spi.remote.Handle;
 import org.jboss.cx.remoting.spi.AbstractAutoCloseable;
 import org.jboss.cx.remoting.RequestListener;
 import org.jboss.cx.remoting.RemotingException;
@@ -49,11 +50,11 @@
         serviceContext = new ServiceContextImpl(executor);
     }
 
-    public RemoteClientEndpoint createClientEndpoint() throws RemotingException {
+    public Handle<RemoteClientEndpoint> createClientEndpoint() throws RemotingException {
         if (isOpen()) {
             final RemoteClientEndpointLocalImpl<I, O> clientEndpoint = new RemoteClientEndpointLocalImpl<I, O>(executor, this, requestListener);
             clientEndpoint.open();
-            return clientEndpoint;
+            return clientEndpoint.getHandle();
         } else {
             throw new RemotingException("RemotingServiceEndpoint is closed");
         }

Modified: remoting3/trunk/core/src/test/java/org/jboss/cx/remoting/core/EndpointTestCase.java
===================================================================
--- remoting3/trunk/core/src/test/java/org/jboss/cx/remoting/core/EndpointTestCase.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/core/src/test/java/org/jboss/cx/remoting/core/EndpointTestCase.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -35,6 +35,7 @@
 import org.jboss.cx.remoting.RemotingException;
 import org.jboss.cx.remoting.test.support.LoggingHelper;
 import org.jboss.cx.remoting.spi.remote.RemoteClientEndpoint;
+import org.jboss.cx.remoting.spi.remote.Handle;
 import org.jboss.xnio.IoUtils;
 
 /**
@@ -78,7 +79,7 @@
             endpoint.setExecutor(executorService);
             endpoint.start();
             try {
-                final RemoteClientEndpoint clientEndpoint = endpoint.createClientEndpoint(new AbstractRequestListener<Object, Object>() {
+                final Handle<RemoteClientEndpoint> handle = endpoint.createClientEndpoint(new AbstractRequestListener<Object, Object>() {
                     public void handleRequest(final RequestContext<Object> context, final Object request) throws RemoteExecutionException {
                         assertEquals(request, requestObj);
                         try {
@@ -92,6 +93,7 @@
                         }
                     }
                 });
+                final RemoteClientEndpoint clientEndpoint = handle.getResource();
                 try {
                     clientEndpoint.addCloseHandler(new CloseHandler<RemoteClientEndpoint>() {
                         public void handleClose(final RemoteClientEndpoint closed) {
@@ -106,7 +108,6 @@
                             }
                         });
                         assertEquals(replyObj, client.invoke(requestObj));
-                        clientEndpoint.autoClose();
                         client.close();
                     } finally {
                         IoUtils.safeClose(client);
@@ -137,7 +138,7 @@
             endpoint.setExecutor(executorService);
             endpoint.start();
             try {
-                final RemoteClientEndpoint clientEndpoint = endpoint.createClientEndpoint(new AbstractRequestListener<Object, Object>() {
+                final Handle<RemoteClientEndpoint> handle = endpoint.createClientEndpoint(new AbstractRequestListener<Object, Object>() {
                     public void handleRequest(final RequestContext<Object> context, final Object request) throws RemoteExecutionException {
                         assertEquals(request, requestObj);
                         try {
@@ -151,6 +152,7 @@
                         }
                     }
                 });
+                final RemoteClientEndpoint clientEndpoint = handle.getResource();
                 try {
                     clientEndpoint.addCloseHandler(new CloseHandler<RemoteClientEndpoint>() {
                         public void handleClose(final RemoteClientEndpoint closed) {
@@ -165,7 +167,6 @@
                             }
                         });
                         assertEquals(replyObj, client.send(requestObj).get());
-                        clientEndpoint.autoClose();
                         client.close();
                     } finally {
                         IoUtils.safeClose(client);

Modified: remoting3/trunk/protocol/basic/src/main/java/org/jboss/cx/remoting/protocol/basic/BasicHandler.java
===================================================================
--- remoting3/trunk/protocol/basic/src/main/java/org/jboss/cx/remoting/protocol/basic/BasicHandler.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/protocol/basic/src/main/java/org/jboss/cx/remoting/protocol/basic/BasicHandler.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -269,7 +269,6 @@
                         break;
                     }
                     final RemoteServiceEndpoint serviceEndpoint = handle.getResource();
-                    final RemoteClientEndpoint clientEndpoint = serviceEndpoint.createClientEndpoint();
 
                     break;
                 }
@@ -617,7 +616,7 @@
             });
         }
 
-        public RemoteClientEndpoint createClientEndpoint() throws RemotingException {
+        public Handle<RemoteClientEndpoint> createClientEndpoint() throws RemotingException {
             final int id = openClientFromService();
             final ByteBuffer buffer = allocator.allocate();
             buffer.putInt(identifier);
@@ -629,7 +628,7 @@
                 try {
                     registerWriter(channel, new SimpleWriteHandler(allocator, buffer));
                     try {
-                        return new RemoteClientEndpointImpl(id, marshallerFactory, allocator);
+                        return new RemoteClientEndpointImpl(id, marshallerFactory, allocator).getHandle();
                     } finally {
                         if (intr) {
                             Thread.currentThread().interrupt();

Modified: remoting3/trunk/protocol/basic/src/main/java/org/jboss/cx/remoting/protocol/basic/BasicProtocol.java
===================================================================
--- remoting3/trunk/protocol/basic/src/main/java/org/jboss/cx/remoting/protocol/basic/BasicProtocol.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/protocol/basic/src/main/java/org/jboss/cx/remoting/protocol/basic/BasicProtocol.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -62,16 +62,8 @@
         return new IoHandlerFactory<AllocatedMessageChannel>() {
             public IoHandler<? super AllocatedMessageChannel> createHandler() {
                 try {
-                    final RemoteClientEndpoint remoteClientEndpoint = localRootSource.createClientEndpoint();
-                    try {
-                        return new BasicHandler(true, allocator, remoteClientEndpoint, executor, remoteListener, new JavaSerializationMarshallerFactory(executor));
-                    } finally {
-                        try {
-                            remoteClientEndpoint.autoClose();
-                        } catch (RemotingException e) {
-                            log.error(e, "Error setting auto-close mode");
-                        }
-                    }
+                    final RemoteClientEndpoint remoteClientEndpoint = localRootSource.createClientEndpoint().getResource();
+                    return new BasicHandler(true, allocator, remoteClientEndpoint, executor, remoteListener, new JavaSerializationMarshallerFactory(executor));
                 } catch (RemotingException e) {
                     throw new IllegalStateException("The local root endpoint is unusable", e);
                 }
@@ -95,7 +87,7 @@
         return new AbstractConvertingIoFuture<RemoteClientEndpoint, AllocatedMessageChannel>(futureChannel) {
             protected RemoteClientEndpoint convert(final AllocatedMessageChannel channel) throws RemotingException {
                 final RemoteClientEndpoint remoteClientEndpoint = basicHandler.getRemoteClient(0);
-                return (RemoteClientEndpoint) remoteClientEndpoint;
+                return remoteClientEndpoint;
             }
         };
     }

Modified: remoting3/trunk/protocol/basic/src/main/java/org/jboss/cx/remoting/protocol/basic/ServiceRegistry.java
===================================================================
--- remoting3/trunk/protocol/basic/src/main/java/org/jboss/cx/remoting/protocol/basic/ServiceRegistry.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/protocol/basic/src/main/java/org/jboss/cx/remoting/protocol/basic/ServiceRegistry.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -23,6 +23,7 @@
 package org.jboss.cx.remoting.protocol.basic;
 
 import org.jboss.cx.remoting.spi.remote.RemoteServiceEndpoint;
+import org.jboss.cx.remoting.spi.remote.Handle;
 import org.jboss.cx.remoting.RemotingException;
 
 /**
@@ -35,5 +36,5 @@
 
     void unbind(int id) throws RemotingException;
 
-    RemoteServiceEndpoint lookup(int id) throws RemotingException;
+    Handle<RemoteServiceEndpoint> lookup(int id) throws RemotingException;
 }

Modified: remoting3/trunk/protocol/basic/src/test/java/org/jboss/cx/remoting/protocol/basic/ConnectionTestCase.java
===================================================================
--- remoting3/trunk/protocol/basic/src/test/java/org/jboss/cx/remoting/protocol/basic/ConnectionTestCase.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/protocol/basic/src/test/java/org/jboss/cx/remoting/protocol/basic/ConnectionTestCase.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -88,109 +88,6 @@
                 final EndpointImpl endpoint = new EndpointImpl();
                 endpoint.setExecutor(executorService);
                 endpoint.start();
-                try {
-                    final RemoteServiceEndpoint serverServiceEndpoint = endpoint.createServiceEndpoint(new RequestListener<Object, Object>() {
-                        public void handleClientOpen(final ClientContext context) {
-                            if (clientOpened.getAndSet(true)) {
-                                if (client2Opened.getAndSet(true)) {
-                                    problems.add(new IllegalStateException("Too many client opens"));
-                                }
-                            }
-                        }
-
-                        public void handleServiceOpen(final ServiceContext context) {
-                            if (serviceOpened.getAndSet(true)) {
-                                problems.add(new IllegalStateException("Multiple service opens"));
-                            }
-                        }
-
-                        public void handleRequest(final RequestContext<Object> context, final Object request) throws RemoteExecutionException {
-                            try {
-                                System.out.println("Received request; sending response!");
-                                context.sendReply("response");
-                            } catch (RemotingException e) {
-                                try {
-                                    context.sendFailure("failed", e);
-                                } catch (RemotingException e1) {
-                                    problems.add(e1);
-                                }
-                            }
-                        }
-
-                        public void handleServiceClose(final ServiceContext context) {
-                            if (serviceClosed.getAndSet(true)) {
-                                problems.add(new IllegalStateException("Multiple service closes"));
-                            }
-                            cleanupLatch.countDown();
-                        }
-
-                        public void handleClientClose(final ClientContext context) {
-                            if (clientClosed.getAndSet(true)) {
-                                problems.add(new IllegalStateException("Multiple client closes"));
-                            }
-                            cleanupLatch.countDown();
-                        }
-                    });
-                    try {
-                        final Handle<RemoteServiceEndpoint> serviceHandle = serverServiceEndpoint.getHandle();
-                        serverServiceEndpoint.autoClose();
-                        try {
-                            final RemoteClientEndpointListener remoteListener = new RemoteClientEndpointListener() {
-                                public <I, O> void notifyCreated(final RemoteClientEndpoint endpoint) {
-                                }
-                            };
-                            final ConfigurableFactory<Closeable> tcpServer = xnio.createTcpServer(executorService, Channels.convertStreamToAllocatedMessage(BasicProtocol.createServer(executorService, serverServiceEndpoint, allocator, remoteListener), 32768, 32768), new InetSocketAddress(12345));
-                            final Closeable tcpServerCloseable = tcpServer.create();
-                            try {
-                                // now create a client to connect to it
-                                final RemoteClientEndpoint localRoot = ;
-                                try {
-                                    serverServiceEndpoint.autoClose();
-                                    final InetSocketAddress destAddr = new InetSocketAddress("localhost", 12345);
-                                    final TcpClient tcpClient = xnio.createTcpConnector().create().createChannelSource(destAddr);
-                                    try {
-                                        final ChannelSource<AllocatedMessageChannel> messageChannelSource = Channels.convertStreamToAllocatedMessage(tcpClient, 32768, 32768);
-                                        final IoFuture<RemoteClientEndpoint> futureClient = BasicProtocol.connect(executorService, localRoot, messageChannelSource, allocator);
-                                        final RemoteClientEndpoint clientEndpoint = futureClient.get();
-                                        try {
-                                            final Client<Object,Object> client = endpoint.createClient(clientEndpoint);
-                                            try {
-                                                clientEndpoint.autoClose();
-                                                final Object result = client.send("Test").get();
-                                                assertEquals("response", result);
-                                                client.close();
-                                                cleanupLatch.await(500L, TimeUnit.MILLISECONDS);
-                                                tcpServerCloseable.close();
-                                                serviceHandle.close();
-                                                assertTrue(serviceOpened.get());
-                                                assertTrue(clientOpened.get());
-                                                assertTrue(client2Opened.get());
-                                                assertTrue(clientClosed.get());
-                                                assertTrue(serviceClosed.get());
-                                            } finally {
-                                                IoUtils.safeClose(client);
-                                            }
-                                        } finally {
-                                            IoUtils.safeClose(clientEndpoint);
-                                        }
-                                    } finally {
-                                        // todo close tcpClient
-                                    }
-                                } finally {
-                                    IoUtils.safeClose(localRoot);
-                                }
-                            } finally {
-                                IoUtils.safeClose(tcpServerCloseable);
-                            }
-                        } finally {
-                            IoUtils.safeClose(serviceHandle);
-                        }
-                    } finally {
-                        IoUtils.safeClose(serverServiceEndpoint);
-                    }
-                } finally {
-                    endpoint.stop();
-                }
             } finally {
                 IoUtils.safeClose(xnio);
             }

Modified: remoting3/trunk/standalone/src/main/java/org/jboss/cx/remoting/Remoting.java
===================================================================
--- remoting3/trunk/standalone/src/main/java/org/jboss/cx/remoting/Remoting.java	2008-07-18 23:28:49 UTC (rev 4397)
+++ remoting3/trunk/standalone/src/main/java/org/jboss/cx/remoting/Remoting.java	2008-07-19 00:04:32 UTC (rev 4398)
@@ -4,6 +4,8 @@
 import org.jboss.cx.remoting.core.EndpointImpl;
 import org.jboss.cx.remoting.spi.remote.RemoteClientEndpoint;
 import org.jboss.cx.remoting.spi.remote.RemoteServiceEndpoint;
+import org.jboss.cx.remoting.spi.remote.Handle;
+import org.jboss.xnio.IoUtils;
 
 /**
  *
@@ -31,20 +33,20 @@
     }
 
     public static <I, O> Client<I, O> createLocalClient(Endpoint endpoint, RequestListener<I, O> requestListener) throws RemotingException {
-        final RemoteClientEndpoint clientEndpoint = endpoint.createClientEndpoint(requestListener);
+        final Handle<RemoteClientEndpoint> handle = endpoint.createClientEndpoint(requestListener);
         try {
-            return endpoint.createClient(clientEndpoint);
+            return endpoint.createClient(handle.getResource());
         } finally {
-            clientEndpoint.autoClose();
+            IoUtils.safeClose(handle);
         }
     }
 
     public static <I, O> ClientSource<I, O> createLocalClientSource(Endpoint endpoint, RequestListener<I, O> requestListener) throws RemotingException {
-        final RemoteServiceEndpoint serviceEndpoint = endpoint.createServiceEndpoint(requestListener);
+        final Handle<RemoteServiceEndpoint> handle = endpoint.createServiceEndpoint(requestListener);
         try {
-            return endpoint.createClientSource(serviceEndpoint);
+            return endpoint.createClientSource(handle.getResource());
         } finally {
-            serviceEndpoint.autoClose();
+            IoUtils.safeClose(handle);
         }
     }
 




More information about the jboss-remoting-commits mailing list