01: /*
02: * Copyright 2004-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 not 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 final ThreadLocal externalContextHolder = new ThreadLocal();
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(
43: ExternalContext externalContext) {
44: externalContextHolder.set(externalContext);
45: }
46:
47: /**
48: * Return the ExternalContext associated with the current thread, if any.
49: * @return the current ExternalContext
50: * @throws IllegalStateException if no ExternalContext is bound to this thread
51: */
52: public static ExternalContext getExternalContext() {
53: Assert.state(externalContextHolder.get() != null,
54: "No external context is bound to this thread");
55: return (ExternalContext) externalContextHolder.get();
56: }
57:
58: // not instantiable
59: private ExternalContextHolder() {
60: }
61: }
|