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 java.util.Map; 020 021 import javax.resource.spi.ConnectionManager; 022 023 import org.apache.geronimo.connector.outbound.GenericConnectionManager; 024 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoPool; 025 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoTransactions; 026 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.PoolingSupport; 027 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.TransactionSupport; 028 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTracker; 029 import org.apache.geronimo.transaction.context.TransactionContextManager; 030 import org.springframework.beans.factory.FactoryBean; 031 import org.springframework.beans.factory.InitializingBean; 032 import org.springframework.context.ApplicationContext; 033 import org.springframework.context.ApplicationContextAware; 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, ApplicationContextAware { 051 052 private ApplicationContext applicationContext; 053 private TransactionSupport transactionSupport; 054 private PoolingSupport poolingSupport; 055 private boolean containerManagedSecurity; 056 private TransactionContextManager transactionContextManager; 057 private ConnectionTracker connectionTracker; 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 public void setApplicationContext(ApplicationContext applicationContext) { 073 this.applicationContext = applicationContext; 074 } 075 076 /** 077 * Set the pooling support for the Geronimo Connection Manager. 078 * Geronimo provides two kinds of pool: single and partitioned. 079 * 080 * @see org.apache.geronimo.connector.outbound.connectionmanagerconfig.SinglePool 081 * @see org.apache.geronimo.connector.outbound.connectionmanagerconfig.PartitionedPool 082 */ 083 public void setPoolingSupport(PoolingSupport support) { 084 poolingSupport = support; 085 } 086 087 /** 088 * Set the transaction context manager for the Geronimo Connection Manager. 089 */ 090 public void setTransactionContextManager(TransactionContextManager manager) { 091 transactionContextManager = manager; 092 } 093 094 /** 095 * Set the transaction support for the Geronimo Connection Manager. 096 * Geronimo provides in this case three kinds of support like the 097 * JCA specification: no transaction, local transactions, XA transactions. 098 * 099 * @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 }