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