View Javadoc

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  	private static final String[] PACKAGES_TO_FILTER = new String[] { "org.springframework" };
42  	private Set<QName> servicesToCache = new HashSet<QName>();
43  	private Map<QName, Object> serviceCache = Collections.synchronizedMap(new HashMap<QName, Object>());
44  
45  	public BaseWrappingResourceLoader(QName name, ClassLoader classLoader, ServiceLocator serviceLocator) {
46  		super(name, classLoader, serviceLocator);
47  	}
48  
49  	public BaseWrappingResourceLoader(QName name, ClassLoader classLoader) {
50  		super(name, classLoader);
51  	}
52  
53  	public BaseWrappingResourceLoader(QName name, ServiceLocator serviceLocator) {
54  		super(name, serviceLocator);
55  	}
56  
57  	public BaseWrappingResourceLoader(QName name) {
58  		super(name);
59  	}
60  
61  
62  
63  	@Override
64  	public void start() throws Exception {
65  	    String servicesToCacheFromConfig = ConfigContext.getCurrentContextConfig().getProperty(RiceConstants.SERVICES_TO_CACHE);
66  	    if (!StringUtils.isEmpty(servicesToCacheFromConfig)) {
67  		String[] services = servicesToCacheFromConfig.split(",");
68  		for (String serviceName : services) {
69  		    serviceName = serviceName.trim();
70  		    try {
71  			servicesToCache.add(QName.valueOf(serviceName));
72  			LOG.info("Adding service " + serviceName + " to service cache.");
73  		    } catch (IllegalArgumentException e) {
74  			LOG.error("Failed to parse serviceName into QName from property " + RiceConstants.SERVICES_TO_CACHE +".  Service name given was: " + serviceName);
75  		    }
76  		}
77  	    }
78  	    super.start();
79  	}
80  
81  	@Override
82  	public Object getService(QName serviceName) {
83  	    Object service = serviceCache.get(serviceName);
84  	    if (service != null) {
85  		if (LOG.isDebugEnabled()) {
86  		    LOG.debug("Service with QName " + serviceName + " was retrieved from the service cache.");
87  		}
88  		return service;
89  	    }
90  	    return super.getService(serviceName);
91  	}
92  
93  	protected Object postProcessService(QName serviceName, Object service) {
94  		if (service != null && shouldWrapService(serviceName, service)) {
95  			service = ContextClassLoaderProxy.wrap(service, ClassLoaderUtils.getInterfacesToProxy(service, getClassLoader(), getPackageNamesToFilter()), getClassLoader());
96  		}
97  		cacheService(serviceName, service);
98  		return service;
99  	}
100 
101 	protected Object postProcessObject(ObjectDefinition definition, Object object) {
102 		if (object != null && shouldWrapObject(definition, object)) {
103 			return ContextClassLoaderProxy.wrap(object, ClassLoaderUtils.getInterfacesToProxy(object, getClassLoader(), getPackageNamesToFilter()), getClassLoader(), getClassLoader());
104 		}
105 		return object;
106 	}
107 
108 	protected void cacheService(QName serviceName, Object service) {
109 	    if (shouldCacheService(serviceName, service)) {
110 		LOG.debug("Adding service " + serviceName + " to the service cache.");
111 		serviceCache.put(serviceName, service);
112 	    }
113 	}
114 
115 	protected String[] getPackageNamesToFilter() {
116 		return PACKAGES_TO_FILTER;
117 	}
118 
119 	protected boolean shouldWrapService(QName serviceName, Object service) {
120 		return true;
121 	}
122 
123 	protected boolean shouldCacheService(QName serviceName, Object service) {
124 	    return servicesToCache.contains(serviceName);
125 	}
126 
127 	protected boolean shouldWrapObject(ObjectDefinition definition, Object object) {
128 		return true;
129 	}
130 
131 }