001    /*
002     * Copyright 2002-2005 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.jencks.factory;
018    
019    import org.apache.geronimo.connector.outbound.GenericConnectionManager;
020    import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoPool;
021    import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoTransactions;
022    import org.apache.geronimo.connector.outbound.connectionmanagerconfig.PoolingSupport;
023    import org.apache.geronimo.connector.outbound.connectionmanagerconfig.TransactionSupport;
024    import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTracker;
025    import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinator;
026    import org.apache.geronimo.security.bridge.RealmBridge;
027    import org.apache.geronimo.transaction.context.TransactionContextManager;
028    import org.springframework.beans.factory.FactoryBean;
029    import org.springframework.beans.factory.InitializingBean;
030    
031    import javax.resource.spi.ConnectionManager;
032    import javax.security.auth.Subject;
033    import javax.security.auth.login.LoginException;
034    
035    /**
036     * This FactoryBean creates a local JCA connection factory outside
037     * a J2EE application server.
038     * <p/>
039     * The connection manager will be then injected in the
040     * LocalConnectionFactoryBean, class of the JCA support of Spring.
041     *
042     * @author Thierry Templier
043     * @see org.springframework.jca.support.LocalConnectionFactoryBean#setConnectionManager(ConnectionManager)
044     * @see NoTransactionFactoryBean
045     * @see LocalTransactionFactoryBean
046     * @see XATransactionFactoryBean
047     * @see PartitionedPoolFactoryBean
048     * @see SinglePoolFactoryBean
049     */
050    public class ConnectionManagerFactoryBean implements FactoryBean, InitializingBean {
051    
052        private TransactionSupport transactionSupport;
053        private PoolingSupport poolingSupport;
054        private RealmBridge realmBridge;
055        private TransactionContextManager transactionContextManager;
056        private ConnectionTracker connectionTracker;
057    
058        private ConnectionManager connectionManager;
059    
060        public Object getObject() throws Exception {
061            return connectionManager;
062        }
063    
064        public Class getObjectType() {
065            return ConnectionManager.class;
066        }
067    
068        public boolean isSingleton() {
069            return true;
070        }
071    
072        /**
073         * Set the pooling support for the Geronimo Connection Manager.
074         * Geronimo provides two kinds of pool: single and partitioned.
075         *
076         * @see org.apache.geronimo.connector.outbound.connectionmanagerconfig.SinglePool
077         * @see org.apache.geronimo.connector.outbound.connectionmanagerconfig.PartitionedPool
078         */
079        public void setPoolingSupport(PoolingSupport support) {
080            poolingSupport = support;
081        }
082    
083        /**
084         * Set the realm bridge for the Geronimo Connection Manager. This
085         * mechnism allows the application to map the application subject
086         * with the subject used by the EIS.
087         */
088        public void setRealmBridge(RealmBridge bridge) {
089            realmBridge = bridge;
090        }
091    
092        /**
093         * Set the transaction context manager for the Geronimo Connection Manager.
094         */
095        public void setTransactionContextManager(TransactionContextManager manager) {
096            transactionContextManager = manager;
097        }
098    
099        /**
100         * Set the transaction support for the Geronimo Connection Manager.
101         * Geronimo provides in this case three kinds of support like the
102         * JCA specification: no transaction, local transactions, XA transactions.
103         *
104         * @see NoTransactions
105         * @see org.apache.geronimo.connector.outbound.connectionmanagerconfig.LocalTransactions
106         * @see org.apache.geronimo.connector.outbound.connectionmanagerconfig.XATransactions
107         */
108        public void setTransactionSupport(TransactionSupport support) {
109            transactionSupport = support;
110        }
111    
112        /**
113         * Set the connection tracker for the Geronimo Connection Manager.
114         */
115        public void setConnectionTracker(ConnectionTracker tracker) {
116            connectionTracker = tracker;
117        }
118    
119        /**
120         * This method checks all the needed parameters to construct
121         * the Geronimo connection manager which is implemented by the
122         * GenericConnectionManager class.
123         * If the transaction support property is not set, the method
124         * configures the connection manager with the no transaction value.
125         * If the pooling support property is not set, the method
126         * configures the connection manager with the no pool value.
127         * If the realm bridge is not set, the method configure
128         * the connection manager with an identity realm bridge.
129         * @see GenericConnectionManager
130         */
131        public void afterPropertiesSet() throws Exception {
132            //Apply the default value for property if necessary
133            if (this.transactionSupport == null) {
134                //No transaction
135                this.transactionSupport = NoTransactions.INSTANCE;
136            }
137            if (this.poolingSupport == null) {
138                //No pool
139                this.poolingSupport = new NoPool();
140            }
141            if (this.realmBridge == null) {
142                //Identity subject bridge
143                this.realmBridge = new RealmBridge() {
144                    public Subject mapSubject(Subject subject) throws LoginException {
145                        return subject;
146                    }
147                };
148            }
149            if (this.connectionTracker == null) {
150                //Instanciate the Geronimo Connection Track implementation
151                this.connectionTracker = new ConnectionTrackingCoordinator();
152            }
153    
154            //Instanciate the Geronimo Connection Manager
155            this.connectionManager = new GenericConnectionManager(this.transactionSupport, this.poolingSupport,
156                    this.realmBridge, this.connectionTracker, this.transactionContextManager,
157                    getClass().getName(), getClass().getClassLoader());
158            }
159    
160    }