001    /**
002     * Copyright 2011-2012 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     */
016    package org.kuali.rice.ojb;
017    
018    import java.util.Properties;
019    
020    import org.apache.commons.beanutils.ConstructorUtils;
021    import org.apache.log4j.Logger;
022    import org.apache.ojb.broker.PersistenceBroker;
023    import org.apache.ojb.broker.accesslayer.JdbcAccess;
024    import org.apache.ojb.broker.metadata.ClassDescriptor;
025    import org.apache.ojb.broker.metadata.FieldDescriptor;
026    import org.apache.ojb.broker.util.sequence.SequenceManager;
027    import org.apache.ojb.broker.util.sequence.SequenceManagerException;
028    import org.kuali.maven.plugins.ingester.PropertyLoadingFactoryBean;
029    import org.kuali.rice.core.config.ConfigurationException;
030    
031    /**
032     * Overriding corresponding Rice implementation
033     */
034    public class ConfigurableSequenceManager implements SequenceManager {
035        private static final Logger LOG = Logger.getLogger(ConfigurableSequenceManager.class);
036        private static final String SEQUENCE_MANAGER_CLASS_NAME_PROPERTY = "datasource.ojb.sequence.manager";
037        private SequenceManager sequenceManager;
038    
039        public ConfigurableSequenceManager(PersistenceBroker broker) {
040            this.sequenceManager = createSequenceManager(broker);
041        }
042    
043        protected SequenceManager createSequenceManager(PersistenceBroker broker) {
044            Properties props = PropertyLoadingFactoryBean.getProperties();
045            String sequenceManagerClassName = props.getProperty(SEQUENCE_MANAGER_CLASS_NAME_PROPERTY);
046            try {
047                Object sequenceManagerObject = ConstructorUtils.invokeConstructor(Class.forName(sequenceManagerClassName),
048                        broker);
049                if (!(sequenceManagerObject instanceof SequenceManager)) {
050                    throw new ConfigurationException("The configured sequence manager ('" + sequenceManagerClassName
051                            + "') is not an instance of '" + SequenceManager.class.getName() + "'");
052                }
053                return (SequenceManager) sequenceManagerObject;
054            } catch (Exception e) {
055                String message = "Unable to configure SequenceManager specified by " + SEQUENCE_MANAGER_CLASS_NAME_PROPERTY
056                        + " KualiConfigurationService property";
057                LOG.fatal(message, e);
058                throw new IllegalStateException(message, e);
059            }
060        }
061    
062        protected SequenceManager getConfiguredSequenceManager() {
063            return this.sequenceManager;
064        }
065    
066        @Override
067        public void afterStore(JdbcAccess jdbcAccess, ClassDescriptor classDescriptor, Object object)
068                throws SequenceManagerException {
069            sequenceManager.afterStore(jdbcAccess, classDescriptor, object);
070        }
071    
072        @Override
073        public Object getUniqueValue(FieldDescriptor fieldDescriptor) throws SequenceManagerException {
074            return sequenceManager.getUniqueValue(fieldDescriptor);
075        }
076    }