KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jca > cci > core > support > CciDaoSupport


1 /*
2  * Copyright 2002-2007 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.jca.cci.core.support;
18
19 import javax.resource.cci.Connection JavaDoc;
20 import javax.resource.cci.ConnectionFactory JavaDoc;
21 import javax.resource.cci.ConnectionSpec JavaDoc;
22
23 import org.springframework.dao.support.DaoSupport;
24 import org.springframework.jca.cci.CannotGetCciConnectionException;
25 import org.springframework.jca.cci.connection.ConnectionFactoryUtils;
26 import org.springframework.jca.cci.core.CciTemplate;
27
28 /**
29  * Convenient super class for CCI-based data access objects.
30  *
31  * <p>Requires a {@link javax.resource.cci.ConnectionFactory} to be set,
32  * providing a {@link org.springframework.jca.cci.core.CciTemplate} based
33  * on it to subclasses through the {@link #getCciTemplate()} method.
34  *
35  * <p>This base class is mainly intended for CciTemplate usage but can
36  * also be used when working with a Connection directly or when using
37  * <code>org.springframework.jca.cci.object</code> classes.
38  *
39  * @author Thierry Templier
40  * @author Juergen Hoeller
41  * @since 1.2
42  * @see #setConnectionFactory
43  * @see #getCciTemplate
44  * @see org.springframework.jca.cci.core.CciTemplate
45  */

46 public abstract class CciDaoSupport extends DaoSupport {
47
48     private CciTemplate cciTemplate;
49
50
51     /**
52      * Set the ConnectionFactory to be used by this DAO.
53      */

54     public final void setConnectionFactory(ConnectionFactory JavaDoc connectionFactory) {
55       this.cciTemplate = createCciTemplate(connectionFactory);
56     }
57
58     /**
59      * Create a CciTemplate for the given ConnectionFactory.
60      * Only invoked if populating the DAO with a ConnectionFactory reference!
61      * <p>Can be overridden in subclasses to provide a CciTemplate instance
62      * with different configuration, or a custom CciTemplate subclass.
63      * @param connectionFactory the CCI ConnectionFactory to create a CciTemplate for
64      * @return the new CciTemplate instance
65      * @see #setConnectionFactory(javax.resource.cci.ConnectionFactory)
66      */

67     protected CciTemplate createCciTemplate(ConnectionFactory JavaDoc connectionFactory) {
68         return new CciTemplate(connectionFactory);
69     }
70
71     /**
72      * Return the ConnectionFactory used by this DAO.
73      */

74     public final ConnectionFactory JavaDoc getConnectionFactory() {
75         return this.cciTemplate.getConnectionFactory();
76     }
77
78     /**
79      * Set the CciTemplate for this DAO explicitly,
80      * as an alternative to specifying a ConnectionFactory.
81      */

82     public final void setCciTemplate(CciTemplate cciTemplate) {
83         this.cciTemplate = cciTemplate;
84     }
85
86     /**
87      * Return the CciTemplate for this DAO,
88      * pre-initialized with the ConnectionFactory or set explicitly.
89      */

90     public final CciTemplate getCciTemplate() {
91       return this.cciTemplate;
92     }
93
94     protected final void checkDaoConfig() {
95         if (this.cciTemplate == null) {
96             throw new IllegalArgumentException JavaDoc("'connectionFactory' or 'cciTemplate' is required");
97         }
98     }
99
100
101     /**
102      * Obtain a CciTemplate derived from the main template instance,
103      * inheriting the ConnectionFactory and other settings but
104      * overriding the ConnectionSpec used for obtaining Connections.
105      * @param connectionSpec the CCI ConnectionSpec that the returned
106      * template instance is supposed to obtain Connections for
107      * @return the derived template instance
108      * @see org.springframework.jca.cci.core.CciTemplate#getDerivedTemplate(javax.resource.cci.ConnectionSpec)
109      */

110     protected final CciTemplate getCciTemplate(ConnectionSpec JavaDoc connectionSpec) {
111         return getCciTemplate().getDerivedTemplate(connectionSpec);
112     }
113
114     /**
115      * Get a CCI Connection, either from the current transaction or a new one.
116      * @return the CCI Connection
117      * @throws org.springframework.jca.cci.CannotGetCciConnectionException
118      * if the attempt to get a Connection failed
119      * @see org.springframework.jca.cci.connection.ConnectionFactoryUtils#getConnection(javax.resource.cci.ConnectionFactory)
120      */

121     protected final Connection JavaDoc getConnection() throws CannotGetCciConnectionException {
122         return ConnectionFactoryUtils.getConnection(getConnectionFactory());
123     }
124
125     /**
126      * Close the given CCI Connection, created via this bean's ConnectionFactory,
127      * if it isn't bound to the thread.
128      * @param con Connection to close
129      * @see org.springframework.jca.cci.connection.ConnectionFactoryUtils#releaseConnection
130      */

131     protected final void releaseConnection(Connection JavaDoc con) {
132         ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory());
133     }
134
135 }
136
Popular Tags