View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client.cache;
28  
29  import java.util.concurrent.ExecutionException;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.http.HttpHost;
34  import org.apache.http.HttpResponse;
35  import org.apache.http.ProtocolException;
36  import org.apache.http.client.cache.HttpCacheContext;
37  import org.apache.http.client.cache.HttpCacheEntry;
38  import org.apache.http.client.methods.HttpRequestWrapper;
39  import org.apache.http.concurrent.BasicFuture;
40  import org.apache.http.concurrent.FutureCallback;
41  
42  /**
43   * Class used to represent an asynchronous revalidation event, such as with
44   * "stale-while-revalidate"
45   */
46  class AsynchronousAsyncValidationRequest implements Runnable {
47      private final AsynchronousAsyncValidator parent;
48      private final CachingHttpAsyncClient cachingAsyncClient;
49      private final HttpHost target;
50      private final HttpRequestWrapper request;
51      private final HttpCacheContext clientContext;
52      private final HttpCacheEntry cacheEntry;
53      private final String identifier;
54  
55      private final Log log = LogFactory.getLog(getClass());
56  
57      /**
58       * Used internally by {@link AsynchronousValidator} to schedule a
59       * revalidation.
60       */
61      AsynchronousAsyncValidationRequest(final AsynchronousAsyncValidator parent,
62              final CachingHttpAsyncClient cachingClient, final HttpHost target, final HttpRequestWrapper request,
63              final HttpCacheContext clientContext, final HttpCacheEntry cacheEntry, final String identifier) {
64          this.parent = parent;
65          this.cachingAsyncClient = cachingClient;
66          this.target = target;
67          this.request = request;
68          this.clientContext = clientContext;
69          this.cacheEntry = cacheEntry;
70          this.identifier = identifier;
71      }
72  
73      public void run() {
74          try {
75              final FutureCallback<HttpResponse> callback = new FutureCallback<HttpResponse>() {
76  
77                  public void cancelled() {
78                  }
79  
80                  public void completed(final HttpResponse httpResponse) {
81                  }
82  
83                  public void failed(final Exception e) {
84                      log.debug("Asynchronous revalidation failed", e);
85                  }
86              };
87              final BasicFuture<HttpResponse> future = new BasicFuture<HttpResponse>(callback);
88              this.cachingAsyncClient.revalidateCacheEntry(future, this.target, this.request,
89                      this.clientContext, this.cacheEntry);
90              future.get();
91          } catch (final ProtocolException pe) {
92              this.log.error("ProtocolException thrown during asynchronous revalidation", pe);
93          } catch (ExecutionException e) {
94              this.log.error("Exception thrown during asynchronous revalidation", e.getCause());
95          } catch (InterruptedException e) {
96              Thread.currentThread().interrupt();
97          } finally {
98              this.parent.markComplete(this.identifier);
99          }
100     }
101 
102     String getIdentifier() {
103         return this.identifier;
104     }
105 
106 }