读书人

Tapestry3源码阅览笔记4:ApplicationS

发布时间: 2012-08-31 12:55:03 作者: rapoo

Tapestry3源码阅读笔记4:ApplicationServlet/RequestContext

之前都是从Page类以及Page类的创建缓存相关的类去阅读代码,现在换个角度,从请求开始的角度去出发,这样才能将整个流程联系起来。那么开始看tapestry的servlet。

?

ApplicationServlet类作为默认的tapestry的servlet。实际就是将请求调用传递给engine进行调用。
RequestContext类其实就是个封装request/response/session等引用的对象,感觉主要是为了方便传递而创立。
这里细说一下ApplicationServlet,因为应用首先会进入到ApplicationServlet,engine等都是通过ApplicationServlet来触发的。
首先ApplicationServlet的主要方法就是一个doService。这个方法是每次无论是get请求还是post请求都会触发的,它做了下面几件事:1.根据requestt和response创建requestcontext对象。2.获取engine。3.调用engine的service方法,将requestcontext对象传递进去。4.对engine对象进行保存。
这里再细说engine的保存。首先它会判断有没session存在,session存在的话那么如果是第一次那么会将engine存到session里面,后续的会判断engine的是否是有状态的,有状态的话替换掉之前的engine。如果没有session,如果engine是有状态的,那么抛错。否则,它将engine放到enginpool里面,这个enginepool其实也是之前提到Pool类的实例,是ApplicationServlet的实例变量。
获取engine就是上述过程的一个反过程。首先从session里获取,然后从enginepool里获取,如果没有了,就根据配置文件生成一个engine出来。
也就是说,engine基本都是每个用户有一个的,因为都存在这个用户对应的session里。
// Create a context from the various bits and pieces.context = createRequestContext(request, response);// The subclass provides the engine.IEngine engine = getEngine(context);boolean dirty = engine.service(context);HttpSession session = context.getSession();// When there's an active session, we *may* store it into// the HttpSession and we *will not* store the engine// back into the engine pool.if (session != null){// If the service may have changed the engine and the// special storeEngine flag is on, then re-save the engine// into the session. Otherwise, we only save the engine// into the session when the session is first created (is new).try{boolean forceStore =engine.isStateful() && (session.getAttribute(_attributeName) == null);if (forceStore || dirty){if (LOG.isDebugEnabled())LOG.debug("Storing " + engine + " into session as " + _attributeName);session.setAttribute(_attributeName, engine);}}catch (IllegalStateException ex){}// The engine is stateful and stored in a session. Even if it started// the request cycle in the pool, it doesn't go back.return;}if (engine.isStateful()){LOG.error(Tapestry.format("ApplicationServlet.engine-stateful-without-session",engine));return;}// No session; the engine contains no state particular to// the client (except for locale). Don't throw it away,// instead save it in a pool for later reuse (by this, or another// client in the same locale)._enginePool.store(engine.getLocale(), engine);
?

?

读书人网 >编程

热点推荐