1 16 17 package org.springframework.jca.cci.connection; 18 19 import java.lang.reflect.InvocationHandler ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Proxy ; 23 24 import javax.resource.ResourceException ; 25 import javax.resource.cci.Connection ; 26 import javax.resource.cci.ConnectionFactory ; 27 28 67 public class TransactionAwareConnectionFactoryProxy extends DelegatingConnectionFactory { 68 69 73 public TransactionAwareConnectionFactoryProxy() { 74 } 75 76 80 public TransactionAwareConnectionFactoryProxy(ConnectionFactory targetConnectionFactory) { 81 setTargetConnectionFactory(targetConnectionFactory); 82 afterPropertiesSet(); 83 } 84 85 86 92 public Connection getConnection() throws ResourceException { 93 Connection con = ConnectionFactoryUtils.doGetConnection(getTargetConnectionFactory()); 94 return getTransactionAwareConnectionProxy(con, getTargetConnectionFactory()); 95 } 96 97 106 protected Connection getTransactionAwareConnectionProxy(Connection target, ConnectionFactory cf) { 107 return (Connection ) Proxy.newProxyInstance( 108 Connection .class.getClassLoader(), 109 new Class [] {Connection .class}, 110 new TransactionAwareInvocationHandler(target, cf)); 111 } 112 113 114 118 private static class TransactionAwareInvocationHandler implements InvocationHandler { 119 120 private final Connection target; 121 122 private final ConnectionFactory connectionFactory; 123 124 public TransactionAwareInvocationHandler(Connection target, ConnectionFactory cf) { 125 this.target = target; 126 this.connectionFactory = cf; 127 } 128 129 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable { 130 132 if (method.getName().equals("equals")) { 133 return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE); 135 } 136 else if (method.getName().equals("hashCode")) { 137 return new Integer (hashCode()); 139 } 140 else if (method.getName().equals("getLocalTransaction")) { 141 if (ConnectionFactoryUtils.isConnectionTransactional(this.target, this.connectionFactory)) { 142 throw new javax.resource.spi.IllegalStateException ( 143 "Local transaction handling not allowed within a managed transaction"); 144 } 145 } 146 else if (method.getName().equals("close")) { 147 ConnectionFactoryUtils.doReleaseConnection(this.target, this.connectionFactory); 149 return null; 150 } 151 152 try { 154 return method.invoke(this.target, args); 155 } 156 catch (InvocationTargetException ex) { 157 throw ex.getTargetException(); 158 } 159 } 160 } 161 162 } 163 | Popular Tags |