View Javadoc

1   /*
2    * Copyright 2007 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  package nl.hippo.client.caching;
17  
18  import java.io.IOException;
19  import java.io.Serializable;
20  import java.util.Iterator;
21  
22  import nl.hippo.client.api.caching.Cache;
23  import nl.hippo.client.api.caching.CachedResponse;
24  import nl.hippo.client.api.caching.EventAwareCache;
25  import nl.hippo.client.api.caching.SourceValidity;
26  import nl.hippo.client.api.event.EventValidity;
27  import nl.hippo.client.api.event.NamedEvent;
28  
29  public class EventCache implements EventAwareCache {
30  
31      private EventCacheConfig config;
32      private EventRegistry eventRegistry;
33  
34      public EventCache(EventCacheConfig config) {
35          this.config = config;
36          this.eventRegistry = new EventRegistry();
37      }
38  
39      /**
40       * Implementation of nl.hippo.client.event.api.EventAware
41       */
42      public void processEvent(NamedEvent e) {
43          if (e != null) {
44              Serializable[] keys = eventRegistry.keysForEvent(e);
45              if (keys == null)
46                  return;
47              for (int i = 0; i < keys.length; i++) {
48                  if (keys[i] != null) {
49                      /*
50                       * every cachekey associated with this event needs to be removed --
51                       * regardless of event mapping. event map will be cleaned by jvm
52                       * through WeakReferences
53                       */
54                      getDelegate().remove(keys[i]);
55                  }
56              }
57          }
58      }
59  
60      public Object get(Object key) {
61          Object obj = getDelegate().get(key);
62          if (obj != null && obj instanceof CachedResponse) {
63              Object cachedObj = ((CachedResponse) obj).getResponse();
64              return cachedObj;
65          }
66          return obj;
67      }
68  
69      public void store(Object key, Object value) throws IOException {
70          if (value instanceof CachedResponse) {
71              CachedResponse cr = (CachedResponse) value;
72              SourceValidity[] srcvals = cr.getValidityObjects();
73              for (int i = 0; i < srcvals.length; i++) {
74                  if (srcvals[i] instanceof EventValidity) {
75                      EventValidity validity = (EventValidity) srcvals[i];
76                      NamedEvent event = validity.getEvent();
77                      eventRegistry.register(event, key);
78                  }
79              }
80          }
81          getDelegate().store(key, value);
82      }
83  
84      public void remove(Object key) {
85          getDelegate().remove(key);
86      }
87  
88      public void clear() {
89          getDelegate().clear();
90      }
91  
92      public boolean containsKey(Object key) {
93          return getDelegate().containsKey(key);
94      }
95  
96      public Iterator memoryKeys() {
97          return getDelegate().memoryKeys();
98      }
99  
100     public int memorySize() {
101         return getDelegate().memorySize();
102     }
103 
104     public String getStatisticsOverview() {
105         return getDelegate().getStatisticsOverview();
106     }
107 
108     private Cache getDelegate() {
109         return config.getDelegate();
110     }
111 
112 }