Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * 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 io.undertow.server.handlers.resource;
 
 import java.util.Set;
 

Author(s):
Stuart Douglas
 
 public class CachingResourceManager implements ResourceManager {

    
The biggest file size we cache
 
     private final long maxFileSize;

    
The underlying resource manager
 
     private final ResourceManager underlyingResourceManager;

    
A cache of byte buffers
 
     private final DirectBufferCache dataCache;

    
A cache of file metadata, such as if a file exists or not
 
     private final LRUCache<StringObjectcache;
 
     private final int maxAge;
 
     public CachingResourceManager(final int metadataCacheSizefinal long maxFileSizefinal DirectBufferCache dataCachefinal ResourceManager underlyingResourceManagerfinal int maxAge) {
         this. = maxFileSize;
         this. = underlyingResourceManager;
         this. = dataCache;
         this. = new LRUCache<>(metadataCacheSizemaxAge);
         this. = maxAge;
         if(underlyingResourceManager.isResourceChangeListenerSupported()) {
             try {
                 underlyingResourceManager.registerResourceChangeListener(new ResourceChangeListener() {
                     @Override
                     public void handleChanges(Collection<ResourceChangeEventchanges) {
                         for(ResourceChangeEvent change : changes) {
                             invalidate(change.getResource());
                         }
                     }
                 });
             } catch (Exception e) {
                 ..couldNotRegisterChangeListener(e);
             }
         }
     }
 
     @Override
     public CachedResource getResource(final String paththrows IOException {
         Object res = .get(path);
         if (res instanceof NoResourceMarker) {
             NoResourceMarker marker = (NoResourceMarkerres;
             long nextCheck = marker.getNextCheckTime();
             if(nextCheck > 0) {
                 long time = System.currentTimeMillis();
                 if(time > nextCheck) {
                     marker.setNextCheckTime(time + );
                     if(.getResource(path) != null) {
                         .remove(path);
                     } else {
                         return null;
                     }
                 } else {
                     return null;
                 }
             } else {
                 return null;
             }
         } else if (res != null) {
            CachedResource resource = (CachedResourceres;
            if (resource.checkStillValid()) {
                return resource;
            } else {
                invalidate(path);
            }
        }
        final Resource underlying = .getResource(path);
        if (underlying == null) {
            .add(pathnew NoResourceMarker( > 0 ? System.currentTimeMillis() +  : -1));
            return null;
        }
        final CachedResource resource = new CachedResource(thisunderlyingpath);
        .add(pathresource);
        return resource;
    }
    @Override
    public boolean isResourceChangeListenerSupported() {
    }
    @Override
    public void registerResourceChangeListener(ResourceChangeListener listener) {
    }
    @Override
    public void removeResourceChangeListener(ResourceChangeListener listener) {
    }
    public void invalidate(final String path) {
        Object entry = .remove(path);
        if (entry instanceof CachedResource) {
            ((CachedResourceentry).invalidate();
        }
    }
        return ;
    }
    public long getMaxFileSize() {
        return ;
    }
    public int getMaxAge() {
        return ;
    }
    @Override
    public void close() throws IOException {
        try {
            //clear all cached data on close
            if( != null) {
                Set<Objectkeys = .getAllKeys();
                for(final Object key : keys) {
                    if(key instanceof CachedResource.CacheKey) {
                        if(((CachedResource.CacheKeykey). == this) {
                            .remove(key);
                        }
                    }
                }
            }
        } finally {
            .close();
        }
    }
    private static final class NoResourceMarker {
        volatile long nextCheckTime;
        private NoResourceMarker(long nextCheckTime) {
            this. = nextCheckTime;
        }
        public long getNextCheckTime() {
            return ;
        }
        public void setNextCheckTime(long nextCheckTime) {
            this. = nextCheckTime;
        }
    }
New to GrepCode? Check out our FAQ X