Skip to content

Commit 4c366d0

Browse files
author
Marcel Overdijk
committed
Fixed #18 which now makes the RiveScrip instance thread safe
1 parent 0acbd0c commit 4c366d0

1 file changed

Lines changed: 37 additions & 33 deletions

File tree

rivescript-core/src/main/java/com/rivescript/RiveScript.java

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public class RiveScript {
105105
private String[] person_s = null; // sorted persons
106106

107107
// The current user ID when reply() is called.
108-
private String currentUser = null;
108+
private ThreadLocal<String> currentUser = new ThreadLocal<>();
109109

110110
/*-------------------------*/
111111
/*-- Constructor Methods --*/
@@ -518,7 +518,7 @@ public String getUservar(String user, String name) {
518518
* @return string user id or {@code null}.
519519
*/
520520
public String currentUser() {
521-
return this.currentUser;
521+
return this.currentUser.get();
522522
}
523523

524524
/**
@@ -988,46 +988,50 @@ public String reply(String username, String message) {
988988
say("Get reply to [" + username + "] " + message);
989989

990990
// Store the current ID in case an object macro wants it.
991-
this.currentUser = username;
991+
this.currentUser.set(username);
992992

993-
// Format their message first.
994-
message = formatMessage(message);
993+
try {
995994

996-
// This will hold the final reply.
997-
String reply;
995+
// Format their message first.
996+
message = formatMessage(message);
998997

999-
// If the BEGIN statement exists, consult it first.
1000-
if (topics.exists("__begin__")) {
1001-
String begin = this.reply(username, "request", true, 0);
998+
// This will hold the final reply.
999+
String reply;
10021000

1003-
// OK to continue?
1004-
if (begin.indexOf("{ok}") > -1) {
1005-
// Get a reply then.
1006-
reply = this.reply(username, message, false, 0);
1007-
begin = begin.replaceAll("\\{ok\\}", reply);
1008-
reply = begin;
1001+
// If the BEGIN statement exists, consult it first.
1002+
if (topics.exists("__begin__")) {
1003+
String begin = this.reply(username, "request", true, 0);
1004+
1005+
// OK to continue?
1006+
if (begin.indexOf("{ok}") > -1) {
1007+
// Get a reply then.
1008+
reply = this.reply(username, message, false, 0);
1009+
begin = begin.replaceAll("\\{ok\\}", reply);
1010+
reply = begin;
1011+
} else {
1012+
reply = begin;
1013+
}
1014+
1015+
// Run final substitutions.
1016+
reply = processTags(username, clients.client(username), message, reply,
1017+
new Vector<String>(), new Vector<String>(),
1018+
0);
10091019
} else {
1010-
reply = begin;
1020+
// No BEGIN, just continue.
1021+
reply = this.reply(username, message, false, 0);
10111022
}
10121023

1013-
// Run final substitutions.
1014-
reply = processTags(username, clients.client(username), message, reply,
1015-
new Vector<String>(), new Vector<String>(),
1016-
0);
1017-
} else {
1018-
// No BEGIN, just continue.
1019-
reply = this.reply(username, message, false, 0);
1020-
}
1021-
1022-
// Save their chat history.
1023-
clients.client(username).addInput(message);
1024-
clients.client(username).addReply(reply);
1024+
// Save their chat history.
1025+
clients.client(username).addInput(message);
1026+
clients.client(username).addReply(reply);
10251027

1026-
// Clear the current user.
1027-
this.currentUser = null;
1028+
// Return their reply.
1029+
return reply;
10281030

1029-
// Return their reply.
1030-
return reply;
1031+
} finally {
1032+
// Clear the current user.
1033+
this.currentUser.remove();
1034+
}
10311035
}
10321036

10331037
/**

0 commit comments

Comments
 (0)