1 package org.apache.velocity.runtime.resource;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.Map;
25
26 import org.apache.commons.collections.map.LRUMap;
27 import org.apache.velocity.runtime.RuntimeConstants;
28 import org.apache.velocity.runtime.RuntimeServices;
29 import org.apache.velocity.util.MapFactory;
30
31 /**
32 * Default implementation of the resource cache for the default
33 * ResourceManager. The cache uses a <i>least recently used</i> (LRU)
34 * algorithm, with a maximum size specified via the
35 * <code>resource.manager.cache.size</code> property (idenfied by the
36 * {@link
37 * org.apache.velocity.runtime.RuntimeConstants#RESOURCE_MANAGER_DEFAULTCACHE_SIZE}
38 * constant). This property get be set to <code>0</code> or less for
39 * a greedy, unbounded cache (the behavior from pre-v1.5).
40 *
41 * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
42 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
43 * @version $Id: ResourceCacheImpl.java 898032 2010-01-11 19:51:03Z nbubna $
44 */
45 public class ResourceCacheImpl implements ResourceCache
46 {
47 /**
48 * Cache storage, assumed to be thread-safe.
49 */
50 protected Map cache = MapFactory.create(512, 0.5f, 30, false);
51
52 /**
53 * Runtime services, generally initialized by the
54 * <code>initialize()</code> method.
55 */
56 protected RuntimeServices rsvc = null;
57
58 /**
59 * @see org.apache.velocity.runtime.resource.ResourceCache#initialize(org.apache.velocity.runtime.RuntimeServices)
60 */
61 public void initialize( RuntimeServices rs )
62 {
63 rsvc = rs;
64
65 int maxSize =
66 rsvc.getInt(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE, 89);
67 if (maxSize > 0)
68 {
69 // Create a whole new Map here to avoid hanging on to a
70 // handle to the unsynch'd LRUMap for our lifetime.
71 Map lruCache = Collections.synchronizedMap(new LRUMap(maxSize));
72 lruCache.putAll(cache);
73 cache = lruCache;
74 }
75 rsvc.getLog().debug("ResourceCache: initialized ("+this.getClass()+") with "+
76 cache.getClass()+" cache map.");
77 }
78
79 /**
80 * @see org.apache.velocity.runtime.resource.ResourceCache#get(java.lang.Object)
81 */
82 public Resource get( Object key )
83 {
84 return (Resource) cache.get( key );
85 }
86
87 /**
88 * @see org.apache.velocity.runtime.resource.ResourceCache#put(java.lang.Object, org.apache.velocity.runtime.resource.Resource)
89 */
90 public Resource put( Object key, Resource value )
91 {
92 return (Resource) cache.put( key, value );
93 }
94
95 /**
96 * @see org.apache.velocity.runtime.resource.ResourceCache#remove(java.lang.Object)
97 */
98 public Resource remove( Object key )
99 {
100 return (Resource) cache.remove( key );
101 }
102
103 /**
104 * @see org.apache.velocity.runtime.resource.ResourceCache#enumerateKeys()
105 */
106 public Iterator enumerateKeys()
107 {
108 return cache.keySet().iterator();
109 }
110 }
111