001: // Copyright 2006, 2007 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.tapestry.internal.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newConcurrentMap;
018:
019: import java.net.URL;
020: import java.util.Map;
021:
022: import org.apache.tapestry.internal.event.InvalidationEventHubImpl;
023: import org.apache.tapestry.internal.events.UpdateListener;
024: import org.apache.tapestry.internal.util.URLChangeTracker;
025: import org.apache.tapestry.ioc.Resource;
026: import org.apache.tapestry.services.ResourceDigestGenerator;
027:
028: public class ResourceCacheImpl extends InvalidationEventHubImpl
029: implements ResourceCache, UpdateListener {
030: private final URLChangeTracker _tracker;
031:
032: private final ResourceDigestGenerator _digestGenerator;
033:
034: private final Map<Resource, Cached> _cache = newConcurrentMap();
035:
036: final static long MISSING_RESOURCE_TIME_MODIFIED = -1L;
037:
038: private class Cached {
039: final boolean _requiresDigest;
040:
041: final String _digest;
042:
043: final long _timeModified;
044:
045: Cached(Resource resource) {
046: _requiresDigest = _digestGenerator.requiresDigest(resource
047: .getPath());
048:
049: URL url = resource.toURL();
050:
051: // The url may be null when a request for a protected asset arrives, because the
052: // Resource initially is for the file with the digest incorporated into the path, which
053: // means
054: // no underlying file exists. Subsequently, we'll strip out the digest and resolve
055: // to an actual resource.
056:
057: _digest = (_requiresDigest && url != null) ? _digestGenerator
058: .generateDigest(url)
059: : null;
060:
061: _timeModified = url != null ? _tracker.add(url)
062: : MISSING_RESOURCE_TIME_MODIFIED;
063: }
064: }
065:
066: public ResourceCacheImpl(
067: final ResourceDigestGenerator digestGenerator) {
068: _digestGenerator = digestGenerator;
069: _tracker = new URLChangeTracker(true);
070: }
071:
072: public void checkForUpdates() {
073: if (_tracker.containsChanges()) {
074: _cache.clear();
075: _tracker.clear();
076:
077: fireInvalidationEvent();
078: }
079: }
080:
081: private Cached get(Resource resource) {
082: Cached result = _cache.get(resource);
083:
084: if (result == null) {
085: result = new Cached(resource);
086: _cache.put(resource, result);
087: }
088:
089: return result;
090: }
091:
092: public String getDigest(Resource resource) {
093: return get(resource)._digest;
094: }
095:
096: public long getTimeModified(Resource resource) {
097: return get(resource)._timeModified;
098: }
099:
100: public boolean requiresDigest(Resource resource) {
101: return get(resource)._requiresDigest;
102: }
103:
104: }
|