001/*
002 * Copyright 2007 The Kuali Foundation
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.sys.batch;
017
018import java.util.Properties;
019
020import javax.sql.DataSource;
021
022import org.kuali.rice.core.api.config.property.ConfigContext;
023import org.kuali.rice.core.api.util.RiceConstants;
024import org.kuali.rice.ksb.messaging.quartz.MessageServiceExecutorJobListener;
025import org.quartz.Scheduler;
026import org.quartz.SchedulerException;
027import org.quartz.SchedulerFactory;
028
029/**
030 * This class wraps the spring version to allow deploy time determination of whether to actually create a scheduler and whether to
031 * use the jdbc or ram job store.
032 */
033public class SchedulerFactoryBean extends org.springframework.scheduling.quartz.SchedulerFactoryBean {
034    private static final Scheduler SCHEDULER_DUMMY = new SchedulerDummy();
035    private boolean useQuartzScheduling;
036    private boolean useJdbcJobstore;
037    private Properties quartzPropertiesReference;
038
039    @Override
040    public void destroy() throws SchedulerException {
041        if (useQuartzScheduling) {
042            super.destroy();
043        }
044    }
045
046    @Override
047    public void afterPropertiesSet() throws Exception {
048        if (useQuartzScheduling) {
049            if (useJdbcJobstore) {
050                quartzPropertiesReference.put("org.quartz.jobStore.useProperties", "false");
051                quartzPropertiesReference.put("org.quartz.jobStore.isClustered", "true");
052                setDataSource((DataSource) ConfigContext.getCurrentContextConfig().getObject(RiceConstants.DATASOURCE_OBJ));
053                setNonTransactionalDataSource((DataSource) ConfigContext.getCurrentContextConfig().getObject(RiceConstants.NON_TRANSACTIONAL_DATASOURCE_OBJ));
054            }
055            setQuartzProperties(quartzPropertiesReference);
056            super.afterPropertiesSet();
057        }
058    }
059
060    @Override
061    public Scheduler getObject() {
062        if (useQuartzScheduling) {
063            return super.getObject();
064        }
065        return SCHEDULER_DUMMY;
066    }
067
068    /**
069     * @see org.springframework.scheduling.quartz.SchedulerFactoryBean#createScheduler(org.quartz.SchedulerFactory, java.lang.String)
070     */
071    @Override
072    protected Scheduler createScheduler(SchedulerFactory schedulerFactory, String schedulerName) throws SchedulerException {
073        Scheduler scheduler = super.createScheduler(schedulerFactory, schedulerName);
074        scheduler.addJobListener(new MessageServiceExecutorJobListener());
075        return scheduler;
076    }
077    
078    /**
079     * Sets the useJdbcJobstore attribute value.
080     * 
081     * @param useJdbcJobstore The useJdbcJobstore to set.
082     */
083    public void setUseJdbcJobstore(boolean useJdbcJobstore) {
084        this.useJdbcJobstore = useJdbcJobstore;
085    }
086
087    /**
088     * Sets the useQuartzScheduling attribute value.
089     * 
090     * @param useQuartzScheduling The useQuartzScheduling to set.
091     */
092    public void setUseQuartzScheduling(boolean useQuartzScheduling) {
093        this.useQuartzScheduling = useQuartzScheduling;
094    }
095
096    /**
097     * Sets the quartzPropertiesReference attribute value.
098     * 
099     * @param quartzPropertiesReference The quartzPropertiesReference to set.
100     */
101    public void setQuartzPropertiesReference(Properties quartzPropertiesReference) {
102        this.quartzPropertiesReference = quartzPropertiesReference;
103    }
104}