View Javadoc

1   package org.apache.ojb.broker.util.sequence;
2   
3   /* Copyright 2002-2005 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  import org.apache.ojb.broker.PersistenceBroker;
19  import org.apache.ojb.broker.PersistenceBrokerException;
20  import org.apache.ojb.broker.metadata.SequenceDescriptor;
21  import org.apache.ojb.broker.util.ClassHelper;
22  import org.apache.ojb.broker.util.logging.Logger;
23  import org.apache.ojb.broker.util.logging.LoggerFactory;
24  
25  /**
26   * Threadsafe factory class, creates <code>SequenceManager</code> instances.
27   * The implementation class is configured by the OJB.properties file.
28   */
29  public class SequenceManagerFactory
30  {
31      private static Logger log = LoggerFactory.getLogger(SequenceManagerFactory.class);
32      private static SequenceManagerFactory singleton;
33  
34      private Class defaultSeqManagerClass;
35  
36      public SequenceManagerFactory()
37      {
38          defaultSeqManagerClass = SequenceManagerHighLowImpl.class;
39          if(log.isDebugEnabled()) log.debug("Default sequence manager class was " + defaultSeqManagerClass.getName());
40      }
41  
42      public synchronized static SequenceManager getSequenceManager(PersistenceBroker broker)
43      {
44          if (singleton == null)
45          {
46              singleton = new SequenceManagerFactory();
47          }
48          return singleton.createNewSequenceManager(broker);
49      }
50  
51      private SequenceManager createNewSequenceManager(PersistenceBroker broker)
52      {
53          synchronized (singleton)
54          {
55              if (log.isDebugEnabled()) log.debug("create new sequence manager for broker " + broker);
56              try
57              {
58                  // first we use seqMan defined in the OJB.properties
59                  Class seqManClass = defaultSeqManagerClass;
60                  SequenceDescriptor sd = broker.serviceConnectionManager().getConnectionDescriptor().getSequenceDescriptor();
61                  if (sd != null && sd.getSequenceManagerClass() != null)
62                  {
63                      // if a seqMan was defined in repository, use that
64                      seqManClass = sd.getSequenceManagerClass();
65                      if (log.isDebugEnabled())
66                      {
67                          log.debug("Jdbc-Connection-Descriptor '" +
68                                  broker.serviceConnectionManager().getConnectionDescriptor().getJcdAlias() +
69                                  "' use sequence manager: " + seqManClass);
70                      }
71                  }
72                  return (SequenceManager) ClassHelper.newInstance(seqManClass, PersistenceBroker.class, broker);
73              }
74              catch (Exception ex)
75              {
76                  log.error("Could not create sequence manager for broker " + broker, ex);
77                  throw new PersistenceBrokerException(ex);
78              }
79          }
80      }
81  }