Coverage Report - org.kuali.rice.ksb.messaging.config.KSBConfigurer
 
Classes in this File Line Coverage Branch Coverage Complexity
KSBConfigurer
0%
0/133
0%
0/34
1.692
KSBConfigurer$1
0%
0/5
0%
0/2
1.692
KSBConfigurer$ServicePublisher
0%
0/8
0%
0/4
1.692
 
 1  
 /*
 2  
  * Copyright 2006-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.kuali.rice.ksb.messaging.config;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Arrays;
 21  
 import java.util.Collection;
 22  
 import java.util.Collections;
 23  
 import java.util.LinkedList;
 24  
 import java.util.List;
 25  
 
 26  
 import javax.sql.DataSource;
 27  
 
 28  
 import org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory;
 29  
 import org.apache.commons.httpclient.protocol.Protocol;
 30  
 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
 31  
 import org.kuali.rice.core.api.config.CoreConfigHelper;
 32  
 import org.kuali.rice.core.api.config.module.RunMode;
 33  
 import org.kuali.rice.core.api.config.property.Config;
 34  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 35  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 36  
 import org.kuali.rice.core.api.lifecycle.BaseLifecycle;
 37  
 import org.kuali.rice.core.api.lifecycle.Lifecycle;
 38  
 import org.kuali.rice.core.api.resourceloader.ResourceLoader;
 39  
 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
 40  
 import org.kuali.rice.core.impl.config.module.ModuleConfigurer;
 41  
 import org.kuali.rice.core.impl.lifecycle.ServiceDelegatingLifecycle;
 42  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 43  
 import org.kuali.rice.core.util.RiceConstants;
 44  
 import org.kuali.rice.ksb.api.KsbApiConstants;
 45  
 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
 46  
 import org.kuali.rice.ksb.api.bus.ServiceDefinition;
 47  
 import org.kuali.rice.ksb.messaging.AlternateEndpoint;
 48  
 import org.kuali.rice.ksb.messaging.AlternateEndpointLocation;
 49  
 import org.kuali.rice.ksb.messaging.MessageFetcher;
 50  
 import org.kuali.rice.ksb.messaging.resourceloader.KSBResourceLoaderFactory;
 51  
 import org.kuali.rice.ksb.messaging.serviceconnectors.HttpInvokerConnector;
 52  
 import org.kuali.rice.ksb.service.KSBServiceLocator;
 53  
 import org.kuali.rice.ksb.util.KSBConstants;
 54  
 import org.quartz.Scheduler;
 55  
 import org.springframework.transaction.PlatformTransactionManager;
 56  
 
 57  
 
 58  
 /**
 59  
  * Used to configure the embedded workflow. This could be used to configure
 60  
  * embedded workflow programmatically but mostly this is a base class by which
 61  
  * to hang specific configuration behavior off of through subclassing
 62  
  * 
 63  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 64  
  * 
 65  
  */
 66  0
 public class KSBConfigurer extends ModuleConfigurer {
 67  
         
 68  
         private static final String SERVICE_BUS_CLIENT_SPRING = "classpath:org/kuali/rice/ksb/config/KsbServiceBusClientSpringBeans.xml";
 69  
         private static final String MESSAGE_CLIENT_SPRING = "classpath:org/kuali/rice/ksb/config/KsbMessageClientSpringBeans.xml";
 70  
         private static final String OJB_MESSAGE_CLIENT_SPRING = "classpath:org/kuali/rice/ksb/config/KsbOjbMessageClientSpringBeans.xml";
 71  
         private static final String BAM_SPRING = "classpath:org/kuali/rice/ksb/config/KsbBamSpringBeans.xml";
 72  
         private static final String OJB_BAM_SPRING = "classpath:org/kuali/rice/ksb/config/KsbOjbBamSpringBeans.xml";
 73  
         private static final String MODULE_SPRING = "classpath:org/kuali/rice/ksb/config/KsbModuleConfigurationSpringBeans.xml";
 74  
         private static final String REGISTRY_SERVER_SPRING = "classpath:org/kuali/rice/ksb/config/KsbRegistryServerSpringBeans.xml";
 75  
         private static final String OJB_REGISTRY_SPRING = "classpath:org/kuali/rice/ksb/config/KsbOjbRegistrySpringBeans.xml";
 76  
         private static final String WEB_SPRING = "classpath:org/kuali/rice/ksb/config/KsbWebSpringBeans.xml";
 77  
 
 78  0
         private List<ServiceDefinition> services = new ArrayList<ServiceDefinition>();
 79  
         
 80  0
     private List<AlternateEndpointLocation> alternateEndpointLocations = new ArrayList<AlternateEndpointLocation>();
 81  
 
 82  0
         private List<AlternateEndpoint> alternateEndpoints = new ArrayList<AlternateEndpoint>();
 83  
 
 84  
         private DataSource registryDataSource;
 85  
 
 86  
         private DataSource messageDataSource;
 87  
         
 88  
         private DataSource nonTransactionalMessageDataSource;
 89  
         
 90  
         private DataSource bamDataSource;
 91  
 
 92  
         private Scheduler exceptionMessagingScheduler;
 93  
 
 94  
         private PlatformTransactionManager platformTransactionManager;
 95  
         
 96  
         private List<Lifecycle> internalLifecycles;
 97  
         
 98  
         public KSBConfigurer() {
 99  0
                 super(KsbApiConstants.KSB_MODULE_NAME);
 100  0
                 setValidRunModes(Arrays.asList(RunMode.REMOTE, RunMode.LOCAL));
 101  0
                 this.internalLifecycles = new ArrayList<Lifecycle>();
 102  0
         }
 103  
         
 104  
         @Override
 105  
         public void addAdditonalToConfig() {
 106  0
                 configureDataSource();
 107  0
                 configureScheduler();
 108  0
                 configurePlatformTransactionManager();
 109  0
                 configureAlternateEndpoints();
 110  0
         }
 111  
 
 112  
         @Override
 113  
         public List<String> getPrimarySpringFiles(){
 114  0
                 final List<String> springFileLocations = new ArrayList<String>();
 115  
                                 
 116  0
                 boolean isJpa = OrmUtils.isJpaEnabled("rice.ksb");
 117  0
                 if (isJpa) {
 118  
                         // TODO redo this once we're back to JPA
 119  
                 // springFileLocations.add("classpath:org/kuali/rice/ksb/config/KSBJPASpringBeans.xml");
 120  0
                 throw new UnsupportedOperationException("JPA not currently supported for KSB");
 121  
                 }
 122  
                 
 123  0
                 springFileLocations.add(SERVICE_BUS_CLIENT_SPRING);
 124  
                 
 125  0
                 if (isMessagePersistenceEnabled()) {
 126  0
                         springFileLocations.add(MESSAGE_CLIENT_SPRING);
 127  0
                         springFileLocations.add(OJB_MESSAGE_CLIENT_SPRING);
 128  
                 }
 129  
         
 130  0
         if (isBamEnabled()) {
 131  0
                 springFileLocations.add(BAM_SPRING);
 132  0
                 springFileLocations.add(OJB_BAM_SPRING);
 133  
         }
 134  
         
 135  0
         if (getRunMode().equals( RunMode.LOCAL )) {
 136  
                     // TODO hack 'cause KSB used KNS - this needs to be fixed!!!
 137  0
                     springFileLocations.add("classpath:org/kuali/rice/kns/config/KNSSpringBeans.xml");
 138  0
                 springFileLocations.add(REGISTRY_SERVER_SPRING);
 139  0
                 springFileLocations.add(OJB_REGISTRY_SPRING);
 140  0
                 if (ConfigContext.getCurrentContextConfig().getBooleanProperty(KSBConstants.Config.LOAD_KNS_MODULE_CONFIGURATION, false)) {
 141  0
                     springFileLocations.add(MODULE_SPRING);
 142  0
                     springFileLocations.add(WEB_SPRING);
 143  
             }
 144  
         }
 145  
         
 146  0
         return springFileLocations;
 147  
         }
 148  
         
 149  
         /**
 150  
          * Returns true - KSB UI should always be included.
 151  
          * 
 152  
          * @see org.kuali.rice.core.ConfigContext.getCurrentContextConfig().ModuleConfigurer#shouldRenderWebInterface()
 153  
          */
 154  
         @Override
 155  
         public boolean shouldRenderWebInterface() {
 156  0
                 return true;
 157  
         }
 158  
         
 159  
         @Override
 160  
         public Collection<ResourceLoader> getResourceLoadersToRegister() throws Exception{
 161  0
                 ResourceLoader ksbRemoteResourceLoader = KSBResourceLoaderFactory.createRootKSBRemoteResourceLoader();
 162  0
                 ksbRemoteResourceLoader.start();
 163  0
                 return Collections.singletonList(ksbRemoteResourceLoader);
 164  
         }
 165  
         
 166  
         @Override
 167  
         public List<Lifecycle> loadLifecycles() throws Exception {
 168  0
                 List<Lifecycle> lifecycles = new LinkedList<Lifecycle>();
 169  
                 // this validation of our service list needs to happen after we've
 170  
                 // loaded our configs so it's a lifecycle
 171  0
                 lifecycles.add(new BaseLifecycle() {
 172  
 
 173  
                         @Override
 174  
                         public void start() throws Exception {
 175  
                                 // first check if we want to allow self-signed certificates for SSL communication
 176  0
                                 if (Boolean.valueOf(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.KSB_ALLOW_SELF_SIGNED_SSL)).booleanValue()) {
 177  0
                                     Protocol.registerProtocol("https", new Protocol("https",
 178  
                                             (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(), 443));
 179  
                                 }
 180  0
                                 super.start();
 181  0
                         }
 182  
                 });
 183  0
                 lifecycles.add(new ServiceDelegatingLifecycle(KSBConstants.ServiceNames.BUS_ADMIN_SERVICE));
 184  0
                 return lifecycles;
 185  
         }
 186  
         
 187  
         
 188  
 
 189  
     @Override
 190  
     public void doAdditonalConfigurerValidations() {
 191  0
         for (final ServiceDefinition serviceDef : KSBConfigurer.this.services) {
 192  0
                         serviceDef.validate();
 193  
                 }
 194  0
     }
 195  
 
 196  
         @Override
 197  
         public void doAdditionalContextStartedLogic() {
 198  0
                 ServicePublisher servicePublisher = new ServicePublisher(getServices());
 199  0
                 Lifecycle serviceBus = new ServiceDelegatingLifecycle(KsbApiServiceLocator.SERVICE_BUS);
 200  0
                 Lifecycle threadPool = new ServiceDelegatingLifecycle(KSBConstants.ServiceNames.THREAD_POOL_SERVICE);
 201  0
                 Lifecycle scheduledThreadPool = new ServiceDelegatingLifecycle(KSBConstants.ServiceNames.SCHEDULED_THREAD_POOL_SERVICE);
 202  
                 
 203  
                 try {
 204  0
                         servicePublisher.start();
 205  0
                         internalLifecycles.add(servicePublisher);
 206  0
                         serviceBus.start();
 207  0
                         internalLifecycles.add(serviceBus);
 208  0
                         threadPool.start();
 209  0
                         internalLifecycles.add(threadPool);
 210  0
                         scheduledThreadPool.start();
 211  0
                         internalLifecycles.add(scheduledThreadPool);
 212  0
                 } catch (Exception e) {
 213  0
                         if (e instanceof RuntimeException) {
 214  0
                                 throw (RuntimeException)e;
 215  
                         }
 216  0
                         throw new RiceRuntimeException("Failed to initialize KSB on context startup");
 217  0
                 }
 218  
 
 219  0
                 requeueMessages();
 220  0
         }
 221  
 
 222  
         @Override
 223  
         protected void doAdditionalModuleStopLogic() throws Exception {
 224  0
                 for (int index = internalLifecycles.size() - 1; index >= 0; index--) {
 225  
                         try {
 226  0
                                 internalLifecycles.get(index).stop();
 227  0
                         } catch (Exception e) {
 228  0
                                 LOG.error("Failed to properly execute shutdown logic.", e);
 229  0
                         }
 230  
                 }
 231  0
         }
 232  
 
 233  
         /**
 234  
      * Used to refresh the service registry after the Application Context is initialized.  This way any services that were exported on startup
 235  
      * will be available in the service registry once startup is complete.
 236  
      */
 237  
     private void requeueMessages() {
 238  0
         LOG.info("Refreshing Service Registry to export services to the bus.");
 239  0
         KsbApiServiceLocator.getServiceBus().synchronize();
 240  
         
 241  
                 //automatically requeue documents sitting with status of 'R'
 242  0
                 MessageFetcher messageFetcher = new MessageFetcher((Integer) null);
 243  0
                 KSBServiceLocator.getThreadPool().execute(messageFetcher);
 244  0
     }
 245  
     
 246  
     protected boolean isMessagePersistenceEnabled() {
 247  0
             return ConfigContext.getCurrentContextConfig().getBooleanProperty(KSBConstants.Config.MESSAGE_PERSISTENCE, true);
 248  
     }
 249  
     
 250  
     protected boolean isBamEnabled() {
 251  0
             return ConfigContext.getCurrentContextConfig().getBooleanProperty(Config.BAM_ENABLED, false);
 252  
     }
 253  
 
 254  
         protected void configureScheduler() {
 255  0
                 if (this.getExceptionMessagingScheduler() != null) {
 256  0
                         LOG.info("Configuring injected exception messaging Scheduler");
 257  0
                         ConfigContext.getCurrentContextConfig().putObject(KSBConstants.Config.INJECTED_EXCEPTION_MESSAGE_SCHEDULER_KEY, this.getExceptionMessagingScheduler());
 258  
                 }
 259  0
         }
 260  
 
 261  
         protected void configureDataSource() {
 262  0
                 if (isMessagePersistenceEnabled()) {
 263  0
                         if (getMessageDataSource() != null) {
 264  0
                                 ConfigContext.getCurrentContextConfig().putObject(KSBConstants.Config.KSB_MESSAGE_DATASOURCE, getMessageDataSource());
 265  
                         }
 266  0
                         if (getNonTransactionalMessageDataSource() != null) {
 267  0
                     ConfigContext.getCurrentContextConfig().putObject(KSBConstants.Config.KSB_MESSAGE_NON_TRANSACTIONAL_DATASOURCE, getNonTransactionalMessageDataSource());
 268  
                         }
 269  
                 }
 270  0
         if (getRunMode().equals(RunMode.LOCAL)) {
 271  0
                 if (getRegistryDataSource() != null) {
 272  0
                 ConfigContext.getCurrentContextConfig().putObject(KSBConstants.Config.KSB_REGISTRY_DATASOURCE, getRegistryDataSource());
 273  
             }
 274  
         }
 275  0
         if (isBamEnabled()) {
 276  0
                 if (getBamDataSource() != null) {
 277  0
                         ConfigContext.getCurrentContextConfig().putObject(KSBConstants.Config.KSB_BAM_DATASOURCE, getBamDataSource());
 278  
                 }
 279  
         }
 280  0
     }
 281  
 
 282  
         protected void configurePlatformTransactionManager() {
 283  0
                 if (getPlatformTransactionManager() == null) {
 284  0
                         return;
 285  
                 }
 286  0
                 ConfigContext.getCurrentContextConfig().putObject(RiceConstants.SPRING_TRANSACTION_MANAGER, getPlatformTransactionManager());
 287  0
         }
 288  
         
 289  
         protected void configureAlternateEndpoints() {
 290  0
                 ConfigContext.getCurrentContextConfig().putObject(KSBConstants.Config.KSB_ALTERNATE_ENDPOINT_LOCATIONS, getAlternateEndpointLocations());
 291  0
                 ConfigContext.getCurrentContextConfig().putObject(KSBConstants.Config.KSB_ALTERNATE_ENDPOINTS, getAlternateEndpoints());
 292  0
         }
 293  
         
 294  
         @Override
 295  
         public void doAdditionalContextStoppedLogic() {
 296  
                 try {
 297  0
                         HttpInvokerConnector.shutdownIdleConnectionTimeout();
 298  0
                 } catch (Exception e) {
 299  0
                         LOG.error("Failed to shutdown idle connection timeout evictor thread.", e);
 300  0
                 }
 301  0
             cleanUpConfiguration();
 302  0
         }
 303  
         
 304  
         /**
 305  
      * Because our configuration is global, shutting down Rice does not get rid of objects stored there.  For that reason
 306  
      * we need to manually clean these up.  This is most important in the case of the service bus because the configuration
 307  
      * is used to store services to be exported.  If we don't clean this up then a shutdown/startup within the same
 308  
      * class loading context causes the service list to be doubled and results in "multiple endpoint" error messages.
 309  
      *
 310  
      */
 311  
     protected void cleanUpConfiguration() {
 312  0
         ConfigContext.getCurrentContextConfig().removeObject(KSBConstants.Config.KSB_ALTERNATE_ENDPOINTS);
 313  0
     }
 314  
 
 315  
         public List<ServiceDefinition> getServices() {
 316  0
                 return this.services;
 317  
         }
 318  
 
 319  
         public void setServices(List<ServiceDefinition> javaServices) {
 320  0
                 this.services = javaServices;
 321  0
         }
 322  
 
 323  
         public DataSource getMessageDataSource() {
 324  0
                 return this.messageDataSource;
 325  
         }
 326  
 
 327  
         public void setMessageDataSource(DataSource messageDataSource) {
 328  0
                 this.messageDataSource = messageDataSource;
 329  0
         }
 330  
 
 331  
     public DataSource getNonTransactionalMessageDataSource() {
 332  0
         return this.nonTransactionalMessageDataSource;
 333  
     }
 334  
 
 335  
     public void setNonTransactionalMessageDataSource(DataSource nonTransactionalMessageDataSource) {
 336  0
         this.nonTransactionalMessageDataSource = nonTransactionalMessageDataSource;
 337  0
     }
 338  
 
 339  
     public DataSource getRegistryDataSource() {
 340  0
                 return this.registryDataSource;
 341  
         }
 342  
 
 343  
         public void setRegistryDataSource(DataSource registryDataSource) {
 344  0
                 this.registryDataSource = registryDataSource;
 345  0
         }
 346  
         
 347  
         public DataSource getBamDataSource() {
 348  0
                 return this.bamDataSource;
 349  
         }
 350  
 
 351  
         public void setBamDataSource(DataSource bamDataSource) {
 352  0
                 this.bamDataSource = bamDataSource;
 353  0
         }
 354  
 
 355  
         public Scheduler getExceptionMessagingScheduler() {
 356  0
                 return this.exceptionMessagingScheduler;
 357  
         }
 358  
 
 359  
         public void setExceptionMessagingScheduler(Scheduler exceptionMessagingScheduler) {
 360  0
                 this.exceptionMessagingScheduler = exceptionMessagingScheduler;
 361  0
         }
 362  
 
 363  
         public PlatformTransactionManager getPlatformTransactionManager() {
 364  0
                 return platformTransactionManager;
 365  
         }
 366  
 
 367  
         public void setPlatformTransactionManager(PlatformTransactionManager springTransactionManager) {
 368  0
                 this.platformTransactionManager = springTransactionManager;
 369  0
         }
 370  
 
 371  
     public List<AlternateEndpointLocation> getAlternateEndpointLocations() {
 372  0
             return this.alternateEndpointLocations;
 373  
     }
 374  
 
 375  
     public void setAlternateEndpointLocations(List<AlternateEndpointLocation> alternateEndpointLocations) {
 376  0
             this.alternateEndpointLocations = alternateEndpointLocations;
 377  0
         }
 378  
 
 379  
     public List<AlternateEndpoint> getAlternateEndpoints() {
 380  0
         return this.alternateEndpoints;
 381  
     }
 382  
 
 383  
     public void setAlternateEndpoints(List<AlternateEndpoint> alternateEndpoints) {
 384  0
         this.alternateEndpoints = alternateEndpoints;
 385  0
     }
 386  
     
 387  
     private final class ServicePublisher extends BaseLifecycle {
 388  
 
 389  
             private final List<ServiceDefinition> serviceDefinitions;
 390  
             
 391  0
             ServicePublisher(List<ServiceDefinition> serviceDefinitions) {
 392  0
                     this.serviceDefinitions = serviceDefinitions;
 393  0
             }
 394  
             
 395  
                 @Override
 396  
                 public void start() throws Exception {
 397  0
                         if (serviceDefinitions != null && !serviceDefinitions.isEmpty()) {
 398  0
                                 LOG.debug("Configuring " + serviceDefinitions.size() + " services for application id " + CoreConfigHelper.getApplicationId() + " using config for classloader " + ClassLoaderUtils.getDefaultClassLoader());
 399  0
                                 KsbApiServiceLocator.getServiceBus().publishServices(serviceDefinitions, true);
 400  0
                                 super.start();
 401  
                         }
 402  0
                 }
 403  
             
 404  
     }
 405  
     
 406  
 }