Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * ====================================================================
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you 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.
  * ====================================================================
  *
  * This software consists of voluntary contributions made by many
  * individuals on behalf of the Apache Software Foundation.  For more
  * information on the Apache Software Foundation, please see
  * <http://www.apache.org/>.
  *
  */
 package org.apache.http.impl.client.cache;
 
 import java.util.Set;
 

org.apache.http.client.cache.HttpCacheStorage implementation capable of deallocating resources associated with the cache entries.

This cache keeps track of cache entries using java.lang.ref.PhantomReference and maintains a collection of all resources that are no longer in use. The cache, however, does not automatically deallocates associated resources by invoking org.apache.http.client.cache.Resource.dispose() method. The consumer MUST periodically call cleanResources() method to trigger resource deallocation. The cache can be permanently shut down using shutdown() method. All resources associated with the entries used by the cache will be deallocated.

This org.apache.http.client.cache.HttpCacheStorage implementation is intended for use with FileResource and similar.

Compatibility note. Prior to version 4.4 this storage implementation used to dispose of all resource entries upon close(). As of version 4.4 the close() method disposes only of those resources that have been explicitly removed from the cache with removeEntry(java.lang.String) method.

The shutdown() ()} method can still be used to shut down the storage and dispose of all resources currently managed by it.

Since:
4.1
 
 public class ManagedHttpCacheStorage implements HttpCacheStorageCloseable {
 
     private final CacheMap entries;
     private final ReferenceQueue<HttpCacheEntrymorque;
     private final Set<ResourceReferenceresources;
     private final AtomicBoolean active;
 
     public ManagedHttpCacheStorage(final CacheConfig config) {
         super();
         this. = new CacheMap(config.getMaxCacheEntries());
         this. = new ReferenceQueue<HttpCacheEntry>();
         this. = new HashSet<ResourceReference>();
         this. = new AtomicBoolean(true);
     }
 
     private void ensureValidState() throws IllegalStateException {
         if (!this..get()) {
             throw new IllegalStateException("Cache has been shut down");
         }
     }
 
     private void keepResourceReference(final HttpCacheEntry entry) {
         final Resource resource = entry.getResource();
         if (resource != null) {
             // Must deallocate the resource when the entry is no longer in used
             final ResourceReference ref = new ResourceReference(entrythis.);
            this..add(ref);
        }
    }
    @Override
    public void putEntry(final String urlfinal HttpCacheEntry entrythrows IOException {
        Args.notNull(url"URL");
        Args.notNull(entry"Cache entry");
        ensureValidState();
        synchronized (this) {
            this..put(urlentry);
            keepResourceReference(entry);
        }
    }
    @Override
    public HttpCacheEntry getEntry(final String urlthrows IOException {
        Args.notNull(url"URL");
        ensureValidState();
        synchronized (this) {
            return this..get(url);
        }
    }
    @Override
    public void removeEntry(final String urlthrows IOException {
        Args.notNull(url"URL");
        ensureValidState();
        synchronized (this) {
            // Cannot deallocate the associated resources immediately as the
            // cache entry may still be in use
            this..remove(url);
        }
    }
    @Override
    public void updateEntry(
            final String url,
            final HttpCacheUpdateCallback callbackthrows IOException {
        Args.notNull(url"URL");
        Args.notNull(callback"Callback");
        ensureValidState();
        synchronized (this) {
            final HttpCacheEntry existing = this..get(url);
            final HttpCacheEntry updated = callback.update(existing);
            this..put(urlupdated);
            if (existing != updated) {
                keepResourceReference(updated);
            }
        }
    }
    public void cleanResources() {
        if (this..get()) {
            ResourceReference ref;
            while ((ref = (ResourceReferencethis..poll()) != null) {
                synchronized (this) {
                    this..remove(ref);
                }
                ref.getResource().dispose();
            }
        }
    }
    public void shutdown() {
        if (this..compareAndSet(truefalse)) {
            synchronized (this) {
                this..clear();
                for (final ResourceReference refthis.) {
                    ref.getResource().dispose();
                }
                this..clear();
                while (this..poll() != null) {
                }
            }
        }
    }
    @Override
    public void close() {
        if (this..compareAndSet(truefalse)) {
            synchronized (this) {
                ResourceReference ref;
                while ((ref = (ResourceReferencethis..poll()) != null) {
                    this..remove(ref);
                    ref.getResource().dispose();
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X