1 /**
2 *
3 * Copyright 2004-2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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 javax.resource.ResourceException;
21 import javax.resource.spi.ConnectionManager;
22 import javax.resource.spi.ConnectionRequestInfo;
23 import javax.resource.spi.LazyAssociatableConnectionManager;
24 import javax.resource.spi.ManagedConnectionFactory;
25
26 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.PoolingSupport;
27 import org.apache.geronimo.transaction.manager.NamedXAResource;
28
29 /**
30 * @version $Rev: 393679 $ $Date: 2006-04-12 18:43:53 -0700 (Wed, 12 Apr 2006) $
31 */
32 public abstract class AbstractConnectionManager implements ConnectionManagerContainer, ConnectionManager, LazyAssociatableConnectionManager, PoolingAttributes {
33 protected final Interceptors interceptors;
34
35
36 public AbstractConnectionManager() {
37 interceptors = null;
38 }
39
40 public AbstractConnectionManager(Interceptors interceptors) {
41 this.interceptors = interceptors;
42 }
43
44 public Object createConnectionFactory(ManagedConnectionFactory mcf) throws ResourceException {
45 return mcf.createConnectionFactory(this);
46 }
47
48 public ConnectionManager getConnectionManager() {
49 return this;
50 }
51
52 /**
53 * in: mcf != null, is a deployed mcf
54 * out: useable connection object.
55 */
56 public Object allocateConnection(ManagedConnectionFactory managedConnectionFactory,
57 ConnectionRequestInfo connectionRequestInfo)
58 throws ResourceException {
59 ManagedConnectionInfo mci = new ManagedConnectionInfo(managedConnectionFactory, connectionRequestInfo);
60 ConnectionInfo ci = new ConnectionInfo(mci);
61 getStack().getConnection(ci);
62 return ci.getConnectionHandle();
63 }
64
65 /**
66 * in: non-null connection object, from non-null mcf.
67 * connection object is not associated with a managed connection
68 * out: supplied connection object is assiciated with a non-null ManagedConnection from mcf.
69 */
70 public void associateConnection(Object connection,
71 ManagedConnectionFactory managedConnectionFactory,
72 ConnectionRequestInfo connectionRequestInfo)
73 throws ResourceException {
74 ManagedConnectionInfo mci = new ManagedConnectionInfo(managedConnectionFactory, connectionRequestInfo);
75 ConnectionInfo ci = new ConnectionInfo(mci);
76 ci.setConnectionHandle(connection);
77 getStack().getConnection(ci);
78 }
79
80 ConnectionInterceptor getConnectionInterceptor() {
81 return getStack();
82 }
83
84
85 public ConnectionManagerContainer.ReturnableXAResource getRecoveryXAResource(ManagedConnectionFactory managedConnectionFactory) throws ResourceException {
86 ManagedConnectionInfo mci = new ManagedConnectionInfo(managedConnectionFactory, null);
87 NamedXAResource namedXAResource = (NamedXAResource) mci.getXAResource();
88 if (namedXAResource == null) {
89
90 return null;
91 }
92 ConnectionInfo recoveryConnectionInfo = new ConnectionInfo(mci);
93 getRecoveryStack().getConnection(recoveryConnectionInfo);
94 return new ConnectionManagerContainer.ReturnableXAResource(namedXAResource, getRecoveryStack(), recoveryConnectionInfo);
95 }
96
97
98
99 public int getPartitionCount() {
100 return getPooling().getPartitionCount();
101 }
102
103 public int getPartitionMaxSize() {
104 return getPooling().getPartitionMaxSize();
105 }
106
107 public void setPartitionMaxSize(int maxSize) throws InterruptedException {
108 getPooling().setPartitionMaxSize(maxSize);
109 }
110
111 public int getPartitionMinSize() {
112 return getPooling().getPartitionMinSize();
113 }
114
115 public void setPartitionMinSize(int minSize) {
116 getPooling().setPartitionMinSize(minSize);
117 }
118
119 public int getIdleConnectionCount() {
120 return getPooling().getIdleConnectionCount();
121 }
122
123 public int getConnectionCount() {
124 return getPooling().getConnectionCount();
125 }
126
127 public int getBlockingTimeoutMilliseconds() {
128 return getPooling().getBlockingTimeoutMilliseconds();
129 }
130
131 public void setBlockingTimeoutMilliseconds(int timeoutMilliseconds) {
132 getPooling().setBlockingTimeoutMilliseconds(timeoutMilliseconds);
133 }
134
135 public int getIdleTimeoutMinutes() {
136 return getPooling().getIdleTimeoutMinutes();
137 }
138
139 public void setIdleTimeoutMinutes(int idleTimeoutMinutes) {
140 getPooling().setIdleTimeoutMinutes(idleTimeoutMinutes);
141 }
142
143 private ConnectionInterceptor getStack() {
144 return interceptors.getStack();
145 }
146
147 private ConnectionInterceptor getRecoveryStack() {
148 return interceptors.getRecoveryStack();
149 }
150
151
152 public PoolingSupport getPooling() {
153 return interceptors.getPoolingAttributes();
154 }
155
156 public interface Interceptors {
157 ConnectionInterceptor getStack();
158
159 ConnectionInterceptor getRecoveryStack();
160
161 PoolingSupport getPoolingAttributes();
162 }
163
164 public void doStart() throws Exception {
165
166 }
167
168 public void doStop() throws Exception {
169 interceptors.getStack().destroy();
170 }
171
172 public void doFail() {
173 interceptors.getStack().destroy();
174 }
175 }