1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
package org.kuali.rice.kns.service.impl; |
14 | |
|
15 | |
import java.util.ArrayList; |
16 | |
import java.util.HashMap; |
17 | |
import java.util.HashSet; |
18 | |
import java.util.List; |
19 | |
import java.util.Set; |
20 | |
|
21 | |
import javax.xml.namespace.QName; |
22 | |
|
23 | |
import org.kuali.rice.core.api.component.Component; |
24 | |
import org.kuali.rice.core.api.namespace.Namespace; |
25 | |
import org.kuali.rice.core.api.namespace.NamespaceService; |
26 | |
import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; |
27 | |
import org.kuali.rice.core.util.MaxAgeSoftReference; |
28 | |
import org.kuali.rice.kns.datadictionary.AttributeDefinition; |
29 | |
import org.kuali.rice.kns.service.KNSServiceLocatorInternal; |
30 | |
import org.kuali.rice.kns.service.RiceApplicationConfigurationMediationService; |
31 | |
import org.kuali.rice.kns.service.RiceApplicationConfigurationService; |
32 | |
import org.kuali.rice.ksb.api.KsbApiServiceLocator; |
33 | |
import org.kuali.rice.ksb.api.bus.Endpoint; |
34 | |
|
35 | |
|
36 | 0 | public class RiceApplicationConfigurationMediationServiceImpl implements RiceApplicationConfigurationMediationService { |
37 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RiceApplicationConfigurationMediationServiceImpl.class); |
38 | |
|
39 | |
|
40 | 0 | protected int configurationParameterCacheMaxSize = 200; |
41 | 0 | protected int configurationParameterCacheMaxAgeSeconds = 3600; |
42 | 0 | protected int nonDatabaseComponentsCacheMaxSize = 50; |
43 | 0 | protected int nonDatabaseComponentsCacheMaxAgeSeconds = 3600; |
44 | |
|
45 | 0 | protected final HashMap<String,MaxAgeSoftReference<String>> configurationParameterCache = new HashMap<String,MaxAgeSoftReference<String>>( configurationParameterCacheMaxSize ); |
46 | 0 | protected final HashMap<String,MaxAgeSoftReference<List<Component>>> nonDatabaseComponentsCache = new HashMap<String,MaxAgeSoftReference<List<Component>>>( nonDatabaseComponentsCacheMaxSize ); |
47 | 0 | protected final HashMap<String,MaxAgeSoftReference<RiceApplicationConfigurationService>> responsibleServiceByPackageClass = new HashMap<String,MaxAgeSoftReference<RiceApplicationConfigurationService>>( configurationParameterCacheMaxSize ); |
48 | |
|
49 | |
public String getConfigurationParameter( String namespaceCode, String parameterName ){ |
50 | |
|
51 | 0 | String parameterValue = null; |
52 | 0 | if ( namespaceCode != null ) { |
53 | 0 | String parameterKey = (new StringBuffer(namespaceCode).append("|").append(parameterName)).toString(); |
54 | 0 | parameterValue = getParameterValueFromConfigurationParameterCache( parameterKey ); |
55 | 0 | if ( parameterValue != null ) { |
56 | 0 | return parameterValue; |
57 | |
} |
58 | 0 | NamespaceService nsService = KNSServiceLocatorInternal.getNamespaceService(); |
59 | |
final String applicationNamespaceCode; |
60 | 0 | Namespace namespace = nsService.getNamespace(namespaceCode); |
61 | 0 | if (namespace != null) { |
62 | 0 | applicationNamespaceCode = namespace.getApplicationId(); |
63 | |
} else { |
64 | 0 | applicationNamespaceCode = namespaceCode; |
65 | |
} |
66 | 0 | if (applicationNamespaceCode != null) { |
67 | 0 | RiceApplicationConfigurationService rac = findRiceApplicationConfigurationService(applicationNamespaceCode); |
68 | 0 | if (rac != null) { |
69 | 0 | parameterValue = rac.getConfigurationParameter(parameterName); |
70 | |
} |
71 | |
} |
72 | 0 | if (parameterValue != null){ |
73 | 0 | synchronized (configurationParameterCache) { |
74 | 0 | configurationParameterCache.put( parameterKey, new MaxAgeSoftReference<String>( configurationParameterCacheMaxAgeSeconds, parameterValue ) ); |
75 | 0 | } |
76 | |
} |
77 | |
} |
78 | 0 | return parameterValue; |
79 | |
} |
80 | |
|
81 | |
|
82 | |
protected String getParameterValueFromConfigurationParameterCache(String parameterKey) { |
83 | 0 | MaxAgeSoftReference<String> parameterValue = configurationParameterCache.get( parameterKey ); |
84 | 0 | if ( parameterValue != null ) { |
85 | 0 | return parameterValue.get(); |
86 | |
} |
87 | 0 | return null; |
88 | |
} |
89 | |
|
90 | |
protected List<Component> getComponentListFromNonDatabaseComponentsCache(String nonDatabaseServiceNameKey) { |
91 | 0 | MaxAgeSoftReference<List<Component>> nonDatabaseComponent = nonDatabaseComponentsCache.get( nonDatabaseServiceNameKey ); |
92 | 0 | if ( nonDatabaseComponent != null ) { |
93 | 0 | return nonDatabaseComponent.get(); |
94 | |
} |
95 | 0 | return null; |
96 | |
} |
97 | |
|
98 | |
public List<Component> getNonDatabaseComponents() { |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | 0 | Set<QName> serviceNames = findApplicationConfigurationServices(); |
109 | |
|
110 | 0 | List<Component> nonDatabaseComponents = new ArrayList<Component>(); |
111 | |
|
112 | 0 | for ( QName serviceName : serviceNames ) { |
113 | 0 | List<Component> nonDatabaseComponentFromCache = this.getComponentListFromNonDatabaseComponentsCache(serviceName.toString()); |
114 | 0 | if (nonDatabaseComponentFromCache != null) { |
115 | 0 | nonDatabaseComponents.addAll(nonDatabaseComponentFromCache); |
116 | |
} else { |
117 | 0 | RiceApplicationConfigurationService rac = findRiceApplicationConfigurationService(serviceName); |
118 | |
try { |
119 | 0 | if (rac != null) { |
120 | 0 | nonDatabaseComponents.addAll(rac.getNonDatabaseComponents()); |
121 | 0 | synchronized (nonDatabaseComponentsCache) { |
122 | 0 | nonDatabaseComponentsCache.put(serviceName.toString(), new MaxAgeSoftReference<List<Component>>( nonDatabaseComponentsCacheMaxAgeSeconds, rac.getNonDatabaseComponents() )); |
123 | 0 | } |
124 | |
} |
125 | 0 | } catch (Exception e) { |
126 | |
|
127 | 0 | LOG.warn("Invalid RiceApplicationConfigurationService with name: " + serviceName + ". "); |
128 | 0 | } |
129 | |
} |
130 | |
|
131 | 0 | } |
132 | |
|
133 | 0 | return nonDatabaseComponents; |
134 | |
} |
135 | |
|
136 | |
protected Set<QName> findApplicationConfigurationServices() { |
137 | 0 | Set<QName> names = new HashSet<QName>(); |
138 | 0 | List<Endpoint> allEndpoints = KsbApiServiceLocator.getServiceBus().getAllEndpoints(); |
139 | 0 | for (Endpoint endpoint : allEndpoints) { |
140 | 0 | QName serviceName = endpoint.getServiceConfiguration().getServiceName(); |
141 | 0 | if (serviceName.getLocalPart().equals(KNSServiceLocatorInternal.RICE_APPLICATION_CONFIGURATION_SERVICE)) { |
142 | 0 | names.add(serviceName); |
143 | |
} |
144 | 0 | } |
145 | 0 | return names; |
146 | |
} |
147 | |
|
148 | |
protected RiceApplicationConfigurationService findRiceApplicationConfigurationService(QName serviceName) { |
149 | |
try { |
150 | 0 | return (RiceApplicationConfigurationService) GlobalResourceLoader.getService(serviceName); |
151 | 0 | } catch (Exception e) { |
152 | |
|
153 | 0 | LOG.warn("Failed to locate RiceApplicationConfigurationService with name: " + serviceName,e); |
154 | |
} |
155 | 0 | return null; |
156 | |
} |
157 | |
|
158 | |
protected RiceApplicationConfigurationService findRiceApplicationConfigurationService(String namespace) { |
159 | |
try { |
160 | 0 | return (RiceApplicationConfigurationService)GlobalResourceLoader.getService(new QName(namespace, KNSServiceLocatorInternal.RICE_APPLICATION_CONFIGURATION_SERVICE)); |
161 | 0 | } catch (Exception e) { |
162 | |
|
163 | 0 | LOG.warn("Failed to locate RiceApplicationConfigurationService with namespace: " + namespace,e); |
164 | |
} |
165 | 0 | return null; |
166 | |
} |
167 | |
|
168 | |
|
169 | |
public void setConfigurationParameterCacheMaxSize( |
170 | |
int configurationParameterCacheMaxSize) { |
171 | 0 | this.configurationParameterCacheMaxSize = configurationParameterCacheMaxSize; |
172 | 0 | } |
173 | |
|
174 | |
|
175 | |
public void setConfigurationParameterCacheMaxAgeSeconds( |
176 | |
int configurationParameterCacheMaxAgeSeconds) { |
177 | 0 | this.configurationParameterCacheMaxAgeSeconds = configurationParameterCacheMaxAgeSeconds; |
178 | 0 | } |
179 | |
|
180 | |
|
181 | |
public void setNonDatabaseComponentsCacheMaxSize( |
182 | |
int nonDatabaseComponentsCacheMaxSize) { |
183 | 0 | this.nonDatabaseComponentsCacheMaxSize = nonDatabaseComponentsCacheMaxSize; |
184 | 0 | } |
185 | |
|
186 | |
|
187 | |
public void setNonDatabaseComponentsCacheMaxAgeSeconds( |
188 | |
int nonDatabaseComponentsCacheMaxAgeSeconds) { |
189 | 0 | this.nonDatabaseComponentsCacheMaxAgeSeconds = nonDatabaseComponentsCacheMaxAgeSeconds; |
190 | 0 | } |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
protected RiceApplicationConfigurationService findServiceResponsibleForPackageOrClass( String packageOrClassName ) { |
197 | 0 | if ( LOG.isDebugEnabled() ) { |
198 | 0 | LOG.debug( "Checking for app config service responsible for: " + packageOrClassName ); |
199 | |
} |
200 | 0 | RiceApplicationConfigurationService racService = null; |
201 | 0 | MaxAgeSoftReference<RiceApplicationConfigurationService> ref = responsibleServiceByPackageClass.get(packageOrClassName); |
202 | 0 | if ( ref != null ) { |
203 | 0 | racService = ref.get(); |
204 | 0 | if ( racService != null ) { |
205 | 0 | if ( LOG.isDebugEnabled() ) { |
206 | 0 | LOG.debug( "Service found in cache: " + racService.getClass().getName() ); |
207 | |
} |
208 | |
} |
209 | |
} |
210 | 0 | if ( racService == null ) { |
211 | 0 | Set<QName> serviceNames = findApplicationConfigurationServices(); |
212 | 0 | for ( QName serviceName : serviceNames ) { |
213 | 0 | racService = findRiceApplicationConfigurationService(serviceName); |
214 | 0 | if ( racService != null ) { |
215 | |
|
216 | |
try { |
217 | 0 | if ( racService.isResponsibleForPackage(packageOrClassName) ) { |
218 | 0 | if ( LOG.isDebugEnabled() ) { |
219 | 0 | LOG.debug( "Found responsible class on bus with name: " + serviceName ); |
220 | |
} |
221 | 0 | responsibleServiceByPackageClass.put(packageOrClassName, new MaxAgeSoftReference<RiceApplicationConfigurationService>( configurationParameterCacheMaxAgeSeconds, racService) ); |
222 | 0 | break; |
223 | |
} else { |
224 | 0 | racService = null; |
225 | |
} |
226 | 0 | } catch (Exception e) { |
227 | 0 | LOG.warn( "Assuming this racService is not responsible for the package or class. racService: " + |
228 | |
racService.toString() + " ; packageOrClassName: " + packageOrClassName); |
229 | 0 | } |
230 | |
} |
231 | |
} |
232 | |
} |
233 | 0 | if ( racService == null ) { |
234 | 0 | LOG.warn( "Unable to find service which handles package/class: " + packageOrClassName + " -- returning null." ); |
235 | |
} |
236 | 0 | return racService; |
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
public String getBaseInquiryUrl(String businessObjectClassName) { |
243 | 0 | RiceApplicationConfigurationService racService = findServiceResponsibleForPackageOrClass(businessObjectClassName); |
244 | 0 | if ( racService != null ) { |
245 | 0 | return racService.getBaseInquiryUrl(businessObjectClassName); |
246 | |
} |
247 | 0 | return null; |
248 | |
} |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
public String getBaseLookupUrl(String businessObjectClassName) { |
254 | 0 | RiceApplicationConfigurationService racService = findServiceResponsibleForPackageOrClass(businessObjectClassName); |
255 | 0 | if ( racService != null ) { |
256 | 0 | return racService.getBaseLookupUrl(businessObjectClassName); |
257 | |
} |
258 | 0 | return null; |
259 | |
} |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
public String getBaseHelpUrl(String businessObjectClassName) { |
265 | 0 | RiceApplicationConfigurationService racService = findServiceResponsibleForPackageOrClass(businessObjectClassName); |
266 | 0 | if ( racService != null ) { |
267 | 0 | return racService.getBaseHelpUrl(businessObjectClassName); |
268 | |
} |
269 | 0 | return null; |
270 | |
} |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
public AttributeDefinition getBusinessObjectAttributeDefinition(String businessObjectClassName, String attributeName) { |
276 | 0 | if ( LOG.isDebugEnabled() ) { |
277 | 0 | LOG.debug( "Querying for an AttributeDefinition for: " + businessObjectClassName + " / " + attributeName ); |
278 | |
} |
279 | 0 | RiceApplicationConfigurationService racService = findServiceResponsibleForPackageOrClass(businessObjectClassName); |
280 | 0 | if ( racService != null ) { |
281 | 0 | return racService.getBusinessObjectAttributeDefinition(businessObjectClassName, attributeName); |
282 | |
} |
283 | 0 | return null; |
284 | |
} |
285 | |
} |