View Javadoc

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