Introduced Callback.Nested.

Refactored code that was using nested callbacks and removed
unnecessary overrides now that we have default methods.
This commit is contained in:
Simone Bordet 2016-03-15 15:24:27 +01:00
parent 30f50c01ed
commit 26f8deddf7
13 changed files with 75 additions and 160 deletions

View File

@ -300,25 +300,18 @@ public class HttpSenderOverHTTP extends HttpSender
}
}
private class ByteBufferRecyclerCallback implements Callback
private class ByteBufferRecyclerCallback extends Callback.Nested
{
private final Callback callback;
private final ByteBufferPool pool;
private final ByteBuffer[] buffers;
private ByteBufferRecyclerCallback(Callback callback, ByteBufferPool pool, ByteBuffer... buffers)
{
this.callback = callback;
super(callback);
this.pool = pool;
this.buffers = buffers;
}
@Override
public boolean isNonBlocking()
{
return callback.isNonBlocking();
}
@Override
public void succeeded()
{
@ -327,7 +320,7 @@ public class HttpSenderOverHTTP extends HttpSender
assert !buffer.hasRemaining();
pool.release(buffer);
}
callback.succeeded();
super.succeeded();
}
@Override
@ -335,7 +328,7 @@ public class HttpSenderOverHTTP extends HttpSender
{
for (ByteBuffer buffer : buffers)
pool.release(buffer);
callback.failed(x);
super.failed(x);
}
}
}

View File

@ -217,11 +217,6 @@ public class DeferredContentProvider implements AsyncContentProvider, Callback,
return closed.get();
}
@Override
public void succeeded()
{
}
@Override
public void failed(Throwable failure)
{

View File

@ -136,11 +136,6 @@ public class InputStreamContentProvider implements ContentProvider, Callback, Cl
}
}
@Override
public void succeeded()
{
}
@Override
public void failed(Throwable failure)
{

View File

@ -312,17 +312,16 @@ public class HTTP2Flusher extends IteratingCallback
entry.failed(failure);
}
public static abstract class Entry implements Callback
public static abstract class Entry extends Callback.Nested
{
protected final Frame frame;
protected final IStream stream;
protected final Callback callback;
protected Entry(Frame frame, IStream stream, Callback callback)
{
super(callback);
this.frame = frame;
this.stream = stream;
this.callback = callback;
}
public int dataRemaining()
@ -348,7 +347,7 @@ public class HTTP2Flusher extends IteratingCallback
stream.close();
stream.getSession().removeStream(stream);
}
callback.failed(x);
super.failed(x);
}
public boolean isProtocol()

View File

@ -1148,7 +1148,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
break;
}
}
callback.succeeded();
super.succeeded();
}
}
@ -1223,7 +1223,7 @@ public abstract class HTTP2Session extends ContainerLifeCycle implements ISessio
// and eventually remove the stream.
if (stream.updateClose(dataFrame.isEndStream(), true))
removeStream(stream);
callback.succeeded();
super.succeeded();
}
}
}

View File

@ -110,26 +110,20 @@ public class HttpReceiverOverHTTP2 extends HttpReceiver implements Stream.Listen
copy.put(original);
BufferUtil.flipToFlush(copy, 0);
Callback delegate = new Callback()
Callback delegate = new Callback.Nested(callback)
{
@Override
public boolean isNonBlocking()
{
return callback.isNonBlocking();
}
@Override
public void succeeded()
{
byteBufferPool.release(copy);
callback.succeeded();
super.succeeded();
}
@Override
public void failed(Throwable x)
{
byteBufferPool.release(copy);
callback.failed(x);
super.failed(x);
}
};

View File

@ -1,68 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.http2.server;
import java.nio.ByteBuffer;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.util.Callback;
public class ByteBufferCallback implements Callback
{
private final ByteBufferPool byteBufferPool;
private final ByteBuffer buffer;
private final Callback callback;
public ByteBufferCallback(ByteBufferPool byteBufferPool, ByteBuffer buffer, Callback callback)
{
this.byteBufferPool = byteBufferPool;
this.buffer = buffer;
this.callback = callback;
}
@Override
public boolean isNonBlocking()
{
return callback.isNonBlocking();
}
public ByteBuffer getByteBuffer()
{
return buffer;
}
@Override
public void succeeded()
{
recycle();
callback.succeeded();
}
@Override
public void failed(Throwable x)
{
recycle();
callback.failed(x);
}
private void recycle()
{
byteBufferPool.release(buffer);
}
}

View File

@ -303,11 +303,6 @@ public class SslConnection extends AbstractConnection
failedCallback(new Callback()
{
@Override
public void succeeded()
{
}
@Override
public void failed(Throwable x)
{
@ -315,7 +310,6 @@ public class SslConnection extends AbstractConnection
getFillInterest().onFail(x);
getWriteFlusher().onFail(x);
}
},x);
}
};

View File

