001 /*
002 * Copyright 2011 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.kfs.sys.context.PropertyLoadingFactoryBean;
027 import org.kuali.rice.core.config.ConfigurationException;
028
029
030 /**
031 * Overriding corresponding rice implementation that doesn't work for KFS.
032 */
033 public 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 = PropertyLoadingFactoryBean.getBaseProperty(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 public void afterStore(JdbcAccess jdbcAccess, ClassDescriptor classDescriptor, Object object) throws SequenceManagerException {
063 sequenceManager.afterStore(jdbcAccess, classDescriptor, object);
064 }
065
066 public Object getUniqueValue(FieldDescriptor fieldDescriptor) throws SequenceManagerException {
067 return sequenceManager.getUniqueValue(fieldDescriptor);
068 }
069 }