KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > context > ExternalContextHolder


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.context;
17
18 import org.springframework.util.Assert;
19
20 /**
21  * Simple holder class that associates an {@link ExternalContext} instance with
22  * the current thread. The ExternalContext will be inherited by any child
23  * threads spawned by the current thread.
24  * <p>
25  * Used as a central holder for the current ExternalContext in Spring Web Flow,
26  * wherever necessary. Often used by artifacts needing access to the current
27  * application session.
28  *
29  * @see ExternalContext
30  *
31  * @author Keith Donald
32  */

33 public final class ExternalContextHolder {
34
35     private static ThreadLocal JavaDoc externalContextHolder = new InheritableThreadLocal JavaDoc();
36
37     /**
38      * Associate the given ExternalContext with the current thread.
39      * @param externalContext the current ExternalContext, or <code>null</code>
40      * to reset the thread-bound context
41      */

42     public static void setExternalContext(ExternalContext externalContext) {
43         externalContextHolder.set(externalContext);
44     }
45
46     /**
47      * Return the ExternalContext associated with the current thread, if any.
48      * @return the current ExternalContext, or <code>null</code> if none
49      */

50     public static ExternalContext getExternalContext() {
51         Assert.state(externalContextHolder.get() != null, "No external context is bound to this thread");
52         return (ExternalContext)externalContextHolder.get();
53     }
54
55     // not instantiable
56
private ExternalContextHolder() {
57     }
58 }
Popular Tags