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