Browse code

BM-14875 Imp: LDAP pool management

Anthony Prades authored on 06/06/2019 14:12:11
Showing 2 changed files
... ...
@@ -86,7 +86,7 @@ public class LdapPoolWrapper {
86 86
 			try {
87 87
 				tryConnection(ldapHost, ldapConnectionConfig);
88 88
 			} catch (StartTlsFault stf) {
89
-				logger.error("Unable to connect tls:" + ldapHost.hostname + ":" + ldapHost.port, stf);
89
+				logger.error("Unable to connect tls:{}:{}", ldapHost.hostname, ldapHost.port, stf);
90 90
 
91 91
 				if (ldapParameters.ldapServer.protocol == LdapProtocol.TLSPLAIN) {
92 92
 					ldapConnectionConfig.setUseTls(false);
... ...
@@ -97,9 +97,9 @@ public class LdapPoolWrapper {
97 97
 	}
98 98
 
99 99
 	private void tryConnection(Host ldapHost, LdapConnectionConfig ldapConnectionConfig) {
100
-		logger.info("Trying to connect to: "
101
-				+ (ldapConnectionConfig.isUseSsl() ? "ssl:" : ldapConnectionConfig.isUseTls() ? "tls:" : "")
102
-				+ ldapHost.hostname + ":" + ldapHost.port);
100
+		logger.info("Trying to connect to: {} {}:{}",
101
+				ldapConnectionConfig.isUseSsl() ? "ssl:" : ldapConnectionConfig.isUseTls() ? "tls:" : "",
102
+				ldapHost.hostname, ldapHost.port);
103 103
 
104 104
 		DefaultPoolableLdapConnectionFactory bpcf = new DefaultPoolableLdapConnectionFactory(ldapConnectionConfig);
105 105
 
... ...
@@ -107,6 +107,7 @@ public class LdapPoolWrapper {
107 107
 		LdapConnection conn = null;
108 108
 		try {
109 109
 			tmpPool = new LdapConnectionPool(bpcf);
110
+			tmpPool.setMaxWait(10000L);
110 111
 
111 112
 			conn = tmpPool.getConnection();
112 113
 			tmpPool.releaseConnection(conn);
... ...
@@ -81,14 +81,18 @@ public class ImportLdapAuthenticationService extends ImportAuthenticationService
81 81
 			if (result.next()) {
82 82
 				ldapUserLogin = result.get().getDn().getName();
83 83
 			}
84
+		} catch (RuntimeException re) {
85
+			if (re.getCause() != null && re.getCause() instanceof InterruptedException) {
86
+				logger.error(String.format("Getting an interrupted exception, reseting pool for %s", parameters), re);
87
+				ldapPoolByDomain.resetPool(parameters);
88
+			}
89
+
90
+			throw re;
84 91
 		} catch (Exception e) {
85 92
 			logger.error("Fail to get LDAP DN for user: " + userLogin + "@" + domainName, e);
86 93
 			return null;
87 94
 		} finally {
88
-			try {
89
-				ldapPoolByDomain.releaseConnectionContext(ldapConCtx);
90
-			} catch (Exception e1) {
91
-			}
95
+			releaseConnection(ldapPoolByDomain, parameters, ldapConCtx);
92 96
 		}
93 97
 
94 98
 		return ldapUserLogin;
... ...
@@ -113,10 +117,18 @@ public class ImportLdapAuthenticationService extends ImportAuthenticationService
113 117
 			} else {
114 118
 				logger.warn("uuid " + uuid + " not found with filter " + filter);
115 119
 			}
120
+		} catch (RuntimeException re) {
121
+			if (re.getCause() != null && re.getCause() instanceof InterruptedException) {
122
+				logger.error(String.format("Getting an interrupted exception, reseting pool for %s", parameters), re);
123
+				ldapPoolByDomain.resetPool(parameters);
124
+			}
125
+
126
+			throw re;
116 127
 		} catch (Exception e) {
117 128
 			logger.error(String.format("Error searching external ID %s", uuid), e);
118 129
 			throw e;
119 130
 		} finally {
131
+			// https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
120 132
 			if (result != null) {
121 133
 				try {
122 134
 					result.close();
... ...
@@ -125,10 +137,7 @@ public class ImportLdapAuthenticationService extends ImportAuthenticationService
125 137
 				}
126 138
 			}
127 139
 
128
-			try {
129
-				ldapPoolByDomain.releaseConnectionContext(ldapConCtx);
130
-			} catch (Exception e) {
131
-			}
140
+			releaseConnection(ldapPoolByDomain, parameters, ldapConCtx);
132 141
 		}
133 142
 
134 143
 		if (ldapUserLogin == null) {
... ...
@@ -170,14 +179,29 @@ public class ImportLdapAuthenticationService extends ImportAuthenticationService
170 179
 					ldapConCtx.getConnectedProtocol().name(), ldapConCtx.ldapConnectionConfig.getLdapHost(),
171 180
 					ldapConCtx.ldapConnectionConfig.getLdapPort(), userDn, ldSearchTime);
172 181
 			return true;
182
+		} catch (RuntimeException re) {
183
+			if (re.getCause() != null && re.getCause() instanceof InterruptedException) {
184
+				logger.error(String.format("Getting an interrupted exception, reseting pool for %s", parameters), re);
185
+				ldapPoolByDomain.resetPool(parameters);
186
+			}
187
+
188
+			throw re;
173 189
 		} catch (Exception e) {
174 190
 			logger.error("Fail to check LDAP authentication", e);
175 191
 			return false;
176 192
 		} finally {
177
-			try {
178
-				ldapPoolByDomain.releaseConnectionContext(ldapConCtx);
179
-			} catch (Exception e) {
180
-			}
193
+			// https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
194
+			releaseConnection(ldapPoolByDomain, parameters, ldapConCtx);
195
+		}
196
+	}
197
+
198
+	private void releaseConnection(LdapPoolByDomain ldapPoolByDomain, Parameters parameters,
199
+			LdapConnectionContext ldapConCtx) {
200
+		try {
201
+			ldapPoolByDomain.releaseConnectionContext(ldapConCtx);
202
+		} catch (Exception e) {
203
+			logger.error(String.format("Unable to release LDAP connection for %s, closing this connection", parameters),
204
+					e);
181 205
 		}
182 206
 	}
183 207
 }