1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.directwebremoting.guice;
17
18 import com.google.inject.Key;
19 import com.google.inject.Provider;
20 import com.google.inject.Scope;
21 import com.google.inject.util.ToStringBuilder;
22
23 import java.util.ArrayList;
24 import java.util.Enumeration;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpSession;
30 import javax.servlet.ServletContext;
31
32 import org.directwebremoting.ScriptSession;
33 import org.directwebremoting.WebContext;
34 import org.directwebremoting.WebContextFactory;
35 import org.directwebremoting.util.Logger;
36
37 import static org.directwebremoting.guice.DwrGuiceUtil.getServletContext;
38
39 /**
40 * Scopes available to DWR applications.
41 * @author Tim Peierls [tim at peierls dot net]
42 */
43 public class DwrScopes
44 {
45 /**
46 * HTTP request scope.
47 */
48 public static final ContextScope<HttpServletRequest> REQUEST =
49 new AbstractSimpleContextScope<HttpServletRequest>(
50 HttpServletRequest.class, "DwrScopes.REQUEST")
51 {
52 public HttpServletRequest get()
53 {
54 return WebContextFactory.get().getHttpServletRequest();
55 }
56
57 public Object get(HttpServletRequest request, String name)
58 {
59 return request.getAttribute(name);
60 }
61
62 public void put(HttpServletRequest request, String name, Object value)
63 {
64 request.setAttribute(name, value);
65 }
66 };
67
68 /**
69 * DWR script session scope.
70 */
71 public static final ContextScope<ScriptSession> SCRIPT =
72 new AbstractSimpleContextScope<ScriptSession>(ScriptSession.class, "DwrScopes.SCRIPT")
73 {
74 public ScriptSession get()
75 {
76 return WebContextFactory.get().getScriptSession();
77 }
78
79 public Object get(ScriptSession scriptSession, String name)
80 {
81 return scriptSession.getAttribute(name);
82 }
83
84 public void put(ScriptSession scriptSession, String name, Object value)
85 {
86 scriptSession.setAttribute(name, value);
87 }
88 };
89
90 /**
91 * HTTP session scope. The implementation uses session identity to
92 * to track which sessions are open. Since the servlet spec doesn't
93 * guarantee identity of sessions between requests, don't rely on
94 * {@code getOpenContexts()} or {@code close(session, handlers)} to
95 * work correctly for this scope.
96 */
97 public static final ContextScope<HttpSession> SESSION =
98 new AbstractSimpleContextScope<HttpSession>(HttpSession.class, "DwrScopes.SESSION")
99 {
100 public HttpSession get()
101 {
102 return WebContextFactory.get().getSession();
103 }
104
105 public Object get(HttpSession session, String name)
106 {
107 return session.getAttribute(name);
108 }
109
110 public void put(HttpSession session, String name, Object value)
111 {
112 session.setAttribute(name, value);
113 }
114 };
115
116 /**
117 * Application scope: objects in this scope <em>are</em> eagerly initialized
118 * during DWR servlet initialization, and Closeable objects in this scope are
119 * closed during DWR servlet destruction.
120 */
121 public static final ContextScope<ServletContext> APPLICATION =
122 new ApplicationScope("DwrScopes.APPLICATION");
123
124 /**
125 * Global application scope: like {@link #APPLICATION}, but objects in
126 * this scope are <em>not</em> eagerly initialized and Closeable objects
127 * in this scope are closed during servlet context destruction (not
128 * during DWR servlet destruction).
129 */
130 public static final ContextScope<ServletContext> GLOBAL =
131 new ApplicationScope("DwrScopes.GLOBAL");
132
133
134 static class ApplicationScope extends AbstractSimpleContextScope<ServletContext>
135 {
136 ApplicationScope(String scopeName)
137 {
138 super(ServletContext.class, scopeName);
139 }
140
141 public ServletContext get()
142 {
143 return getServletContext();
144 }
145
146 public Object get(ServletContext servletContext, String name)
147 {
148 if (log.isDebugEnabled())
149 {
150 log.debug(String.format("servletContext.getAttribute(%s)", name));
151 }
152 return servletContext.getAttribute(name);
153 }
154
155 public void put(ServletContext servletContext, String name, Object value)
156 {
157 if (log.isDebugEnabled())
158 {
159 log.debug(String.format("servletContext.setAttribute(%s, %s)", name, value));
160 }
161 servletContext.setAttribute(name, value);
162 }
163 };
164
165 private DwrScopes() {
166
167 /**
168 * The log stream
169 */
170 private static final Logger log = Logger.getLogger(DwrScopes.class);
171 }