01: package org.apache.ojb.broker.util.sequence;
02:
03: /* Copyright 2002-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * 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: private static Logger log = LoggerFactory
31: .getLogger(SequenceManagerFactory.class);
32: private static SequenceManagerFactory singleton;
33:
34: private Class defaultSeqManagerClass;
35:
36: public SequenceManagerFactory() {
37: defaultSeqManagerClass = SequenceManagerHighLowImpl.class;
38: if (log.isDebugEnabled())
39: log.debug("Default sequence manager class was "
40: + defaultSeqManagerClass.getName());
41: }
42:
43: public synchronized static SequenceManager getSequenceManager(
44: PersistenceBroker broker) {
45: if (singleton == null) {
46: singleton = new SequenceManagerFactory();
47: }
48: return singleton.createNewSequenceManager(broker);
49: }
50:
51: private SequenceManager createNewSequenceManager(
52: PersistenceBroker broker) {
53: synchronized (singleton) {
54: if (log.isDebugEnabled())
55: log.debug("create new sequence manager for broker "
56: + broker);
57: try {
58: // first we use seqMan defined in the OJB.properties
59: Class seqManClass = defaultSeqManagerClass;
60: SequenceDescriptor sd = broker
61: .serviceConnectionManager()
62: .getConnectionDescriptor()
63: .getSequenceDescriptor();
64: if (sd != null && sd.getSequenceManagerClass() != null) {
65: // if a seqMan was defined in repository, use that
66: seqManClass = sd.getSequenceManagerClass();
67: if (log.isDebugEnabled()) {
68: log.debug("Jdbc-Connection-Descriptor '"
69: + broker.serviceConnectionManager()
70: .getConnectionDescriptor()
71: .getJcdAlias()
72: + "' use sequence manager: "
73: + seqManClass);
74: }
75: }
76: return (SequenceManager) ClassHelper.newInstance(
77: seqManClass, PersistenceBroker.class, broker);
78: } catch (Exception ex) {
79: log.error(
80: "Could not create sequence manager for broker "
81: + broker, ex);
82: throw new PersistenceBrokerException(ex);
83: }
84: }
85: }
86: }
|