1 17 18 package org.apache.geronimo.connector.outbound; 19 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.util.Iterator ; 23 24 import javax.resource.ResourceException ; 25 import javax.resource.spi.ConnectionRequestInfo ; 26 import javax.security.auth.Subject ; 27 28 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.PoolingSupport; 29 30 39 public class MultiPoolConnectionInterceptor implements ConnectionInterceptor, PoolingAttributes{ 40 41 private final ConnectionInterceptor next; 42 private final PoolingSupport singlePoolFactory; 43 44 private final boolean useSubject; 45 46 private final boolean useCRI; 47 48 private final Map pools = new HashMap (); 49 50 private volatile boolean destroyed = false; 52 53 public MultiPoolConnectionInterceptor( 54 final ConnectionInterceptor next, 55 PoolingSupport singlePoolFactory, 56 final boolean useSubject, 57 final boolean useCRI) { 58 this.next = next; 59 this.singlePoolFactory = singlePoolFactory; 60 this.useSubject = useSubject; 61 this.useCRI = useCRI; 62 } 63 64 public void getConnection(ConnectionInfo connectionInfo) throws ResourceException { 65 ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo(); 66 SubjectCRIKey key = 67 new SubjectCRIKey( 68 useSubject ? mci.getSubject() : null, 69 useCRI ? mci.getConnectionRequestInfo() : null); 70 ConnectionInterceptor poolInterceptor = null; 71 synchronized (pools) { 72 if (destroyed) { 73 throw new ResourceException ("ConnectionManaged has been destroyed"); 74 } 75 poolInterceptor = (ConnectionInterceptor) pools.get(key); 76 if (poolInterceptor == null) { 77 poolInterceptor = singlePoolFactory.addPoolingInterceptors(next); 78 pools.put(key, poolInterceptor); 79 } 80 } 81 mci.setPoolInterceptor(poolInterceptor); 82 poolInterceptor.getConnection(connectionInfo); 83 } 84 85 public void returnConnection( 87 ConnectionInfo connectionInfo, 88 ConnectionReturnAction connectionReturnAction) { 89 ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo(); 90 ConnectionInterceptor poolInterceptor = mci.getPoolInterceptor(); 91 poolInterceptor.returnConnection(connectionInfo, connectionReturnAction); 92 } 93 94 public void destroy() { 95 synchronized (pools) { 96 destroyed = true; 97 for (Iterator it = pools.entrySet().iterator(); it.hasNext(); ) { 98 ((ConnectionInterceptor)((Map.Entry )it.next()).getValue()).destroy(); 99 it.remove(); 100 } 101 } 102 next.destroy(); 103 } 104 105 public int getPartitionCount() { 106 return pools.size(); 107 } 108 109 public int getPartitionMaxSize() { 110 return singlePoolFactory.getPartitionMaxSize(); 111 } 112 113 public void setPartitionMaxSize(int maxSize) throws InterruptedException { 114 singlePoolFactory.setPartitionMaxSize(maxSize); 115 for (Iterator iterator = pools.entrySet().iterator(); iterator.hasNext();) { 116 PoolingAttributes poolingAttributes = (PoolingAttributes) ((Map.Entry ) iterator.next()).getValue(); 117 poolingAttributes.setPartitionMaxSize(maxSize); 118 } 119 } 120 121 public int getPartitionMinSize() { 122 return singlePoolFactory.getPartitionMinSize(); 123 } 124 125 public void setPartitionMinSize(int minSize) { 126 singlePoolFactory.setPartitionMinSize(minSize); 127 for (Iterator iterator = pools.entrySet().iterator(); iterator.hasNext();) { 128 PoolingAttributes poolingAttributes = (PoolingAttributes) ((Map.Entry ) iterator.next()).getValue(); 129 poolingAttributes.setPartitionMinSize(minSize); 130 } 131 } 132 133 public int getIdleConnectionCount() { 134 int count = 0; 135 for (Iterator iterator = pools.entrySet().iterator(); iterator.hasNext();) { 136 PoolingAttributes poolingAttributes = (PoolingAttributes) ((Map.Entry ) iterator.next()).getValue(); 137 count += poolingAttributes.getIdleConnectionCount(); 138 } 139 return count; 140 } 141 142 public int getConnectionCount() { 143 int count = 0; 144 for (Iterator iterator = pools.entrySet().iterator(); iterator.hasNext();) { 145 PoolingAttributes poolingAttributes = (PoolingAttributes) ((Map.Entry ) iterator.next()).getValue(); 146 count += poolingAttributes.getConnectionCount(); 147 } 148 return count; 149 } 150 151 public int getBlockingTimeoutMilliseconds() { 152 return singlePoolFactory.getBlockingTimeoutMilliseconds(); 153 } 154 155 public void setBlockingTimeoutMilliseconds(int timeoutMilliseconds) { 156 singlePoolFactory.setBlockingTimeoutMilliseconds(timeoutMilliseconds); 157 for (Iterator iterator = pools.entrySet().iterator(); iterator.hasNext();) { 158 PoolingAttributes poolingAttributes = (PoolingAttributes) ((Map.Entry ) iterator.next()).getValue(); 159 poolingAttributes.setBlockingTimeoutMilliseconds(timeoutMilliseconds); 160 } 161 } 162 163 public int getIdleTimeoutMinutes() { 164 return singlePoolFactory.getIdleTimeoutMinutes(); 165 } 166 167 public void setIdleTimeoutMinutes(int idleTimeoutMinutes) { 168 singlePoolFactory.setIdleTimeoutMinutes(idleTimeoutMinutes); 169 for (Iterator iterator = pools.entrySet().iterator(); iterator.hasNext();) { 170 PoolingAttributes poolingAttributes = (PoolingAttributes) ((Map.Entry ) iterator.next()).getValue(); 171 poolingAttributes.setIdleTimeoutMinutes(idleTimeoutMinutes); 172 } 173 } 174 175 static class SubjectCRIKey { 176 private final Subject subject; 177 private final ConnectionRequestInfo cri; 178 private final int hashcode; 179 180 public SubjectCRIKey( 181 final Subject subject, 182 final ConnectionRequestInfo cri) { 183 this.subject = subject; 184 this.cri = cri; 185 this.hashcode = 186 (subject == null ? 17 : subject.hashCode() * 17) 187 ^ (cri == null ? 1 : cri.hashCode()); 188 } 189 190 public int hashCode() { 191 return hashcode; 192 } 193 194 public boolean equals(Object other) { 195 if (!(other instanceof SubjectCRIKey)) { 196 return false; 197 } SubjectCRIKey o = (SubjectCRIKey) other; 199 if (hashcode != o.hashcode) { 200 return false; 201 } return subject == null 203 ? o.subject == null 204 : subject.equals(o.subject) 205 && cri == null ? o.cri == null : cri.equals(o.cri); 206 } 207 } 208 } | Popular Tags |