1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
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 | |
|
35 | |
|
36 | |
|
37 | |
|
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 | |
} |