This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push: new 5fe0c9c Update the refactoring to improve thread-safety of JNDIConnection 5fe0c9c is described below commit 5fe0c9cd303c1974b21651d9281521ce9c52d659 Author: Mark Thomas <ma...@apache.org> AuthorDate: Tue Jun 22 12:01:54 2021 +0100 Update the refactoring to improve thread-safety of JNDIConnection I'm not planning to back-port this due to the changes in the constructor --- java/org/apache/catalina/realm/JNDIRealm.java | 65 +++++++++++++++++---------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/java/org/apache/catalina/realm/JNDIRealm.java b/java/org/apache/catalina/realm/JNDIRealm.java index 4949862..ed5b4bd 100644 --- a/java/org/apache/catalina/realm/JNDIRealm.java +++ b/java/org/apache/catalina/realm/JNDIRealm.java @@ -445,7 +445,7 @@ public class JNDIRealm extends RealmBase { /** * Non pooled connection to our directory server. */ - protected JNDIConnection singleConnection = new JNDIConnection(); + protected JNDIConnection singleConnection; /** * The lock to ensure single connection thread safety. @@ -2394,6 +2394,9 @@ public class JNDIRealm extends RealmBase { } } else { singleConnectionLock.lock(); + if (singleConnection == null) { + singleConnection = create(); + } connection = singleConnection; } if (connection.context == null) { @@ -2426,24 +2429,7 @@ public class JNDIRealm extends RealmBase { * @return the new connection */ protected JNDIConnection create() { - JNDIConnection connection = new JNDIConnection(); - if (userSearch != null) { - connection.userSearchFormat = new MessageFormat(userSearch); - } - if (userPattern != null) { - int len = userPatternArray.length; - connection.userPatternFormatArray = new MessageFormat[len]; - for (int i = 0; i < len; i++) { - connection.userPatternFormatArray[i] = new MessageFormat(userPatternArray[i]); - } - } - if (roleBase != null) { - connection.roleBaseFormat = new MessageFormat(roleBase); - } - if (roleSearch != null) { - connection.roleFormat = new MessageFormat(roleSearch); - } - return connection; + return new JNDIConnection(userSearch, userPatternArray, roleBase, roleSearch); } @@ -3059,29 +3045,60 @@ public class JNDIRealm extends RealmBase { * The MessageFormat object associated with the current * <code>userSearch</code>. */ - public MessageFormat userSearchFormat = null; + public final MessageFormat userSearchFormat; /** * An array of MessageFormat objects associated with the current * <code>userPatternArray</code>. */ - public MessageFormat[] userPatternFormatArray = null; + public final MessageFormat[] userPatternFormatArray; /** * The MessageFormat object associated with the current * <code>roleBase</code>. */ - public MessageFormat roleBaseFormat = null; + public final MessageFormat roleBaseFormat; /** * The MessageFormat object associated with the current * <code>roleSearch</code>. */ - public MessageFormat roleFormat = null; + public final MessageFormat roleFormat ; /** * The directory context linking us to our directory server. */ - public DirContext context = null; + public volatile DirContext context = null; + + + public JNDIConnection(String userSearch, String[] userPatternArray, String roleBase, String roleSearch) { + if (userSearch == null) { + userSearchFormat = null; + } else { + userSearchFormat = new MessageFormat(userSearch); + } + + if (userPatternArray == null) { + userPatternFormatArray = null; + } else { + int len = userPatternArray.length; + userPatternFormatArray = new MessageFormat[len]; + for (int i = 0; i < len; i++) { + userPatternFormatArray[i] = new MessageFormat(userPatternArray[i]); + } + } + + if (roleBase == null) { + roleBaseFormat = null; + } else { + roleBaseFormat = new MessageFormat(roleBase); + } + + if (roleSearch == null) { + roleFormat = null; + } else { + roleFormat = new MessageFormat(roleSearch); + } + } } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org