1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.framework.persistence.ojb; |
18 | |
|
19 | |
import org.apache.commons.beanutils.ConstructorUtils; |
20 | |
import org.apache.commons.lang.StringUtils; |
21 | |
import org.apache.ojb.broker.PersistenceBroker; |
22 | |
import org.apache.ojb.broker.accesslayer.JdbcAccess; |
23 | |
import org.apache.ojb.broker.metadata.ClassDescriptor; |
24 | |
import org.apache.ojb.broker.metadata.FieldDescriptor; |
25 | |
import org.apache.ojb.broker.metadata.SequenceDescriptor; |
26 | |
import org.apache.ojb.broker.util.sequence.AbstractSequenceManager; |
27 | |
import org.apache.ojb.broker.util.sequence.SequenceManager; |
28 | |
import org.apache.ojb.broker.util.sequence.SequenceManagerException; |
29 | |
import org.apache.ojb.broker.util.sequence.SequenceManagerNextValImpl; |
30 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
31 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
32 | |
import org.kuali.rice.core.api.util.ClassLoaderUtils; |
33 | |
|
34 | |
import java.util.Iterator; |
35 | |
import java.util.Properties; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class ConfigurableSequenceManager implements SequenceManager { |
45 | |
|
46 | |
private static final String PROPERTY_PREFIX_ATTRIBUTE = "property.prefix"; |
47 | |
private static final String DEFAULT_PROPERTY_PREFIX = "datasource.ojb.sequenceManager"; |
48 | 0 | private static final String DEFAULT_SEQUENCE_MANAGER_CLASSNAME = SequenceManagerNextValImpl.class.getName(); |
49 | |
|
50 | |
private PersistenceBroker broker; |
51 | |
private SequenceManager sequenceManager; |
52 | |
|
53 | 0 | public ConfigurableSequenceManager(PersistenceBroker broker) { |
54 | 0 | this.broker = broker; |
55 | 0 | this.sequenceManager = createSequenceManager(broker); |
56 | 0 | } |
57 | |
|
58 | |
protected SequenceManager createSequenceManager(PersistenceBroker broker) { |
59 | 0 | String propertyPrefix = getPropertyPrefix(); |
60 | 0 | String sequenceManagerClassName = ConfigContext.getCurrentContextConfig().getProperty(getSequenceManagerClassNameProperty(propertyPrefix)); |
61 | 0 | if (StringUtils.isBlank(sequenceManagerClassName)) { |
62 | 0 | sequenceManagerClassName = DEFAULT_SEQUENCE_MANAGER_CLASSNAME; |
63 | |
} |
64 | |
try { |
65 | 0 | Class sequenceManagerClass = ClassLoaderUtils.getDefaultClassLoader().loadClass(sequenceManagerClassName); |
66 | 0 | Object sequenceManagerObject = ConstructorUtils.invokeConstructor(sequenceManagerClass, broker); |
67 | 0 | if (!(sequenceManagerObject instanceof SequenceManager)) { |
68 | 0 | throw new ConfigurationException("The configured sequence manager ('" + sequenceManagerClassName + "') is not an instance of '" + SequenceManager.class.getName() + "'"); |
69 | |
} |
70 | 0 | SequenceManager sequenceManager = (SequenceManager)sequenceManagerObject; |
71 | 0 | if (sequenceManager instanceof AbstractSequenceManager) { |
72 | 0 | ((AbstractSequenceManager)sequenceManager).setConfigurationProperties(getSequenceManagerConfigProperties(propertyPrefix)); |
73 | |
} |
74 | 0 | return sequenceManager; |
75 | 0 | } catch (ClassNotFoundException e) { |
76 | 0 | throw new ConfigurationException("Could not locate sequence manager with the given class '" + sequenceManagerClassName + "'"); |
77 | 0 | } catch (Exception e) { |
78 | 0 | throw new ConfigurationException("Property loading sequence manager class '" + sequenceManagerClassName + "'", e); |
79 | |
} |
80 | |
} |
81 | |
|
82 | |
protected String getSequenceManagerClassNameProperty(String propertyPrefix) { |
83 | 0 | return propertyPrefix + ".className"; |
84 | |
} |
85 | |
|
86 | |
protected SequenceManager getConfiguredSequenceManager() { |
87 | 0 | return this.sequenceManager; |
88 | |
} |
89 | |
|
90 | |
protected Properties getSequenceManagerConfigProperties(String propertyPrefix) { |
91 | 0 | Properties sequenceManagerProperties = new Properties(); |
92 | 0 | Properties properties = ConfigContext.getCurrentContextConfig().getProperties(); |
93 | 0 | String attributePrefix = propertyPrefix + ".attribute."; |
94 | 0 | for (Iterator iterator = properties.keySet().iterator(); iterator.hasNext();) { |
95 | 0 | String key = (String) iterator.next(); |
96 | 0 | if (key.startsWith(attributePrefix)) { |
97 | 0 | String value = properties.getProperty(key); |
98 | 0 | String attributeName = key.substring(attributePrefix.length()); |
99 | 0 | sequenceManagerProperties.setProperty(attributeName, value); |
100 | |
} |
101 | 0 | } |
102 | 0 | return sequenceManagerProperties; |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
public void afterStore(JdbcAccess jdbcAccess, ClassDescriptor classDescriptor, Object object) throws SequenceManagerException { |
108 | 0 | getConfiguredSequenceManager().afterStore(jdbcAccess, classDescriptor, object); |
109 | 0 | } |
110 | |
|
111 | |
public Object getUniqueValue(FieldDescriptor fieldDescriptor) throws SequenceManagerException { |
112 | 0 | return getConfiguredSequenceManager().getUniqueValue(fieldDescriptor); |
113 | |
} |
114 | |
|
115 | |
public PersistenceBroker getBroker() { |
116 | 0 | return this.broker; |
117 | |
} |
118 | |
|
119 | |
public String getPropertyPrefix() { |
120 | 0 | SequenceDescriptor sd = getBroker().serviceConnectionManager().getConnectionDescriptor().getSequenceDescriptor(); |
121 | 0 | String propertyPrefix = null; |
122 | 0 | if (sd != null) { |
123 | 0 | propertyPrefix = sd.getConfigurationProperties().getProperty(PROPERTY_PREFIX_ATTRIBUTE); |
124 | |
} |
125 | 0 | if (StringUtils.isBlank(propertyPrefix)) { |
126 | 0 | propertyPrefix = DEFAULT_PROPERTY_PREFIX; |
127 | |
} |
128 | 0 | return propertyPrefix; |
129 | |
} |
130 | |
} |