@ -178,18 +178,12 @@ public class ProxyServlet extends AbstractProxyServlet
offset = 0;
}
onResponseContent(request, response, proxyResponse, buffer, offset, length, new Callback()
onResponseContent(request, response, proxyResponse, buffer, offset, length, new Callback.Nested(callback)
{
@Override
public void succeeded()
{
callback.succeeded();
}
@Override
public void failed(Throwable x)
{
callback.failed(x);
super.failed(x);
proxyResponse.abort(x);
}
});

View File

@ -21,7 +21,6 @@ package org.eclipse.jetty.server;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
@ -318,7 +317,7 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
{
_request.setHandled(false);
_response.getHttpOutput().reopen();
try
{
_request.setDispatcherType(DispatcherType.ASYNC);
@ -386,7 +385,7 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
_state.getAsyncContextEvent().setDispatchPath(error_page);
}
try
{
_request.setDispatcherType(DispatcherType.ERROR);
@ -758,25 +757,11 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
_transport.abort(failure);
}
private class CommitCallback implements Callback
private class CommitCallback extends Callback.Nested
{
private final Callback _callback;
private CommitCallback(Callback callback)
{
_callback = callback;
}
@Override
public boolean isNonBlocking()
{
return _callback.isNonBlocking();
}
@Override
public void succeeded()
{
_callback.succeeded();
super(callback);
}
@Override
@ -787,12 +772,12 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
if (x instanceof BadMessageException)
{
_transport.send(HttpGenerator.RESPONSE_500_INFO, false, null, true, new Callback()
_transport.send(HttpGenerator.RESPONSE_500_INFO, false, null, true, new Callback.Nested(this)
{
@Override
public void succeeded()
{
_callback.failed(x);
super.failed(x);
_response.getHttpOutput().closed();
}
@ -800,14 +785,14 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor
public void failed(Throwable th)
{
_transport.abort(x);
_callback.failed(x);
super.failed(x);
}
});
}
else
{
_transport.abort(x);
_callback.failed(x);
super.failed(x);
}
}
}

View File

@ -673,20 +673,20 @@ public class HttpOutput extends ServletOutputStream implements Runnable
if (LOG.isDebugEnabled())
LOG.debug("sendContent(buffer={},{})",BufferUtil.toDetailString(content),callback);
write(content, true, new Callback()
write(content, true, new Callback.Nested(callback)
{
@Override
public void succeeded()
{
closed();
callback.succeeded();
super.succeeded();
}
@Override
public void failed(Throwable x)
{
abort(x);
callback.failed(x);
super.failed(x);
}
});
}

View File

@ -30,8 +30,9 @@ public interface Callback
* Instance of Adapter that can be used when the callback methods need an empty
* implementation without incurring in the cost of allocating a new Adapter object.
*/
static Callback NOOP = new Callback(){};
Callback NOOP = new Callback()
{
};
/**
* <p>Callback invoked when the operation completes.</p>
@ -39,14 +40,16 @@ public interface Callback
* @see #failed(Throwable)
*/
default void succeeded()
{}
{
}
/**
* <p>Callback invoked when the operation fails.</p>
* @param x the reason for the operation failure
*/
default void failed(Throwable x)
{}
{
}
/**
* @return True if the callback is known to never block the caller
@ -55,25 +58,57 @@ public interface Callback
{
return false;
}
/**
* Callback interface that declares itself as non-blocking
*/
interface NonBlocking extends Callback
{
@Override
public default boolean isNonBlocking()
default boolean isNonBlocking()
{
return true;
}
}
class Nested implements Callback
{
private final Callback callback;
public Nested(Callback callback)
{
this.callback = callback;
}
public Nested(Nested nested)
{
this.callback = nested.callback;
}
@Override
public void succeeded()
{
callback.succeeded();
}
@Override
public void failed(Throwable x)
{
callback.failed(x);
}
@Override
public boolean isNonBlocking()
{
return callback.isNonBlocking();
}
}
/**
* <p>Empty implementation of {@link Callback}</p>
*/
@Deprecated
static class Adapter implements Callback
{}
class Adapter implements Callback
{
}
}

View File

@ -38,14 +38,13 @@ import java.util.concurrent.atomic.AtomicInteger;
* }
* </pre>
*/
public class CountingCallback implements Callback
public class CountingCallback extends Callback.Nested
{
private final Callback callback;
private final AtomicInteger count;
public CountingCallback(Callback callback, int count)
{
this.callback = callback;
super(callback);
this.count = new AtomicInteger(count);
}
@ -64,7 +63,7 @@ public class CountingCallback implements Callback
if (count.compareAndSet(current, current - 1))
{
if (current == 1)
callback.succeeded();
super.succeeded();
return;
}
}
@ -84,7 +83,7 @@ public class CountingCallback implements Callback
if (count.compareAndSet(current, 0))
{
callback.failed(failure);
super.failed(failure);
return;
}
}