1 18 package org.apache.beehive.netui.pageflow; 19 20 import org.apache.beehive.netui.util.logging.Logger; 21 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils; 22 import org.apache.beehive.netui.pageflow.internal.InternalConstants; 23 import org.apache.beehive.netui.pageflow.internal.AdapterManager; 24 import org.apache.beehive.netui.pageflow.internal.InternalUtils; 25 import org.apache.beehive.netui.pageflow.interceptor.action.InterceptorForward; 26 import org.apache.beehive.netui.pageflow.interceptor.action.ActionInterceptor; 27 import org.apache.beehive.netui.pageflow.interceptor.action.ActionInterceptorContext; 28 29 import javax.servlet.http.HttpSessionBindingListener ; 30 import javax.servlet.http.HttpSessionBindingEvent ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.ServletContext ; 33 import java.util.Stack ; 34 import java.io.Serializable ; 35 36 37 41 public class PageFlowStack 42 implements HttpSessionBindingListener , Serializable 43 { 44 private static final Logger _log = Logger.getInstance( PageFlowStack.class ); 45 private static final String JPF_STACK_ATTR = InternalConstants.ATTR_PREFIX + "nestingStack"; 46 47 private Stack _stack = new Stack (); 48 49 50 53 public static class PushedPageFlow implements Serializable 54 { 55 private PageFlowController _pageFlow; 56 private ActionInterceptor _interceptor; 57 private InterceptorForward _interceptedForward; 58 private String _interceptedActionName; 59 60 public PushedPageFlow( PageFlowController pageFlow ) 61 { 62 _pageFlow = pageFlow; 63 } 64 65 public PushedPageFlow( PageFlowController pageFlow, ActionInterceptor interceptor, 66 InterceptorForward interceptedFwd, String interceptedActionName ) 67 { 68 this( pageFlow ); 69 _interceptor = interceptor; 70 _interceptedForward = interceptedFwd; 71 _interceptedActionName = interceptedActionName; 72 } 73 74 public PageFlowController getPageFlow() 75 { 76 return _pageFlow; 77 } 78 79 public ActionInterceptor getInterceptor() 80 { 81 return _interceptor; 82 } 83 84 public InterceptorForward getInterceptedForward() 85 { 86 return _interceptedForward; 87 } 88 89 public String getInterceptedActionName() 90 { 91 return _interceptedActionName; 92 } 93 } 94 95 102 public static final PageFlowStack get( HttpServletRequest request ) 103 { 104 return get( request, true ); 105 } 106 107 114 public static PageFlowStack get( HttpServletRequest request, boolean createIfNotExist ) 115 { 116 PageFlowStack jpfStack = 117 ( PageFlowStack ) ScopedServletUtils.getScopedSessionAttr( JPF_STACK_ATTR, 118 PageFlowUtils.unwrapMultipart( request ) ); 119 120 if ( jpfStack == null && createIfNotExist ) 121 { 122 jpfStack = new PageFlowStack(); 123 jpfStack.save( request ); 124 } 125 126 return jpfStack; 127 } 128 129 134 public static void destroy( HttpServletRequest request ) 135 { 136 ScopedServletUtils.removeScopedSessionAttr( JPF_STACK_ATTR, PageFlowUtils.unwrapMultipart( request ) ); 137 } 138 139 145 PageFlowController popUntil( HttpServletRequest request, Class stopAt ) 146 { 147 while ( ! isEmpty() ) 148 { 149 PageFlowController popped = pop( request ).getPageFlow(); 150 151 if ( popped.getClass().equals( stopAt ) ) 152 { 153 if ( isEmpty() ) destroy( request ); 157 return popped; 158 } 159 else 160 { 161 if ( ! popped.isLongLived() ) popped.destroy( request.getSession() ); 165 } 166 } 167 168 destroy( request ); return null; 170 } 171 172 176 PageFlowController popUntil( HttpServletRequest request, int index ) 177 { 178 for ( int i = _stack.size() - 1; i > index; --i ) 179 { 180 pop( request ); 181 } 182 183 return pop( request ).getPageFlow(); 184 } 185 186 int lastIndexOf( HttpServletRequest request, Class target ) 187 { 188 for ( int i = _stack.size() - 1; i >= 0; --i ) 189 { 190 if ( ( ( PushedPageFlow ) _stack.elementAt( i ) ).getPageFlow().getClass().equals( target ) ) 191 { 192 return i; 193 } 194 } 195 196 return -1; 197 } 198 199 void ensureFailover( HttpServletRequest request, ServletContext servletContext ) 200 { 201 String name = 202 ScopedServletUtils.getScopedSessionAttrName( JPF_STACK_ATTR, PageFlowUtils.unwrapMultipart( request ) ); 203 AdapterManager.getServletContainerAdapter( servletContext ).ensureFailover( name, this, request ); 204 } 205 206 void save( HttpServletRequest request ) 207 { 208 ScopedServletUtils.setScopedSessionAttr( JPF_STACK_ATTR, this, PageFlowUtils.unwrapMultipart( request ) ); 209 } 210 211 PageFlowStack() 212 { 213 } 214 215 221 public void push( PageFlowController pageFlow, HttpServletRequest request ) 222 { 223 ActionInterceptorContext interceptorContext = ActionInterceptorContext.getActiveContext( request, true ); 224 225 if ( interceptorContext != null ) 226 { 227 ActionInterceptor interceptor = interceptorContext.getOverridingActionInterceptor(); 228 InterceptorForward originalForward = interceptorContext.getOriginalForward(); 229 String actionName = interceptorContext.getActionName(); 230 _stack.push( new PushedPageFlow( pageFlow, interceptor, originalForward, actionName ) ); 231 } 232 else 233 { 234 _stack.push( new PushedPageFlow( pageFlow ) ); 235 } 236 237 pageFlow.setIsOnNestingStack( true ); 239 240 ensureFailover( request, pageFlow.getServletContext() ); 242 } 243 244 250 public PushedPageFlow pop( HttpServletRequest request ) 251 { 252 PushedPageFlow ppf = ( PushedPageFlow ) _stack.pop(); 253 PageFlowController pfc = ppf.getPageFlow(); 254 pfc.setIsOnNestingStack( false ); 255 256 if ( request != null ) { 258 ServletContext servletContext = pfc.getServletContext(); 259 260 if ( servletContext == null ) 262 { 263 servletContext = InternalUtils.getServletContext( request ); 264 pfc.reinitialize( request, null, servletContext ); 265 } 266 267 ensureFailover( request, servletContext ); } 269 270 return ppf; 271 } 272 273 278 public PushedPageFlow peek() 279 { 280 return ( PushedPageFlow ) _stack.peek(); 281 } 282 283 288 public boolean isEmpty() 289 { 290 return _stack.isEmpty(); 291 } 292 293 298 public int size() 299 { 300 return _stack.size(); 301 } 302 303 306 public void valueBound( HttpSessionBindingEvent event ) 307 { 308 } 309 310 313 public void valueUnbound( HttpSessionBindingEvent event ) 314 { 315 if ( _log.isDebugEnabled() ) 316 { 317 _log.debug( "The page flow stack is being unbound from the session." ); 318 } 319 320 while ( ! isEmpty() ) 321 { 322 PageFlowController jpf = pop( null ).getPageFlow(); 323 324 if ( jpf.getServletContext() == null ) 327 { 328 jpf.reinitialize( null, null, event.getSession().getServletContext() ); 329 } 330 331 if ( ! jpf.isLongLived() ) jpf.destroy( event.getSession() ); 332 } 333 } 334 335 338 Stack getLegacyStack() 339 { 340 Stack ret = new Stack (); 341 342 for ( int i = 0; i < _stack.size(); ++i ) 343 { 344 ret.push( ( ( PushedPageFlow ) _stack.get( i ) ).getPageFlow() ); 345 } 346 347 return ret; 348 } 349 } 350 | Popular Tags |