Skip to content

Commit

Permalink
isCloseRequired -> Closed
Browse files Browse the repository at this point in the history
Align behaviour of methods with closed state

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1661514 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Feb 22, 2015
1 parent 3d68474 commit fb5e337
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
2 changes: 2 additions & 0 deletions java/org/apache/coyote/http11/upgrade/LocalStrings.properties
Expand Up @@ -25,11 +25,13 @@ upgrade.sis.isReady.ise=It is illegal to call isReady() when the ServletInputStr
upgrade.sis.onErrorFail=onError processing for the registered ReadListener triggered this further error which was swallowed upgrade.sis.onErrorFail=onError processing for the registered ReadListener triggered this further error which was swallowed
upgrade.sis.readListener.null=It is illegal to pass null to setReadListener() upgrade.sis.readListener.null=It is illegal to pass null to setReadListener()
upgrade.sis.readListener.set=It is illegal to call setReadListener() more than once for the same upgraded connection upgrade.sis.readListener.set=It is illegal to call setReadListener() more than once for the same upgraded connection
upgrade.sis.read.closed=The InputStream has been closed
upgrade.sis.read.ise=It is illegal to call any of the read() methods in non-blocking mode without first checking that there is data available by calling isReady() upgrade.sis.read.ise=It is illegal to call any of the read() methods in non-blocking mode without first checking that there is data available by calling isReady()
upgrade.sos.errorCloseFail=Failed to close OutputStream cleanly after a previous error upgrade.sos.errorCloseFail=Failed to close OutputStream cleanly after a previous error
upgrade.sos.canWrite.ise=It is illegal to call canWrite() when the ServletOutputStream is not in non-blocking mode (i.e. setWriteListener() must be called first) upgrade.sos.canWrite.ise=It is illegal to call canWrite() when the ServletOutputStream is not in non-blocking mode (i.e. setWriteListener() must be called first)
upgrade.sos.onErrorFail=onError processing for the registered WriteListener triggered this further error which was swallowed upgrade.sos.onErrorFail=onError processing for the registered WriteListener triggered this further error which was swallowed
upgrade.sos.writeListener.null=It is illegal to pass null to setWriteListener() upgrade.sos.writeListener.null=It is illegal to pass null to setWriteListener()
upgrade.sos.writeListener.set=It is illegal to call setWriteListener() more than once for the same upgraded connection upgrade.sos.writeListener.set=It is illegal to call setWriteListener() more than once for the same upgraded connection
upgrade.sos.write.closed=The OutputStream has been closed
upgrade.sos.write.ise=It is illegal to call any of the write() methods in non-blocking mode without first checking that there is space available by calling isReady() upgrade.sos.write.ise=It is illegal to call any of the write() methods in non-blocking mode without first checking that there is space available by calling isReady()


8 changes: 4 additions & 4 deletions java/org/apache/coyote/http11/upgrade/UpgradeProcessor.java
Expand Up @@ -123,12 +123,12 @@ public final SocketState upgradeDispatch(SocketStatus status) {
} }
return SocketState.CLOSED; return SocketState.CLOSED;
} }
if (upgradeServletInputStream.isCloseRequired() || if (upgradeServletInputStream.isClosed() ||
upgradeServletOutputStream.isCloseRequired()) { upgradeServletOutputStream.isClosed()) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug(sm.getString("upgradeProcessor.requiredClose", log.debug(sm.getString("upgradeProcessor.requiredClose",
Boolean.valueOf(upgradeServletInputStream.isCloseRequired()), Boolean.valueOf(upgradeServletInputStream.isClosed()),
Boolean.valueOf(upgradeServletOutputStream.isCloseRequired()))); Boolean.valueOf(upgradeServletOutputStream.isClosed())));
} }
return SocketState.CLOSED; return SocketState.CLOSED;
} }
Expand Down
Expand Up @@ -35,7 +35,7 @@ public class UpgradeServletInputStream extends ServletInputStream {


private final SocketWrapperBase<?> socketWrapper; private final SocketWrapperBase<?> socketWrapper;


private volatile boolean closeRequired = false; private volatile boolean closed = false;
// Start in blocking-mode // Start in blocking-mode
private volatile Boolean ready = Boolean.TRUE; private volatile Boolean ready = Boolean.TRUE;
private volatile ReadListener listener = null; private volatile ReadListener listener = null;
Expand Down Expand Up @@ -66,6 +66,10 @@ public final boolean isReady() {
sm.getString("upgrade.sis.isReady.ise")); sm.getString("upgrade.sis.isReady.ise"));
} }


if (closed) {
return false;
}

// If we already know the current state, return it. // If we already know the current state, return it.
if (ready != null) { if (ready != null) {
return ready.booleanValue(); return ready.booleanValue();
Expand All @@ -90,6 +94,10 @@ public final void setReadListener(ReadListener listener) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
sm.getString("upgrade.sis.readListener.set")); sm.getString("upgrade.sis.readListener.set"));
} }
if (closed) {
throw new IllegalStateException(sm.getString("upgrade.sis.read.closed"));
}

