001/**
002 * Copyright 2005-2018 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.core.framework.persistence.ojb;
017
018import org.apache.commons.beanutils.ConstructorUtils;
019import org.apache.commons.lang.StringUtils;
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.metadata.SequenceDescriptor;
025import org.apache.ojb.broker.util.sequence.AbstractSequenceManager;
026import org.apache.ojb.broker.util.sequence.SequenceManager;
027import org.apache.ojb.broker.util.sequence.SequenceManagerException;
028import org.apache.ojb.broker.util.sequence.SequenceManagerNextValImpl;
029import org.kuali.rice.core.api.config.ConfigurationException;
030import org.kuali.rice.core.api.config.property.ConfigContext;
031import org.kuali.rice.core.api.util.ClassLoaderUtils;
032
033import java.util.Iterator;
034import java.util.Properties;
035
036
037/**
038 * A sequence manager implementation which can be configured at runtime via the KEW
039 * Configuration API.
040 *
041 * @author Kuali Rice Team (rice.collab@kuali.org)
042 */
043@Deprecated
044public class ConfigurableSequenceManager implements SequenceManager {
045
046        private static final String PROPERTY_PREFIX_ATTRIBUTE = "property.prefix";
047        private static final String DEFAULT_PROPERTY_PREFIX = "datasource.ojb.sequenceManager";
048        private static final String DEFAULT_SEQUENCE_MANAGER_CLASSNAME = SequenceManagerNextValImpl.class.getName();
049
050        private PersistenceBroker broker;
051        private SequenceManager sequenceManager;
052
053        public ConfigurableSequenceManager(PersistenceBroker broker) {
054                this.broker = broker;
055                this.sequenceManager = createSequenceManager(broker);
056        }
057
058        protected SequenceManager createSequenceManager(PersistenceBroker broker) {
059                String propertyPrefix = getPropertyPrefix();
060                String sequenceManagerClassName = ConfigContext.getCurrentContextConfig().getProperty(getSequenceManagerClassNameProperty(propertyPrefix));
061                if (StringUtils.isBlank(sequenceManagerClassName)) {
062                        sequenceManagerClassName = DEFAULT_SEQUENCE_MANAGER_CLASSNAME;
063                }
064                try {
065                        Class sequenceManagerClass = ClassLoaderUtils.getDefaultClassLoader().loadClass(sequenceManagerClassName);
066                        Object sequenceManagerObject = ConstructorUtils.invokeConstructor(sequenceManagerClass, broker);
067                        if (!(sequenceManagerObject instanceof SequenceManager)) {
068                                throw new ConfigurationException("The configured sequence manager ('" + sequenceManagerClassName + "') is not an instance of '" + SequenceManager.class.getName() + "'");
069                        }
070                        SequenceManager sequenceManager = (SequenceManager)sequenceManagerObject;
071                        if (sequenceManager instanceof AbstractSequenceManager) {
072                                ((AbstractSequenceManager)sequenceManager).setConfigurationProperties(getSequenceManagerConfigProperties(propertyPrefix));
073                        }
074                        return sequenceManager;
075                } catch (ClassNotFoundException e) {
076                        throw new ConfigurationException("Could not locate sequence manager with the given class '" + sequenceManagerClassName + "'");
077                } catch (Exception e) {
078                        throw new ConfigurationException("Property loading sequence manager class '" + sequenceManagerClassName + "'", e);
079                }
080        }
081
082        protected String getSequenceManagerClassNameProperty(String propertyPrefix) {
083                return propertyPrefix + ".className";
084        }
085
086        protected SequenceManager getConfiguredSequenceManager() {
087                return this.sequenceManager;
088        }
089
090        protected Properties getSequenceManagerConfigProperties(String propertyPrefix) {
091                Properties sequenceManagerProperties = new Properties();
092                Properties properties = ConfigContext.getCurrentContextConfig().getProperties();
093                String attributePrefix = propertyPrefix + ".attribute.";
094                for (Iterator iterator = properties.keySet().iterator(); iterator.hasNext();) {
095                        String key = (String) iterator.next();
096                        if (key.startsWith(attributePrefix)) {
097                                String value = properties.getProperty(key);
098                                String attributeName = key.substring(attributePrefix.length());
099                                sequenceManagerProperties.setProperty(attributeName, value);
100                        }
101                }
102                return sequenceManagerProperties;
103        }
104
105
106
107        public void afterStore(JdbcAccess jdbcAccess, ClassDescriptor classDescriptor, Object object) throws SequenceManagerException {
108                getConfiguredSequenceManager().afterStore(jdbcAccess, classDescriptor, object);
109        }
110
111        public Object getUniqueValue(FieldDescriptor fieldDescriptor) throws SequenceManagerException {
112                return getConfiguredSequenceManager().getUniqueValue(fieldDescriptor);
113        }
114
115        public PersistenceBroker getBroker() {
116                return this.broker;
117        }
118
119        public String getPropertyPrefix() {
120                SequenceDescriptor sd = getBroker().serviceConnectionManager().getConnectionDescriptor().getSequenceDescriptor();
121                String propertyPrefix = null;
122                if (sd != null) {
123                        propertyPrefix = sd.getConfigurationProperties().getProperty(PROPERTY_PREFIX_ATTRIBUTE);
124                }
125                if (StringUtils.isBlank(propertyPrefix)) {
126                        propertyPrefix = DEFAULT_PROPERTY_PREFIX;
127                }
128                return propertyPrefix;
129        }
130}