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