Use async reload() in CacheLoader

CacheLoaders need asynchronous refresh()s for refreshAfterWrite() to
work effectively. The Cache will call CacheLoader.refresh() when
appropriate during a get() due to refreshAfterWrite(), but if the
refresh() blocks, then that is effectively as if the element expired.
With an asynchronous reload(), the reloading starts on that get() but
users of the Cache see the old value until refresh() completes.
diff --git a/src/com/google/enterprise/adaptor/sharepoint/AsyncCacheLoader.java b/src/com/google/enterprise/adaptor/sharepoint/AsyncCacheLoader.java
new file mode 100644
index 0000000..fa8df00
--- /dev/null
+++ b/src/com/google/enterprise/adaptor/sharepoint/AsyncCacheLoader.java
@@ -0,0 +1,44 @@
+// Copyright 2013 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.enterprise.adaptor.sharepoint;
+
+import com.google.common.cache.CacheLoader;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
+
+import java.util.concurrent.Executor;
+
+/**
+ * An abstract CacheLoader whose reload() is asynchronous (unlike the default).
+ */
+abstract class AsyncCacheLoader<K, V> extends CacheLoader<K, V> {
+  protected abstract Executor executor();
+
+  @Override
+  public ListenableFuture<V> reload(final K key, V oldValue) {
+    final SettableFuture<V> future = SettableFuture.create();
+    executor().execute(new Runnable() {
+      @Override
+      public void run() {
+        try {
+          future.set(load(key));
+        } catch (Throwable t) {
+          future.setException(t);
+        }
+      }
+    });
+    return future;
+  }
+}
diff --git a/src/com/google/enterprise/adaptor/sharepoint/SharePointAdaptor.java b/src/com/google/enterprise/adaptor/sharepoint/SharePointAdaptor.java
index 9153e0b..2b4c6c6 100644
--- a/src/com/google/enterprise/adaptor/sharepoint/SharePointAdaptor.java
+++ b/src/com/google/enterprise/adaptor/sharepoint/SharePointAdaptor.java
@@ -16,7 +16,6 @@
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
 import com.google.enterprise.adaptor.AbstractAdaptor;
 import com.google.enterprise.adaptor.Acl;
@@ -2208,7 +2207,12 @@
   }
 
   private class MemberIdsCacheLoader
-      extends CacheLoader<String, MemberIdMapping> {
+      extends AsyncCacheLoader<String, MemberIdMapping> {
+    @Override
+    protected Executor executor() {
+      return executor;
+    }
+
     @Override
     public MemberIdMapping load(String site) throws IOException {
       return getSiteDataClient(site, site).retrieveMemberIdMapping();
@@ -2216,7 +2220,12 @@
   }
 
   private class SiteUserCacheLoader
-      extends CacheLoader<String, MemberIdMapping> {
+      extends AsyncCacheLoader<String, MemberIdMapping> {
+    @Override
+    protected Executor executor() {
+      return executor;
+    }
+
     @Override
     public MemberIdMapping load(String site) throws IOException {
       return getSiteDataClient(site, site).retrieveSiteUserMapping();