Coverage Report - org.kuali.rice.core.impl.cache.CacheManagerRegistryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
CacheManagerRegistryImpl
0%
0/33
0%
0/16
4.2
 
 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.apache.commons.logging.Log;
 19  
 import org.apache.commons.logging.LogFactory;
 20  
 import org.kuali.rice.core.api.cache.CacheManagerRegistry;
 21  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
 22  
 import org.springframework.beans.factory.NamedBean;
 23  
 import org.springframework.cache.CacheManager;
 24  
 
 25  
 import java.lang.reflect.InvocationTargetException;
 26  
 import java.lang.reflect.Method;
 27  
 import java.util.Collections;
 28  
 import java.util.HashMap;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 import java.util.concurrent.ConcurrentHashMap;
 32  
 import java.util.concurrent.CopyOnWriteArrayList;
 33  
 
 34  
 /**
 35  
  * A simple class that holds a global registry to the cache managers.
 36  
  */
 37  0
 public final class CacheManagerRegistryImpl implements CacheManagerRegistry {
 38  
     private static final String GET_NAME = "getName";
 39  0
     private static final Log LOG = LogFactory.getLog(CacheManagerRegistryImpl.class);
 40  
     private static final String GET_NAME_MSG = "unable to get the getName method on the cache manager";
 41  
 
 42  0
     private static final List<CacheManager> CACHE_MANAGERS = new CopyOnWriteArrayList<CacheManager>();
 43  0
     private static final Map<String, CacheManager> CACHE_MANAGER_MAP = new ConcurrentHashMap<String, CacheManager> ();
 44  
 
 45  
     public void setCacheManager(CacheManager c) {
 46  0
         if (c == null) {
 47  0
             throw new IllegalArgumentException("c is null");
 48  
         }
 49  
 
 50  0
         CACHE_MANAGERS.add(c);
 51  
 
 52  
         //keep map as well
 53  0
         for (String cacheName : c.getCacheNames()) {
 54  0
             CACHE_MANAGER_MAP.put(cacheName, c);
 55  
         }
 56  0
     }
 57  
 
 58  
     @Override
 59  
     public List<CacheManager> getCacheManagers() {
 60  0
         return Collections.unmodifiableList(CACHE_MANAGERS);
 61  
     }
 62  
 
 63  
     @Override
 64  
     public CacheManager getCacheManager(String name) {
 65  0
         for (CacheManager cm : getCacheManagers()) {
 66  0
             if (name.equals(getCacheManagerName(cm))) {
 67  0
                 return cm;
 68  
             }
 69  
         }
 70  0
         return null;
 71  
     }
 72  
 
 73  
     @Override
 74  
     public String getCacheManagerName(CacheManager cm) {
 75  0
         if (cm instanceof NamedBean) {
 76  0
             return ((NamedBean) cm).getBeanName();
 77  
         }
 78  
 
 79  0
         String v = "Unnamed CacheManager " + cm.hashCode();
 80  
         try {
 81  0
             final Method nameMethod = cm.getClass().getMethod(GET_NAME, new Class[] {});
 82  0
             if (nameMethod != null && nameMethod.getReturnType() == String.class) {
 83  0
                 v = (String) nameMethod.invoke(cm, new Object[] {});
 84  
             }
 85  0
         } catch (NoSuchMethodException e) {
 86  0
             LOG.warn(GET_NAME_MSG, e);
 87  0
         } catch (InvocationTargetException e) {
 88  0
             LOG.warn(GET_NAME_MSG, e);
 89  0
         } catch (IllegalAccessException e) {
 90  0
             LOG.warn(GET_NAME_MSG, e);
 91  0
         }
 92  
 
 93  0
         return v;
 94  
     }
 95  
 
 96  
     @Override
 97  
     public CacheManager getCacheManagerByCacheName(String cacheName) {
 98  0
         CacheManager cm = CACHE_MANAGER_MAP.get(cacheName);
 99  0
         if (cm != null) {
 100  0
             return cm;
 101  
         }
 102  0
         throw new RiceIllegalArgumentException("Cache not found : " + cacheName);
 103  
     }
 104  
 }