Cosmetics: removed unused imports, cleaned up source code, improved javadocs.

This commit is contained in:
Simone Bordet 2014-09-26 09:39:38 +02:00
parent 8af9ea4030
commit a8b461fe91
10 changed files with 62 additions and 92 deletions

View File

@ -49,7 +49,6 @@ public class HttpClientTransportOverHTTP extends AbstractHttpClientTransport
@Override
public org.eclipse.jetty.io.Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException
{
HttpDestination destination = (HttpDestination)context.get(HTTP_DESTINATION_CONTEXT_KEY);
HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, destination);
@SuppressWarnings("unchecked")

View File

@ -41,8 +41,6 @@ import org.eclipse.jetty.server.HttpInput;
import org.eclipse.jetty.server.HttpTransport;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

View File

@ -51,9 +51,8 @@ import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.Scheduler;
/* ------------------------------------------------------------ */
/** HttpChannel.
* Represents a single endpoint for HTTP semantic processing.
/**
* HttpChannel represents a single endpoint for HTTP semantic processing.
* The HttpChannel is both a HttpParser.RequestHandler, where it passively receives events from
* an incoming HTTP request, and a Runnable, where it actively takes control of the request/response
* life cycle and calls the application (perhaps suspending and resuming with multiple calls to run).
@ -67,8 +66,8 @@ public class HttpChannel implements Runnable
private static final Logger LOG = Log.getLogger(HttpChannel.class);
private static final ThreadLocal<HttpChannel> __currentChannel = new ThreadLocal<>();
/* ------------------------------------------------------------ */
/** Get the current channel that this thread is dispatched to.
/**
* Get the current channel that this thread is dispatched to.
* @see Request#getAttribute(String) for a more general way to access the HttpChannel
* @return the current HttpChannel or null
*/
@ -218,7 +217,6 @@ public class HttpChannel implements Runnable
handle();
}
/* ------------------------------------------------------------ */
/**
* @return True if the channel is ready to continue handling (ie it is not suspended)
*/

View File

