mirror of https://github.com/apache/activemq.git
make the validators thread safe
This commit is contained in:
parent
1a91decf9f
commit
b34336cc0a
|
@ -16,6 +16,8 @@
|
|||
*/
|
||||
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.transport.Attach;
|
||||
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
|
||||
* the state of a remote resource after a variety of lifecycle events.
|
||||
* the values of incoming or outgoing AMQP frames.
|
||||
*/
|
||||
public class AmqpFrameValidator {
|
||||
|
||||
private boolean valid = true;
|
||||
private String errorMessage;
|
||||
private AtomicReference<String> errorMessage = new AtomicReference<String>();
|
||||
|
||||
public void inspectOpen(Open open, Binary encoded) {
|
||||
|
||||
|
@ -73,31 +74,29 @@ public class AmqpFrameValidator {
|
|||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return valid;
|
||||
return errorMessage.get() != null;
|
||||
}
|
||||
|
||||
protected void setValid(boolean valid) {
|
||||
this.valid = valid;
|
||||
public final void clearErrorMessage() {
|
||||
errorMessage.set(null);
|
||||
}
|
||||
|
||||
public String getErrorMessage() {
|
||||
return errorMessage;
|
||||
public final String getErrorMessage() {
|
||||
return errorMessage.get();
|
||||
}
|
||||
|
||||
protected void setErrorMessage(String errorMessage) {
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
protected void markAsInvalid(String errorMessage) {
|
||||
if (valid) {
|
||||
setValid(false);
|
||||
setErrorMessage(errorMessage);
|
||||
protected final boolean markAsInvalid(String message) {
|
||||
if (message == null) {
|
||||
throw new NullPointerException("Provided error message cannot be null!");
|
||||
}
|
||||
|
||||
return errorMessage.compareAndSet(null, message);
|
||||
}
|
||||
|
||||
public void assertValid() {
|
||||
if (!isValid()) {
|
||||
throw new AssertionError(errorMessage);
|
||||
public final void assertValid() {
|
||||
final String assertionErrorMessage = errorMessage.get();
|
||||
if (assertionErrorMessage != null) {
|
||||
throw new AssertionError(assertionErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,19 +16,20 @@
|
|||
*/
|
||||
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.Receiver;
|
||||
import org.apache.qpid.proton.engine.Sender;
|
||||
import org.apache.qpid.proton.engine.Session;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* 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.
|
||||
*/
|
||||
public class AmqpValidator {
|
||||
|
||||
private boolean valid = true;
|
||||
private String errorMessage;
|
||||
private AtomicReference<String> errorMessage = new AtomicReference<String>();
|
||||
|
||||
public void inspectOpenedResource(Connection connection) {
|
||||
|
||||
|
@ -71,31 +72,29 @@ public class AmqpValidator {
|
|||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return valid;
|
||||
return errorMessage.get() != null;
|
||||
}
|
||||
|
||||
protected void setValid(boolean valid) {
|
||||
this.valid = valid;
|
||||
public final void clearErrorMessage() {
|
||||
errorMessage.set(null);
|
||||
}
|
||||
|
||||
public String getErrorMessage() {
|
||||
return errorMessage;
|
||||
public final String getErrorMessage() {
|
||||
return errorMessage.get();
|
||||
}
|
||||
|
||||
protected void setErrorMessage(String errorMessage) {
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
protected void markAsInvalid(String errorMessage) {
|
||||
if (valid) {
|
||||
setValid(false);
|
||||
setErrorMessage(errorMessage);
|
||||
protected final boolean markAsInvalid(String message) {
|
||||
if (message == null) {
|
||||
throw new NullPointerException("Provided error message cannot be null!");
|
||||
}
|
||||
|
||||
return errorMessage.compareAndSet(null, message);
|
||||
}
|
||||
|
||||
public void assertValid() {
|
||||
if (!isValid()) {
|
||||
throw new AssertionError(errorMessage);
|
||||
public final void assertValid() {
|
||||
final String assertionErrorMessage = errorMessage.get();
|
||||
if (assertionErrorMessage != null) {
|
||||
throw new AssertionError(assertionErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue