View Javadoc

1   /**
2    * Copyright 2005-2012 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.resourceloader;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.config.property.ConfigContext;
20  import org.kuali.rice.core.api.reflect.ObjectDefinition;
21  import org.kuali.rice.core.api.resourceloader.ServiceLocator;
22  import org.kuali.rice.core.api.util.ClassLoaderUtils;
23  import org.kuali.rice.core.api.util.RiceConstants;
24  import org.kuali.rice.core.framework.resourceloader.BaseResourceLoader;
25  import org.kuali.rice.core.api.util.ContextClassLoaderProxy;
26  
27  import javax.xml.namespace.QName;
28  import java.util.HashSet;
29  import java.util.Set;
30  import java.util.concurrent.ConcurrentHashMap;
31  
32  /**
33   * A BaseResourceLoader implementation which wraps services with a Proxy that
34   * switches the current context ClassLoader of the Thread.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class BaseWrappingResourceLoader extends BaseResourceLoader {
39  
40  	private static final String[] PACKAGES_TO_FILTER = new String[] { "org.springframework" };
41  	private Set<QName> servicesToCache = new HashSet<QName>();
42  	private final ConcurrentHashMap<QName, Object> serviceCache = new ConcurrentHashMap<QName, Object>();
43  
44  	public BaseWrappingResourceLoader(QName name, ClassLoader classLoader, ServiceLocator serviceLocator) {
45  		super(name, classLoader, serviceLocator);
46  	}
47  
48  	public BaseWrappingResourceLoader(QName name, ClassLoader classLoader) {
49  		super(name, classLoader);
50  	}
51  
52  	public BaseWrappingResourceLoader(QName name, ServiceLocator serviceLocator) {
53  		super(name, serviceLocator);
54  	}
55  
56  	public BaseWrappingResourceLoader(QName name) {
57  		super(name);
58  	}
59  
60  
61  
62  	@Override
63  	public void start() throws Exception {
64  	    String servicesToCacheFromConfig = ConfigContext.getCurrentContextConfig().getProperty(RiceConstants.SERVICES_TO_CACHE);
65  	    if (!StringUtils.isEmpty(servicesToCacheFromConfig)) {
66  		String[] services = servicesToCacheFromConfig.split(",");
67  		for (String serviceName : services) {
68  		    serviceName = serviceName.trim();
69  		    try {
70  			servicesToCache.add(QName.valueOf(serviceName));
71  			LOG.info("Adding service " + serviceName + " to service cache.");
72  		    } catch (IllegalArgumentException e) {
73  			LOG.error("Failed to parse serviceName into QName from property " + RiceConstants.SERVICES_TO_CACHE +".  Service name given was: " + serviceName);
74  		    }
75  		}
76  	    }
77  	    super.start();
78  	}
79  
80  	@Override
81  	public Object getService(QName serviceName) {
82  	    Object service = serviceCache.get(serviceName);
83  	    if (service != null) {
84  		if (LOG.isDebugEnabled()) {
85  		    LOG.debug("Service with QName " + serviceName + " was retrieved from the service cache.");
86  		}
87  		return service;
88  	    }
89  	    return super.getService(serviceName);
90  	}
91  
92  	protected Object postProcessService(QName serviceName, Object service) {
93  		if (service != null && shouldWrapService(serviceName, service)) {
94  			service = ContextClassLoaderProxy.wrap(service, ClassLoaderUtils.getInterfacesToProxy(service, getClassLoader(), getPackageNamesToFilter()), getClassLoader());
95  		}
96  		cacheService(serviceName, service);
97  		return service;
98  	}
99  
100 	protected Object postProcessObject(ObjectDefinition definition, Object object) {
101 		if (object != null && shouldWrapObject(definition, object)) {
102 			return ContextClassLoaderProxy.wrap(object, ClassLoaderUtils.getInterfacesToProxy(object, getClassLoader(), getPackageNamesToFilter()), getClassLoader(), getClassLoader());
103 		}
104 		return object;
105 	}
106 
107 	protected void cacheService(QName serviceName, Object service) {
108 	    if (shouldCacheService(serviceName, service)) {
109 		LOG.debug("Adding service " + serviceName + " to the service cache.");
110 		    serviceCache.put(serviceName, service);
111 	    }
112 	}
113 
114 	protected String[] getPackageNamesToFilter() {
115 		return PACKAGES_TO_FILTER;
116 	}
117 
118 	protected boolean shouldWrapService(QName serviceName, Object service) {
119 		return true;
120 	}
121 
122 	protected boolean shouldCacheService(QName serviceName, Object service) {
123 	    return servicesToCache.contains(serviceName) && service != null;
124 	}
125 
126 	protected boolean shouldWrapObject(ObjectDefinition definition, Object object) {
127 		return true;
128 	}
129 
130 }