|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| SecurityContextHolder.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 1 | /* Copyright 2004, 2005 Acegi Technology Pty Limited | |
| 2 | * | |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 | * you may not use this file except in compliance with the License. | |
| 5 | * You may obtain a copy of the License at | |
| 6 | * | |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | * | |
| 9 | * Unless required by applicable law or agreed to in writing, software | |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 | * See the License for the specific language governing permissions and | |
| 13 | * limitations under the License. | |
| 14 | */ | |
| 15 | ||
| 16 | package org.acegisecurity.context; | |
| 17 | ||
| 18 | import org.springframework.util.Assert; | |
| 19 | ||
| 20 | ||
| 21 | /** | |
| 22 | * Associates a given {@link SecurityContext} with the current execution | |
| 23 | * thread and any new threads the current execution thread may spawn. | |
| 24 | * | |
| 25 | * <p> | |
| 26 | * To guarantee that {@link #getContext()} never returns <code>null</code>, this | |
| 27 | * class defaults to returning <code>SecurityContextImpl</code> if no | |
| 28 | * <code>SecurityContext</code> has ever been associated with the current | |
| 29 | * thread of execution. Despite this behaviour, in general another class will | |
| 30 | * select the concrete <code>SecurityContext</code> implementation to use and | |
| 31 | * expressly set an instance of that implementation against the | |
| 32 | * <code>SecurityContextHolder</code>. | |
| 33 | * </p> | |
| 34 | * | |
| 35 | * @author Ben Alex | |
| 36 | * @version $Id: SecurityContextHolder.java,v 1.4 2005/11/17 00:55:49 benalex Exp $ | |
| 37 | * | |
| 38 | * @see java.lang.ThreadLocal | |
| 39 | * @see org.acegisecurity.context.HttpSessionContextIntegrationFilter | |
| 40 | */ | |
| 41 | public class SecurityContextHolder { | |
| 42 | //~ Static fields/initializers ============================================= | |
| 43 | ||
| 44 | private static ThreadLocal contextHolder = new ThreadLocal(); | |
| 45 | ||
| 46 | //~ Methods ================================================================ | |
| 47 | ||
| 48 | /** | |
| 49 | * Associates a new <code>SecurityContext</code> with the current thread of | |
| 50 | * execution. | |
| 51 | * | |
| 52 | * @param context the new <code>SecurityContext</code> (may not be | |
| 53 | * <code>null</code>) | |
| 54 | */ | |
| 55 | 190 | public static void setContext(SecurityContext context) { |
| 56 | 190 | Assert.notNull(context, |
| 57 | "Only non-null SecurityContext instances are permitted"); | |
| 58 | 189 | contextHolder.set(context); |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * Obtains the <code>SecurityContext</code> associated with the current | |
| 63 | * thread of execution. If no <code>SecurityContext</code> has been | |
| 64 | * associated with the current thread of execution, a new instance of | |
| 65 | * {@link SecurityContextImpl} is associated with the current thread and | |
| 66 | * then returned. | |
| 67 | * | |
| 68 | * @return the current <code>SecurityContext</code> (guaranteed to never be | |
| 69 | * <code>null</code>) | |
| 70 | */ | |
| 71 | 414 | public static SecurityContext getContext() { |
| 72 | 414 | if (contextHolder.get() == null) { |
| 73 | 17 | contextHolder.set(new SecurityContextImpl()); |
| 74 | } | |
| 75 | ||
| 76 | 414 | return (SecurityContext) contextHolder.get(); |
| 77 | } | |
| 78 | } |
|
||||||||||