1 45 package org.exolab.jms.net.connector; 46 47 import org.exolab.jms.net.uri.URI; 48 49 50 56 final class ManagedConnectionHandle implements ManagedConnection { 57 58 61 private final ManagedConnection _connection; 62 63 66 private final ConnectionFactory _resolver; 67 68 71 private long _lastUsed = 0; 72 73 76 private volatile int _connectionCount = 0; 77 78 79 86 public ManagedConnectionHandle(ManagedConnection connection, 87 ConnectionFactory resolver) { 88 _connection = connection; 89 _resolver = resolver; 90 } 91 92 100 public void setInvocationHandler(InvocationHandler handler) 101 throws ResourceException { 102 _connection.setInvocationHandler(handler); 103 } 104 105 111 public void setConnectionEventListener(ManagedConnectionListener listener) 112 throws ResourceException { 113 _connection.setConnectionEventListener(listener); 114 } 115 116 124 public Connection getConnection() throws ResourceException { 125 Connection connection = _connection.getConnection(); 126 return new ConnectionHandle(connection); 127 } 128 129 134 public boolean isAlive() { 135 return _connection.isAlive(); 136 } 137 138 144 public URI getRemoteURI() throws ResourceException { 145 return _connection.getRemoteURI(); 146 } 147 148 154 public URI getLocalURI() throws ResourceException { 155 return _connection.getLocalURI(); 156 } 157 158 163 public void destroy() throws ResourceException { 164 _connection.destroy(); 165 } 166 167 175 public synchronized long getLastUsedTimestamp() { 176 return _lastUsed; 177 } 178 179 182 public synchronized void updateLastUsedTimestamp() { 183 _lastUsed = System.currentTimeMillis(); 184 } 185 186 192 public boolean canDestroy() { 193 return (_connectionCount == 0); 194 } 195 196 199 private void incActiveConnections() { 200 ++_connectionCount; 201 } 202 203 206 private void decActiveConnections() { 207 --_connectionCount; 208 } 209 210 215 private class ConnectionHandle implements Connection { 216 217 220 private final Connection _connection; 221 222 227 public ConnectionHandle(Connection connection) { 228 _connection = connection; 229 incActiveConnections(); 230 } 231 232 239 public Response invoke(Request request) throws Throwable { 240 Response response = null; 241 try { 242 ConnectionContext.push(_resolver, this); 243 response = _connection.invoke(request); 244 } finally { 245 updateLastUsedTimestamp(); 246 ConnectionContext.pop(); 247 } 248 return response; 249 } 250 251 257 public URI getRemoteURI() throws ResourceException { 258 return _connection.getRemoteURI(); 259 } 260 261 267 public URI getLocalURI() throws ResourceException { 268 return _connection.getLocalURI(); 269 } 270 271 275 protected void finalize() { 276 decActiveConnections(); 277 } 278 } 279 } 280 | Popular Tags |