談笑有鴻儒,往來無白丁

          在恰當(dāng)?shù)臅r(shí)間、地點(diǎn)以恰當(dāng)?shù)姆绞奖磉_(dá)給恰當(dāng)?shù)娜?..  閱讀的時(shí)候請(qǐng)注意分類,佛曰我日里面是談笑文章,其他是各個(gè)分類的文章,積極的熱情投入到寫博的隊(duì)伍中來,支持blogjava做大做強(qiáng)!向dudu站長致敬>> > 我的微博敬請(qǐng)收聽
          Portal開源實(shí)現(xiàn)-Liferay的Portlet Session處理(2)


          二、LIFERAY中的實(shí)現(xiàn)

          LIFERAY在構(gòu)建ActionRequestImpl和RenderRequestImpl時(shí),會(huì)設(shè)置PORTLET SESSION,如下代碼所示:
          public RenderRequestImpl(HttpServletRequest req, Portlet portlet,
          ??????? CachePortlet cachePortlet,
          ??????? PortletContext portletCtx,
          ??????? WindowState windowState, PortletMode portletMode,
          ??????? PortletPreferences prefs, String layoutId) {
          ?? ...
          ??_req = dynamicReq;
          ??_portlet = portlet;
          ??_cachePortlet = cachePortlet;
          ??_portalCtx = new PortalContextImpl();
          ??_portletCtx = portletCtx;
          ??_windowState = windowState;
          ??_portletMode = portletMode;
          ??_prefs = prefs;
          ??_ses = new PortletSessionImpl(
          ???_req.getSession(), _portletName, _portletCtx);
          ?? ...
          ?}
          從蘭色的部分(? _ses = new PortletSessionImpl(_req.getSession(),_portletName, _portletCtx);? )我們可以看到,這個(gè)PORTLET SESSION其實(shí)就是PORTAL SYSTEM的 SESSION 對(duì)象。
          所以無論request調(diào)用getSession()或者getPortletSession()都將獲取Portal 系統(tǒng)的SESSION 對(duì)象,而無論該P(yáng)ORTLET? 是或者不是屬于PORTAL SYSTEM上下文。而且即使不同PORTAL APPLICATION的PORTLET也將使用同一個(gè)SESSION 對(duì)象(PORTAL 系統(tǒng))。
          也就是說,對(duì)于某一個(gè)PORTLET來說,如果有對(duì)其的SESSION進(jìn)行的操作,并沒有真正的在該APPLICATION上下文中的SESSION進(jìn)行操作,而是在PORTAL系統(tǒng)上下文的SESSION中進(jìn)行操作。

          而且LIFERAY提供getPortletSession來獲取PortletSession對(duì)象,而不是getSession()方法,所以即使getPortletSession()可以獲取正確的Session對(duì)象,開發(fā)人員由于習(xí)慣問題,也因使用getSession()而得不到。

          另外如果調(diào)用request.getSession(true)還可能會(huì)出現(xiàn)錯(cuò)誤,因?yàn)長IFERAY在包含某一個(gè)PORTLET內(nèi)容是,調(diào)用PortletRequestDispatcherImpl.include()方法,該方法將生成PortletServletRequest 和PortletServletResponse,請(qǐng)見如下代碼:

          PortletServletRequest portletServletReq = new PortletServletRequest(
          ????httpReq, reqImpl, pathInfo, queryString, requestURI,
          ????servletPath);

          ???PortletServletResponse portletServletRes =
          ????new PortletServletResponse(
          ?????resImpl.getHttpServletResponse(), resImpl);
          而PortletServletRequest的構(gòu)造函數(shù)是如下定義的:
          public PortletServletRequest(HttpServletRequest req,
          ???????? RenderRequest renderRequest, String pathInfo,
          ???????? String queryString, String requestURI,
          ???????? String servletPath) {

          ??super(req);

          ??_ses = req.getSession();
          ??_renderRequest = renderRequest;
          ??_pathInfo = pathInfo;
          ??_queryString = queryString;
          ??_requestURI = requestURI;
          ??_servletPath = servletPath;
          ?}
          所以其SESSION依然是PORTAL系統(tǒng)上下文的。然后問題就出在這里,PortletServletRequest實(shí)現(xiàn)了getSession()方法,但是沒有實(shí)現(xiàn)getSession(boolen create)方法,如果用戶在此階段調(diào)用getSession(true)的話,在某些情況下就會(huì)拋出NullPointerException

          原因見如下代碼(請(qǐng)注意我添加的注釋部分)
          //ApplicationHttpRequest:??

          ?public HttpSession getSession(boolean create) {

          ??????? if (crossContext) {
          ???????????
          ??????????? // There cannot be a session if no context has been assigned yet
          ??????????? if (context == null)
          ??????????????? return (null);

          ??????????? // Return the current session if it exists and is valid
          ??????????? if (session != null)
          ??????????????? return (session.getSession());
          ???? // 我的注釋:這里將獲取PORTAL系統(tǒng)的SESSION對(duì)象。
          ??????????? HttpSession other = super.getSession(false);
          ??????????? if (create && (other == null)) {
          ??????????????? // First create a session in the first context: the problem is
          ??????????????? // that the top level request is the only one which can
          ??????????????? // create the cookie safely
          ??????????????? other = super.getSession(true);
          ??????????? }
          ??????????? if (other != null) {
          ??????????????? Session localSession = null;
          ??????????????? try {
          ??????????????????? // 我的注釋:this context did not have the session with session id. It can just be found in the Portal
          ??????????????????? // context. So here it will return a null value.
          ??????????????????? localSession =
          ??????????????????????? context.getManager().findSession(other.getId());
          ??????????????????? localSession.access(); //我的注釋:Here, localSession is null. So it throws a NullPointException.
          ??????????????? } catch (IOException e) {
          ??????????????????? // Ignore
          ??????????????? }
          ??????????????? if (localSession == null) {
          ??????????????????? localSession = context.getManager().createEmptySession();
          ??????????????????? localSession.setNew(true);
          ??????????????????? localSession.setValid(true);
          ??????????????????? localSession.setCreationTime(System.currentTimeMillis());
          ??????????????????? localSession.setMaxInactiveInterval
          ??????????????????????? (context.getManager().getMaxInactiveInterval());

          posted on 2006-07-31 10:09 壞男孩 閱讀(1123) 評(píng)論(0)  編輯  收藏 所屬分類: Portal相關(guān)文檔

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 新郑市| 屏东县| 高平市| 玛沁县| 灌南县| 大兴区| 上蔡县| 巩留县| 二连浩特市| 扎囊县| 社会| 九台市| 宁化县| 新乡市| 宜黄县| 黔江区| 长宁县| 小金县| 田东县| 弋阳县| 宽甸| 城市| 浦北县| 永和县| 贡觉县| 栾川县| 长治市| 建水县| 南靖县| 陇南市| 旌德县| 民乐县| 逊克县| 桂林市| 迭部县| 平谷区| 扬中市| 安吉县| 永城市| 通山县| 高唐县|