001: /*
002: * Copyright 2007 Tim Peierls
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.guice;
017:
018: import java.util.Map;
019:
020: import javax.servlet.ServletContext;
021: import javax.servlet.ServletRequest;
022: import javax.servlet.ServletResponse;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025: import javax.servlet.http.HttpSession;
026:
027: import org.directwebremoting.ScriptSession;
028: import org.directwebremoting.ServerContext;
029: import org.directwebremoting.ServerContextFactory;
030: import org.directwebremoting.WebContext;
031: import org.directwebremoting.WebContextFactory;
032:
033: import com.google.inject.AbstractModule;
034: import com.google.inject.Provider;
035: import com.google.inject.TypeLiteral;
036:
037: import static org.directwebremoting.guice.DwrGuiceUtil.getServletContext;
038: import static org.directwebremoting.guice.DwrScopes.APPLICATION;
039: import static org.directwebremoting.guice.DwrScopes.GLOBAL;
040: import static org.directwebremoting.guice.DwrScopes.REQUEST;
041: import static org.directwebremoting.guice.DwrScopes.SCRIPT;
042: import static org.directwebremoting.guice.DwrScopes.SESSION;
043:
044: /**
045: * Configures DWR scopes and creates bindings for commonly
046: * used objects related to those scopes: request, response,
047: * script session, session, servlet context, and web context.
048: * <p>
049: * Since Guice's ServletModule makes its own bindings for requests,
050: * responses, and sessions, this class has a constructor that lets
051: * the user specify whether to avoid conflicts by not binding these types.
052: * </p>
053: * @author Tim Peierls [tim at peierls dot net]
054: */
055: class DwrGuiceServletModule extends AbstractModule {
056: /**
057: * Creates a module to configure DWR scopes and bindings;
058: * conflicts with the bindings provided by Guice's ServletModule.
059: * <p>
060: * Normally you would not use this constructor directly, but instead call
061: * {@link AbstractDwrModule#bindDwrScopes() bindDwrScopes} in your binding
062: * code.
063: * </p>
064: */
065: public DwrGuiceServletModule() {
066: this .bindPotentiallyConflictingTypes = true;
067: }
068:
069: /**
070: * Creates a module to configure DWR scopes and bindings;
071: * conflicts with the bindings provided by Guice's ServletModule
072: * unless {@code bindPotentiallyConflictingTypes} is false, in which
073: * case this module will not create its own bindings for requests,
074: * responses, and sessions.
075: * <p>
076: * Normally you would not use this constructor directly, but instead call
077: * {@link AbstractDwrModule#bindDwrScopes(boolean) bindDwrScopes} in your binding
078: * code.
079: * </p>
080: */
081: public DwrGuiceServletModule(boolean bindPotentiallyConflictingTypes) {
082: this .bindPotentiallyConflictingTypes = bindPotentiallyConflictingTypes;
083: }
084:
085: @Override
086: protected void configure() {
087: bindScope(RequestScoped.class, REQUEST);
088: bindScope(SessionScoped.class, SESSION);
089: bindScope(ScriptSessionScoped.class, SCRIPT);
090: bindScope(ApplicationScoped.class, APPLICATION);
091: bindScope(GlobalApplicationScoped.class, GLOBAL);
092:
093: if (bindPotentiallyConflictingTypes) {
094: // Use DWR request and session scopes for request, response, and session.
095:
096: Provider<HttpServletRequest> requestProvider = new Provider<HttpServletRequest>() {
097: public HttpServletRequest get() {
098: WebContext webcx = WebContextFactory.get();
099: return webcx.getHttpServletRequest();
100: }
101:
102: @Override
103: public String toString() {
104: return "RequestProvider";
105: }
106: };
107: bind(HttpServletRequest.class).toProvider(requestProvider);
108: bind(ServletRequest.class).toProvider(requestProvider);
109:
110: Provider<HttpServletResponse> responseProvider = new Provider<HttpServletResponse>() {
111: public HttpServletResponse get() {
112: WebContext webcx = WebContextFactory.get();
113: return webcx.getHttpServletResponse();
114: }
115:
116: @Override
117: public String toString() {
118: return "ResponseProvider";
119: }
120: };
121: bind(HttpServletResponse.class)
122: .toProvider(responseProvider);
123: bind(ServletResponse.class).toProvider(responseProvider);
124:
125: Provider<HttpSession> sessionProvider = new Provider<HttpSession>() {
126: public HttpSession get() {
127: WebContext webcx = WebContextFactory.get();
128: return webcx.getSession();
129: }
130:
131: @Override
132: public String toString() {
133: return "SessionProvider";
134: }
135: };
136: bind(HttpSession.class).toProvider(sessionProvider);
137: }
138:
139: Provider<Map<String, String[]>> requestParametersProvider = new Provider<Map<String, String[]>>() {
140: @SuppressWarnings({"unchecked"})
141: public Map<String, String[]> get() {
142: WebContext webcx = WebContextFactory.get();
143: return webcx.getHttpServletRequest().getParameterMap();
144: }
145:
146: @Override
147: public String toString() {
148: return "RequestParametersProvider";
149: }
150: };
151: bind(new TypeLiteral<Map<String, String[]>>() {
152: }).annotatedWith(RequestParameters.class).toProvider(
153: requestParametersProvider);
154:
155: Provider<ScriptSession> scriptSessionProvider = new Provider<ScriptSession>() {
156: public ScriptSession get() {
157: WebContext webcx = WebContextFactory.get();
158: return webcx.getScriptSession();
159: }
160:
161: @Override
162: public String toString() {
163: return "ScriptSessionProvider";
164: }
165: };
166: bind(ScriptSession.class).toProvider(scriptSessionProvider);
167:
168: Provider<ServletContext> servletContextProvider = new Provider<ServletContext>() {
169: public ServletContext get() {
170: // Can work even if WebContext isn't found.
171: return getServletContext();
172: }
173:
174: @Override
175: public String toString() {
176: return "ServletContextProvider";
177: }
178: };
179: bind(ServletContext.class).toProvider(servletContextProvider);
180:
181: Provider<WebContext> webContextProvider = new Provider<WebContext>() {
182: public WebContext get() {
183: return WebContextFactory.get();
184: }
185:
186: @Override
187: public String toString() {
188: return "WebContextProvider";
189: }
190: };
191: bind(WebContext.class).toProvider(webContextProvider);
192:
193: Provider<ServerContext> serverContextProvider = new Provider<ServerContext>() {
194: public ServerContext get() {
195: return ServerContextFactory.get(getServletContext());
196: }
197:
198: @Override
199: public String toString() {
200: return "ServerContextProvider";
201: }
202: };
203: bind(ServerContext.class).toProvider(serverContextProvider);
204: }
205:
206: private final boolean bindPotentiallyConflictingTypes;
207: }
|