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