01: /* RequestContexts.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Fri Apr 8 12:16:23 2005, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.web.servlet.xel;
20:
21: import java.util.List;
22: import java.util.LinkedList;
23:
24: /**
25: * RequestContexts maintains a stack of {@link RequestContext} to simplify
26: * the signatures of the XEL function.
27: *
28: * <p>It is designed to make the signature of XEL functions
29: * (see {@link org.zkoss.web.fn.ServletFns}) simpler.
30: * For example, {@link org.zkoss.web.fn.ServletFns#isExplorer} requires
31: * no argument, since it assumes the current context can be retrieved
32: * from {@link #getCurrent}.
33: *
34: * <p>Spec Issue:<br/>
35: * It is controversial whether the introduction of {@link RequestContext} and
36: * {@link RequestContexts} is worth. However, we have to maintain the backward
37: * compatibility of the XEL/EL function signatures.
38: *
39: * @author tomyeh
40: * @since 3.0.0
41: */
42: public class RequestContexts {
43: protected RequestContexts() {
44: } //prevent from instantiated
45:
46: /** A list of RequestContext. */
47: private static final ThreadLocal _elCtxs = new ThreadLocal();
48:
49: /** Returns the current page context if this thread is evaluating a page,
50: * or null if not.
51: */
52: public static final RequestContext getCurrent() {
53: final List jcs = (List) _elCtxs.get();
54: return jcs != null && !jcs.isEmpty() ? (RequestContext) jcs
55: .get(0) : null;
56: }
57:
58: /** Pushes the context as the current context, such that it will
59: * be returned by {@link #getCurrent}. The reason this method exists is
60: * many functions ({@link org.zkoss.web.fn.ServletFns}) counts on it.
61: *
62: * <p>However, you don't need to invoke this method if you are using
63: * DSP.
64: * <ol>
65: * <li>If go thru DSP, it is done automatically
66: * (by {@link org.zkoss.web.servlet.dsp.Interpreter}</li>
67: * </ol>
68: *
69: * <p>Note: you must use try/finally as follows:
70: * <pre><code>RequestContexts.push(jc);
71: *try {
72: * ...
73: *} finally {
74: * RequestContexts.pop();
75: *}</code></pre>
76: */
77: public static final void push(RequestContext jc) {
78: if (jc == null)
79: throw new IllegalArgumentException("null");
80:
81: List jcs = (List) _elCtxs.get();
82: if (jcs == null)
83: _elCtxs.set(jcs = new LinkedList());
84: jcs.add(0, jc);
85: }
86:
87: /** Pops the context out and use the previous one as the current context.
88: *
89: * <p>However, you don't need to invoke this method if you are using
90: * DSP.
91: *
92: * @see #push
93: */
94: public static final void pop() {
95: ((List) _elCtxs.get()).remove(0);
96: }
97: }
|