KickJava   Java API By Example, From Geeks To Geeks.

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


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 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: ConnectionContext.java,v 1.3 2005/06/04 14:39:00 tanderson Exp $
44  */

45 package org.exolab.jms.net.connector;
46
47 import java.util.ArrayList JavaDoc;
48 import java.util.List JavaDoc;
49
50 import org.apache.commons.logging.Log;
51 import org.apache.commons.logging.LogFactory;
52
53 import org.exolab.jms.net.uri.URI;
54 import org.exolab.jms.net.uri.URIHelper;
55
56
57 /**
58  * <code>ConnectionContext</code> enables connectors to associate a {@link
59  * Connection} and {@link ConnectionFactory} with th current thread, to enable
60  * deserialized {@link org.exolab.jms.net.proxy.Proxy} instances to resolve a
61  * {@link Connection} back to the server.
62  *
63  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
64  * @version $Revision: 1.3 $ $Date: 2005/06/04 14:39:00 $
65  */

66 public final class ConnectionContext {
67
68     /**
69      * Maintains a List of {@link Context} instances on a per-thread basis.
70      */

71     private static ThreadLocal JavaDoc _contexts = new ThreadLocal JavaDoc();
72
73     /**
74      * The logger.
75      */

76     private static final Log _log = LogFactory.getLog(ConnectionContext.class);
77
78
79     /**
80      * Prevent construction of helper class.
81      */

82     private ConnectionContext() {
83     }
84
85     /**
86      * Adds the connection context for the current thread.
87      *
88      * @param factory the connection factory
89      * @param connection the connection
90      */

91     public static void push(ConnectionFactory factory, Connection connection) {
92         List JavaDoc stack = (List JavaDoc) _contexts.get();
93         if (stack == null) {
94             stack = new ArrayList JavaDoc(2);
95             _contexts.set(stack);
96         }
97         stack.add(new Context(factory, connection));
98     }
99
100     /**
101      * Removes the last-pushed context for the current thread.
102      */

103     public static void pop() {
104         List JavaDoc stack = (List JavaDoc) _contexts.get();
105         stack.remove(stack.size() - 1);
106     }
107
108     /**
109      * Returns the connection for the current thread, if its remote URI matches
110      * that supplied.
111      *
112      * @param uri the URI
113      * @return the connection for the current thread, or <code>null</code> if
114      * none is available matching the specified URI
115      */

116     public static Connection getConnection(URI uri) {
117         Connection result = null;
118         Context context = top();
119         if (context != null) {
120             Connection connection = context.getConnection();
121             if (connection != null) {
122                 try {
123                     URI remoteURI = connection.getRemoteURI();
124                     if (remoteURI.equals(uri)) {
125                         result = connection;
126                     } else {
127                         URI normalised = URIHelper.convertHostToAddress(uri);
128                         if (normalised.equals(remoteURI)) {
129                             result = connection;
130                         }
131                     }
132                 } catch (ResourceException exception) {
133                     _log.debug(exception, exception);
134                 }
135             }
136         }
137         return result;
138     }
139
140     /**
141      * Returns the connection factory for the current thread.
142      *
143      * @return the connection factory for the current thread, or
144      * <code>null</code> if no factory is set
145      */

146     public static ConnectionFactory getConnectionFactory() {
147         ConnectionFactory result = null;
148         Context context = top();
149         if (context != null) {
150             result = context.getConnectionFactory();
151         }
152         return result;
153     }
154
155     /**
156      * Returns the context at the top of the local threads stack.
157      *
158      * @return the top context, or <code>null</code> if there are no contexts
159      * associated with this thread.
160      */

161     private static Context top() {
162         Context context = null;
163         List JavaDoc stack = (List JavaDoc) _contexts.get();
164         if (stack != null && !stack.isEmpty()) {
165             context = (Context) stack.get(stack.size() - 1);
166         }
167         return context;
168     }
169
170
171     private static class Context {
172
173         /**
174          * The connection factory.
175          */

176         private final ConnectionFactory _factory;
177
178         /**
179          * The connection.
180          */

181         private final Connection _connection;
182
183
184         /**
185          * Construct a new <code>Context</code>.
186          *
187          * @param factory the connection factory
188          * @param connection the connection
189          */

190         public Context(ConnectionFactory factory, Connection connection) {
191             _factory = factory;
192             _connection = connection;
193         }
194
195         /**
196          * Returns the connection factory.
197          *
198          * @return the connection factory.
199          */

200         public ConnectionFactory getConnectionFactory() {
201             return _factory;
202         }
203
204         /**
205          * Returns the connection.
206          *
207          * @return the connection
208          */

209         public Connection getConnection() {
210             return _connection;
211         }
212
213     }
214 }
215
Popular Tags