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