View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * 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 org.kuali.rice.core.impl.cache;
17  
18  import org.kuali.rice.core.api.cache.CacheService;
19  import org.kuali.rice.core.api.cache.CacheTarget;
20  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
21  import org.springframework.beans.factory.InitializingBean;
22  import org.springframework.cache.Cache;
23  import org.springframework.cache.CacheManager;
24  
25  import java.util.Collection;
26  
27  public class CacheServiceImpl implements CacheService, InitializingBean {
28  
29      private CacheManager cacheManager;
30  
31      @Override
32      public void flush(Collection<CacheTarget> cacheTargets) throws RiceIllegalArgumentException {
33          if (cacheTargets == null) {
34              throw new RiceIllegalArgumentException("cacheTargets is null");
35          }
36          for (CacheTarget cacheTarget : cacheTargets) {
37              if (cacheTarget == null) {
38                  throw new RiceIllegalArgumentException("cacheTarget is null");
39              }
40              final Cache c = getCache(cacheTarget.getCache());
41              if (c != null) {
42                  if (cacheTarget.containsKey()) {
43                      c.evict(cacheTarget.getKey());
44                  } else {
45                      c.clear();
46                  }
47              }
48          }
49      }
50  
51      private Cache getCache(String cache) {
52          return cacheManager.getCache(cache);
53      }
54  
55      public void setCacheManager(CacheManager cacheManager) {
56          this.cacheManager = cacheManager;
57      }
58  
59      @Override
60      public void afterPropertiesSet() throws Exception {
61          if (cacheManager == null) {
62              throw new IllegalStateException("the cacheManager must be set");
63          }
64      }
65  }