KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > request > RequestContextHolder


1 /*
2  * Copyright 2002-2007 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
17 package org.springframework.web.context.request;
18
19 /**
20  * Holder class to expose the web request in the form of a thread-bound
21  * {@link RequestAttributes} object.
22  *
23  * <p>Use {@link RequestContextListener} or
24  * {@link org.springframework.web.filter.RequestContextFilter} to expose
25  * the current web request. Note that
26  * {@link org.springframework.web.servlet.DispatcherServlet} and
27  * {@link org.springframework.web.portlet.DispatcherPortlet} already
28  * expose the current request by default.
29  *
30  * @author Juergen Hoeller
31  * @author Rod Johnson
32  * @since 2.0
33  * @see RequestContextListener
34  * @see org.springframework.web.filter.RequestContextFilter
35  * @see org.springframework.web.servlet.DispatcherServlet
36  * @see org.springframework.web.portlet.DispatcherPortlet
37  */

38 public abstract class RequestContextHolder {
39     
40     private static final ThreadLocal JavaDoc requestAttributesHolder = new ThreadLocal JavaDoc();
41
42     private static final ThreadLocal JavaDoc inheritableRequestAttributesHolder = new InheritableThreadLocal JavaDoc();
43
44
45     /**
46      * Reset the RequestAttributes for the current thread.
47      */

48     public static void resetRequestAttributes() {
49         requestAttributesHolder.set(null);
50         inheritableRequestAttributesHolder.set(null);
51     }
52
53     /**
54      * Bind the given RequestAttributes to the current thread,
55      * <i>not</i> exposing it as inheritable for child threads.
56      * @param attributes the RequestAttributes to expose
57      * @see #setRequestAttributes(RequestAttributes, boolean)
58      */

59     public static void setRequestAttributes(RequestAttributes attributes) {
60         setRequestAttributes(attributes, false);
61     }
62
63     /**
64      * Bind the given RequestAttributes to the current thread.
65      * @param attributes the RequestAttributes to expose
66      * @param inheritable whether to expose the RequestAttributes as inheritable
67      * for child threads (using an {@link java.lang.InheritableThreadLocal})
68      */

69     public static void setRequestAttributes(RequestAttributes attributes, boolean inheritable) {
70         if (inheritable) {
71             inheritableRequestAttributesHolder.set(attributes);
72             requestAttributesHolder.set(null);
73         }
74         else {
75             requestAttributesHolder.set(attributes);
76             inheritableRequestAttributesHolder.set(null);
77         }
78     }
79
80     /**
81      * Return the RequestAttributes currently bound to the thread.
82      * @return the RequestAttributes currently bound to the thread,
83      * or <code>null</code> if none bound
84      */

85     public static RequestAttributes getRequestAttributes() {
86         RequestAttributes attributes = (RequestAttributes) requestAttributesHolder.get();
87         if (attributes == null) {
88             attributes = (RequestAttributes) inheritableRequestAttributesHolder.get();
89         }
90         return attributes;
91     }
92
93     /**
94      * Return the RequestAttributes currently bound to the thread.
95      * @return the RequestAttributes currently bound to the thread
96      * @throws IllegalStateException if no RequestAttributes object
97      * is bound to the current thread
98      */

99     public static RequestAttributes currentRequestAttributes() throws IllegalStateException JavaDoc {
100         RequestAttributes attributes = getRequestAttributes();
101         if (attributes == null) {
102             throw new IllegalStateException JavaDoc("No thread-bound request found: " +
103                     "Are you referring to request attributes outside of an actual web request? " +
104                     "If you are actually operating within a web request and still receive this message," +
105                     "your code is probably running outside of DispatcherServlet/DispatcherPortlet: " +
106                     "In this case, use RequestContextListener or RequestContextFilter to expose the current request.");
107         }
108         return attributes;
109     }
110
111 }
112
Popular Tags