make the validators thread safe
This commit is contained in:
Timothy Bish 2016-10-26 14:48:42 -04:00
parent 1a91decf9f
commit b34336cc0a
2 changed files with 37 additions and 39 deletions

View File

@ -16,6 +16,8 @@
*/ */
package org.apache.activemq.transport.amqp.client; package org.apache.activemq.transport.amqp.client;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.qpid.proton.amqp.Binary; import org.apache.qpid.proton.amqp.Binary;
import org.apache.qpid.proton.amqp.transport.Attach; import org.apache.qpid.proton.amqp.transport.Attach;
import org.apache.qpid.proton.amqp.transport.Begin; import org.apache.qpid.proton.amqp.transport.Begin;
@ -29,12 +31,11 @@ import org.apache.qpid.proton.amqp.transport.Transfer;
/** /**
* Abstract base for a validation hook that is used in tests to check * Abstract base for a validation hook that is used in tests to check
* the state of a remote resource after a variety of lifecycle events. * the values of incoming or outgoing AMQP frames.
*/ */
public class AmqpFrameValidator { public class AmqpFrameValidator {
private boolean valid = true; private AtomicReference<String> errorMessage = new AtomicReference<String>();
private String errorMessage;
public void inspectOpen(Open open, Binary encoded) { public void inspectOpen(Open open, Binary encoded) {
@ -73,31 +74,29 @@ public class AmqpFrameValidator {
} }
public boolean isValid() { public boolean isValid() {
return valid; return errorMessage.get() != null;
} }
protected void setValid(boolean valid) { public final void clearErrorMessage() {
this.valid = valid; errorMessage.set(null);
} }
public String getErrorMessage() { public final String getErrorMessage() {
return errorMessage; return errorMessage.get();
} }
protected void setErrorMessage(String errorMessage) { protected final boolean markAsInvalid(String message) {
this.errorMessage = errorMessage; if (message == null) {
} throw new NullPointerException("Provided error message cannot be null!");
protected void markAsInvalid(String errorMessage) {
if (valid) {
setValid(false);
setErrorMessage(errorMessage);
} }
return errorMessage.compareAndSet(null, message);
} }
public void assertValid() { public final void assertValid() {
if (!isValid()) { final String assertionErrorMessage = errorMessage.get();
throw new AssertionError(errorMessage); if (assertionErrorMessage != null) {
throw new AssertionError(assertionErrorMessage);
} }
} }
} }

View File

@ -16,19 +16,20 @@
*/ */
package org.apache.activemq.transport.amqp.client; package org.apache.activemq.transport.amqp.client;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.qpid.proton.engine.Connection; import org.apache.qpid.proton.engine.Connection;
import org.apache.qpid.proton.engine.Receiver; import org.apache.qpid.proton.engine.Receiver;
import org.apache.qpid.proton.engine.Sender; import org.apache.qpid.proton.engine.Sender;
import org.apache.qpid.proton.engine.Session; import org.apache.qpid.proton.engine.Session;
/** /**
* Abstract base for a validation hook that is used in tests to check * Abstract base for a validation hook that is used in tests to check the state
* the state of a remote resource after a variety of lifecycle events. * of a remote resource after a variety of lifecycle events.
*/ */
public class AmqpValidator { public class AmqpValidator {
private boolean valid = true; private AtomicReference<String> errorMessage = new AtomicReference<String>();
private String errorMessage;
public void inspectOpenedResource(Connection connection) { public void inspectOpenedResource(Connection connection) {
@ -71,31 +72,29 @@ public class AmqpValidator {
} }
public boolean isValid() { public boolean isValid() {
return valid; return errorMessage.get() != null;
} }
protected void setValid(boolean valid) { public final void clearErrorMessage() {
this.valid = valid; errorMessage.set(null);
} }
public String getErrorMessage() { public final String getErrorMessage() {
return errorMessage; return errorMessage.get();
} }
protected void setErrorMessage(String errorMessage) { protected final boolean markAsInvalid(String message) {
this.errorMessage = errorMessage; if (message == null) {
} throw new NullPointerException("Provided error message cannot be null!");
protected void markAsInvalid(String errorMessage) {
if (valid) {
setValid(false);
setErrorMessage(errorMessage);
} }
return errorMessage.compareAndSet(null, message);
} }
public void assertValid() { public final void assertValid() {
if (!isValid()) { final String assertionErrorMessage = errorMessage.get();
throw new AssertionError(errorMessage); if (assertionErrorMessage != null) {
throw new AssertionError(assertionErrorMessage);
} }
} }
} }