001/**
002 * Copyright 2005-2015 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.rice.ksb.messaging.quartz;
017
018import org.kuali.rice.core.api.config.property.ConfigContext;
019import org.kuali.rice.ksb.util.KSBConstants;
020import org.springframework.beans.factory.config.AbstractFactoryBean;
021
022import java.util.Properties;
023
024/**
025 * A factory bean which reads quartz-related properties from the Config system and
026 * generates a Properites instance for use when configuration quartz.
027 * 
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 *
030 */
031public class QuartzConfigPropertiesFactoryBean extends AbstractFactoryBean {
032
033    private static final String QUARTZ_PREFIX = "ksb.org.quartz";
034    private static final String QUARTZ_IS_CLUSTERED = QUARTZ_PREFIX + ".jobStore.isClustered";
035    private static final String QUARTZ_TABLE_PREFIX = QUARTZ_PREFIX + ".jobStore.tablePrefix";
036
037    // Added another configuration parameter which should be ignored when the "useQuartzDatabase" configuration parameter is false
038    private static final String QUARTZ_DRIVER_DELEGATE_CLASS = QUARTZ_PREFIX + ".jobStore.driverDelegateClass";
039    
040    @Override
041    protected Object createInstance() throws Exception {
042        Properties properties = new Properties();
043        Properties configProps = ConfigContext.getCurrentContextConfig().getProperties();
044        boolean useQuartzDatabase = Boolean.valueOf(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.USE_QUARTZ_DATABASE));
045        for (Object keyObj : configProps.keySet()) {
046            if (keyObj instanceof String) {
047                String key = (String)keyObj;
048                if (key.startsWith(QUARTZ_PREFIX) && !propertyShouldBeFiltered(useQuartzDatabase, key)) {
049                        properties.put(key.substring(4), configProps.get(key));
050                }
051            }
052        }
053        return properties;
054    }
055    
056    /**
057     * When we aren't using the quartz database, prevents some of the parameters for quartz database mode from
058     * being passed to quartz.  If we pass these to quartz when it's using a RAMJobStore, we get an error.  So
059     * in order to allow us to provide good defaults in common-config-defaults.xml, we will filter these out
060     * if useQuartzDatabase=false
061     */
062    protected boolean propertyShouldBeFiltered(boolean useQuartzDatabase, String propertyName) {
063        if (!useQuartzDatabase) {
064                if (propertyName.startsWith(QUARTZ_TABLE_PREFIX) || propertyName.startsWith(QUARTZ_IS_CLUSTERED) || propertyName.startsWith(QUARTZ_DRIVER_DELEGATE_CLASS)) {
065                        return true;
066                }
067        }
068        return false;
069    }
070    
071    
072    @Override
073    public Class getObjectType() {
074        return Properties.class;
075    }
076
077}