KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jca > cci > connection > ConnectionSpecConnectionFactoryAdapter


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.connection;
18
19 import javax.resource.ResourceException JavaDoc;
20 import javax.resource.cci.Connection JavaDoc;
21 import javax.resource.cci.ConnectionSpec JavaDoc;
22
23 /**
24  * An adapter for a target CCI {@link javax.resource.cci.ConnectionFactory},
25  * applying the given ConnectionSpec to every standard <code>getConnection()</code>
26  * call, that is, implicitly invoking <code>getConnection(ConnectionSpec)</code>
27  * on the target. All other methods simply delegate to the corresponding methods
28  * of the target ConnectionFactory.
29  *
30  * <p>Can be used to proxy a target JNDI ConnectionFactory that does not have a
31  * ConnectionSpec configured. Client code can work with the ConnectionFactory
32  * without passing in a ConnectionSpec on every <code>getConnection()</code> call.
33  *
34  * <p>In the following example, client code can simply transparently work with
35  * the preconfigured "myConnectionFactory", implicitly accessing
36  * "myTargetConnectionFactory" with the specified user credentials.
37  *
38  * <pre class="code">
39  * &lt;bean id="myTargetConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
40  * &lt;property name="jndiName" value="java:comp/env/cci/mycf"/&gt;
41  * &lt;/bean>
42  *
43  * &lt;bean id="myConnectionFactory" class="org.springframework.jca.cci.connection.ConnectionSpecConnectionFactoryAdapter"&gt;
44  * &lt;property name="targetConnectionFactory" ref="myTargetConnectionFactory"/&gt;
45  * &lt;property name="connectionSpec"&gt;
46  * &lt;bean class="your.resource.adapter.ConnectionSpecImpl"&gt;
47  * &lt;property name="username" value="myusername"/&gt;
48  * &lt;property name="password" value="mypassword"/&gt;
49  * &lt;/bean&gt;
50  * &lt;/property&gt;
51  * &lt;/bean&gt;</pre>
52  *
53  * <p>If the "connectionSpec" is empty, this proxy will simply delegate to the
54  * standard <code>getConnection()</code> method of the target ConnectionFactory.
55  * This can be used to keep a UserCredentialsConnectionFactoryAdapter bean definition
56  * just for the <i>option</i> of implicitly passing in a ConnectionSpec if the
57  * particular target ConnectionFactory requires it.
58  *
59  * @author Juergen Hoeller
60  * @since 1.2
61  * @see #getConnection
62  */

63 public class ConnectionSpecConnectionFactoryAdapter extends DelegatingConnectionFactory {
64
65     private ConnectionSpec JavaDoc connectionSpec;
66
67     private final ThreadLocal JavaDoc threadBoundSpec = new ThreadLocal JavaDoc();
68
69
70     /**
71      * Set the ConnectionSpec that this adapter should use for retrieving Connections.
72      * Default is none.
73      */

74     public void setConnectionSpec(ConnectionSpec JavaDoc connectionSpec) {
75         this.connectionSpec = connectionSpec;
76     }
77
78     /**
79      * Set a ConnectionSpec for this proxy and the current thread.
80      * The given ConnectionSpec will be applied to all subsequent
81      * <code>getConnection()</code> calls on this ConnectionFactory proxy.
82      * <p>This will override any statically specified "connectionSpec" property.
83      * @param spec the ConnectionSpec to apply
84      * @see #removeConnectionSpecFromCurrentThread
85      */

86     public void setConnectionSpecForCurrentThread(ConnectionSpec JavaDoc spec) {
87         this.threadBoundSpec.set(spec);
88     }
89
90     /**
91      * Remove any ConnectionSpec for this proxy from the current thread.
92      * A statically specified ConnectionSpec applies again afterwards.
93      * @see #setConnectionSpecForCurrentThread
94      */

95     public void removeConnectionSpecFromCurrentThread() {
96         this.threadBoundSpec.set(null);
97     }
98
99
100     /**
101      * Determine whether there is currently a thread-bound ConnectionSpec,
102      * using it if available, falling back to the statically specified
103      * "connectionSpec" property else.
104      * @see #doGetConnection
105      */

106     public final Connection JavaDoc getConnection() throws ResourceException JavaDoc {
107         ConnectionSpec JavaDoc threadSpec = (ConnectionSpec JavaDoc) this.threadBoundSpec.get();
108         if (threadSpec != null) {
109             return doGetConnection(threadSpec);
110         }
111         else {
112             return doGetConnection(this.connectionSpec);
113         }
114     }
115
116     /**
117      * This implementation delegates to the <code>getConnection(ConnectionSpec)</code>
118      * method of the target ConnectionFactory, passing in the specified user credentials.
119      * If the specified username is empty, it will simply delegate to the standard
120      * <code>getConnection()</code> method of the target ConnectionFactory.
121      * @param spec the ConnectionSpec to apply
122      * @return the Connection
123      * @see javax.resource.cci.ConnectionFactory#getConnection(javax.resource.cci.ConnectionSpec)
124      * @see javax.resource.cci.ConnectionFactory#getConnection()
125      */

126     protected Connection JavaDoc doGetConnection(ConnectionSpec JavaDoc spec) throws ResourceException JavaDoc {
127         if (getTargetConnectionFactory() == null) {
128             throw new IllegalStateException JavaDoc("targetConnectionFactory is required");
129         }
130         if (spec != null) {
131             return getTargetConnectionFactory().getConnection(spec);
132         }
133         else {
134             return getTargetConnectionFactory().getConnection();
135         }
136     }
137
138 }
139
Popular Tags