View Javadoc

1   /**
2    * Copyright 2005-2013 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  package org.kuali.rice.kcb.config;
17  
18  import org.kuali.rice.kcb.service.GlobalKCBServiceLocator;
19  import org.kuali.rice.ksb.service.KSBServiceLocator;
20  import org.quartz.JobDetail;
21  import org.quartz.ObjectAlreadyExistsException;
22  import org.quartz.Scheduler;
23  import org.quartz.SchedulerException;
24  import org.quartz.Trigger;
25  import org.springframework.beans.BeansException;
26  import org.springframework.beans.factory.BeanFactory;
27  import org.springframework.beans.factory.BeanFactoryAware;
28  import org.springframework.beans.factory.DisposableBean;
29  import org.springframework.beans.factory.InitializingBean;
30  import org.springframework.beans.factory.annotation.Required;
31  
32  /**
33   * Initializing bean that initializes KCB (specifically the GlobalKCBServiceLocator)
34   * on Spring context initialization.  This bean should be eagerly initialized (not marked lazy) 
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class KCBInitializer implements BeanFactoryAware, InitializingBean, DisposableBean {
39      private BeanFactory beanFactory;
40      private Trigger messageProcessingTrigger;
41      private JobDetail messageProcessingJobDetail;
42      protected Scheduler scheduler;
43  
44      @Override
45      public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
46          this.beanFactory = beanFactory;
47      }
48  
49      /**
50       * Sets the Trigger for the message processing job
51       * @param messageProcessingTrigger the Trigger for the message processing job
52       */
53      @Required
54      public void setMessageProcessingTrigger(Trigger messageProcessingTrigger) {
55          this.messageProcessingTrigger = messageProcessingTrigger;
56      }
57      
58      /**
59       * Sets the JobDetail for the message processing job
60       * @param messageProcessingJobDetail the JobDetail for the message processing job
61       */
62      @Required
63      public void setMessageProcessingJobDetail(JobDetail messageProcessingJobDetail) {
64          this.messageProcessingJobDetail = messageProcessingJobDetail;
65      }
66  
67  
68      @Override
69      public void afterPropertiesSet() throws Exception {
70          GlobalKCBServiceLocator.init(beanFactory);
71          // kill the reference, our job is done
72          beanFactory = null;
73  
74          Scheduler scheduler = getScheduler()==null?KSBServiceLocator.getScheduler():getScheduler();
75          scheduler.addJob(messageProcessingJobDetail, true);
76  
77          addTriggerToScheduler(messageProcessingTrigger);
78      }
79      
80      private void addTriggerToScheduler(Trigger trigger) throws SchedulerException {
81  		boolean triggerExists = (getScheduler().getTrigger(trigger.getName(), trigger.getGroup()) != null);
82  		if (!triggerExists) {
83  			try {
84  				getScheduler().scheduleJob(trigger);
85  			} catch (ObjectAlreadyExistsException ex) {
86  				getScheduler().rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
87  			}
88  		} else {
89  		    getScheduler().rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
90  		}
91  	}
92  
93      @Override
94      public void destroy() throws Exception {
95          // prevent anything from accessing our services after the module has been destroyed/shutdown
96          // our module's lifecycle is tied to the Spring context lifecycle for now
97          GlobalKCBServiceLocator.destroy();
98      }
99  
100 	/**
101 	 * @return the scheduler
102 	 */
103 	public Scheduler getScheduler() {
104 		return this.scheduler;
105 	}
106 
107 	/**
108 	 * @param scheduler the scheduler to set
109 	 */
110 	public void setScheduler(Scheduler scheduler) {
111 		this.scheduler = scheduler;
112 	}
113     
114 }