Fix b/13032911 by reestablishing ldapContext
Bug fix determined by observing new message in the log, followed by
subsequent fetches working again.
(moved AdServer.initialize() call to AdAdaptor.init())
diff --git a/src/com/google/enterprise/adaptor/ad/AdAdaptor.java b/src/com/google/enterprise/adaptor/ad/AdAdaptor.java
index 816bb70..05d43fc 100644
--- a/src/com/google/enterprise/adaptor/ad/AdAdaptor.java
+++ b/src/com/google/enterprise/adaptor/ad/AdAdaptor.java
@@ -103,6 +103,7 @@
+ host);
}
AdServer adServer = newAdServer(method, host, port, principal, passwd);
+ adServer.initialize();
servers.add(adServer);
Map<String, String> dup = new TreeMap<String, String>(singleServerConfig);
dup.put("password", "XXXXXX"); // hide password
@@ -140,7 +141,6 @@
GroupCatalog cumulativeCatalog = new GroupCatalog(localizedStrings,
namespace, feedBuiltinGroups);
for (AdServer server : servers) {
- server.initialize();
try {
GroupCatalog catalog = new GroupCatalog(localizedStrings, namespace,
feedBuiltinGroups);
diff --git a/src/com/google/enterprise/adaptor/ad/AdServer.java b/src/com/google/enterprise/adaptor/ad/AdServer.java
index aaefd1f..0f353dd 100644
--- a/src/com/google/enterprise/adaptor/ad/AdServer.java
+++ b/src/com/google/enterprise/adaptor/ad/AdServer.java
@@ -48,11 +48,15 @@
private static final Logger LOGGER
= Logger.getLogger(AdServer.class.getName());
- private final LdapContext ldapContext;
+ private LdapContext ldapContext;
private final SearchControls searchCtls;
- // properties necessary for connection
+ // properties necessary for connection and reconnection
+ private Method connectMethod;
private final String hostName;
+ private int port;
+ private String principal;
+ private String password;
// retrieved properties of the Active Directory controller
private String nETBIOSName;
@@ -68,6 +72,10 @@
int port, String principal, String password) {
this(hostName, createLdapContext(connectMethod, hostName, port,
principal, password));
+ this.connectMethod = connectMethod;
+ this.port = port;
+ this.principal = principal;
+ this.password = password;
}
@VisibleForTesting
@@ -119,6 +127,12 @@
}
}
+ @VisibleForTesting
+ void recreateLdapContext() {
+ ldapContext = createLdapContext(connectMethod, hostName, port, principal,
+ password);
+ }
+
/**
* Connects to the Active Directory server and retrieves AD configuration
* information.
@@ -127,7 +141,15 @@
* against Active Directory.
*/
public void connect() throws CommunicationException, NamingException {
- Attributes attributes = ldapContext.getAttributes("");
+ Attributes attributes;
+ try {
+ attributes = ldapContext.getAttributes("");
+ } catch (CommunicationException ce) {
+ LOGGER.log(Level.FINER,
+ "Reconnecting to AdServer after detecting issue", ce);
+ recreateLdapContext();
+ attributes = ldapContext.getAttributes("");
+ }
dn = attributes.get("defaultNamingContext").get(0).toString();
dsServiceName = attributes.get("dsServiceName").get(0).toString();
highestCommittedUSN = Long.parseLong(attributes.get(
@@ -171,6 +193,7 @@
protected Object get(String filter, String attribute, String base) {
searchCtls.setReturningAttributes(new String[] {attribute});
try {
+ connect(); // re-establish LDAP connection, if necessary
NamingEnumeration<SearchResult> ldapResults =
ldapContext.search(base, filter, searchCtls);
if (!ldapResults.hasMore()) {
@@ -227,6 +250,7 @@
searchCtls.setReturningAttributes(attributes);
setControls(deleted);
try {
+ connect(); // re-establish LDAP connection, if necessary
byte[] cookie = null;
do {
NamingEnumeration<SearchResult> ldapResults =
diff --git a/test/com/google/enterprise/adaptor/ad/AdAdaptorTest.java b/test/com/google/enterprise/adaptor/ad/AdAdaptorTest.java
index 6115d08..0251917 100644
--- a/test/com/google/enterprise/adaptor/ad/AdAdaptorTest.java
+++ b/test/com/google/enterprise/adaptor/ad/AdAdaptorTest.java
@@ -715,6 +715,10 @@
return super.search(filter, deleted, attributes);
}
}
+ @Override
+ void recreateLdapContext() {
+ // leave ldapContext unchanged
+ }
};
}
};
@@ -985,7 +989,12 @@
} catch (Exception e) {
fail("Could not create LdapContext:" + e);
}
- return new AdServer(host, ldapContext);
+ return new AdServer(host, ldapContext) {
+ @Override
+ void recreateLdapContext() {
+ // leave ldapContext unchanged
+ }
+ };
}
}
}