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