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 java.util.Map;
20  
21  import javax.resource.spi.ConnectionManager;
22  
23  import org.apache.geronimo.connector.outbound.GenericConnectionManager;
24  import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoPool;
25  import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoTransactions;
26  import org.apache.geronimo.connector.outbound.connectionmanagerconfig.PoolingSupport;
27  import org.apache.geronimo.connector.outbound.connectionmanagerconfig.TransactionSupport;
28  import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTracker;
29  import org.apache.geronimo.transaction.context.TransactionContextManager;
30  import org.springframework.beans.factory.FactoryBean;
31  import org.springframework.beans.factory.InitializingBean;
32  import org.springframework.context.ApplicationContext;
33  import org.springframework.context.ApplicationContextAware;
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, ApplicationContextAware {
51  
52  	private ApplicationContext applicationContext;
53      private TransactionSupport transactionSupport;
54      private PoolingSupport poolingSupport;
55      private boolean containerManagedSecurity;
56      private TransactionContextManager transactionContextManager;
57      private ConnectionTracker connectionTracker;
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  	public void setApplicationContext(ApplicationContext applicationContext) {
73  		this.applicationContext = applicationContext;
74  	}
75  
76      /***
77       * Set the pooling support for the Geronimo Connection Manager.
78       * Geronimo provides two kinds of pool: single and partitioned.
79       *
80       * @see org.apache.geronimo.connector.outbound.connectionmanagerconfig.SinglePool
81       * @see org.apache.geronimo.connector.outbound.connectionmanagerconfig.PartitionedPool
82       */
83      public void setPoolingSupport(PoolingSupport support) {
84          poolingSupport = support;
85      }
86  
87      /***
88       * Set the transaction context manager for the Geronimo Connection Manager.
89       */
90      public void setTransactionContextManager(TransactionContextManager manager) {
91          transactionContextManager = manager;
92      }
93  
94      /***
95       * Set the transaction support for the Geronimo Connection Manager.
96       * Geronimo provides in this case three kinds of support like the
97       * JCA specification: no transaction, local transactions, XA transactions.
98       *
99       * @see NoTransactions
100      * @see org.apache.geronimo.connector.outbound.connectionmanagerconfig.LocalTransactions
101      * @see org.apache.geronimo.connector.outbound.connectionmanagerconfig.XATransactions
102      */
103     public void setTransactionSupport(TransactionSupport support) {
104         transactionSupport = support;
105     }
106 
107     /***
108      * Set the connection tracker for the Geronimo Connection Manager.
109      */
110     public void setConnectionTracker(ConnectionTracker tracker) {
111         connectionTracker = tracker;
112     }
113 
114     /***
115      * Enables/disables container managed security
116      */
117     public void setContainerManagedSecurity(boolean containerManagedSecurity) {
118         this.containerManagedSecurity = containerManagedSecurity;
119     }
120 
121     /***
122      * This method checks all the needed parameters to construct
123      * the Geronimo connection manager which is implemented by the
124      * GenericConnectionManager class.
125      * If the transaction support property is not set, the method
126      * configures the connection manager with the no transaction value.
127      * If the pooling support property is not set, the method
128      * configures the connection manager with the no pool value.
129      * If the realm bridge is not set, the method configure
130      * the connection manager with an identity realm bridge.
131      *
132      * @see GenericConnectionManager
133      */
134     public void afterPropertiesSet() throws Exception {
135         // Apply the default value for property if necessary
136         if (this.transactionSupport == null) {
137             // No transaction
138             this.transactionSupport = NoTransactions.INSTANCE;
139         }
140         if (this.poolingSupport == null) {
141             // No pool
142             this.poolingSupport = new NoPool();
143         }
144         // Instanciate the Geronimo Connection Manager
145         this.connectionManager = new GenericConnectionManager(
146         		this.transactionSupport, 
147         		this.poolingSupport,
148                 this.containerManagedSecurity, 
149                 getConnectionTracker(), 
150                 getTransactionContextManager(),
151                 getClass().getName(), 
152                 getClass().getClassLoader());
153     }
154 
155 	public ConnectionTracker getConnectionTracker() {
156 		if (connectionTracker == null) {
157 			Map map = applicationContext.getBeansOfType(ConnectionTracker.class);
158 			if (map.size() == 1) {
159 				connectionTracker = (ConnectionTracker) map.values().iterator().next();
160 			}
161 		}
162 		return connectionTracker;
163 	}
164 
165 	public TransactionContextManager getTransactionContextManager() {
166 		if (transactionContextManager == null) {
167 			Map map = applicationContext.getBeansOfType(TransactionContextManager.class);
168 			if (map.size() == 1) {
169 				transactionContextManager = (TransactionContextManager) map.values().iterator().next();
170 			} else {
171 				throw new IllegalStateException("no TransactionContextManager is registered");
172 			}
173 		}
174 		return transactionContextManager;
175 	}
176 
177 }