KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > guice > DwrScopes


1 /*
2  * Copyright 2007 Tim Peierls
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 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 JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
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 JavaDoc> REQUEST =
49         new AbstractSimpleContextScope<HttpServletRequest JavaDoc>(
50             HttpServletRequest JavaDoc.class, "DwrScopes.REQUEST")
51         {
52             public HttpServletRequest JavaDoc get()
53             {
54                 return WebContextFactory.get().getHttpServletRequest();
55             }
56             
57             public Object JavaDoc get(HttpServletRequest JavaDoc request, String JavaDoc name)
58             {
59                 return request.getAttribute(name);
60             }
61             
62             public void put(HttpServletRequest JavaDoc request, String JavaDoc name, Object JavaDoc 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 JavaDoc get(ScriptSession scriptSession, String JavaDoc name)
80             {
81                 return scriptSession.getAttribute(name);
82             }
83             
84             public void put(ScriptSession scriptSession, String JavaDoc name, Object JavaDoc 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 JavaDoc> SESSION =
98         new AbstractSimpleContextScope<HttpSession JavaDoc>(HttpSession JavaDoc.class, "DwrScopes.SESSION")
99         {
100             public HttpSession JavaDoc get()
101             {
102                 return WebContextFactory.get().getSession();
103             }
104             
105             public Object JavaDoc get(HttpSession JavaDoc session, String JavaDoc name)
106             {
107                 return session.getAttribute(name);
108             }
109             
110             public void put(HttpSession JavaDoc session, String JavaDoc name, Object JavaDoc 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 JavaDoc> 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 JavaDoc> GLOBAL =
131         new ApplicationScope("DwrScopes.GLOBAL");
132     
133     
134     static class ApplicationScope extends AbstractSimpleContextScope<ServletContext JavaDoc>
135     {
136         ApplicationScope(String JavaDoc scopeName)
137         {
138             super(ServletContext JavaDoc.class, scopeName);
139         }
140
141         public ServletContext JavaDoc get()
142         {
143             return getServletContext();
144         }
145         
146         public Object JavaDoc get(ServletContext JavaDoc servletContext, String JavaDoc 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 JavaDoc servletContext, String JavaDoc name, Object JavaDoc 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() { /* uninstantiable */ }
166
167     /**
168      * The log stream
169      */

170     private static final Logger log = Logger.getLogger(DwrScopes.class);
171 }
172
Popular Tags