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      @Override
74      public void run() {
75          try {
76              final FutureCallback<HttpResponse> callback = new FutureCallback<HttpResponse>() {
77  
78                  @Override
79                  public void cancelled() {
80                  }
81  
82                  @Override
83                  public void completed(final HttpResponse httpResponse) {
84                  }
85  
86                  @Override
87                  public void failed(final Exception e) {
88                      log.debug("Asynchronous revalidation failed", e);
89                  }
90              };
91              final BasicFuture<HttpResponse> future = new BasicFuture<HttpResponse>(callback);
92              this.cachingAsyncClient.revalidateCacheEntry(future, this.target, this.request,
93                      this.clientContext, this.cacheEntry);
94              future.get();
95          } catch (final ProtocolException pe) {
96              this.log.error("ProtocolException thrown during asynchronous revalidation", pe);
97          } catch (final ExecutionException e) {
98              this.log.error("Exception thrown during asynchronous revalidation", e.getCause());
99          } catch (final InterruptedException e) {
100             Thread.currentThread().interrupt();
101         } finally {
102             this.parent.markComplete(this.identifier);
103         }
104     }
105 
106     String getIdentifier() {
107         return this.identifier;
108     }
109 
110 }