001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jca.support;
018:
019: import javax.resource.ResourceException;
020: import javax.resource.spi.ConnectionManager;
021: import javax.resource.spi.ManagedConnectionFactory;
022:
023: import org.springframework.beans.factory.FactoryBean;
024: import org.springframework.beans.factory.InitializingBean;
025:
026: /**
027: * {@link org.springframework.beans.factory.FactoryBean} that creates
028: * a local JCA connection factory in "non-managed" mode (as defined by the
029: * Java Connector Architecture specification). This is a direct alternative
030: * to a {@link org.springframework.jndi.JndiObjectFactoryBean} definition that
031: * obtains a connection factory handle from a J2EE server's naming environment.
032: *
033: * <p>The type of the connection factory is dependent on the actual connector:
034: * the connector can either expose its native API (such as a JDBC
035: * {@link javax.sql.DataSource} or a JMS {@link javax.jms.ConnectionFactory})
036: * or follow the standard Common Client Interface (CCI), as defined by the JCA spec.
037: * The exposed interface in the CCI case is {@link javax.resource.cci.ConnectionFactory}.
038: *
039: * <p>In order to use this FactoryBean, you must specify the connector's
040: * {@link #setManagedConnectionFactory "managedConnectionFactory"} (usually
041: * configured as separate JavaBean), which will be used to create the actual
042: * connection factory reference as exposed to the application. Optionally,
043: * you can also specify a {@link #setConnectionManager "connectionManager"},
044: * in order to use a custom ConnectionManager instead of the connector's default.
045: *
046: * <p><b>NOTE:</b> In non-managed mode, a connector is not deployed on an
047: * application server, or more specificially not interacting with an application
048: * server. Consequently, it cannot use a J2EE server's system contracts:
049: * connection management, transaction management, and security management.
050: * A custom ConnectionManager implementation has to be used for applying those
051: * services in conjunction with a standalone transaction coordinator etc.
052: *
053: * <p>The connector will use a local ConnectionManager (included in the connector)
054: * by default, which cannot participate in global transactions due to the lack
055: * of XA enlistment. You need to specify an XA-capable ConnectionManager in
056: * order to make the connector interact with an XA transaction coordinator.
057: * Alternatively, simply use the native local transaction facilities of the
058: * exposed API (e.g. CCI local transactions), or use a corresponding
059: * implementation of Spring's PlatformTransactionManager SPI
060: * (e.g. {@link org.springframework.jca.cci.connection.CciLocalTransactionManager})
061: * to drive local transactions.
062: *
063: * @author Juergen Hoeller
064: * @since 1.2
065: * @see #setManagedConnectionFactory
066: * @see #setConnectionManager
067: * @see javax.resource.cci.ConnectionFactory
068: * @see javax.resource.cci.Connection#getLocalTransaction
069: * @see org.springframework.jca.cci.connection.CciLocalTransactionManager
070: */
071: public class LocalConnectionFactoryBean implements FactoryBean,
072: InitializingBean {
073:
074: private ManagedConnectionFactory managedConnectionFactory;
075:
076: private ConnectionManager connectionManager;
077:
078: private Object connectionFactory;
079:
080: /**
081: * Set the JCA ManagerConnectionFactory that should be used to create
082: * the desired connection factory.
083: * <p>The ManagerConnectionFactory will usually be set up as separate bean
084: * (potentially as inner bean), populated with JavaBean properties:
085: * a ManagerConnectionFactory is encouraged to follow the JavaBean pattern
086: * by the JCA specification, analogous to a JDBC DataSource and a JDO
087: * PersistenceManagerFactory.
088: * <p>Note that the ManagerConnectionFactory implementation might expect
089: * a reference to its JCA 1.5 ResourceAdapter, expressed through the
090: * {@link javax.resource.spi.ResourceAdapterAssociation} interface.
091: * Simply inject the corresponding ResourceAdapter instance into its
092: * "resourceAdapter" bean property in this case, before passing the
093: * ManagerConnectionFactory into this LocalConnectionFactoryBean.
094: * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory()
095: */
096: public void setManagedConnectionFactory(
097: ManagedConnectionFactory managedConnectionFactory) {
098: this .managedConnectionFactory = managedConnectionFactory;
099: }
100:
101: /**
102: * Set the JCA ConnectionManager that should be used to create the
103: * desired connection factory.
104: * <p>A ConnectionManager implementation for local usage is often
105: * included with a JCA connector. Such an included ConnectionManager
106: * might be set as default, with no need to explicitly specify one.
107: * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory(javax.resource.spi.ConnectionManager)
108: */
109: public void setConnectionManager(ConnectionManager connectionManager) {
110: this .connectionManager = connectionManager;
111: }
112:
113: public void afterPropertiesSet() throws ResourceException {
114: if (this .managedConnectionFactory == null) {
115: throw new IllegalArgumentException(
116: "Property 'managedConnectionFactory' is required");
117: }
118: if (this .connectionManager != null) {
119: this .connectionFactory = this .managedConnectionFactory
120: .createConnectionFactory(this .connectionManager);
121: } else {
122: this .connectionFactory = this .managedConnectionFactory
123: .createConnectionFactory();
124: }
125: }
126:
127: public Object getObject() {
128: return this .connectionFactory;
129: }
130:
131: public Class getObjectType() {
132: return (this .connectionFactory != null ? this .connectionFactory
133: .getClass() : null);
134: }
135:
136: public boolean isSingleton() {
137: return true;
138: }
139:
140: }
|