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