1 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 43 public class DwrScopes 44 { 45 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 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 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 121 public static final ContextScope<ServletContext > APPLICATION = 122 new ApplicationScope("DwrScopes.APPLICATION"); 123 124 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 170 private static final Logger log = Logger.getLogger(DwrScopes.class); 171 } 172 | Popular Tags |