View Javadoc

1   /*
2    * Copyright 2002-2005 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.jencks.factory;
18  
19  import org.apache.geronimo.connector.outbound.GenericConnectionManager;
20  import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoPool;
21  import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoTransactions;
22  import org.apache.geronimo.connector.outbound.connectionmanagerconfig.PoolingSupport;
23  import org.apache.geronimo.connector.outbound.connectionmanagerconfig.TransactionSupport;
24  import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTracker;
25  import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinator;
26  import org.apache.geronimo.security.bridge.RealmBridge;
27  import org.apache.geronimo.transaction.context.TransactionContextManager;
28  import org.springframework.beans.factory.FactoryBean;
29  import org.springframework.beans.factory.InitializingBean;
30  
31  import javax.resource.spi.ConnectionManager;
32  import javax.security.auth.Subject;
33  import javax.security.auth.login.LoginException;
34  
35  /***
36   * This FactoryBean creates a local JCA connection factory outside
37   * a J2EE application server.
38   * <p/>
39   * The connection manager will be then injected in the
40   * LocalConnectionFactoryBean, class of the JCA support of Spring.
41   *
42   * @author Thierry Templier
43   * @see org.springframework.jca.support.LocalConnectionFactoryBean#setConnectionManager(ConnectionManager)
44   * @see NoTransactionFactoryBean
45   * @see LocalTransactionFactoryBean
46   * @see XATransactionFactoryBean
47   * @see PartitionedPoolFactoryBean
48   * @see SinglePoolFactoryBean
49   */
50  public class ConnectionManagerFactoryBean implements FactoryBean, InitializingBean {
51  
52      private TransactionSupport transactionSupport;
53      private PoolingSupport poolingSupport;
54      private RealmBridge realmBridge;
55      private TransactionContextManager transactionContextManager;
56      private ConnectionTracker connectionTracker;
57  
58      private ConnectionManager connectionManager;
59  
60      public Object getObject() throws Exception {
61          return connectionManager;
62      }
63  
64      public Class getObjectType() {
65          return ConnectionManager.class;
66      }
67  
68      public boolean isSingleton() {
69          return true;
70      }
71  
72      /***
73       * Set the pooling support for the Geronimo Connection Manager.
74       * Geronimo provides two kinds of pool: single and partitioned.
75       *
76       * @see org.apache.geronimo.connector.outbound.connectionmanagerconfig.SinglePool
77       * @see org.apache.geronimo.connector.outbound.connectionmanagerconfig.PartitionedPool
78       */
79      public void setPoolingSupport(PoolingSupport support) {
80          poolingSupport = support;
81      }
82  
83      /***
84       * Set the realm bridge for the Geronimo Connection Manager. This
85       * mechnism allows the application to map the application subject
86       * with the subject used by the EIS.
87       */
88      public void setRealmBridge(RealmBridge bridge) {
89          realmBridge = bridge;
90      }
91  
92      /***
93       * Set the transaction context manager for the Geronimo Connection Manager.
94       */
95      public void setTransactionContextManager(TransactionContextManager manager) {
96          transactionContextManager = manager;
97      }
98  
99      /***
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 }