Liferay’s session sharing mechanism has always been a bit of a mystery. As with most things Liferay, documentation is minimal, and leaves a lot of questions to be answered. I’d like to share with you what I’ve learnt after testing a bunch of scenarios, and hopefully this will be good reference material for all of you developing portlets on Liferay.
My scenarios focus on how Liferay’s sessions behave for different combinations of the private-session-attributes settings; between the portal, portlets (in the same WAR, and across different WARs) and the servlets in these WARs. This also means that only APPLICATION_SCOPE attributes are relevant to the discussion.
Testing was performed on Liferay 4.3.6, and consisted of basic JSR-168 portlets , and basic Servlet Spec servlets. To emulate code running in the portal scope, a simple servlet Filter was configured on the Portal application.
Private Session True
This is the default setting, in which each WAR has its own session. The session within each WAR is private, and nothing is shared between individual WARs. Portlets and Servlet within the same WAR file will be able to share the session, because they are in the same context. So far, this conforms to the Servlet spec.
Liferay provides an additional functionality under this setting. As the official FAQ states, it also allows shared (namespaced) attributes set by the portal to be visible from portlets. Any session attribute with the configured prefix (e.g. LIFERAY_SHARED_) will be copied over and be visible by the portlets, hence granting private portlets read-access to session attributes set by the portal. This is illustrated in diagram 1.
Diagram 1Private Session False
All portlets under this setting will share the same session, and also share this same session with the portal. This is the easiest way for portlets in individual WAR files to communicate with each other.
However, the downside to this is that servlets in these WAR files will not be able to communicate with the portlets at all. (This is a question often raised in the forums, and one I struggled with for a while to figure out). The most convenient way to think of this is that portlets with this setting use the portal session, and have no access to the WAR session. This is illustrated in diagram 2.
Diagram 2Mixed Scenarios
Diagram 3 depicts the interaction between a private and non-private portlet in individual WAR files. Portlet A, being non-private, has full communication with the portal, but cannot talk to Servlet A. Portlet B, being private, has full communication with Servlet B, but only has read-only access to Portal shared attributes.
You will notice arrow from Portlet A to Portlet B is dotted because this communication happening indirectly through the portal session. Portlet A is using the portal session, so any shared attributes that it sets will also be copied over to Portlet B, and can be read.
Diagram 3Diagram 4 expands on diagram 3, and adds another private portlet to WAR B. Both of the portlets in B are private, meaning they are using the WAR session, so they can freely communicate with each other and the servlet. Same as the previous example, they will be able to read shared attributes set by the portal, or by any non-private portlet like Portlet A.
Diagram 4Diagram 5 is a bit more interesting. Here, Portlet B has been changed to non-private, meaning it will use the portal session. That is why it can now freely communicate with the portal and Portlet A. However, just like anything else using the Portal session, it cannot read the WAR session of B anymore, even though it is in the same WAR! While this may seem a little counter-intuitive, it is consistent with the behaviour we have seen so far.
Diagram 5Copying shared attributes to a Servlet
Update: Liferay has a built in mechanism for calling servlets from the portal context, giving them access to the portal session.
You may have noticed so far that the servlets cannot directly read the shared attributes of the portal session. But you can get around this by using a private portlet as a proxy. Diagram 6 shows the flow of how a shared attribute x, set by non-private Portlet A, will be stored in the portal session and subsequently copied to private Portlet B. Portlet B, having write access to WAR session B, can set another attribute y to the same value as x, and y can be read by the servlet.
Diagram 6Overwriting shared attributes from private portlets?
We know that any session attributes set by private portlets cannot be seen by the portal, or by any other non-private portlets. But what happens if the private portlet sets an attribute of the same key as the shared attribute?
Diagram 7In Diagram 7, the flow is depicted more accurately by showing two sessions – the Portal session and the WAR session of B. I will walk through the numbered scenario as follows:
- Non-private Portlet A sets a session attribute LIFERAY_SHARED_foo to “alice” . This is stored in the portal session.
- Private Portlet B reads the attribute LIFERAY_SHARED_foo. Since this is a shared attribute, it has read access, and returns the value “apple” .
- Private Portlet B2 now sets the same session attribute LIFERAY_SHARED_foo to “bob” . Because it is a private portlet, the value is written to the WAR session.
- When Portlet B tries to read the attribute again, it returns the value “bob”. This shows that values in the WAR session override those copied from the portal. If at this point Portlet A were to set the attribute again, Portlet B cannot see the change.
- Portlet B2 now removes the session attribute LIFERAY_SHARED_foo
- When Portlet B tries to read the attribute again, it returns the value “alice” from the portal session.
This example shows that when private portlets tries to read an attribute, it will first read the attribute from the WAR session. If it can’t be found, it will try to read from any shared attributes copied from the Portal session.
Summary
I am not a core Liferay developer, so I’m not sure what the underlying implementation really is. However, my observations are consistent with the following summary:
- Non-private portlets read and write to the Portal session.
- Private portlets write to their own WAR session.
- Private portlets try to read from their own WAR session first, then looks up any shared attributes copied from the Portal session.
- Servlets only have access to the WAR session, and cannot directly access the Portal session. In order to read shared session attributes, servlets need a private portlet in the same WAR file to copy it for them. Or you can configure your servlet to use the portal session by usingLiferay's PortalDelegateServlet mechanism.
I hope this has helped you in your understanding of Liferay’s session sharing mechanism. If you are a core Liferay developer, please let me know if I have made any mistakes, or if you have anything else to add. I’d be keen to know if this is still relevant to Liferay 4.4.x
相关推荐
liferay-portal详解 Liferay Portal 是一个基于 Java 的开源门户平台,提供了一个灵活的框架来构建企业门户和 web 应用程序。本文档将从架构解析、portal 规范、portlet 容器、portlet 生命周期、liferay portal ...
【Liferay Portlet 对象详解】 在 Liferay 平台中,Portlet 是核心组件,它负责呈现内容和提供互动功能。Portlet 技术的理解和应用是开发 Liferay 应用程序的关键。本文将深入解析 Portlet 的关键概念,包括 ...
在Liferay Portal中,Portlet是可重用的Web组件,用于展示动态内容和服务。随着时间的推移,项目中可能会积累许多不再使用的Portlet代码,这不仅会占用服务器资源,还可能影响系统的性能和维护效率。因此,定期清理...
在本文中,我们将深入探讨Liferay插件的开发过程,包括环境配置、Portlet开发、Layout和Theme开发等关键步骤。Liferay是一个开源的企业级门户平台,它允许开发者通过插件扩展其功能,以满足不同业务需求。 首先,...
Liferay 是一个强大的开源企业门户平台,用于构建和管理数字化体验。它的核心是 Liferay Portal,自2000年在洛杉矶创立以来,已经成为全球范围内许多企业和组织的首选解决方案。Liferay 具备多语言支持、内容管理、...
### Liferay Portal的内容和布局详解 #### 一、布局(Layout) **布局**是Liferay Portal中用于管理Portlet的关键概念之一。布局不仅决定着Portlet的呈现方式,还影响着用户的交互体验。在Liferay中,布局是通过一...
Liferay Portal Liferay IDE
### Liferay环境搭建详解 #### 一、简介 Liferay是一款功能强大的开源企业级门户平台,主要用于构建企业级Web应用程序和服务。本文档旨在详细介绍如何利用Eclipse集成开发环境(IDE)来搭建Liferay开发环境,并...
在portal-setup-wizard.properties里面加上如下配置。 editor.wysiwyg.portal-web....下载ueditor.jar 放到liferay tomcat 下的Root/web-info/lib/下 下载ueditorhook-hook-6.2.0.1.war 安装即可 重启liferay应用
### Liferay Portal 二次开发详解 #### 一、Liferay Portal 概览 **Liferay Portal** 是一款开源的企业级门户平台,它基于Java技术构建,支持多种标准,包括JSR 168 和 WSRP (Web Services for Remote Portlets)。...
Liferay是一个开源公司,我们一般谈Liferay是指的Liferay Portal。Liferay Portal始创于2000年的洛杉机,当时是一个非营利性组织。
8. **社交网络和协作**:Liferay支持社区建设,如论坛、博客、文档共享等,促进团队协作。 9. **集成其他系统**:Liferay能够与CRM、ERP等系统集成,实现企业信息化的一体化。 通过上述文件和这些知识点的学习,...
### Liferay快速入门知识点详解 #### 一、Liferay Portal简介与快速启动 Liferay Portal是一款功能强大且灵活的企业级门户平台,它基于Java技术,遵循JSR-168标准,提供了丰富的特性来满足企业内外部网站的需求。...
标题 "Liferay扩展环境 Liferay Ext" 涉及到的是Liferay门户平台的一个关键概念,Liferay Ext是用于扩展和定制Liferay功能的核心工具。Liferay是一个开源的企业级内容管理平台,它允许用户根据需求构建自定义的数字...
### Liferay 6.0 开发指南核心知识点详解 #### 一、引言与核心技术 在《Liferay 6.0 开发指南》中,首先介绍了Liferay 6.0的核心技术及其开发策略。Liferay是一款强大的开源门户平台,支持企业级应用的构建与部署...
### WAS 7.0 上安装部署 Liferay Portal 步骤详解 #### 一、准备工作 在开始部署之前,需要确保已经完成了以下准备工作: 1. **安装 WAS 7.0**: - 安装 WebSphere Application Server (WAS) 7.0 并配置好应用...
**Liferay 简单Portlet详解** Liferay Portal 是一款开源的企业级门户平台,它提供了丰富的功能,如用户管理、内容管理、社交网络等。Portlet是Liferay中的核心组件,是构建门户应用程序的基本模块。这篇博文将深入...