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