001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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 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.config.ConfigurationException;
033: import org.kuali.rice.core.Core;
034: import org.kuali.rice.util.ClassLoaderUtils;
035:
036: /**
037: * A sequence manager implementation which can be configured at runtime via the KEW
038: * Configuration API.
039: *
040: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
041: */
042: public class ConfigurableSequenceManager implements SequenceManager {
043:
044: private static final String PROPERTY_PREFIX_ATTRIBUTE = "property.prefix";
045: private static final String DEFAULT_PROPERTY_PREFIX = "datasource.ojb.sequenceManager";
046: private static final String DEFAULT_SEQUENCE_MANAGER_CLASSNAME = SequenceManagerNextValImpl.class
047: .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(
058: PersistenceBroker broker) {
059: String propertyPrefix = getPropertyPrefix();
060: String sequenceManagerClassName = Core
061: .getCurrentContextConfig()
062: .getProperty(
063: getSequenceManagerClassNameProperty(propertyPrefix));
064: if (StringUtils.isBlank(sequenceManagerClassName)) {
065: sequenceManagerClassName = DEFAULT_SEQUENCE_MANAGER_CLASSNAME;
066: }
067: try {
068: Class sequenceManagerClass = ClassLoaderUtils
069: .getDefaultClassLoader().loadClass(
070: sequenceManagerClassName);
071: Object sequenceManagerObject = ConstructorUtils
072: .invokeConstructor(sequenceManagerClass, broker);
073: if (!(sequenceManagerObject instanceof SequenceManager)) {
074: throw new ConfigurationException(
075: "The configured sequence manager ('"
076: + sequenceManagerClassName
077: + "') is not an instance of '"
078: + SequenceManager.class.getName() + "'");
079: }
080: SequenceManager sequenceManager = (SequenceManager) sequenceManagerObject;
081: if (sequenceManager instanceof AbstractSequenceManager) {
082: ((AbstractSequenceManager) sequenceManager)
083: .setConfigurationProperties(getSequenceManagerConfigProperties(propertyPrefix));
084: }
085: return sequenceManager;
086: } catch (ClassNotFoundException e) {
087: throw new ConfigurationException(
088: "Could not locate sequence manager with the given class '"
089: + sequenceManagerClassName + "'");
090: } catch (Exception e) {
091: throw new ConfigurationException(
092: "Property loading sequence manager class '"
093: + sequenceManagerClassName + "'", e);
094: }
095: }
096:
097: protected String getSequenceManagerClassNameProperty(
098: String propertyPrefix) {
099: return propertyPrefix + ".className";
100: }
101:
102: protected SequenceManager getConfiguredSequenceManager() {
103: return this .sequenceManager;
104: }
105:
106: protected Properties getSequenceManagerConfigProperties(
107: String propertyPrefix) {
108: Properties sequenceManagerProperties = new Properties();
109: Properties properties = Core.getCurrentContextConfig()
110: .getProperties();
111: String attributePrefix = propertyPrefix + ".attribute.";
112: for (Iterator iterator = properties.keySet().iterator(); iterator
113: .hasNext();) {
114: String key = (String) iterator.next();
115: if (key.startsWith(attributePrefix)) {
116: String value = properties.getProperty(key);
117: String attributeName = key.substring(attributePrefix
118: .length());
119: sequenceManagerProperties.setProperty(attributeName,
120: value);
121: }
122: }
123: return sequenceManagerProperties;
124: }
125:
126: public void afterStore(JdbcAccess jdbcAccess,
127: ClassDescriptor classDescriptor, Object object)
128: throws SequenceManagerException {
129: getConfiguredSequenceManager().afterStore(jdbcAccess,
130: classDescriptor, object);
131: }
132:
133: public Object getUniqueValue(FieldDescriptor fieldDescriptor)
134: throws SequenceManagerException {
135: return getConfiguredSequenceManager().getUniqueValue(
136: fieldDescriptor);
137: }
138:
139: public PersistenceBroker getBroker() {
140: return this .broker;
141: }
142:
143: public String getPropertyPrefix() {
144: SequenceDescriptor sd = getBroker().serviceConnectionManager()
145: .getConnectionDescriptor().getSequenceDescriptor();
146: String propertyPrefix = null;
147: if (sd != null) {
148: propertyPrefix = sd.getConfigurationProperties()
149: .getProperty(PROPERTY_PREFIX_ATTRIBUTE);
150: }
151: if (StringUtils.isBlank(propertyPrefix)) {
152: propertyPrefix = DEFAULT_PROPERTY_PREFIX;
153: }
154: return propertyPrefix;
155: }
156: }
|