LDAP: create a separate thread only for person/contact searches

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1350 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-08-17 09:02:33 +00:00
parent 36aff74010
commit 3eaa08c431
2 changed files with 25 additions and 24 deletions

View File

@ -244,6 +244,9 @@ public class DavExchangeSession extends ExchangeSession {
try { try {
DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod, true); DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod, true);
results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN"); results = XMLStreamUtil.getElementContentsAsMap(getMethod.getResponseBodyAsStream(), "item", "AN");
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(path + ": " + results.size() + " result(s)");
}
} catch (IOException e) { } catch (IOException e) {
LOGGER.debug("GET " + path + " failed: " + e + ' ' + e.getMessage()); LOGGER.debug("GET " + path + " failed: " + e + ' ' + e.getMessage());
disableGalFind = true; disableGalFind = true;
@ -301,9 +304,6 @@ public class DavExchangeSession extends ExchangeSession {
query.append('&').append(searchAttribute).append('=').append(URIUtil.encodeWithinQuery(searchValue)); query.append('&').append(searchAttribute).append('=').append(URIUtil.encodeWithinQuery(searchValue));
} }
Map<String, Map<String, String>> results = galFind(query.toString()); Map<String, Map<String, String>> results = galFind(query.toString());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(query.toString() + ": " + results.size() + " result(s)");
}
for (Map<String, String> result : results.values()) { for (Map<String, String> result : results.values()) {
Contact contact = new Contact(); Contact contact = new Contact();
contact.setName(result.get("AN")); contact.setName(result.get("AN"));

View File

@ -52,6 +52,7 @@ public class LdapConnection extends AbstractConnection {
*/ */
static final String OD_BASE_CONTEXT = "o=od"; static final String OD_BASE_CONTEXT = "o=od";
static final String OD_USER_CONTEXT = "cn=users, o=od"; static final String OD_USER_CONTEXT = "cn=users, o=od";
static final String OD_CONFIG_CONTEXT = "cn=config, o=od";
static final String COMPUTER_CONTEXT = "cn=computers, o=od"; static final String COMPUTER_CONTEXT = "cn=computers, o=od";
/** /**
@ -330,7 +331,7 @@ public class LdapConnection extends AbstractConnection {
/** /**
* Search threads map * Search threads map
*/ */
protected final HashMap<Integer, SearchThread> searchThreadMap = new HashMap<Integer, SearchThread>(); protected final HashMap<Integer, SearchRunnable> searchThreadMap = new HashMap<Integer, SearchRunnable>();
/** /**
* Initialize the streams and start the thread. * Initialize the streams and start the thread.
@ -503,21 +504,28 @@ public class LdapConnection extends AbstractConnection {
reqBer.parseBoolean(); reqBer.parseBoolean();
LdapFilter ldapFilter = parseFilter(reqBer); LdapFilter ldapFilter = parseFilter(reqBer);
Set<String> returningAttributes = parseReturningAttributes(reqBer); Set<String> returningAttributes = parseReturningAttributes(reqBer);
SearchRunnable searchRunnable = new SearchRunnable(currentMessageId, dn, scope, sizeLimit, timelimit, ldapFilter, returningAttributes);
if (BASE_CONTEXT.equalsIgnoreCase(dn) || OD_USER_CONTEXT.equalsIgnoreCase(dn)) {
// launch search in a separate thread // launch search in a separate thread
SearchThread searchThread = new SearchThread(getName(), currentMessageId, dn, scope, sizeLimit, timelimit, ldapFilter, returningAttributes);
synchronized (searchThreadMap) { synchronized (searchThreadMap) {
searchThreadMap.put(currentMessageId, searchThread); searchThreadMap.put(currentMessageId, searchRunnable);
} }
Thread searchThread = new Thread(searchRunnable);
searchThread.setName(getName() + "-Search-" + currentMessageId);
searchThread.start(); searchThread.start();
} else {
// no need to create a separate thread, just run
searchRunnable.run();
}
} else if (requestOperation == LDAP_REQ_ABANDON) { } else if (requestOperation == LDAP_REQ_ABANDON) {
int abandonMessageId = 0; int abandonMessageId = 0;
try { try {
abandonMessageId = (Integer) PARSE_INT_WITH_TAG_METHOD.invoke(reqBer, LDAP_REQ_ABANDON); abandonMessageId = (Integer) PARSE_INT_WITH_TAG_METHOD.invoke(reqBer, LDAP_REQ_ABANDON);
synchronized (searchThreadMap) { synchronized (searchThreadMap) {
SearchThread searchThread = searchThreadMap.get(abandonMessageId); SearchRunnable searchRunnable = searchThreadMap.get(abandonMessageId);
if (searchThread != null) { if (searchRunnable != null) {
searchThread.abandon(); searchRunnable.abandon();
searchThreadMap.remove(currentMessageId); searchThreadMap.remove(currentMessageId);
} }
} }
@ -1169,11 +1177,6 @@ public class LdapConnection extends AbstractConnection {
// Should never be called // Should never be called
DavGatewayTray.error(new BundleMessage("LOG_LDAP_UNSUPPORTED_FILTER", "nested simple filters")); DavGatewayTray.error(new BundleMessage("LOG_LDAP_UNSUPPORTED_FILTER", "nested simple filters"));
} }
public String getGalFindAttributeName() {
return CRITERIA_MAP.get(attributeName);
}
} }
/** /**
@ -1231,7 +1234,7 @@ public class LdapConnection extends AbstractConnection {
return contactReturningAttributes; return contactReturningAttributes;
} }
protected class SearchThread extends Thread { protected class SearchRunnable implements Runnable {
private final int currentMessageId; private final int currentMessageId;
private final String dn; private final String dn;
private final int scope; private final int scope;
@ -1241,8 +1244,7 @@ public class LdapConnection extends AbstractConnection {
private final Set<String> returningAttributes; private final Set<String> returningAttributes;
private boolean abandon; private boolean abandon;
protected SearchThread(String threadName, int currentMessageId, String dn, int scope, int sizeLimit, int timelimit, LdapFilter ldapFilter, Set<String> returningAttributes) { protected SearchRunnable(int currentMessageId, String dn, int scope, int sizeLimit, int timelimit, LdapFilter ldapFilter, Set<String> returningAttributes) {
super(threadName + "-Search-" + currentMessageId);
this.currentMessageId = currentMessageId; this.currentMessageId = currentMessageId;
this.dn = dn; this.dn = dn;
this.scope = scope; this.scope = scope;
@ -1259,7 +1261,6 @@ public class LdapConnection extends AbstractConnection {
abandon = true; abandon = true;
} }
@Override
public void run() { public void run() {
try { try {
int size = 0; int size = 0;
@ -1290,7 +1291,7 @@ public class LdapConnection extends AbstractConnection {
// then in GAL // then in GAL
if (persons == null || persons.isEmpty()) { if (persons == null || persons.isEmpty()) {
persons = session.galFind(session.isEqualTo("uid", uid), persons = session.galFind(session.isEqualTo("imapUid", uid),
convertLdapToContactReturningAttributes(returningAttributes), sizeLimit); convertLdapToContactReturningAttributes(returningAttributes), sizeLimit);
ExchangeSession.Contact person = persons.get(uid.toLowerCase()); ExchangeSession.Contact person = persons.get(uid.toLowerCase());
@ -1328,7 +1329,7 @@ public class LdapConnection extends AbstractConnection {
// full search // full search
for (char c = 'A'; c <= 'Z'; c++) { for (char c = 'A'; c <= 'Z'; c++) {
if (!abandon && persons.size() < sizeLimit) { if (!abandon && persons.size() < sizeLimit) {
for (ExchangeSession.Contact person : session.galFind(session.startsWith("uid", String.valueOf(c)), for (ExchangeSession.Contact person : session.galFind(session.startsWith("imapUid", String.valueOf(c)),
convertLdapToContactReturningAttributes(returningAttributes), sizeLimit).values()) { convertLdapToContactReturningAttributes(returningAttributes), sizeLimit).values()) {
persons.put(person.get("uid"), person); persons.put(person.get("uid"), person);
if (persons.size() == sizeLimit) { if (persons.size() == sizeLimit) {
@ -1373,7 +1374,7 @@ public class LdapConnection extends AbstractConnection {
} else { } else {
DavGatewayTray.debug(new BundleMessage("LOG_LDAP_REQ_SEARCH_ANONYMOUS_ACCESS_FORBIDDEN", currentMessageId, dn)); DavGatewayTray.debug(new BundleMessage("LOG_LDAP_REQ_SEARCH_ANONYMOUS_ACCESS_FORBIDDEN", currentMessageId, dn));
} }
} else if (dn != null && dn.length() > 0) { } else if (dn != null && dn.length() > 0 && !OD_CONFIG_CONTEXT.equals(dn)) {
DavGatewayTray.debug(new BundleMessage("LOG_LDAP_REQ_SEARCH_INVALID_DN", currentMessageId, dn)); DavGatewayTray.debug(new BundleMessage("LOG_LDAP_REQ_SEARCH_INVALID_DN", currentMessageId, dn));
} }