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