KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > connector > ManagedConnectionHandle


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2004-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: ManagedConnectionHandle.java,v 1.5 2005/06/05 13:56:50 tanderson Exp $
44  */

45 package org.exolab.jms.net.connector;
46
47 import org.exolab.jms.net.uri.URI;
48
49
50 /**
51  * A handle to a {@link ManagedConnection} that tracks its utilisation.
52  *
53  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
54  * @version $Revision: 1.5 $ $Date: 2005/06/05 13:56:50 $
55  */

56 final class ManagedConnectionHandle implements ManagedConnection {
57
58     /**
59      * The connection to delegate all requests to.
60      */

61     private final ManagedConnection _connection;
62
63     /**
64      * The connection factory for resolving connections via their URI.
65      */

66     private final ConnectionFactory _resolver;
67
68     /**
69      * The time the connection was last used, in milliseconds.
70      */

71     private long _lastUsed = 0;
72
73     /**
74      * The no. of active ConnectionHandle instances.
75      */

76     private volatile int _connectionCount = 0;
77
78
79     /**
80      * Construct a new <code>ManagedConnectionHandle</code>.
81      *
82      * @param connection the connection to delegate requests to
83      * @param resolver the connection factory for resolving connections via
84      * their URI.
85      */

86     public ManagedConnectionHandle(ManagedConnection connection,
87                                    ConnectionFactory resolver) {
88         _connection = connection;
89         _resolver = resolver;
90     }
91
92     /**
93      * Registers a handler for handling invocations on objects exported via this
94      * connection. Once a handler is registered, it cannot be de-registered.
95      *
96      * @param handler the invocation handler
97      * @throws IllegalStateException if a handler is already registered
98      * @throws ResourceException for any error
99      */

100     public void setInvocationHandler(InvocationHandler handler)
101             throws ResourceException {
102         _connection.setInvocationHandler(handler);
103     }
104
105     /**
106      * Registers a connection event listener.
107      *
108      * @param listener the connection event listener
109      * @throws ResourceException for any error
110      */

111     public void setConnectionEventListener(ManagedConnectionListener listener)
112             throws ResourceException {
113         _connection.setConnectionEventListener(listener);
114     }
115
116     /**
117      * Creates a new connection handle for the underlying physical connection.
118      *
119      * @return a new connection handle
120      * @throws IllegalStateException if an <code>InvocationHandler</code> hasn't
121      * been registered
122      * @throws ResourceException for any error
123      */

124     public Connection getConnection() throws ResourceException {
125         Connection connection = _connection.getConnection();
126         return new ConnectionHandle(connection);
127     }
128
129     /**
130      * Determines if the underlying physical connection is alive.
131      *
132      * @return <code>true</code> if the connection is alive
133      */

134     public boolean isAlive() {
135         return _connection.isAlive();
136     }
137
138     /**
139      * Returns the remote address to which this is connected.
140      *
141      * @return the remote address to which this is connected
142      * @throws ResourceException for any error
143      */

144     public URI getRemoteURI() throws ResourceException {
145         return _connection.getRemoteURI();
146     }
147
148     /**
149      * Returns the local address that this connection is bound to.
150      *
151      * @return the local address that this connection is bound to
152      * @throws ResourceException for any error
153      */

154     public URI getLocalURI() throws ResourceException {
155         return _connection.getLocalURI();
156     }
157
158     /**
159      * Destroys the physical connection.
160      *
161      * @throws ResourceException for any error
162      */

163     public void destroy() throws ResourceException {
164         _connection.destroy();
165     }
166
167     /**
168      * Returns the last time the connection was used.
169      * This is updated each time an invocation is performed,
170      * via {@link #updateLastUsedTimestamp}.
171      *
172      * @return the last time the connction was used, in milliseconds, or
173      * <code>0</code> if the connection hasn't been used.
174      */

175     public synchronized long getLastUsedTimestamp() {
176         return _lastUsed;
177     }
178
179     /**
180      * Update the last used timestamp.
181      */

182     public synchronized void updateLastUsedTimestamp() {
183         _lastUsed = System.currentTimeMillis();
184     }
185
186     /**
187      * Determines if the connection can be destroyed. The connection
188      * can be destroyed if there are no associated {@link Connection} instances.
189      *
190      * @return <code>true</code> if the connection may be destroyed
191      */

192     public boolean canDestroy() {
193         return (_connectionCount == 0);
194     }
195
196     /**
197      * Increment the no. of references to this connection.
198      */

199     private void incActiveConnections() {
200         ++_connectionCount;
201     }
202
203     /**
204      * Decrement the no. of references to this connection.
205      */

206     private void decActiveConnections() {
207         --_connectionCount;
208     }
209
210     /**
211      * Helper class to monitor a {@link Connection} instance, and invoke
212      * <code>updateLastUsedTimestamp()</code> each time an invocation is
213      * performed.
214      */

215     private class ConnectionHandle implements Connection {
216
217         /**
218          * The connection to delegate all requests to.
219          */

220         private final Connection _connection;
221
222         /**
223          * Construct a new <code>ConnectionHandle</code>.
224          *
225          * @param connection the connection to delegate requests to
226          */

227         public ConnectionHandle(Connection connection) {
228             _connection = connection;
229             incActiveConnections();
230         }
231
232         /**
233          * Invoke a method on a remote object.
234          *
235          * @param request the request
236          * @return the result of the invocation
237          * @throws Throwable for any transport error
238          */

239         public Response invoke(Request request) throws Throwable JavaDoc {
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         /**
252          * Returns the remote address to which this is connected.
253          *
254          * @return the remote address to which this is connected
255          * @throws ResourceException for any error
256          */

257         public URI getRemoteURI() throws ResourceException {
258             return _connection.getRemoteURI();
259         }
260
261         /**
262          * Returns the local address that this connection is bound to.
263          *
264          * @return the local address that this connection is bound to
265          * @throws ResourceException for any error
266          */

267         public URI getLocalURI() throws ResourceException {
268             return _connection.getLocalURI();
269         }
270
271         /**
272          * Called by the garbage collector when there are no more references to
273          * the object.
274          */

275         protected void finalize() {
276             decActiveConnections();
277         }
278     }
279 }
280
Popular Tags