View Javadoc

1   /**
2    * Copyright 2005-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  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  	/**
45       * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
46       */
47      public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
48          this.beanFactory = beanFactory;
49      }
50  
51      /**
52       * Sets the Trigger for the message processing job
53       * @param messageProcessingTrigger the Trigger for the message processing job
54       */
55      @Required
56      public void setMessageProcessingTrigger(Trigger messageProcessingTrigger) {
57          this.messageProcessingTrigger = messageProcessingTrigger;
58      }
59      
60      /**
61       * Sets the JobDetail for the message processing job
62       * @param messageProcessingJobDetail the JobDetail for the message processing job
63       */
64      @Required
65      public void setMessageProcessingJobDetail(JobDetail messageProcessingJobDetail) {
66          this.messageProcessingJobDetail = messageProcessingJobDetail;
67      }
68  
69      /**
70       * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
71       */
72      public void afterPropertiesSet() throws Exception {
73          GlobalKCBServiceLocator.init(beanFactory);
74          // kill the reference, our job is done
75          beanFactory = null;
76  
77          Scheduler scheduler = getScheduler()==null?KSBServiceLocator.getScheduler():getScheduler();
78          scheduler.addJob(messageProcessingJobDetail, true);
79  
80          addTriggerToScheduler(messageProcessingTrigger);
81      }
82      
83      private void addTriggerToScheduler(Trigger trigger) throws SchedulerException {
84  		boolean triggerExists = (getScheduler().getTrigger(trigger.getName(), trigger.getGroup()) != null);
85  		if (!triggerExists) {
86  			try {
87  				getScheduler().scheduleJob(trigger);
88  			} catch (ObjectAlreadyExistsException ex) {
89  				getScheduler().rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
90  			}
91  		} else {
92  		    getScheduler().rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
93  		}
94  	}
95  
96      public void destroy() throws Exception {
97          // prevent anything from accessing our services after the module has been destroyed/shutdown
98          // our module's lifecycle is tied to the Spring context lifecycle for now
99          GlobalKCBServiceLocator.destroy();
100     }
101 
102 	/**
103 	 * @return the scheduler
104 	 */
105 	public Scheduler getScheduler() {
106 		return this.scheduler;
107 	}
108 
109 	/**
110 	 * @param scheduler the scheduler to set
111 	 */
112 	public void setScheduler(Scheduler scheduler) {
113 		this.scheduler = scheduler;
114 	}
115     
116 }