this.listener = listener; this.listener = listener;
this.applicationLoader = Thread.currentThread().getContextClassLoader(); this.applicationLoader = Thread.currentThread().getContextClassLoader();
// Switching to non-blocking. Don't know if data is available. // Switching to non-blocking. Don't know if data is available.
Expand Down Expand Up @@ -132,7 +140,7 @@ public final int read(byte[] b, int off, int len) throws IOException {
try { try {
return socketWrapper.read(listener == null, b, off, len); return socketWrapper.read(listener == null, b, off, len);
} catch (IOException ioe) { } catch (IOException ioe) {
closeRequired = true; close();
throw ioe; throw ioe;
} }
} }
Expand All @@ -141,14 +149,16 @@ public final int read(byte[] b, int off, int len) throws IOException {


@Override @Override
public void close() throws IOException { public void close() throws IOException {
closeRequired = true; closed = true;
} }




private void preReadChecks() { private void preReadChecks() {
if (listener != null && (ready == null || !ready.booleanValue())) { if (listener != null && (ready == null || !ready.booleanValue())) {
throw new IllegalStateException( throw new IllegalStateException(sm.getString("upgrade.sis.read.ise"));
sm.getString("upgrade.sis.read.ise")); }
if (closed) {
throw new IllegalStateException(sm.getString("upgrade.sis.read.closed"));
} }
// No longer know if data is available // No longer know if data is available
ready = null; ready = null;
Expand All @@ -163,7 +173,7 @@ private int readInternal() throws IOException {
try { try {
result = socketWrapper.read(listener == null, b, 0, 1); result = socketWrapper.read(listener == null, b, 0, 1);
} catch (IOException ioe) { } catch (IOException ioe) {
closeRequired = true; close();
throw ioe; throw ioe;
} }
if (result == 0) { if (result == 0) {
Expand Down Expand Up @@ -224,7 +234,7 @@ private final void onError(Throwable t) {
} }




final boolean isCloseRequired() { final boolean isClosed() {
return closeRequired; return closed;
} }
} }
Expand Up @@ -50,7 +50,7 @@ public class UpgradeServletOutputStream extends ServletOutputStream {


private volatile boolean flushing = false; private volatile boolean flushing = false;


private volatile boolean closeRequired = false; private volatile boolean closed = false;


// Start in blocking-mode // Start in blocking-mode
private volatile WriteListener listener = null; private volatile WriteListener listener = null;
Expand All @@ -72,6 +72,9 @@ public final boolean isReady() {
throw new IllegalStateException( throw new IllegalStateException(
sm.getString("upgrade.sos.canWrite.is")); sm.getString("upgrade.sos.canWrite.is"));
} }
if (closed) {
return false;
}


// Make sure isReady() and onWritePossible() have a consistent view of // Make sure isReady() and onWritePossible() have a consistent view of
// fireListener when determining if the listener should fire // fireListener when determining if the listener should fire
Expand Down Expand Up @@ -104,6 +107,9 @@ public final void setWriteListener(WriteListener listener) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
sm.getString("upgrade.sos.writeListener.set")); sm.getString("upgrade.sos.writeListener.set"));
} }
if (closed) {
throw new IllegalStateException(sm.getString("upgrade.sos.write.closed"));
}
// Container is responsible for first call to onWritePossible() but only // Container is responsible for first call to onWritePossible() but only
// need to do this if setting the listener for the first time. // need to do this if setting the listener for the first time.
synchronized (registeredLock) { synchronized (registeredLock) {
Expand All @@ -120,8 +126,8 @@ public final void setWriteListener(WriteListener listener) {
} }




final boolean isCloseRequired() { final boolean isClosed() {
return closeRequired; return closed;
} }




Expand All @@ -145,6 +151,7 @@ public void write(byte[] b, int off, int len) throws IOException {


@Override @Override
public void flush() throws IOException { public void flush() throws IOException {
preWriteChecks();
flushInternal(listener == null, true); flushInternal(listener == null, true);
} }


Expand Down Expand Up @@ -174,14 +181,21 @@ private void flushInternal(boolean block, boolean updateFlushing) throws IOExcep


@Override @Override
public void close() throws IOException { public void close() throws IOException {
closeRequired = true; if (closed) {
return;
}
closed = true;
flushInternal(true, true);
} }




private void preWriteChecks() { private void preWriteChecks() {
if (listener != null && !socketWrapper.canWrite()) { if (listener != null && !socketWrapper.canWrite()) {
throw new IllegalStateException(sm.getString("upgrade.sos.write.ise")); throw new IllegalStateException(sm.getString("upgrade.sos.write.ise"));
} }
if (closed) {
throw new IllegalStateException(sm.getString("upgrade.sos.write.closed"));
}
} }




Expand Down

0 comments on commit fb5e337

Please sign in to comment.