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