@ -19,29 +19,25 @@
package org.eclipse.jetty.server;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.SharedBlockingCallback;
import org.eclipse.jetty.util.SharedBlockingCallback.Blocker;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.server.HttpInput.Content;
public class HttpInputOverHTTP extends HttpInput implements Callback
{
private static final Logger LOG = Log.getLogger(HttpInputOverHTTP.class);
private final SharedBlockingCallback _readBlocker = new SharedBlockingCallback();
private final HttpConnection _httpConnection;
private Content _content;
private final SharedBlockingCallback _readBlocker;
/**
* @param httpConnection
*/
public HttpInputOverHTTP(HttpConnection httpConnection)
{
_httpConnection = httpConnection;
_readBlocker = new SharedBlockingCallback();
}
@Override

View File

@ -57,14 +57,7 @@ public class HttpOutput extends ServletOutputStream implements Runnable
private static Logger LOG = Log.getLogger(HttpOutput.class);
private final HttpChannel _channel;
private final SharedBlockingCallback _writeblock=new SharedBlockingCallback()
{
@Override
protected long getIdleTimeout()
{
return _channel.getIdleTimeout();
}
};
private final SharedBlockingCallback _writeBlock;
private long _written;
private ByteBuffer _aggregate;
private int _bufferSize;
@ -87,6 +80,14 @@ public class HttpOutput extends ServletOutputStream implements Runnable
public HttpOutput(HttpChannel channel)
{
_channel = channel;
_writeBlock = new SharedBlockingCallback()
{
@Override
protected long getIdleTimeout()
{
return _channel.getIdleTimeout();
}
};
_bufferSize = _channel.getHttpConfiguration().getOutputBufferSize();
_commitSize=_bufferSize/4;
}
@ -124,12 +125,12 @@ public class HttpOutput extends ServletOutputStream implements Runnable
protected Blocker acquireWriteBlockingCallback() throws IOException
{
return _writeblock.acquire();
return _writeBlock.acquire();
}
private void write(ByteBuffer content, boolean complete) throws IOException
{
try (Blocker blocker = _writeblock.acquire())
try (Blocker blocker = _writeBlock.acquire())
{
write(content, complete, blocker);
blocker.block();
@ -572,7 +573,7 @@ public class HttpOutput extends ServletOutputStream implements Runnable
*/
public void sendContent(InputStream in) throws IOException
{
try(Blocker blocker=_writeblock.acquire())
try(Blocker blocker = _writeBlock.acquire())
{
new InputStreamWritingCB(in, blocker).iterate();
blocker.block();
@ -594,7 +595,7 @@ public class HttpOutput extends ServletOutputStream implements Runnable
*/
public void sendContent(ReadableByteChannel in) throws IOException
{
try(Blocker blocker=_writeblock.acquire())
try(Blocker blocker = _writeBlock.acquire())
{
new ReadableByteChannelWritingCB(in, blocker).iterate();
blocker.block();
@ -616,7 +617,7 @@ public class HttpOutput extends ServletOutputStream implements Runnable
*/
public void sendContent(HttpContent content) throws IOException
{
try(Blocker blocker=_writeblock.acquire())
try(Blocker blocker = _writeBlock.acquire())
{
sendContent(content, blocker);
blocker.block();
@ -1062,7 +1063,7 @@ public class HttpOutput extends ServletOutputStream implements Runnable
if (_complete && !_completed)
{
_completed=true;
write(BufferUtil.EMPTY_BUFFER, _complete, this);
write(BufferUtil.EMPTY_BUFFER, true, this);
return Action.SCHEDULED;
}

View File

@ -18,15 +18,12 @@
package org.eclipse.jetty.server;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.io.ArrayByteBufferPool;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.MappedByteBufferPool;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
@ -34,6 +31,8 @@ import org.eclipse.jetty.util.Utf8StringBuilder;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class HttpWriterTest
{
private HttpOutput _httpOut;

View File

@ -26,17 +26,14 @@ import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.NonBlockingThread;
/* ------------------------------------------------------------ */
/**
* An implementation of Callback that blocks until success or failure.
*/
public class BlockingCallback implements Callback
{
private static final Logger LOG = Log.getLogger(BlockingCallback.class);
private static Throwable SUCCEEDED=new Throwable()
private static Throwable SUCCEEDED = new Throwable()
{
@Override
public String toString() { return "SUCCEEDED"; }
@ -44,9 +41,10 @@ public class BlockingCallback implements Callback
private final CountDownLatch _latch = new CountDownLatch(1);
private final AtomicReference<Throwable> _state = new AtomicReference<>();
public BlockingCallback()
{}
{
}
@Override
public void succeeded()
@ -62,7 +60,8 @@ public class BlockingCallback implements Callback
_latch.countDown();
}
/** Block until the Callback has succeeded or failed and
/**
* Blocks until the Callback has succeeded or failed and
* after the return leave in the state to allow reuse.
* This is useful for code that wants to repeatable use a FutureCallback to convert
* an asynchronous API to a blocking API.
@ -70,9 +69,6 @@ public class BlockingCallback implements Callback
*/
public void block() throws IOException
{
if (NonBlockingThread.isNonBlockingThread())
LOG.warn("Blocking a NonBlockingThread: ",new Throwable());
try
{
_latch.await();
@ -94,12 +90,10 @@ public class BlockingCallback implements Callback
_state.set(null);
}
}
@Override
public String toString()
{
return String.format("%s@%x{%s}",BlockingCallback.class.getSimpleName(),hashCode(),_state.get());
}
}

View File

@ -29,32 +29,26 @@ import java.util.concurrent.locks.ReentrantLock;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.NonBlockingThread;
/* ------------------------------------------------------------ */
/** Provides a reusable BlockingCallback.
/**
* Provides a reusable {@link Callback} that can block the thread
* while waiting to be completed.
* <p />
* A typical usage pattern is:
* <pre>
* void someBlockingCall(Object... args) throws IOException
* {
* try(Blocker blocker=sharedBlockingCallback.acquire())
* {
* someAsyncCall(args,blocker);
* blocker.block();
* }
* try(Blocker blocker = sharedBlockingCallback.acquire())
* {
* someAsyncCall(args, blocker);
* blocker.block();
* }
* }
* </pre>
*/
public class SharedBlockingCallback
{
static final Logger LOG = Log.getLogger(SharedBlockingCallback.class);
final ReentrantLock _lock = new ReentrantLock();
final Condition _idle = _lock.newCondition();
final Condition _complete = _lock.newCondition();
private static Throwable IDLE = new Throwable()
{
@Override
@ -63,7 +57,6 @@ public class SharedBlockingCallback
return "IDLE";
}
};
private static Throwable SUCCEEDED = new Throwable()
{
@Override
@ -72,7 +65,6 @@ public class SharedBlockingCallback
return "SUCCEEDED";
}
};
private static Throwable FAILED = new Throwable()
{
@Override
@ -82,12 +74,10 @@ public class SharedBlockingCallback
}
};
Blocker _blocker;
public SharedBlockingCallback()
{
_blocker=new Blocker();
}
private final ReentrantLock _lock = new ReentrantLock();
private final Condition _idle = _lock.newCondition();
private final Condition _complete = _lock.newCondition();
private Blocker _blocker = new Blocker();
protected long getIdleTimeout()
{
@ -131,13 +121,13 @@ public class SharedBlockingCallback
LOG.debug(new Throwable());
}
/* ------------------------------------------------------------ */
/** A Closeable Callback.
/**
* A Closeable Callback.
* Uses the auto close mechanism to check block has been called OK.
*/
public class Blocker implements Callback, Closeable
{
Throwable _state = IDLE;
private Throwable _state = IDLE;
protected Blocker()
{
@ -198,9 +188,6 @@ public class SharedBlockingCallback
*/
public void block() throws IOException
{
if (NonBlockingThread.isNonBlockingThread())
LOG.warn("Blocking a NonBlockingThread: ",new Throwable());
_lock.lock();
long idle = getIdleTimeout();
try
@ -216,7 +203,9 @@ public class SharedBlockingCallback
_state=new BlockerTimeoutException();
}
else
{
_complete.await();
}
}
if (_state == SUCCEEDED)
@ -245,12 +234,9 @@ public class SharedBlockingCallback
/**
* Check the Callback has succeeded or failed and after the return leave in the state to allow reuse.
*
* @throws IOException
* if exception was caught during blocking, or callback was cancelled
*/
@Override
public void close() throws IOException
public void close()
{
_lock.lock();
try

View File

@ -18,12 +18,6 @@
package org.eclipse.jetty.util;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@ -35,6 +29,12 @@ import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
public class SharedBlockingCallbackTest
{
final AtomicInteger notComplete = new AtomicInteger();
@ -201,7 +201,7 @@ public class SharedBlockingCallbackTest
blocker.succeeded();
blocker.block();
};
}
Assert.assertThat(System.currentTimeMillis()-start,Matchers.lessThan(600L));
Assert.assertEquals(0,notComplete.get());
}

View File

@ -24,9 +24,8 @@ import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.SharedBlockingCallback;
import org.eclipse.jetty.websocket.api.WriteCallback;
/* ------------------------------------------------------------ */
/** extend a SharedlBlockingCallback to an websocket WriteCallback
/**
* Extends a {@link SharedBlockingCallback} to a WebSocket {@link WriteCallback}
*/
public class BlockingWriteCallback extends SharedBlockingCallback
{
@ -41,9 +40,9 @@ public class BlockingWriteCallback extends SharedBlockingCallback
public static class WriteBlocker implements WriteCallback, Callback, AutoCloseable
{
Blocker blocker;
private final Blocker blocker;
WriteBlocker(Blocker blocker)
protected WriteBlocker(Blocker blocker)
{
this.blocker=blocker;
}
@ -73,7 +72,7 @@ public class BlockingWriteCallback extends SharedBlockingCallback
}
@Override
public void close() throws IOException
public void close()
{
blocker.close();
}