KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jca > support > LocalConnectionFactoryBean


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.support;
18
19 import javax.resource.ResourceException JavaDoc;
20 import javax.resource.spi.ConnectionManager JavaDoc;
21 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
22
23 import org.springframework.beans.factory.FactoryBean;
24 import org.springframework.beans.factory.InitializingBean;
25
26 /**
27  * {@link org.springframework.beans.factory.FactoryBean} that creates
28  * a local JCA connection factory in "non-managed" mode (as defined by the
29  * Java Connector Architecture specification). This is a direct alternative
30  * to a {@link org.springframework.jndi.JndiObjectFactoryBean} definition that
31  * obtains a connection factory handle from a J2EE server's naming environment.
32  *
33  * <p>The type of the connection factory is dependent on the actual connector:
34  * the connector can either expose its native API (such as a JDBC
35  * {@link javax.sql.DataSource} or a JMS {@link javax.jms.ConnectionFactory})
36  * or follow the standard Common Client Interface (CCI), as defined by the JCA spec.
37  * The exposed interface in the CCI case is {@link javax.resource.cci.ConnectionFactory}.
38  *
39  * <p>In order to use this FactoryBean, you must specify the connector's
40  * {@link #setManagedConnectionFactory "managedConnectionFactory"} (usually
41  * configured as separate JavaBean), which will be used to create the actual
42  * connection factory reference as exposed to the application. Optionally,
43  * you can also specify a {@link #setConnectionManager "connectionManager"},
44  * in order to use a custom ConnectionManager instead of the connector's default.
45  *
46  * <p><b>NOTE:</b> In non-managed mode, a connector is not deployed on an
47  * application server, or more specificially not interacting with an application
48  * server. Consequently, it cannot use a J2EE server's system contracts:
49  * connection management, transaction management, and security management.
50  * A custom ConnectionManager implementation has to be used for applying those
51  * services in conjunction with a standalone transaction coordinator etc.
52  *
53  * <p>The connector will use a local ConnectionManager (included in the connector)
54  * by default, which cannot participate in global transactions due to the lack
55  * of XA enlistment. You need to specify an XA-capable ConnectionManager in
56  * order to make the connector interact with an XA transaction coordinator.
57  * Alternatively, simply use the native local transaction facilities of the
58  * exposed API (e.g. CCI local transactions), or use a corresponding
59  * implementation of Spring's PlatformTransactionManager SPI
60  * (e.g. {@link org.springframework.jca.cci.connection.CciLocalTransactionManager})
61  * to drive local transactions.
62  *
63  * @author Juergen Hoeller
64  * @since 1.2
65  * @see #setManagedConnectionFactory
66  * @see #setConnectionManager
67  * @see javax.resource.cci.ConnectionFactory
68  * @see javax.resource.cci.Connection#getLocalTransaction
69  * @see org.springframework.jca.cci.connection.CciLocalTransactionManager
70  */

71 public class LocalConnectionFactoryBean implements FactoryBean, InitializingBean {
72
73     private ManagedConnectionFactory JavaDoc managedConnectionFactory;
74
75     private ConnectionManager JavaDoc connectionManager;
76
77     private Object JavaDoc connectionFactory;
78
79
80     /**
81      * Set the JCA ManagerConnectionFactory that should be used to create
82      * the desired connection factory.
83      * <p>The ManagerConnectionFactory will usually be set up as separate bean
84      * (potentially as inner bean), populated with JavaBean properties:
85      * a ManagerConnectionFactory is encouraged to follow the JavaBean pattern
86      * by the JCA specification, analogous to a JDBC DataSource and a JDO
87      * PersistenceManagerFactory.
88      * <p>Note that the ManagerConnectionFactory implementation might expect
89      * a reference to its JCA 1.5 ResourceAdapter, expressed through the
90      * {@link javax.resource.spi.ResourceAdapterAssociation} interface.
91      * Simply inject the corresponding ResourceAdapter instance into its
92      * "resourceAdapter" bean property in this case, before passing the
93      * ManagerConnectionFactory into this LocalConnectionFactoryBean.
94      * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory()
95      */

96     public void setManagedConnectionFactory(ManagedConnectionFactory JavaDoc managedConnectionFactory) {
97         this.managedConnectionFactory = managedConnectionFactory;
98     }
99
100     /**
101      * Set the JCA ConnectionManager that should be used to create the
102      * desired connection factory.
103      * <p>A ConnectionManager implementation for local usage is often
104      * included with a JCA connector. Such an included ConnectionManager
105      * might be set as default, with no need to explicitly specify one.
106      * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory(javax.resource.spi.ConnectionManager)
107      */

108     public void setConnectionManager(ConnectionManager JavaDoc connectionManager) {
109         this.connectionManager = connectionManager;
110     }
111
112     public void afterPropertiesSet() throws ResourceException JavaDoc {
113         if (this.managedConnectionFactory == null) {
114             throw new IllegalArgumentException JavaDoc("Property 'managedConnectionFactory' is required");
115         }
116         if (this.connectionManager != null) {
117             this.connectionFactory = this.managedConnectionFactory.createConnectionFactory(this.connectionManager);
118         }
119         else {
120             this.connectionFactory = this.managedConnectionFactory.createConnectionFactory();
121         }
122     }
123
124
125     public Object JavaDoc getObject() {
126         return this.connectionFactory;
127     }
128
129     public Class JavaDoc getObjectType() {
130         return (this.connectionFactory != null ? this.connectionFactory.getClass() : null);
131     }
132
133     public boolean isSingleton() {
134         return true;
135     }
136
137 }
138
Popular Tags