KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > outbound > MultiPoolConnectionInterceptor


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.connector.outbound;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import javax.resource.ResourceException JavaDoc;
25 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
26 import javax.security.auth.Subject JavaDoc;
27
28 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.PoolingSupport;
29
30 /**
31  * MultiPoolConnectionInterceptor maps the provided subject and connection request info to a
32  * "SinglePool". This can be used to make sure all matches will succeed, avoiding synchronization
33  * slowdowns.
34  *
35  * Created: Fri Oct 10 12:53:11 2003
36  *
37  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
38  */

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 JavaDoc pools = new HashMap JavaDoc();
49
50     // volatile is not necessary, here, because of synchronization. but maintained for consistency with other Interceptors...
51
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 JavaDoc {
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 JavaDoc("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     // let underlying pools handle destroyed processing...
86
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 JavaDoc it = pools.entrySet().iterator(); it.hasNext(); ) {
98                 ((ConnectionInterceptor)((Map.Entry JavaDoc)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 JavaDoc {
114         singlePoolFactory.setPartitionMaxSize(maxSize);
115         for (Iterator JavaDoc iterator = pools.entrySet().iterator(); iterator.hasNext();) {
116             PoolingAttributes poolingAttributes = (PoolingAttributes) ((Map.Entry JavaDoc) 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 JavaDoc iterator = pools.entrySet().iterator(); iterator.hasNext();) {
128             PoolingAttributes poolingAttributes = (PoolingAttributes) ((Map.Entry JavaDoc) iterator.next()).getValue();
129             poolingAttributes.setPartitionMinSize(minSize);
130         }
131     }
132
133     public int getIdleConnectionCount() {
134         int count = 0;
135         for (Iterator JavaDoc iterator = pools.entrySet().iterator(); iterator.hasNext();) {
136             PoolingAttributes poolingAttributes = (PoolingAttributes) ((Map.Entry JavaDoc) iterator.next()).getValue();
137             count += poolingAttributes.getIdleConnectionCount();
138         }
139         return count;
140     }
141
142     public int getConnectionCount() {
143         int count = 0;
144         for (Iterator JavaDoc iterator = pools.entrySet().iterator(); iterator.hasNext();) {
145             PoolingAttributes poolingAttributes = (PoolingAttributes) ((Map.Entry JavaDoc) 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 JavaDoc iterator = pools.entrySet().iterator(); iterator.hasNext();) {
158             PoolingAttributes poolingAttributes = (PoolingAttributes) ((Map.Entry JavaDoc) 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 JavaDoc iterator = pools.entrySet().iterator(); iterator.hasNext();) {
170             PoolingAttributes poolingAttributes = (PoolingAttributes) ((Map.Entry JavaDoc) iterator.next()).getValue();
171             poolingAttributes.setIdleTimeoutMinutes(idleTimeoutMinutes);
172         }
173     }
174
175     static class SubjectCRIKey {
176         private final Subject JavaDoc subject;
177         private final ConnectionRequestInfo JavaDoc cri;
178         private final int hashcode;
179
180         public SubjectCRIKey(
181                 final Subject JavaDoc subject,
182                 final ConnectionRequestInfo JavaDoc 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 JavaDoc other) {
195             if (!(other instanceof SubjectCRIKey)) {
196                 return false;
197             } // end of if ()
198
SubjectCRIKey o = (SubjectCRIKey) other;
199             if (hashcode != o.hashcode) {
200                 return false;
201             } // end of if ()
202
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 } // MultiPoolConnectionInterceptor
209
Popular Tags