View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2010 Frederico Caldeira Knabben
4    * 
5    * == BEGIN LICENSE ==
6    * 
7    * Licensed under the terms of any of the following licenses at your
8    * choice:
9    * 
10   *  - GNU General Public License Version 2 or later (the "GPL")
11   *    http://www.gnu.org/licenses/gpl.html
12   * 
13   *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   *    http://www.gnu.org/licenses/lgpl.html
15   * 
16   *  - Mozilla Public License Version 1.1 or later (the "MPL")
17   *    http://www.mozilla.org/MPL/MPL-1.1.html
18   * 
19   * == END LICENSE ==
20   */
21  package net.fckeditor.requestcycle;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  /**
26   * Maintains current {@link Context context} and {@link HttpServletRequest
27   * request} instances. This container relies on {@link ThreadLocal} and provides
28   * static access to the aforementioned objects for the current File Browse
29   * request. This means that this class is thread-safe.
30   * 
31   * @version $Id: ThreadLocalData.java 4785 2009-12-21 20:10:28Z mosipov $
32   */
33  public class ThreadLocalData {
34  	private static ThreadLocal<HttpServletRequest> request = new ThreadLocal<HttpServletRequest>();
35  	private static ThreadLocal<Context> context = new ThreadLocal<Context>();
36  	
37  	/**
38  	 * Initializes the current request cycle.
39  	 * 
40  	 * @param request
41  	 *            current user request instance
42  	 */
43  	public static void beginRequest(final HttpServletRequest request) {
44  		if (request == null)
45  			throw new NullPointerException("the request cannot be null");
46  		ThreadLocalData.request.set(request);
47  		ThreadLocalData.context.set(new Context(request));
48  	}
49  	
50  	/**
51  	 * Returns the current user request instance.
52  	 * 
53  	 * @return the current user request instance
54  	 */
55  	public static HttpServletRequest getRequest() {
56  		return request.get();
57  	}
58  
59  	/**
60  	 * Returns the current context instance.
61  	 * 
62  	 * @return the current context instance
63  	 */
64  	public static Context getContext() {
65  		return context.get();
66  	}
67  	
68  	/**
69  	 * Terminates the current request cycle. <br />
70  	 * <strong>Important: To prevent memory leaks, make sure that this
71  	 * method is called at the end of the current request cycle!</strong>
72  	 */
73  	public static void endRequest() {
74  		// ThreadLocal#remove is not available in Java 1.4
75  		request.set(null);
76  		context.set(null);
77  	}
78  }