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: ConnectionContext.java,v 1.5 2005/12/01 13:44:38 tanderson Exp $ 44 */ 45 package org.exolab.jms.net.connector; 46 47 import java.security.Principal; 48 import java.util.ArrayList; 49 import java.util.List; 50 51 import org.apache.commons.logging.Log; 52 import org.apache.commons.logging.LogFactory; 53 54 import org.exolab.jms.net.uri.URI; 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.5 $ $Date: 2005/12/01 13:44:38 $ 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 _contexts = new ThreadLocal(); 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 principal the security principal 89 * @param factory the connection factory 90 */ 91 public static void push(Principal principal, ConnectionFactory factory) { 92 List stack = (List) _contexts.get(); 93 if (stack == null) { 94 stack = new ArrayList(2); 95 _contexts.set(stack); 96 } 97 stack.add(new Context(principal, factory)); 98 } 99 100 /*** 101 * Removes the last-pushed context for the current thread. 102 */ 103 public static void pop() { 104 List stack = (List) _contexts.get(); 105 stack.remove(stack.size() - 1); 106 } 107 108 /*** 109 * Returns a connection using the principal and connection factory 110 * associated with the current thread. 111 * 112 * @param uri the URI 113 * @return a new connection, or <code>null</code> if a connection can't 114 * be established 115 */ 116 public static Connection getConnection(URI uri) { 117 Connection result = null; 118 Context context = top(); 119 if (context != null) { 120 ConnectionFactory factory = context.getConnectionFactory(); 121 try { 122 result = factory.getConnection(context.getPrincipal(), uri); 123 } catch (ResourceException exception) { 124 _log.debug(exception, exception); 125 } 126 } 127 return result; 128 } 129 130 /*** 131 * Returns the connection factory for the current thread. 132 * 133 * @return the connection factory for the current thread, or 134 * <code>null</code> if no factory is set 135 */ 136 public static ConnectionFactory getConnectionFactory() { 137 ConnectionFactory result = null; 138 Context context = top(); 139 if (context != null) { 140 result = context.getConnectionFactory(); 141 } 142 return result; 143 } 144 145 /*** 146 * Returns the context at the top of the local threads stack. 147 * 148 * @return the top context, or <code>null</code> if there are no contexts 149 * associated with this thread. 150 */ 151 private static Context top() { 152 Context context = null; 153 List stack = (List) _contexts.get(); 154 if (stack != null && !stack.isEmpty()) { 155 context = (Context) stack.get(stack.size() - 1); 156 } 157 return context; 158 } 159 160 161 private static class Context { 162 163 /*** 164 * The security principal. 165 */ 166 private final Principal _principal; 167 168 /*** 169 * The connection factory. 170 */ 171 private final ConnectionFactory _factory; 172 173 174 /*** 175 * Construct a new <code>Context</code>. 176 * 177 * @param principal the security principal 178 * @param factory the connection factory 179 */ 180 public Context(Principal principal, ConnectionFactory factory) { 181 _principal = principal; 182 _factory = factory; 183 } 184 185 /*** 186 * Returns the security principal. 187 * 188 * @return the security principal 189 */ 190 public Principal getPrincipal() { 191 return _principal; 192 } 193 194 /*** 195 * Returns the connection factory. 196 * 197 * @return the connection factory. 198 */ 199 public ConnectionFactory getConnectionFactory() { 200 return _factory; 201 } 202 203 } 204 }