001    // Copyright 2011 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    // http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.internal.services.assets;
016    
017    import org.apache.tapestry5.internal.TapestryInternalUtils;
018    import org.apache.tapestry5.ioc.Resource;
019    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
020    import org.apache.tapestry5.services.InvalidationListener;
021    import org.apache.tapestry5.services.assets.ResourceDependencies;
022    import org.apache.tapestry5.services.assets.StreamableResource;
023    import org.apache.tapestry5.services.assets.StreamableResourceProcessing;
024    import org.apache.tapestry5.services.assets.StreamableResourceSource;
025    
026    import java.io.IOException;
027    import java.lang.ref.SoftReference;
028    import java.util.Map;
029    
030    /**
031     * An interceptor for the {@link StreamableResourceSource} service that handles caching of content.
032     */
033    public class SRSCachingInterceptor implements StreamableResourceSource, InvalidationListener
034    {
035        private final StreamableResourceSource delegate;
036    
037        private final Map<Resource, SoftReference<StreamableResource>> cache = CollectionFactory.newConcurrentMap();
038    
039        public SRSCachingInterceptor(StreamableResourceSource delegate)
040        {
041            this.delegate = delegate;
042        }
043    
044        public StreamableResource getStreamableResource(Resource baseResource, StreamableResourceProcessing processing, ResourceDependencies dependencies)
045                throws IOException
046        {
047            if (!enableCache(processing))
048            {
049                return delegate.getStreamableResource(baseResource, processing, dependencies);
050            }
051    
052            StreamableResource result = TapestryInternalUtils.getAndDeref(cache, baseResource);
053    
054            if (result == null)
055            {
056                result = delegate.getStreamableResource(baseResource, processing, dependencies);
057    
058                if (isCacheable(result))
059                {
060                    dependencies.addDependency(baseResource);
061    
062                    cache.put(baseResource, new SoftReference<StreamableResource>(result));
063                }
064            }
065    
066            return result;
067        }
068    
069        /**
070         * Always returns true; a subclass may extend this to only cache the resource in some circumstances.
071         *
072         * @param resource
073         * @return true to cache the resource
074         */
075        protected boolean isCacheable(StreamableResource resource)
076        {
077            return true;
078        }
079    
080        public void objectWasInvalidated()
081        {
082            cache.clear();
083        }
084    
085        /**
086         * Returns true unless the processing is {@link StreamableResourceProcessing#FOR_AGGREGATION}.
087         * Subclasses may override. When the cache is not enabled, the request is passed on to the interceptor's
088         * {@link #delegate}, and no attempt is made to read or update this interceptor's cache.
089         *
090         * @since 5.3.5
091         */
092        protected boolean enableCache(StreamableResourceProcessing processing)
093        {
094            return processing != StreamableResourceProcessing.FOR_AGGREGATION;
095        }
096    }