KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractModule;
19 import com.google.inject.TypeLiteral;
20 import com.google.inject.Provider;
21
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletRequest JavaDoc;
26 import javax.servlet.ServletResponse JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
30
31 import org.directwebremoting.ScriptSession;
32 import org.directwebremoting.ServerContext;
33 import org.directwebremoting.ServerContextFactory;
34 import org.directwebremoting.WebContext;
35 import org.directwebremoting.WebContextFactory;
36 import static org.directwebremoting.guice.DwrScopes.*;
37 import static org.directwebremoting.guice.DwrGuiceUtil.getServletContext;
38
39 /**
40  * Configures DWR scopes and creates bindings for commonly
41  * used objects related to those scopes: request, response,
42  * script session, session, servlet context, and web context.
43  * <p>
44  * Since Guice's ServletModule makes its own bindings for requests,
45  * responses, and sessions, this class has a constructor that lets
46  * the user specify whether to avoid conflicts by not binding these types.
47  * </p>
48  * @author Tim Peierls [tim at peierls dot net]
49  */

50 class DwrGuiceServletModule extends AbstractModule
51 {
52     /**
53      * Creates a module to configure DWR scopes and bindings;
54      * conflicts with the bindings provided by Guice's {@link ServletModule}.
55      * <p>
56      * Normally you would not use this constructor directly, but instead call
57      * {@link AbstractDwrModule#bindDwrScopes() bindDwrScopes} in your binding
58      * code.
59      * </p>
60      */

61     public DwrGuiceServletModule()
62     {
63         this.bindPotentiallyConflictingTypes = true;
64     }
65     
66     /**
67      * Creates a module to configure DWR scopes and bindings;
68      * conflicts with the bindings provided by Guice's {@link ServletModule}
69      * unless {@code bindPotentiallyConflictingTypes} is false, in which
70      * case this module will not create its own bindings for requests,
71      * responses, and sessions.
72      * <p>
73      * Normally you would not use this constructor directly, but instead call
74      * {@link AbstractDwrModule#bindDwrScopes(boolean) bindDwrScopes} in your binding
75      * code.
76      * </p>
77      */

78     public DwrGuiceServletModule(boolean bindPotentiallyConflictingTypes)
79     {
80         this.bindPotentiallyConflictingTypes = bindPotentiallyConflictingTypes;
81     }
82
83
84     protected void configure()
85     {
86         bindScope(RequestScoped.class, REQUEST);
87         bindScope(SessionScoped.class, SESSION);
88         bindScope(ScriptSessionScoped.class, SCRIPT);
89         bindScope(ApplicationScoped.class, APPLICATION);
90         bindScope(GlobalApplicationScoped.class, GLOBAL);
91         
92         if (bindPotentiallyConflictingTypes)
93         {
94             // Use DWR request and session scopes for request, response, and session.
95

96             Provider<HttpServletRequest JavaDoc> requestProvider =
97                 new Provider<HttpServletRequest JavaDoc>()
98                 {
99                     public HttpServletRequest JavaDoc get()
100                     {
101                         WebContext webcx = WebContextFactory.get();
102                         return webcx.getHttpServletRequest();
103                     }
104
105                     public String JavaDoc toString()
106                     {
107                         return "RequestProvider";
108                     }
109                 };
110             bind(HttpServletRequest JavaDoc.class).toProvider(requestProvider);
111             bind(ServletRequest JavaDoc.class).toProvider(requestProvider);
112
113             Provider<HttpServletResponse JavaDoc> responseProvider =
114                 new Provider<HttpServletResponse JavaDoc>()
115                 {
116                     public HttpServletResponse JavaDoc get()
117                     {
118                         WebContext webcx = WebContextFactory.get();
119                         return webcx.getHttpServletResponse();
120                     }
121
122                     public String JavaDoc toString()
123                     {
124                         return "ResponseProvider";
125                     }
126                 };
127             bind(HttpServletResponse JavaDoc.class).toProvider(responseProvider);
128             bind(ServletResponse JavaDoc.class).toProvider(responseProvider);
129                 
130             Provider<HttpSession JavaDoc> sessionProvider =
131                 new Provider<HttpSession JavaDoc>()
132                 {
133                     public HttpSession JavaDoc get()
134                     {
135                         WebContext webcx = WebContextFactory.get();
136                         return webcx.getSession();
137                     }
138
139                     public String JavaDoc toString()
140                     {
141                         return "SessionProvider";
142                     }
143                 };
144             bind(HttpSession JavaDoc.class).toProvider(sessionProvider);
145         }
146
147         Provider<Map JavaDoc<String JavaDoc, String JavaDoc[]>> requestParametersProvider =
148             new Provider<Map JavaDoc<String JavaDoc, String JavaDoc[]>>()
149             {
150                 @SuppressWarnings JavaDoc({"unchecked"})
151                 public Map JavaDoc<String JavaDoc, String JavaDoc[]> get()
152                 {
153                     WebContext webcx = WebContextFactory.get();
154                     return webcx.getHttpServletRequest().getParameterMap();
155                 }
156
157                 public String JavaDoc toString()
158                 {
159                     return "RequestParametersProvider";
160                 }
161             };
162         bind(new TypeLiteral<Map JavaDoc<String JavaDoc, String JavaDoc[]>>() {})
163             .annotatedWith(RequestParameters.class)
164             .toProvider(requestParametersProvider);
165
166        
167         Provider<ScriptSession> scriptSessionProvider =
168             new Provider<ScriptSession>()
169             {
170                 public ScriptSession get()
171                 {
172                     WebContext webcx = WebContextFactory.get();
173                     return webcx.getScriptSession();
174                 }
175
176                 public String JavaDoc toString()
177                 {
178                     return "ScriptSessionProvider";
179                 }
180             };
181         bind(ScriptSession.class).toProvider(scriptSessionProvider);
182         
183         Provider<ServletContext JavaDoc> servletContextProvider =
184             new Provider<ServletContext JavaDoc>()
185             {
186                 public ServletContext JavaDoc get()
187                 {
188                     // Can work even if WebContext isn't found.
189
return getServletContext();
190                 }
191
192                 public String JavaDoc toString()
193                 {
194                     return "ServletContextProvider";
195                 }
196             };
197         bind(ServletContext JavaDoc.class).toProvider(servletContextProvider);
198         
199         Provider<WebContext> webContextProvider =
200             new Provider<WebContext>()
201             {
202                 public WebContext get()
203                 {
204                     return WebContextFactory.get();
205                 }
206
207                 public String JavaDoc toString()
208                 {
209                     return "WebContextProvider";
210                 }
211             };
212         bind(WebContext.class).toProvider(webContextProvider);
213         
214         Provider<ServerContext> serverContextProvider =
215             new Provider<ServerContext>()
216             {
217                 public ServerContext get()
218                 {
219                     return ServerContextFactory.get(getServletContext());
220                 }
221
222                 public String JavaDoc toString()
223                 {
224                     return "ServerContextProvider";
225                 }
226             };
227         bind(ServerContext.class).toProvider(serverContextProvider);
228     }
229
230     
231     private final boolean bindPotentiallyConflictingTypes;
232 }
233
Popular Tags