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.portal.cms.cache;
17  
18  import java.io.IOException;
19  
20  import nl.hippo.client.api.caching.CachedResponse;
21  import nl.hippo.client.api.caching.CachedResponseImpl;
22  import nl.hippo.client.api.event.EventValidity;
23  import nl.hippo.client.api.service.CachingService;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /***
29   * @author <a href="mailto:d.dam@hippo.nl">Dennis Dam</a>
30   *
31   * @version $Id: ObjectCacheImpl.java 6523 2007-05-22 13:20:55Z ddam $
32   */
33  public class ObjectCacheImpl implements ObjectCache {
34  
35      private static final Log log = LogFactory.getLog(ObjectCacheImpl.class);
36      
37      private CachingService service;
38      private CacheKeyFactory keyFactory;
39      private EventValidityFactory eventValidityFactory;
40      
41      public ObjectCacheImpl(CachingService service){
42          this.service = service;
43          this.keyFactory = new PortalCacheKeyFactoryImpl();
44          this.eventValidityFactory = new EventValidityFactoryImpl();
45      }
46      
47      public Object get(PortalCacheKey cacheKey) {
48          Object o = service.get(cacheKey);
49          if (o instanceof CachedResponse){
50              o = ((CachedResponse) o).getResponse();
51          }
52          return o;
53      }
54  
55      public void store(PortalCacheKey cacheKey, Object value, EventValidity validity) {
56          store(cacheKey, value, new EventValidity[]{validity});
57      }
58      
59      public void store(PortalCacheKey cacheKey, Object value, EventValidity[] validities) {
60          if (value == null) {
61              throw new IllegalArgumentException("object may not be null");
62          }
63          try {
64              CachedResponse cr = new CachedResponseImpl(validities, value);
65              service.store(cacheKey, cr);
66          } catch (IOException e) {
67              log.error("Exception while storing object: ", e);
68          }
69      }
70      
71      public CacheKeyFactory getCacheKeyFactory(){
72          return this.keyFactory;
73      }
74  
75      public EventValidityFactory getEventValidityFactory(){
76          return this.eventValidityFactory;
77      }
78  
79  }