KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > ojb > support > PersistenceBrokerDaoSupport


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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
17 package org.springframework.orm.ojb.support;
18
19 import org.apache.ojb.broker.PersistenceBroker;
20 import org.apache.ojb.broker.PersistenceBrokerException;
21
22 import org.springframework.dao.DataAccessException;
23 import org.springframework.dao.DataAccessResourceFailureException;
24 import org.springframework.dao.support.DaoSupport;
25 import org.springframework.orm.ojb.OjbFactoryUtils;
26 import org.springframework.orm.ojb.PersistenceBrokerTemplate;
27
28 /**
29  * Convenient super class for OJB PersistenceBroker data access objects.
30  *
31  * <p>Allows a JDBC Connection Descriptor alias to be set, providing a
32  * PersistenceBrokerTemplate based on it to subclasses. Can alternatively
33  * be initialized directly via a PersistenceBrokerTemplate, to reuse the
34  * latter's settings like PBKey, DataSource, etc.
35  *
36  * <p>This base class is mainly intended for PersistenceBrokerTemplate usage but
37  * can also be used when working with OjbFactoryUtils directly. Convenience
38  * <code>getPersistenceBroker</code> and <code>releasePersistenceBroker</code>
39  * methods are provided for that usage style.
40  *
41  * <p>This class will create its own PersistenceBrokerTemplate if no explicit
42  * instance is passed in. The allowCreate flag on that PersistenceBrokerTemplate
43  * will be true by default. A custom PersistenceBrokerTemplate instance can be
44  * used through overriding <code>createHibernateTemplate</code>.
45  *
46  * @author Juergen Hoeller
47  * @since 1.1
48  * @see #setJcdAlias
49  * @see #setPersistenceBrokerTemplate
50  * @see #createPersistenceBrokerTemplate
51  * @see #getPersistenceBroker
52  * @see #releasePersistenceBroker
53  * @see org.springframework.orm.ojb.PersistenceBrokerTemplate
54  */

55 public abstract class PersistenceBrokerDaoSupport extends DaoSupport {
56
57     private PersistenceBrokerTemplate persistenceBrokerTemplate = createPersistenceBrokerTemplate();
58
59
60     /**
61      * Create a new default PersistenceBrokerTemplate on initialization.
62      * The returned template instance might be populated with a JCD alias,
63      * or get overwritten by an explicitly passed-in PersistenceBrokerTemplate.
64      * <p>Can be overridden in subclasses to provide a PersistenceBrokerTemplate instance
65      * with different configuration, or a custom PersistenceBrokerTemplate subclass.
66      * @return the new PersistenceBrokerTemplate instance
67      * @see #setJcdAlias
68      * @see #setPersistenceBrokerTemplate
69      */

70     protected PersistenceBrokerTemplate createPersistenceBrokerTemplate() {
71         return new PersistenceBrokerTemplate();
72     }
73
74     /**
75      * Set the JDBC Connection Descriptor alias of the PersistenceBroker
76      * configuration to use. Default is the default connection configured for OJB.
77      */

78     public final void setJcdAlias(String JavaDoc jcdAlias) {
79         this.persistenceBrokerTemplate.setJcdAlias(jcdAlias);
80     }
81
82     /**
83      * Return the JDBC Connection Descriptor alias of the PersistenceBroker
84      * configuration to use.
85      */

86     public final String JavaDoc getJcdAlias() {
87         return this.persistenceBrokerTemplate.getJcdAlias();
88     }
89
90     /**
91      * Set the PersistenceBrokerTemplate for this DAO explicitly,
92      * as an alternative to specifying a JCD alias.
93      */

94     public final void setPersistenceBrokerTemplate(PersistenceBrokerTemplate persistenceBrokerTemplate) {
95         this.persistenceBrokerTemplate = persistenceBrokerTemplate;
96     }
97
98     /**
99      * Return the PersistenceBrokerTemplate for this DAO, pre-initialized
100      * with the JCD alias or set explicitly.
101      */

102     public final PersistenceBrokerTemplate getPersistenceBrokerTemplate() {
103         return persistenceBrokerTemplate;
104     }
105
106     protected final void checkDaoConfig() {
107         if (this.persistenceBrokerTemplate == null) {
108             throw new IllegalArgumentException JavaDoc("jcdAlias or persistenceBrokerTemplate is required");
109         }
110     }
111
112
113     /**
114      * Get an OJB PersistenceBroker. Is aware of a corresponding
115      * PersistenceBroker bound to the current thread, for example when using
116      * PersistenceBrokerTransactionManager. Will create a new PersistenceBroker
117      * else, if allowCreate is true.
118      * @param allowCreate if a non-transactional PersistenceBroker should be created
119      * when no transactional PersistenceBroker can be found for the current thread
120      * @return the PersistenceBroker
121      * @throws DataAccessResourceFailureException if the PersistenceBroker couldn't be created
122      * @throws IllegalStateException if no thread-bound PersistenceBroker found and allowCreate false
123      */

124     protected final PersistenceBroker getPersistenceBroker(boolean allowCreate)
125         throws DataAccessResourceFailureException, IllegalStateException JavaDoc {
126         return OjbFactoryUtils.getPersistenceBroker(this.persistenceBrokerTemplate.getPbKey(), allowCreate);
127     }
128
129     /**
130      * Convert the given PersistenceBrokerException to an appropriate exception
131      * from the org.springframework.dao hierarchy. In case of a wrapped SQLException,
132      * the PersistenceBrokerTemplate's SQLExceptionTranslator gets applied.
133      * @param ex PersistenceBrokerException that occured
134      * @return the corresponding DataAccessException instance
135      * @see org.springframework.orm.ojb.PersistenceBrokerTemplate#convertOjbAccessException
136      */

137     protected final DataAccessException convertOjbAccessException(PersistenceBrokerException ex) {
138         return this.persistenceBrokerTemplate.convertOjbAccessException(ex);
139     }
140
141     /**
142      * Close the given PersistenceBroker if it isn't bound to the thread.
143      * @deprecated in favor of releasePersistenceBroker
144      * @see #releasePersistenceBroker
145      */

146     protected final void closePersistenceBrokerIfNecessary(PersistenceBroker pb) {
147         releasePersistenceBroker(pb);
148     }
149
150     /**
151      * Close the given PersistenceBroker if it isn't bound to the thread.
152      * @param pb PersistenceBroker to close
153      */

154     protected final void releasePersistenceBroker(PersistenceBroker pb) {
155         OjbFactoryUtils.releasePersistenceBroker(pb, this.persistenceBrokerTemplate.getPbKey());
156     }
157
158 }
159
Popular Tags