View Javadoc

1   /*
2    * Copyright 2007 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.ojb;
17  
18  import org.apache.commons.beanutils.ConstructorUtils;
19  import org.apache.log4j.Logger;
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.util.sequence.SequenceManager;
25  import org.apache.ojb.broker.util.sequence.SequenceManagerException;
26  import org.kuali.rice.core.api.config.ConfigurationException;
27  import org.kuali.rice.core.api.config.property.ConfigContext;
28  
29  
30  /**
31   * Overriding corresponding rice implementation that doesn't work for OLE.
32   */
33  public class ConfigurableSequenceManager implements SequenceManager {
34      private static final Logger LOG = Logger.getLogger(ConfigurableSequenceManager.class);
35      private static final String SEQUENCE_MANAGER_CLASS_NAME_PROPERTY = "datasource.ojb.sequence.manager";
36      private SequenceManager sequenceManager;
37  
38      public ConfigurableSequenceManager(PersistenceBroker broker) {
39          this.sequenceManager = createSequenceManager(broker);
40      }
41  
42      protected SequenceManager createSequenceManager(PersistenceBroker broker) {
43          String sequenceManagerClassName = ConfigContext.getCurrentContextConfig().getProperty(SEQUENCE_MANAGER_CLASS_NAME_PROPERTY);
44          try {
45              Object sequenceManagerObject = ConstructorUtils.invokeConstructor(Class.forName(sequenceManagerClassName), broker);
46              if (!(sequenceManagerObject instanceof SequenceManager)) {
47                  throw new ConfigurationException("The configured sequence manager ('" + sequenceManagerClassName + "') is not an instance of '" + SequenceManager.class.getName() + "'");
48              }
49              return (SequenceManager) sequenceManagerObject;
50          }
51          catch (Exception e) {
52              String message = "Unable to configure SequenceManager specified by " + SEQUENCE_MANAGER_CLASS_NAME_PROPERTY + " KualiConfigurationService property";
53              LOG.fatal(message, e);
54              throw new RuntimeException(message, e);
55          }
56      }
57  
58      protected SequenceManager getConfiguredSequenceManager() {
59          return this.sequenceManager;
60      }
61  
62      @Override
63      public void afterStore(JdbcAccess jdbcAccess, ClassDescriptor classDescriptor, Object object) throws SequenceManagerException {
64          sequenceManager.afterStore(jdbcAccess, classDescriptor, object);
65      }
66  
67      @Override
68      public Object getUniqueValue(FieldDescriptor fieldDescriptor) throws SequenceManagerException {
69          return sequenceManager.getUniqueValue(fieldDescriptor);
70      }
71  }