Author: david.lloyd(a)jboss.com
Date: 2009-04-30 19:01:14 -0400 (Thu, 30 Apr 2009)
New Revision: 5101
Removed:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AbstractAutoCloseable.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AutoCloseable.java
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/Handle.java
Modified:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/LocalRequestHandler.java
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/spi/CloseableTestCase.java
Log:
The new connection model does not rely on refcounting, so remove that stuff
Modified:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/LocalRequestHandler.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/LocalRequestHandler.java 2009-04-30
22:58:40 UTC (rev 5100)
+++
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/LocalRequestHandler.java 2009-04-30
23:01:14 UTC (rev 5101)
@@ -25,7 +25,7 @@
import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
-import org.jboss.remoting3.spi.AbstractAutoCloseable;
+import org.jboss.remoting3.spi.AbstractHandleableCloseable;
import org.jboss.remoting3.spi.RemoteRequestContext;
import org.jboss.remoting3.spi.ReplyHandler;
import org.jboss.remoting3.spi.RequestHandler;
@@ -35,7 +35,7 @@
/**
*
*/
-final class LocalRequestHandler<I, O> extends
AbstractAutoCloseable<RequestHandler> implements RequestHandler {
+final class LocalRequestHandler<I, O> extends
AbstractHandleableCloseable<RequestHandler> implements RequestHandler {
private final RequestListener<I, O> requestListener;
private final ClientContextImpl clientContext;
Deleted:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AbstractAutoCloseable.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AbstractAutoCloseable.java 2009-04-30
22:58:40 UTC (rev 5100)
+++
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AbstractAutoCloseable.java 2009-04-30
23:01:14 UTC (rev 5101)
@@ -1,160 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2008, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.jboss.remoting3.spi;
-
-import java.io.Closeable;
-import java.io.IOException;
-import java.lang.ref.WeakReference;
-import java.util.concurrent.Executor;
-import java.util.concurrent.atomic.AtomicInteger;
-import org.jboss.remoting3.CloseHandler;
-import org.jboss.remoting3.RemotingException;
-import org.jboss.remoting3.HandleableCloseable;
-import org.jboss.xnio.IoUtils;
-import org.jboss.xnio.WeakCloseable;
-import org.jboss.xnio.log.Logger;
-
-/**
- * A closeable implementation that supports reference counting. Since the initial
reference count is zero, implementors
- * must be careful to ensure that the first operation invoked is a call to {@link
#getHandle()}.
- */
-public abstract class AbstractAutoCloseable<T extends HandleableCloseable<T>>
extends AbstractHandleableCloseable<T> implements AutoCloseable<T> {
-
- private static final Logger log =
Logger.getLogger("org.jboss.remoting.resource");
-
- private final AtomicInteger refcount = new AtomicInteger(0);
- private final Executor executor;
-
- /**
- * Basic constructor.
- *
- * @param executor the executor used to execute the close notification handlers
- */
- protected AbstractAutoCloseable(final Executor executor) {
- super(executor);
- this.executor = executor;
- }
-
- /**
- * Decrement the reference count by one. If the count drops to zero, the resource is
closed.
- *
- * @throws IOException if the reference count dropped to zero and the close operation
threw an exception
- */
- protected void dec() throws IOException {
- final int v = refcount.decrementAndGet();
- if (v == 0) {
- // we dropped the refcount to zero
- log.trace("Lowering reference count of %s to 0 (closing)", this);
- if (refcount.compareAndSet(0, -65536)) {
- // we are closing
- close();
- }
- // someone incremented it in the meantime... lucky them
- } else if (v < 0) {
- // was already closed; put the count back
- refcount.incrementAndGet();
- } else {
- log.trace("Lowering reference count of %s to %d", this,
Integer.valueOf(v));
- }
- // otherwise, the resource remains open
- }
-
- /**
- * Increment the reference count by one. If the resource is closed, an exception is
thrown.
- *
- * @throws RemotingException if the resource is closed
- */
- protected void inc() throws IOException {
- final int v = refcount.getAndIncrement();
- log.trace("Raising reference count of %s to %d", this,
Integer.valueOf(v + 1));
- if (v < 0) {
- // was already closed
- refcount.decrementAndGet();
- throw new IOException("Resource is closed");
- }
- }
-
- /**
- * Get a handle to this resource. Increments the reference count by one. If the
resource is closed, an exception
- * is thrown.
- *
- * @return the handle
- * @throws RemotingException if the resource is closed
- */
- public Handle<T> getHandle() throws IOException {
- final HandleImpl handle = new HandleImpl();
- final Key key = addCloseHandler(new HandleCloseHandler<T>(handle));
- handle.addCloseHandler(new KeyCloseHandler<Handle<T>>(key));
- return handle;
- }
-
- private final class HandleImpl extends
AbstractHandleableCloseable<Handle<T>> implements Handle<T> {
-
- private HandleImpl() throws IOException {
- super(AbstractAutoCloseable.this.executor);
- inc();
- }
-
- protected void closeAction() throws IOException {
- dec();
- }
-
- @SuppressWarnings({ "unchecked" })
- public T getResource() {
- return (T) AbstractAutoCloseable.this;
- }
-
- public String toString() {
- return "handle <" + Integer.toHexString(hashCode()) + ">
to " + String.valueOf(AbstractAutoCloseable.this);
- }
- }
-
- private static class HandleCloseHandler<T> implements CloseHandler<T> {
-
- private final Closeable handle;
-
- public HandleCloseHandler(final Handle<T> handle) {
- this.handle = new WeakCloseable(new WeakReference<Closeable>(handle));
- }
-
- public void handleClose(final T closed) {
- IoUtils.safeClose(handle);
- }
- }
-
- private static class KeyCloseHandler<T> implements CloseHandler<T> {
- private final Key key;
-
- public KeyCloseHandler(final Key key) {
- this.key = key;
- }
-
- public void handleClose(final T closed) {
- key.remove();
- }
- }
-
- public String toString() {
- return "generic resource <" + Integer.toHexString(hashCode()) +
">";
- }
-}
Deleted:
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AutoCloseable.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AutoCloseable.java 2009-04-30
22:58:40 UTC (rev 5100)
+++
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/AutoCloseable.java 2009-04-30
23:01:14 UTC (rev 5101)
@@ -1,41 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2008, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.jboss.remoting3.spi;
-
-import java.io.IOException;
-import org.jboss.remoting3.HandleableCloseable;
-
-/**
- * A closeable resource which closes automatically when the number of active handles
reaches zero. Handles are considered
- * active until they are closed.
- */
-public interface AutoCloseable<T> extends HandleableCloseable<T> {
-
- /**
- * Get a handle to this resource. When the number of open handles reaches zero, the
resource will be closed.
- *
- * @return a handle
- * @throws IOException if an error occurs, particularly if this resource is already
closed
- */
- Handle<T> getHandle() throws IOException;
-}
Deleted: remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/Handle.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/Handle.java 2009-04-30
22:58:40 UTC (rev 5100)
+++
remoting3/trunk/jboss-remoting/src/main/java/org/jboss/remoting3/spi/Handle.java 2009-04-30
23:01:14 UTC (rev 5101)
@@ -1,55 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2008, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.jboss.remoting3.spi;
-
-import java.io.IOException;
-import org.jboss.remoting3.CloseHandler;
-import org.jboss.remoting3.HandleableCloseable;
-
-/**
- * A handle to a reference-counted {@link org.jboss.remoting3.spi.AutoCloseable
AutoCloseable} resource.
- */
-public interface Handle<T> extends HandleableCloseable<Handle<T>> {
-
- /**
- * Get the resource.
- *
- * @return the resource
- */
- T getResource();
-
- /**
- * Close this handle. If this is the last handle to be closed, also close the
resource (throwing any exception
- * that may result).
- *
- * @throws IOException if the close failed
- */
- void close() throws IOException;
-
- /**
- * Add a handler that is invoked when this handle is closed.
- *
- * @param handler the handler
- */
- Key addCloseHandler(final CloseHandler<? super Handle<T>> handler);
-}
Modified:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/spi/CloseableTestCase.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/spi/CloseableTestCase.java 2009-04-30
22:58:40 UTC (rev 5100)
+++
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/spi/CloseableTestCase.java 2009-04-30
23:01:14 UTC (rev 5101)
@@ -22,17 +22,15 @@
package org.jboss.remoting3.spi;
-import junit.framework.TestCase;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ExecutorService;
import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
+import junit.framework.TestCase;
+import org.jboss.remoting3.CloseHandler;
import org.jboss.xnio.IoUtils;
import org.jboss.xnio.log.Logger;
-import org.jboss.remoting3.CloseHandler;
-import org.jboss.remoting3.HandleableCloseable;
/**
*
@@ -69,135 +67,4 @@
executorService.shutdownNow();
}
}
-
- public void testAutoClose() throws Throwable {
- final ExecutorService executorService = Executors.newCachedThreadPool();
- try {
- final AtomicBoolean closed = new AtomicBoolean();
- final CountDownLatch latch = new CountDownLatch(1);
- final AbstractAutoCloseable closeable = new
AbstractAutoCloseable(executorService) {
- // empty
- };
- final Handle rootHandle = closeable.getHandle();
- try {
- closeable.addCloseHandler(new CloseHandler() {
- public void handleClose(final Object x) {
- closed.set(true);
- latch.countDown();
- }
- });
- assertTrue(closeable.isOpen());
- assertFalse(closed.get());
- rootHandle.close();
- assertTrue(latch.await(500L, TimeUnit.MILLISECONDS));
- assertFalse(closeable.isOpen());
- assertTrue(closed.get());
- } finally {
- IoUtils.safeClose(closeable);
- }
- } finally {
- executorService.shutdownNow();
- }
- }
-
- public void testAutoCloseWithOneRef() throws Throwable {
- final ExecutorService executorService = Executors.newCachedThreadPool();
- try {
- final AtomicBoolean closed = new AtomicBoolean();
- final CountDownLatch latch = new CountDownLatch(1);
- final AbstractAutoCloseable closeable = new
AbstractAutoCloseable(executorService) {
- // empty
- };
- final Handle<Object> rootHandle = closeable.getHandle();
- try {
- closeable.addCloseHandler(new CloseHandler<Object>() {
- public void handleClose(final Object x) {
- closed.set(true);
- latch.countDown();
- }
- });
- assertTrue(closeable.isOpen());
- assertFalse(closed.get());
- final Handle<Object> h1 = closeable.getHandle();
- assertTrue(closeable.isOpen());
- assertFalse(closed.get());
- rootHandle.close();
- assertTrue(closeable.isOpen());
- assertFalse(closed.get());
- h1.close();
- assertTrue(latch.await(500L, TimeUnit.MILLISECONDS));
- assertFalse(closeable.isOpen());
- assertTrue(closed.get());
- } finally {
- IoUtils.safeClose(closeable);
- }
- } finally {
- executorService.shutdownNow();
- }
- }
-
- public void testAutoCloseWithThreeRefs() throws Throwable {
- final ExecutorService executorService = Executors.newCachedThreadPool();
- try {
- final AtomicBoolean closed = new AtomicBoolean();
- final CountDownLatch latch = new CountDownLatch(1);
- final AbstractAutoCloseable closeable = new
AbstractAutoCloseable(executorService) {
- // empty
- };
- final Handle<Object> rootHandle = closeable.getHandle();
- try {
- closeable.addCloseHandler(new CloseHandler<Object>() {
- public void handleClose(final Object x) {
- closed.set(true);
- latch.countDown();
- }
- });
- assertTrue(closeable.isOpen());
- assertFalse(closed.get());
- final Handle<Object> h1 = closeable.getHandle();
- final Handle<Object> h2 = closeable.getHandle();
- final Handle<Object> h3 = closeable.getHandle();
- assertTrue(closeable.isOpen());
- assertFalse(closed.get());
- rootHandle.close();
- assertTrue(closeable.isOpen());
- assertFalse(closed.get());
- h1.close();
- assertTrue(closeable.isOpen());
- assertFalse(closed.get());
- h2.close();
- assertTrue(closeable.isOpen());
- assertFalse(closed.get());
- h3.close();
- assertTrue(latch.await(500L, TimeUnit.MILLISECONDS));
- assertFalse(closeable.isOpen());
- assertTrue(closed.get());
- } finally {
- IoUtils.safeClose(closeable);
- }
- } finally {
- executorService.shutdownNow();
- }
- }
-
- public void testHandlerRemoval() throws Throwable {
- final AtomicBoolean handlerCalled = new AtomicBoolean();
- final Executor executor = IoUtils.directExecutor();
- final AbstractAutoCloseable closeable = new AbstractAutoCloseable(executor) {
- // empty
- };
- final Handle<Object> rootHandle = closeable.getHandle();
- try {
- final HandleableCloseable.Key key = closeable.addCloseHandler(new
CloseHandler<Object>() {
- public void handleClose(final Object closed) {
- handlerCalled.set(true);
- }
- });
- key.remove();
- rootHandle.close();
- assertFalse(handlerCalled.get());
- } finally {
- IoUtils.safeClose(closeable);
- }
- }
}