Attempt to make the bean thread-safe.

This commit is contained in:
jeb228 2010-12-15 15:17:06 +00:00
parent a874f75644
commit bd8ce05082

View file

@ -149,6 +149,11 @@ public class LoginProcessBean {
public String formatMessage(Object[] args) { public String formatMessage(Object[] args) {
return new MessageFormat(this.format).format(args); return new MessageFormat(this.format).format(args);
} }
@Override
public String toString() {
return "Message[" + messageLevel + ", '" + format + "']";
}
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@ -158,18 +163,21 @@ public class LoginProcessBean {
/** Where are we in the process? */ /** Where are we in the process? */
private State currentState = State.NOWHERE; private State currentState = State.NOWHERE;
/** Where is the interaction taking place? */
private volatile String loginPageUrl;
/** Where do we go when finished? */
private volatile String afterLoginUrl;
/** message and messageArguments must be kept consistent. */
private final Object messageSynchronizer = new Object();
/** What message should we display on the screen? */ /** What message should we display on the screen? */
private Message message = Message.NO_MESSAGE; private Message message = Message.NO_MESSAGE;
/** What arguments are needed to format the message? */ /** What arguments are needed to format the message? */
private Object[] messageArguments = NO_ARGUMENTS; private Object[] messageArguments = NO_ARGUMENTS;
/** Where is the interaction taking place? */
private String loginPageUrl;
/** Where do we go when finished? */
private String afterLoginUrl;
/** /**
* What username was submitted to the form? This isn't just for display -- * What username was submitted to the form? This isn't just for display --
* if they are changing passwords, we need to remember who it is. * if they are changing passwords, we need to remember who it is.
@ -185,16 +193,21 @@ public class LoginProcessBean {
} }
public void clearMessage() { public void clearMessage() {
synchronized (messageSynchronizer) {
this.message = Message.NO_MESSAGE; this.message = Message.NO_MESSAGE;
this.messageArguments = NO_ARGUMENTS; this.messageArguments = NO_ARGUMENTS;
} }
}
public void setMessage(Message message, Object... args) { public void setMessage(Message message, Object... args) {
synchronized (messageSynchronizer) {
this.message = message; this.message = message;
this.messageArguments = args; this.messageArguments = args;
} }
}
public String getInfoMessageAndClear() { public String getInfoMessageAndClear() {
synchronized (messageSynchronizer) {
String text = ""; String text = "";
if (message.getMessageLevel() == MLevel.INFO) { if (message.getMessageLevel() == MLevel.INFO) {
text = message.formatMessage(messageArguments); text = message.formatMessage(messageArguments);
@ -202,8 +215,10 @@ public class LoginProcessBean {
} }
return text; return text;
} }
}
public String getErrorMessageAndClear() { public String getErrorMessageAndClear() {
synchronized (messageSynchronizer) {
String text = ""; String text = "";
if (message.getMessageLevel() == MLevel.ERROR) { if (message.getMessageLevel() == MLevel.ERROR) {
text = message.formatMessage(messageArguments); text = message.formatMessage(messageArguments);
@ -211,6 +226,7 @@ public class LoginProcessBean {
} }
return text; return text;
} }
}
public String getUsername() { public String getUsername() {
return username; return username;
@ -238,8 +254,8 @@ public class LoginProcessBean {
@Override @Override
public String toString() { public String toString() {
return "LoginProcessBean[state=" + currentState + ", message=" return "LoginProcessBean(" + hashCode() + ")[state=" + currentState
+ message + ", messageArguments=" + ", message=" + message + ", messageArguments="
+ Arrays.deepToString(messageArguments) + ", username=" + Arrays.deepToString(messageArguments) + ", username="
+ username + ", loginPageUrl=" + loginPageUrl + username + ", loginPageUrl=" + loginPageUrl
+ ", afterLoginUrl=" + afterLoginUrl + "]"; + ", afterLoginUrl=" + afterLoginUrl + "]";