Coverage Report - org.kuali.rice.core.framework.persistence.ojb.ConfigurableSequenceManager
 
Classes in this File Line Coverage Branch Coverage Complexity
ConfigurableSequenceManager
0%
0/45
0%
0/14
2.444
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.framework.persistence.ojb;
 17  
 
 18  
 import org.apache.commons.beanutils.ConstructorUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.apache.ojb.broker.PersistenceBroker;
 21  
 import org.apache.ojb.broker.accesslayer.JdbcAccess;
 22  
 import org.apache.ojb.broker.metadata.ClassDescriptor;
 23  
 import org.apache.ojb.broker.metadata.FieldDescriptor;
 24  
 import org.apache.ojb.broker.metadata.SequenceDescriptor;
 25  
 import org.apache.ojb.broker.util.sequence.AbstractSequenceManager;
 26  
 import org.apache.ojb.broker.util.sequence.SequenceManager;
 27  
 import org.apache.ojb.broker.util.sequence.SequenceManagerException;
 28  
 import org.apache.ojb.broker.util.sequence.SequenceManagerNextValImpl;
 29  
 import org.kuali.rice.core.api.config.ConfigurationException;
 30  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 31  
 import org.kuali.rice.core.api.util.ClassLoaderUtils;
 32  
 
 33  
 import java.util.Iterator;
 34  
 import java.util.Properties;
 35  
 
 36  
 
 37  
 /**
 38  
  * A sequence manager implementation which can be configured at runtime via the KEW
 39  
  * Configuration API.
 40  
  *
 41  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 42  
  */
 43  
 public class ConfigurableSequenceManager implements SequenceManager {
 44  
 
 45  
         private static final String PROPERTY_PREFIX_ATTRIBUTE = "property.prefix";
 46  
         private static final String DEFAULT_PROPERTY_PREFIX = "datasource.ojb.sequenceManager";
 47  0
         private static final String DEFAULT_SEQUENCE_MANAGER_CLASSNAME = SequenceManagerNextValImpl.class.getName();
 48  
 
 49  
         private PersistenceBroker broker;
 50  
         private SequenceManager sequenceManager;
 51  
 
 52  0
         public ConfigurableSequenceManager(PersistenceBroker broker) {
 53  0
                 this.broker = broker;
 54  0
                 this.sequenceManager = createSequenceManager(broker);
 55  0
         }
 56  
 
 57  
         protected SequenceManager createSequenceManager(PersistenceBroker broker) {
 58  0
                 String propertyPrefix = getPropertyPrefix();
 59  0
                 String sequenceManagerClassName = ConfigContext.getCurrentContextConfig().getProperty(getSequenceManagerClassNameProperty(propertyPrefix));
 60  0
                 if (StringUtils.isBlank(sequenceManagerClassName)) {
 61  0
                         sequenceManagerClassName = DEFAULT_SEQUENCE_MANAGER_CLASSNAME;
 62  
                 }
 63  
                 try {
 64  0
                         Class sequenceManagerClass = ClassLoaderUtils.getDefaultClassLoader().loadClass(sequenceManagerClassName);
 65  0
                         Object sequenceManagerObject = ConstructorUtils.invokeConstructor(sequenceManagerClass, broker);
 66  0
                         if (!(sequenceManagerObject instanceof SequenceManager)) {
 67  0
                                 throw new ConfigurationException("The configured sequence manager ('" + sequenceManagerClassName + "') is not an instance of '" + SequenceManager.class.getName() + "'");
 68  
                         }
 69  0
                         SequenceManager sequenceManager = (SequenceManager)sequenceManagerObject;
 70  0
                         if (sequenceManager instanceof AbstractSequenceManager) {
 71  0
                                 ((AbstractSequenceManager)sequenceManager).setConfigurationProperties(getSequenceManagerConfigProperties(propertyPrefix));
 72  
                         }
 73  0
                         return sequenceManager;
 74  0
                 } catch (ClassNotFoundException e) {
 75  0
                         throw new ConfigurationException("Could not locate sequence manager with the given class '" + sequenceManagerClassName + "'");
 76  0
                 } catch (Exception e) {
 77  0
                         throw new ConfigurationException("Property loading sequence manager class '" + sequenceManagerClassName + "'", e);
 78  
                 }
 79  
         }
 80  
 
 81  
         protected String getSequenceManagerClassNameProperty(String propertyPrefix) {
 82  0
                 return propertyPrefix + ".className";
 83  
         }
 84  
 
 85  
         protected SequenceManager getConfiguredSequenceManager() {
 86  0
                 return this.sequenceManager;
 87  
         }
 88  
 
 89  
         protected Properties getSequenceManagerConfigProperties(String propertyPrefix) {
 90  0
                 Properties sequenceManagerProperties = new Properties();
 91  0
                 Properties properties = ConfigContext.getCurrentContextConfig().getProperties();
 92  0
                 String attributePrefix = propertyPrefix + ".attribute.";
 93  0
                 for (Iterator iterator = properties.keySet().iterator(); iterator.hasNext();) {
 94  0
                         String key = (String) iterator.next();
 95  0
                         if (key.startsWith(attributePrefix)) {
 96  0
                                 String value = properties.getProperty(key);
 97  0
                                 String attributeName = key.substring(attributePrefix.length());
 98  0
                                 sequenceManagerProperties.setProperty(attributeName, value);
 99  
                         }
 100  0
                 }
 101  0
                 return sequenceManagerProperties;
 102  
         }
 103  
 
 104  
 
 105  
 
 106  
         public void afterStore(JdbcAccess jdbcAccess, ClassDescriptor classDescriptor, Object object) throws SequenceManagerException {
 107  0
                 getConfiguredSequenceManager().afterStore(jdbcAccess, classDescriptor, object);
 108  0
         }
 109  
 
 110  
         public Object getUniqueValue(FieldDescriptor fieldDescriptor) throws SequenceManagerException {
 111  0
                 return getConfiguredSequenceManager().getUniqueValue(fieldDescriptor);
 112  
         }
 113  
 
 114  
         public PersistenceBroker getBroker() {
 115  0
                 return this.broker;
 116  
         }
 117  
 
 118  
         public String getPropertyPrefix() {
 119  0
                 SequenceDescriptor sd = getBroker().serviceConnectionManager().getConnectionDescriptor().getSequenceDescriptor();
 120  0
                 String propertyPrefix = null;
 121  0
                 if (sd != null) {
 122  0
                         propertyPrefix = sd.getConfigurationProperties().getProperty(PROPERTY_PREFIX_ATTRIBUTE);
 123  
                 }
 124  0
                 if (StringUtils.isBlank(propertyPrefix)) {
 125  0
                         propertyPrefix = DEFAULT_PROPERTY_PREFIX;
 126  
                 }
 127  0
                 return propertyPrefix;
 128  
         }
 129  
 }