1 package org.apache.beehive.controls.runtime.servlet; 2 19 20 import java.util.Stack ; 21 import java.io.InputStream ; 22 import java.beans.beancontext.BeanContextChild ; 23 import java.net.URL ; 24 import java.net.MalformedURLException ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletRequest ; 28 import javax.servlet.ServletResponse ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.apache.beehive.controls.runtime.bean.ControlContainerContext; 33 34 38 public class ServletBeanContext extends ControlContainerContext 39 { 40 private static class RequestContext 41 { 42 RequestContext(ServletContext context, ServletRequest req, ServletResponse resp) 43 { 44 _context = context; 45 _request = req; 46 _response = resp; 47 } 48 49 ServletContext _context; 50 ServletResponse _response; 51 ServletRequest _request; 52 } 53 54 public ServletBeanContext() 55 { 56 super(); 57 } 58 59 63 public void initialize() 64 { 65 super.initialize(); 66 67 ServletServiceProvider ssp = ServletServiceProvider.getProvider(); 71 addService(ServletContext .class, ssp); 72 addService(ServletRequest .class, ssp); 73 addService(ServletResponse .class, ssp); 74 addService(HttpServletRequest .class, ssp); 75 addService(HttpServletResponse .class, ssp); 76 } 77 78 81 public void beginContext(ServletContext context, ServletRequest req, ServletResponse resp) 82 { 83 pushRequestContext(context, req, resp); 84 super.beginContext(); 85 } 86 87 90 public void endContext() 91 { 92 super.endContext(); 93 popRequestContext(); 94 } 95 96 private Stack <RequestContext> getRequestStack() 97 { 98 if (_reqStack == null) 99 _reqStack = new Stack <RequestContext>(); 100 101 return _reqStack; 102 } 103 104 107 private synchronized void pushRequestContext(ServletContext context, ServletRequest req, 108 ServletResponse resp) 109 { 110 getRequestStack().push(new RequestContext(context, req, resp)); 111 } 112 113 116 private synchronized void popRequestContext() 117 { 118 getRequestStack().pop(); 119 } 120 121 124 private synchronized RequestContext peekRequestContext() 125 { 126 Stack <RequestContext> reqStack = getRequestStack(); 127 if (reqStack.empty()) 128 return null; 129 130 return reqStack.peek(); 131 } 132 133 137 public ServletContext getServletContext() 138 { 139 RequestContext reqContext = peekRequestContext(); 140 if (reqContext == null) 141 return null; 142 143 return reqContext._context; 144 } 145 146 150 public ServletRequest getServletRequest() 151 { 152 RequestContext reqContext = peekRequestContext(); 153 if (reqContext == null) 154 return null; 155 156 return reqContext._request; 157 } 158 159 163 public ServletResponse getServletResponse() 164 { 165 RequestContext reqContext = peekRequestContext(); 166 if (reqContext == null) 167 return null; 168 169 return reqContext._response; 170 } 171 172 176 public void setWrappers(boolean useWrappers) 177 { 178 _useWrappers = useWrappers; 179 } 180 181 192 public InputStream getResourceAsStream(String name, BeanContextChild bcc) throws IllegalArgumentException 193 { 194 ServletContext sc = getServletContext(); 195 if ( sc != null ) 196 return sc.getResourceAsStream( name ); 197 198 return null; 199 } 200 201 211 public URL getResource(String name, BeanContextChild bcc) throws IllegalArgumentException 212 { 213 ServletContext sc = getServletContext(); 214 if ( sc != null ) 215 { 216 try 217 { 218 return sc.getResource( name ); 219 } 220 catch ( MalformedURLException mue ) 221 { 222 throw new IllegalArgumentException ( mue.getMessage() ); 223 } 224 } 225 226 return null; 227 } 228 229 protected boolean useWrappers() { return _useWrappers; } 230 231 private boolean _useWrappers = true; 232 transient private Stack <RequestContext> _reqStack; 233 } 234 | Popular Tags |