View Javadoc

1   /***
2    * 
3    * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.logicblaze.lingo;
19  
20  import org.aopalliance.intercept.MethodInvocation;
21  import org.logicblaze.lingo.util.LRUCache;
22  
23  import java.lang.reflect.Method;
24  import java.util.Map;
25  
26  /***
27   * Provides caching of metadata for performance.
28   * 
29   * @version $Revision$
30   */
31  public class CachingMetadataStrategy implements MetadataStrategy {
32  
33      private static final long serialVersionUID = -6008790663804471523L;
34      
35      private MetadataStrategy proxy;
36      private Map cache;
37      private int cacheSize = 5000;
38  
39      public CachingMetadataStrategy(MetadataStrategy proxy) {
40          this.proxy = proxy;
41      }
42  
43      public CachingMetadataStrategy(MetadataStrategy proxy, Map cache) {
44          this.proxy = proxy;
45          this.cache = cache;
46      }
47  
48      public MethodMetadata getMethodMetadata(Method method) {
49          MethodMetadata answer = (MethodMetadata) getCache().get(method);
50          if (answer == null) {
51              answer = proxy.getMethodMetadata(method);
52              getCache().put(method, answer);
53          }
54          return answer;
55      }
56  
57      public ResultJoinStrategy getResultJoinStrategy(MethodInvocation methodInvocation, MethodMetadata metadata) {
58          return proxy.getResultJoinStrategy(methodInvocation, metadata);
59      }
60  
61      // Properties
62      // -------------------------------------------------------------------------
63      public Map getCache() {
64          if (cache == null) {
65              cache = createCache();
66          }
67          return cache;
68      }
69  
70      public void setCache(Map cache) {
71          this.cache = cache;
72      }
73  
74      public int getCacheSize() {
75          return cacheSize;
76      }
77  
78      public void setCacheSize(int cacheSize) {
79          this.cacheSize = cacheSize;
80      }
81  
82      // Implementation methods
83      // -------------------------------------------------------------------------
84      protected Map createCache() {
85          return new LRUCache(getCacheSize());
86      }
87  }