Liferay 启动过程分析6

在MainServlet中,初始化插件包之后,就开始初始化Portlets:

对应的代码是:

if (_log.isDebugEnabled()) {             _log.debug("Initialize portlets");         }          List<Portlet> portlets = null         try {             portlets = initPortlets(pluginPackage);         } ... 

它会去调用initPortlets方法,并且传入一个我们上一步刚初始化的核心pluginPackage,我们来细看它究竟做了哪些事情:

读取配置文件:

首先,它去取得servletContext,然后获取一组xml配置文件:

protected List<Portlet> initPortlets(PluginPackage pluginPackage)         throws Exception {          ServletContext servletContext = getServletContext();          String[] xmls = new String[] {             HttpUtil.URLtoString(                 servletContext.getResource(                     "/WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_CUSTOM)),             HttpUtil.URLtoString(                 servletContext.getResource("/WEB-INF/portlet-ext.xml")),             HttpUtil.URLtoString(                 servletContext.getResource("/WEB-INF/liferay-portlet.xml")),             HttpUtil.URLtoString(                 servletContext.getResource("/WEB-INF/liferay-portlet-ext.xml")),             HttpUtil.URLtoString(                 servletContext.getResource("/WEB-INF/web.xml"))         }; .... 

这些xml配置文件中,我研究了下,大概是提供如下的功能。

portlet_custom.xml可以用来根据请求的p_p_id字段,到这个文件中找到相应的portlet的<init-param>的<view-action>元素,然后去struts-config.xml中找到action的配置信息,最终到tiles-config.xml中找到对应的页面信息。

portlet-ext.xml,定义了portlet的初始化信息。

liferay-portlet.xml,定义了portlet在liferay框架中的一般外观信息

liferay-portlet-ext.xml,定义了portlet在liferay框架中的模板和是否允许重复。

初始化EAR:

然后,调用以下代码来初始化:

PortletLocalServiceUtil.initEAR(servletContext, xmls, pluginPackage); 

它最终会调用PortletLocalServiceImpl的initEAR方法:

public void initEAR(         ServletContext servletContext, String[] xmls,         PluginPackage pluginPackage) {          // Clear pools every time initEAR is called. See LEP-5452.          portletLocalService.clearCompanyPortletsPool();          _portletAppsPool.clear();         _portletsPool.clear();         _portletIdsByStrutsPath.clear();         _friendlyURLMapperPortlets.clear();          Map<String, Portlet> portletsPool = _getPortletsPool();          try {             Set<String> servletURLPatterns = _readWebXML(xmls[4]);              Set<String> portletIds = _readPortletXML(                 servletContext, xmls[0], portletsPool, servletURLPatterns,                 pluginPackage);              portletIds.addAll(                 _readPortletXML(                     servletContext, xmls[1], portletsPool, servletURLPatterns,                     pluginPackage));              Set<String> liferayPortletIds =                 _readLiferayPortletXML(xmls[2], portletsPool);              liferayPortletIds.addAll(                 _readLiferayPortletXML(xmls[3], portletsPool));              // Check for missing entries in liferay-portlet.xml              for (String portletId : portletIds) {                 if (_log.isWarnEnabled() &&                     !liferayPortletIds.contains(portletId)) {                      _log.warn(                         "Portlet with the name " + portletId +                             " is described in portlet.xml but does not " +                                 "have a matching entry in liferay-portlet.xml");                 }             }              // Check for missing entries in portlet.xml              for (String portletId : liferayPortletIds) {                 if (_log.isWarnEnabled() && !portletIds.contains(portletId)) {                     _log.warn(                         "Portlet with the name " + portletId +                             " is described in liferay-portlet.xml but does " +                                 "not have a matching entry in portlet.xml");                 }             }              // Remove portlets that should not be included              Iterator<Map.Entry<String, Portlet>> portletPoolsItr =                 portletsPool.entrySet().iterator();              while (portletPoolsItr.hasNext()) {                 Map.Entry<String, Portlet> entry = portletPoolsItr.next();                  Portlet portletModel = entry.getValue();                  if (!portletModel.getPortletId().equals(PortletKeys.ADMIN) &&                     !portletModel.getPortletId().equals(                         PortletKeys.MY_ACCOUNT) &&                     !portletModel.isInclude()) {                      portletPoolsItr.remove();                      _friendlyURLMapperPortlets.remove(                         portletModel.getPortletId());                 }             }              // Sprite images              PortletApp portletApp = _getPortletApp(StringPool.BLANK);              _setSpriteImages(servletContext, portletApp, "/html/icons/");         }         catch (Exception e) {             _log.error(e, e);         }     } 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3796441e0cd9a66d36092b238f34a6aa.html