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 javax.servlet.ServletContext;
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpSession;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.directwebremoting.ScriptSession;
025: import org.directwebremoting.WebContextFactory;
026:
027: import static org.directwebremoting.guice.DwrGuiceUtil.getServletContext;
028:
029: /**
030: * Scopes available to DWR applications.
031: * @author Tim Peierls [tim at peierls dot net]
032: */
033: public class DwrScopes {
034: /**
035: * HTTP request scope.
036: */
037: public static final ContextScope<HttpServletRequest> REQUEST = new AbstractSimpleContextScope<HttpServletRequest>(
038: HttpServletRequest.class, "DwrScopes.REQUEST") {
039: @Override
040: public HttpServletRequest get() {
041: return WebContextFactory.get().getHttpServletRequest();
042: }
043:
044: @Override
045: public Object get(HttpServletRequest request, String name) {
046: return request.getAttribute(name);
047: }
048:
049: @Override
050: public void put(HttpServletRequest request, String name,
051: Object value) {
052: request.setAttribute(name, value);
053: }
054: };
055:
056: /**
057: * DWR script session scope.
058: */
059: public static final ContextScope<ScriptSession> SCRIPT = new AbstractSimpleContextScope<ScriptSession>(
060: ScriptSession.class, "DwrScopes.SCRIPT") {
061: @Override
062: public ScriptSession get() {
063: return WebContextFactory.get().getScriptSession();
064: }
065:
066: @Override
067: public Object get(ScriptSession scriptSession, String name) {
068: return scriptSession.getAttribute(name);
069: }
070:
071: @Override
072: public void put(ScriptSession scriptSession, String name,
073: Object value) {
074: scriptSession.setAttribute(name, value);
075: }
076: };
077:
078: /**
079: * HTTP session scope. The implementation uses session identity to
080: * to track which sessions are open. Since the servlet spec doesn't
081: * guarantee identity of sessions between requests, don't rely on
082: * {@code getOpenContexts()} or {@code close(session, handlers)} to
083: * work correctly for this scope.
084: */
085: public static final ContextScope<HttpSession> SESSION = new AbstractSimpleContextScope<HttpSession>(
086: HttpSession.class, "DwrScopes.SESSION") {
087: @Override
088: public HttpSession get() {
089: return WebContextFactory.get().getSession();
090: }
091:
092: @Override
093: public Object get(HttpSession session, String name) {
094: return session.getAttribute(name);
095: }
096:
097: @Override
098: public void put(HttpSession session, String name, Object value) {
099: session.setAttribute(name, value);
100: }
101: };
102:
103: /**
104: * Application scope: objects in this scope <em>are</em> eagerly initialized
105: * during DWR servlet initialization, and Closeable objects in this scope are
106: * closed during DWR servlet destruction.
107: */
108: public static final ContextScope<ServletContext> APPLICATION = new ApplicationScope(
109: "DwrScopes.APPLICATION");
110:
111: /**
112: * Global application scope: like {@link #APPLICATION}, but objects in
113: * this scope are <em>not</em> eagerly initialized and Closeable objects
114: * in this scope are closed during servlet context destruction (not
115: * during DWR servlet destruction).
116: */
117: public static final ContextScope<ServletContext> GLOBAL = new ApplicationScope(
118: "DwrScopes.GLOBAL");
119:
120: static class ApplicationScope extends
121: AbstractSimpleContextScope<ServletContext> {
122: ApplicationScope(String scopeName) {
123: super (ServletContext.class, scopeName);
124: }
125:
126: @Override
127: public ServletContext get() {
128: return getServletContext();
129: }
130:
131: @Override
132: public Object get(ServletContext servletContext, String name) {
133: if (log.isDebugEnabled()) {
134: log.debug(String.format(
135: "servletContext.getAttribute(%s)", name));
136: }
137: return servletContext.getAttribute(name);
138: }
139:
140: @Override
141: public void put(ServletContext servletContext, String name,
142: Object value) {
143: if (log.isDebugEnabled()) {
144: log.debug(String.format(
145: "servletContext.setAttribute(%s, %s)", name,
146: value));
147: }
148: servletContext.setAttribute(name, value);
149: }
150: }
151:
152: private DwrScopes() { /* uninstantiable */
153: }
154:
155: /**
156: * The log stream
157: */
158: protected static final Log log = LogFactory.getLog(DwrScopes.class);
159: }
|