/* ------------------------------------------------------------ */
  private void shrinkCache() {
    // While we need to shrink
    while (_cache.size() > 0
        && (_cachedFiles.get() > _maxCachedFiles || _cachedSize.get() > _maxCacheSize)) {
      // Scan the entire cache and generate an ordered list by last accessed time.
      SortedSet<CachedHttpContent> sorted =
          new TreeSet<CachedHttpContent>(
              new Comparator<CachedHttpContent>() {
                public int compare(CachedHttpContent c1, CachedHttpContent c2) {
                  if (c1._lastAccessed < c2._lastAccessed) return -1;

                  if (c1._lastAccessed > c2._lastAccessed) return 1;

                  if (c1._contentLengthValue < c2._contentLengthValue) return -1;

                  return c1._key.compareTo(c2._key);
                }
              });
      for (CachedHttpContent content : _cache.values()) sorted.add(content);

      // Invalidate least recently used first
      for (CachedHttpContent content : sorted) {
        if (_cachedFiles.get() <= _maxCachedFiles && _cachedSize.get() <= _maxCacheSize) break;
        if (content == _cache.remove(content.getKey())) content.invalidate();
      }
    }
  }
 /* ------------------------------------------------------------ */
 public void flushCache() {
   if (_cache != null) {
     while (_cache.size() > 0) {
       for (String path : _cache.keySet()) {
         CachedHttpContent content = _cache.remove(path);
         if (content != null) content.invalidate();
       }
     }
   }
 }
    CachedGzipHttpContent(CachedHttpContent content, CachedHttpContent contentGz) {
      super(content, contentGz);
      _content = content;
      _contentGz = contentGz;

      _etag =
          (ResourceCache.this._etags)
              ? new PreEncodedHttpField(
                  HttpHeader.ETAG, _content.getResource().getWeakETag("--gzip"))
              : null;
    }
 public boolean isValid() {
   return _contentGz.isValid()
       && _content.isValid()
       && _content.getResource().lastModified() <= _contentGz.getResource().lastModified();
 }
  /* ------------------------------------------------------------ */
  private HttpContent load(String pathInContext, Resource resource, int maxBufferSize)
      throws IOException {
    if (resource == null || !resource.exists()) return null;

    if (resource.isDirectory())
      return new ResourceHttpContent(
          resource, _mimeTypes.getMimeByExtension(resource.toString()), getMaxCachedFileSize());

    // Will it fit in the cache?
    if (isCacheable(resource)) {
      CachedHttpContent content = null;

      // Look for a gzip resource
      if (_gzip) {
        String pathInContextGz = pathInContext + ".gz";
        CachedHttpContent contentGz = _cache.get(pathInContextGz);
        if (contentGz == null || !contentGz.isValid()) {
          contentGz = null;
          Resource resourceGz = _factory.getResource(pathInContextGz);
          if (resourceGz.exists()
              && resourceGz.lastModified() >= resource.lastModified()
              && resourceGz.length() < resource.length()) {
            contentGz = new CachedHttpContent(pathInContextGz, resourceGz, null);
            CachedHttpContent added = _cache.putIfAbsent(pathInContextGz, contentGz);
            if (added != null) {
              contentGz.invalidate();
              contentGz = added;
            }
          }
        }
        content = new CachedHttpContent(pathInContext, resource, contentGz);
      } else content = new CachedHttpContent(pathInContext, resource, null);

      // Add it to the cache.
      CachedHttpContent added = _cache.putIfAbsent(pathInContext, content);
      if (added != null) {
        content.invalidate();
        content = added;
      }

      return content;
    }

    // Look for non Cacheable gzip resource or content
    String mt = _mimeTypes.getMimeByExtension(pathInContext);
    if (_gzip) {
      // Is the gzip content cached?
      String pathInContextGz = pathInContext + ".gz";
      CachedHttpContent contentGz = _cache.get(pathInContextGz);
      if (contentGz != null
          && contentGz.isValid()
          && contentGz.getResource().lastModified() >= resource.lastModified())
        return new ResourceHttpContent(resource, mt, maxBufferSize, contentGz);

      // Is there a gzip resource?
      Resource resourceGz = _factory.getResource(pathInContextGz);
      if (resourceGz.exists()
          && resourceGz.lastModified() >= resource.lastModified()
          && resourceGz.length() < resource.length())
        return new ResourceHttpContent(
            resource,
            mt,
            maxBufferSize,
            new ResourceHttpContent(
                resourceGz, _mimeTypes.getMimeByExtension(pathInContextGz), maxBufferSize));
    }

    return new ResourceHttpContent(resource, mt, maxBufferSize);
  }