View Javadoc

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.HashMap;
24  import java.util.Hashtable;
25  import java.util.Map;
26  import java.util.Iterator;
27  
28  import org.apache.commons.collections.map.LRUMap;
29  import org.apache.velocity.runtime.RuntimeConstants;
30  import org.apache.velocity.runtime.RuntimeServices;
31  import org.apache.velocity.util.MapFactory;
32  
33  /**
34   * Default implementation of the resource cache for the default
35   * ResourceManager.  The cache uses a <i>least recently used</i> (LRU)
36   * algorithm, with a maximum size specified via the
37   * <code>resource.manager.cache.size</code> property (idenfied by the
38   * {@link
39   * org.apache.velocity.runtime.RuntimeConstants#RESOURCE_MANAGER_DEFAULTCACHE_SIZE}
40   * constant).  This property get be set to <code>0</code> or less for
41   * a greedy, unbounded cache (the behavior from pre-v1.5).
42   *
43   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
44   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
45   * @version $Id: ResourceCacheImpl.java 685385 2008-08-12 23:59:06Z nbubna $
46   */
47  public class ResourceCacheImpl implements ResourceCache
48  {
49      /**
50       * Cache storage, assumed to be thread-safe.
51       */
52      protected Map cache = MapFactory.create(512, 0.5f, 30, false);
53  
54      /**
55       * Runtime services, generally initialized by the
56       * <code>initialize()</code> method.
57       */
58      protected RuntimeServices rsvc = null;
59  
60      /**
61       * @see org.apache.velocity.runtime.resource.ResourceCache#initialize(org.apache.velocity.runtime.RuntimeServices)
62       */
63      public void initialize( RuntimeServices rs )
64      {
65          rsvc = rs;
66  
67          int maxSize =
68              rsvc.getInt(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE, 89);
69          if (maxSize > 0)
70          {
71              // Create a whole new Map here to avoid hanging on to a
72              // handle to the unsynch'd LRUMap for our lifetime.
73              Map lruCache = Collections.synchronizedMap(new LRUMap(maxSize));
74              lruCache.putAll(cache);
75              cache = lruCache;
76          }
77          rsvc.getLog().debug("ResourceCache: initialized ("+this.getClass()+") with "+
78                 cache.getClass()+" cache map.");
79      }
80  
81      /**
82       * @see org.apache.velocity.runtime.resource.ResourceCache#get(java.lang.Object)
83       */
84      public Resource get( Object key )
85      {
86          return (Resource) cache.get( key );
87      }
88  
89      /**
90       * @see org.apache.velocity.runtime.resource.ResourceCache#put(java.lang.Object, org.apache.velocity.runtime.resource.Resource)
91       */
92      public Resource put( Object key, Resource value )
93      {
94          return (Resource) cache.put( key, value );
95      }
96  
97      /**
98       * @see org.apache.velocity.runtime.resource.ResourceCache#remove(java.lang.Object)
99       */
100     public Resource remove( Object key )
101     {
102         return (Resource) cache.remove( key );
103     }
104 
105     /**
106      * @see org.apache.velocity.runtime.resource.ResourceCache#enumerateKeys()
107      */
108     public Iterator enumerateKeys()
109     {
110         return cache.keySet().iterator();
111     }
112 }
113