View Javadoc
1   /**
2    * Copyright 2005-2014 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.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.log4j.Logger;
21  import org.kuali.rice.core.api.cache.CacheAdminService;
22  import org.kuali.rice.core.api.cache.CacheTarget;
23  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
24  import org.springframework.beans.factory.InitializingBean;
25  import org.springframework.cache.Cache;
26  import org.springframework.cache.CacheManager;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.List;
31  
32  /**
33   * Implementation of the cache administration service which handles requests to flush cache targets from local caches
34   * managed by the injected cache manager.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class CacheAdminServiceImpl implements CacheAdminService, InitializingBean {
39  
40      private static final Logger LOG = Logger.getLogger(CacheAdminServiceImpl.class);
41  
42      private CacheManager cacheManager;
43  
44      @Override
45      public void flush(Collection<CacheTarget> cacheTargets) throws RiceIllegalArgumentException {
46          if (CollectionUtils.isNotEmpty(cacheTargets)) {
47              logCacheFlush(cacheTargets);
48              for (CacheTarget cacheTarget : cacheTargets) {
49                  if (cacheTarget == null) {
50                      throw new RiceIllegalArgumentException("cacheTarget is null");
51                  }
52                  final Cache c = getCache(cacheTarget.getCache());
53                  if (c != null) {
54                      if (cacheTarget.containsKey()) {
55                          c.evict(cacheTarget.getKey());
56                      } else {
57                          c.clear();
58                      }
59                  }
60              }
61          }
62      }
63  
64      protected void logCacheFlush(Collection<CacheTarget> cacheTargets) {
65          if (LOG.isDebugEnabled()) {
66              List<String> cacheTargetLog = new ArrayList<String>(cacheTargets.size());
67              for (CacheTarget cacheTarget : cacheTargets) {
68                  cacheTargetLog.add(cacheTarget.toString());
69              }
70              LOG.debug("Performing local flush of cache targets [" + StringUtils.join(cacheTargetLog, ", ") + "]");
71          }
72      }
73  
74      private Cache getCache(String cache) {
75          return cacheManager.getCache(cache);
76      }
77  
78      public void setCacheManager(CacheManager cacheManager) {
79          this.cacheManager = cacheManager;
80      }
81  
82      @Override
83      public void afterPropertiesSet() throws Exception {
84          if (cacheManager == null) {
85              throw new IllegalStateException("the cacheManager must be set");
86          }
87      }
88  }