Liferay 启动过程分析15-初始化公司信息

简介:

 在MainServlet中,当初始化Resource Code完毕后,就开始初始化公司信息,对应代码如下:

 
 
  1. if (_log.isDebugEnabled()) { 
  2.             _log.debug("Initialize companies"); 
  3.         } 
  4.  
  5.         try { 
  6.             initCompanies(); 
  7.         } 
  8. ..

 

它会调用initCompanies()方法:

 
 
  1. protected void initCompanies() throws Exception { 
  2.     ServletContext servletContext = getServletContext(); 
  3.  
  4.     String[] webIds = PortalInstances.getWebIds(); 
  5.  
  6.     for (int i = 0; i < webIds.length; i++) { 
  7.         PortalInstances.initCompany(servletContext, webIds[i]); 
  8.     } 

 

从这里可以看出,它先02行获得Servlet上下文,然后04行获得webId的数组:

 

分析04行:

我们跟进到PortalInstances的getWebIds方法:

 
 
  1. public static String[] getWebIds() { 
  2.         return _instance._getWebIds(); 
  3.     } 

它最终是调用_getWebIds()方法:

 
 
  1. private String[] _getWebIds() { 
  2.         if (_webIds != null) { 
  3.             return _webIds; 
  4.         } 
  5.  
  6.         if (Validator.isNull(PropsValues.COMPANY_DEFAULT_WEB_ID)) { 
  7.             throw new RuntimeException("Default web id must not be null"); 
  8.         } 
  9.  
  10.         try { 
  11.             List<Company> companies = CompanyLocalServiceUtil.getCompanies( 
  12.                 false); 
  13.  
  14.             List<String> webIdsList = new ArrayList<String>(companies.size()); 
  15.  
  16.             for (Company company : companies) { 
  17.                 String webId = company.getWebId(); 
  18.  
  19.                 if (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) { 
  20.                     webIdsList.add(0, webId); 
  21.                 } 
  22.                 else { 
  23.                     webIdsList.add(webId); 
  24.                 } 
  25.             } 
  26.  
  27.             _webIds = webIdsList.toArray(new String[webIdsList.size()]); 
  28.         } 
  29.         catch (Exception e) { 
  30.             _log.error(e, e); 
  31.         } 
  32.  
  33.         if ((_webIds == null) || (_webIds.length == 0)) { 
  34.             _webIds = new String[] {PropsValues.COMPANY_DEFAULT_WEB_ID}; 
  35.         } 
  36.  
  37.         return _webIds; 
  38.     } 

在这个方法中,首先06行校验是否设置了company的default web id,这个值最终在portal.propeties中获得:

 
 
  1.    # This sets the default web id. Omniadmin users must belong to the company 
  2.    # with this web id. 
  3.    # 
  4.    company.default.web.id=liferay.com 

然后第11行会调用 CompanyLocalServiceUtil类的getCompanies方法来获取Company列表,它最终会在CompanyLocalServiceImpl中发起一个数据库查询,见getCompanies(boolean)方法:

 
 
  1. public List<Company> getCompanies(boolean system) throws SystemException { 
  2.         return companyPersistence.findBySystem(system); 
  3.     } 

然后后面代码就是遍历所有的Company对象,分离出webId,然后填入数组中,并且返回

 

分析06-08行:

既然04行已经获得了数据库中所有Company的webId,那么现在就是遍历webId,用这些webId来初始化Company.(从数据库信息构建一个一个的服务器内存中的对象):

我们跟进到PortalInstances的initCompany方法,它最终会调用_initCompany方法:

 
 
  1. private long _initCompany(ServletContext servletContext, String webId) { 
  2.  
  3.         // Begin initializing company 
  4.  
  5.         if (_log.isDebugEnabled()) { 
  6.             _log.debug("Begin initializing company with web id " + webId); 
  7.         } 
  8.  
  9.         long companyId = 0
  10.  
  11.         try { 
  12.             Company company = CompanyLocalServiceUtil.checkCompany(webId); 
  13.  
  14.             companyId = company.getCompanyId(); 
  15.         } 
  16.         catch (Exception e) { 
  17.             _log.error(e, e); 
  18.         } 
  19.  
  20.         CompanyThreadLocal.setCompanyId(companyId); 
  21.  
  22.         // Lucene 
  23.  
  24.         LuceneHelperUtil.startup(companyId); 
  25.  
  26.         // Initialize display 
  27.  
  28.         if (_log.isDebugEnabled()) { 
  29.             _log.debug("Initialize display"); 
  30.         } 
  31.  
  32.         try { 
  33.             String xml = HttpUtil.URLtoString(servletContext.getResource( 
  34.                 "/WEB-INF/liferay-display.xml")); 
  35.  
  36.             PortletCategory portletCategory = (PortletCategory)WebAppPool.get( 
  37.                 companyId, WebKeys.PORTLET_CATEGORY); 
  38.  
  39.             if (portletCategory == null) { 
  40.                 portletCategory = new PortletCategory(); 
  41.             } 
  42.  
  43.             PortletCategory newPortletCategory = 
  44.                 PortletLocalServiceUtil.getEARDisplay(xml); 
  45.  
  46.             portletCategory.merge(newPortletCategory); 
  47.  
  48.             for (int i = 0; i < _companyIds.length; i++) { 
  49.                 long currentCompanyId = _companyIds[i]; 
  50.  
  51.                 PortletCategory currentPortletCategory = 
  52.                     (PortletCategory)WebAppPool.get( 
  53.                         currentCompanyId, WebKeys.PORTLET_CATEGORY); 
  54.  
  55.                 if (currentPortletCategory != null) { 
  56.                     portletCategory.merge(currentPortletCategory); 
  57.                 } 
  58.             } 
  59.  
  60.             WebAppPool.put( 
  61.                 companyId, WebKeys.PORTLET_CATEGORY, portletCategory); 
  62.         } 
  63.         catch (Exception e) { 
  64.             _log.error(e, e); 
  65.         } 
  66.  
  67.         // Check journal content search 
  68.  
  69.         if (_log.isDebugEnabled()) { 
  70.             _log.debug("Check journal content search"); 
  71.         } 
  72.  
  73.         if (GetterUtil.getBoolean( 
  74.                 PropsUtil.get( 
  75.                     PropsKeys.JOURNAL_SYNC_CONTENT_SEARCH_ON_STARTUP))) { 
  76.  
  77.             try { 
  78.                 JournalContentSearchLocalServiceUtil.checkContentSearches( 
  79.                     companyId); 
  80.             } 
  81.             catch (Exception e) { 
  82.                 _log.error(e, e); 
  83.             } 
  84.         } 
  85.  
  86.         // LDAP Import 
  87.  
  88.         try { 
  89.             if (LDAPSettingsUtil.isImportOnStartup(companyId)) { 
  90.                 PortalLDAPImporterUtil.importFromLDAP(companyId); 
  91.             } 
  92.         } 
  93.         catch (Exception e) { 
  94.             _log.error(e, e); 
  95.         } 
  96.  
  97.         // Process application startup events 
  98.  
  99.         if (_log.isDebugEnabled()) { 
  100.             _log.debug("Process application startup events"); 
  101.         } 
  102.  
  103.         try { 
  104.             EventsProcessorUtil.process( 
  105.                 PropsKeys.APPLICATION_STARTUP_EVENTS, 
  106.                 PropsValues.APPLICATION_STARTUP_EVENTS, 
  107.                 new String[] {String.valueOf(companyId)}); 
  108.         } 
  109.         catch (Exception e) { 
  110.             _log.error(e, e); 
  111.         } 
  112.  
  113.         // End initializing company 
  114.  
  115.         if (_log.isDebugEnabled()) { 
  116.             _log.debug( 
  117.                 "End initializing company with web id " + webId + 
  118.                     " and company id " + companyId); 
  119.         } 
  120.  
  121.         addCompanyId(companyId); 
  122.  
  123.         return companyId; 
  124.     } 

这里又做了很多事情:

第12行做了许多检查,确保company包含默认的用户和组。

第22-24行中为companyId建立Lucene索引。

第26-65行读取liferay-display.xml,并且吧companyId,以及每个portlet分类都放入WebAppPool中。

其他略。

 

所以,在这一步,所有的company信息都从数据库读出,并且正确的初始化了。





本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/907645,如需转载请自行联系原作者

目录
相关文章
多model项目下,某个项目引用了公共lib下的service, 其他模块想不受影响的启动解决办法
多model项目下,某个项目引用了公共lib下的service, 其他模块想不受影响的启动解决办法
56 0