001/**
002 * Copyright 2005-2016 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    @Override
038    protected Object createInstance() throws Exception {
039        Properties properties = new Properties();
040        Properties configProps = ConfigContext.getCurrentContextConfig().getProperties();
041        boolean useQuartzDatabase = Boolean.valueOf(ConfigContext.getCurrentContextConfig().getProperty(KSBConstants.Config.USE_QUARTZ_DATABASE));
042        for (Object keyObj : configProps.keySet()) {
043            if (keyObj instanceof String) {
044                String key = (String)keyObj;
045                if (key.startsWith(QUARTZ_PREFIX) && !propertyShouldBeFiltered(useQuartzDatabase, key)) {
046                        properties.put(key.substring(4), configProps.get(key));
047                }
048            }
049        }
050        return properties;
051    }
052    
053    /**
054     * When we aren't using the quartz database, prevents some of the parameters for quartz database mode from
055     * being passed to quartz.  If we pass these to quartz when it's using a RAMJobStore, we get an error.  So
056     * in order to allow us to provide good defaults in common-config-defaults.xml, we will filter these out
057     * if useQuartzDatabase=false
058     */
059    protected boolean propertyShouldBeFiltered(boolean useQuartzDatabase, String propertyName) {
060        if (!useQuartzDatabase) {
061                if (propertyName.startsWith(QUARTZ_TABLE_PREFIX) || propertyName.startsWith(QUARTZ_IS_CLUSTERED)) {
062                        return true;
063                }
064        }
065        return false;
066    }
067    
068    
069    @Override
070    public Class getObjectType() {
071        return Properties.class;
072    }
073
074}