Coverage Report - org.kuali.rice.core.resourceloader.BaseWrappingResourceLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseWrappingResourceLoader
0%
0/44
0%
0/18
1.923
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.core.resourceloader;
 18  
 
 19  
 import java.util.Collections;
 20  
 import java.util.HashMap;
 21  
 import java.util.HashSet;
 22  
 import java.util.Map;
 23  
 import java.util.Set;
 24  
 
 25  
 import javax.xml.namespace.QName;
 26  
 
 27  
 import org.apache.commons.lang.StringUtils;
 28  
 import org.kuali.rice.core.config.ConfigContext;
 29  
 import org.kuali.rice.core.reflect.ObjectDefinition;
 30  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 31  
 import org.kuali.rice.core.util.RiceConstants;
 32  
 
 33  
 /**
 34  
  * A BaseResourceLoader implementation which wraps services with a Proxy that
 35  
  * switches the current context ClassLoader of the Thread.
 36  
  *
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  */
 39  
 public class BaseWrappingResourceLoader extends BaseResourceLoader {
 40  
 
 41  0
         private static final String[] PACKAGES_TO_FILTER = new String[] { "org.springframework" };
 42  0
         private Set<QName> servicesToCache = new HashSet<QName>();
 43  0
         private Map<QName, Object> serviceCache = Collections.synchronizedMap(new HashMap<QName, Object>());
 44  
 
 45  
         public BaseWrappingResourceLoader(QName name, ClassLoader classLoader, ServiceLocator serviceLocator) {
 46  0
                 super(name, classLoader, serviceLocator);
 47  0
         }
 48  
 
 49  
         public BaseWrappingResourceLoader(QName name, ClassLoader classLoader) {
 50  0
                 super(name, classLoader);
 51  0
         }
 52  
 
 53  
         public BaseWrappingResourceLoader(QName name, ServiceLocator serviceLocator) {
 54  0
                 super(name, serviceLocator);
 55  0
         }
 56  
 
 57  
         public BaseWrappingResourceLoader(QName name) {
 58  0
                 super(name);
 59  0
         }
 60  
 
 61  
 
 62  
 
 63  
         @Override
 64  
         public void start() throws Exception {
 65  0
             String servicesToCacheFromConfig = ConfigContext.getCurrentContextConfig().getProperty(RiceConstants.SERVICES_TO_CACHE);
 66  0
             if (!StringUtils.isEmpty(servicesToCacheFromConfig)) {
 67  0
                 String[] services = servicesToCacheFromConfig.split(",");
 68  0
                 for (String serviceName : services) {
 69  0
                     serviceName = serviceName.trim();
 70  
                     try {
 71  0
                         servicesToCache.add(QName.valueOf(serviceName));
 72  0
                         LOG.info("Adding service " + serviceName + " to service cache.");
 73  0
                     } catch (IllegalArgumentException e) {
 74  0
                         LOG.error("Failed to parse serviceName into QName from property " + RiceConstants.SERVICES_TO_CACHE +".  Service name given was: " + serviceName);
 75  0
                     }
 76  
                 }
 77  
             }
 78  0
             super.start();
 79  0
         }
 80  
 
 81  
         @Override
 82  
         public Object getService(QName serviceName) {
 83  0
             Object service = serviceCache.get(serviceName);
 84  0
             if (service != null) {
 85  0
                 if (LOG.isDebugEnabled()) {
 86  0
                     LOG.debug("Service with QName " + serviceName + " was retrieved from the service cache.");
 87  
                 }
 88  0
                 return service;
 89  
             }
 90  0
             return super.getService(serviceName);
 91  
         }
 92  
 
 93  
         protected Object postProcessService(QName serviceName, Object service) {
 94  0
                 if (service != null && shouldWrapService(serviceName, service)) {
 95  0
                         service = ContextClassLoaderProxy.wrap(service, ClassLoaderUtils.getInterfacesToProxy(service, getClassLoader(), getPackageNamesToFilter()), getClassLoader());
 96  
                 }
 97  0
                 cacheService(serviceName, service);
 98  0
                 return service;
 99  
         }
 100  
 
 101  
         protected Object postProcessObject(ObjectDefinition definition, Object object) {
 102  0
                 if (object != null && shouldWrapObject(definition, object)) {
 103  0
                         return ContextClassLoaderProxy.wrap(object, ClassLoaderUtils.getInterfacesToProxy(object, getClassLoader(), getPackageNamesToFilter()), getClassLoader(), getClassLoader());
 104  
                 }
 105  0
                 return object;
 106  
         }
 107  
 
 108  
         protected void cacheService(QName serviceName, Object service) {
 109  0
             if (shouldCacheService(serviceName, service)) {
 110  0
                 LOG.debug("Adding service " + serviceName + " to the service cache.");
 111  0
                 serviceCache.put(serviceName, service);
 112  
             }
 113  0
         }
 114  
 
 115  
         protected String[] getPackageNamesToFilter() {
 116  0
                 return PACKAGES_TO_FILTER;
 117  
         }
 118  
 
 119  
         protected boolean shouldWrapService(QName serviceName, Object service) {
 120  0
                 return true;
 121  
         }
 122  
 
 123  
         protected boolean shouldCacheService(QName serviceName, Object service) {
 124  0
             return servicesToCache.contains(serviceName);
 125  
         }
 126  
 
 127  
         protected boolean shouldWrapObject(ObjectDefinition definition, Object object) {
 128  0
                 return true;
 129  
         }
 130  
 
 131  
 }