Liferay 启动过程分析14-初始化resource code

 在MainServlet中,当初始化resource actions之后,就开始初始化resource code ,这是由以下代码来完成的:

  
  
  
  
  1. if (_log.isDebugEnabled()) { 
  2.             _log.debug("Initialize resource codes"); 
  3.         } 
  4.  
  5.         try { 
  6.             initResourceCodes(portlets); 
  7.         } 
  8. .. 

 

它会去调用initResourceCodes方法:

  
  
  
  
  1. protected void initResourceCodes(List<Portlet> portlets) throws Exception { 
  2.         long[] companyIds = PortalInstances.getCompanyIdsBySQL(); 
  3.  
  4.         Iterator<Portlet> itr = portlets.iterator(); 
  5.  
  6.         while (itr.hasNext()) { 
  7.             Portlet portlet = itr.next(); 
  8.  
  9.             List<String> modelNames = 
  10.                 ResourceActionsUtil.getPortletModelResources( 
  11.                     portlet.getPortletId()); 
  12.  
  13.             for (long companyId : companyIds) { 
  14.                 ResourceCodeLocalServiceUtil.checkResourceCodes( 
  15.                     companyId, portlet.getPortletId()); 
  16.  
  17.                 for (String modelName : modelNames) { 
  18.                     ResourceCodeLocalServiceUtil.checkResourceCodes( 
  19.                         companyId, modelName); 
  20.                 } 
  21.             } 
  22.         } 
  23.     } 

 

我们逐行分析:

 

02行:

02行会发起一个数据库的查询,来获取Liferay实例对应的数据库中存放的所有company的id,这个方法在PortalInstances类中:

  
  
  
  
  1. private long[] _getCompanyIdsBySQL() throws SQLException { 
  2.         List<Long> companyIds = new ArrayList<Long>(); 
  3.  
  4.         ...
  5.         try { 
  6.             con = DataAccess.getConnection(); 
  7.  
  8.             ps = con.prepareStatement(_GET_COMPANY_IDS); 
  9.  
  10.             rs = ps.executeQuery(); 
  11.  
  12.             while (rs.next()) { 
  13.                 long companyId = rs.getLong("companyId"); 
  14.  
  15.                 companyIds.add(companyId); 
  16.             } 
  17.         } 
  18.         finally { 
  19.             DataAccess.cleanUp(con, ps, rs); 
  20.         } 
  21.  
  22.         return ArrayUtil.toArray( 
  23.             companyIds.toArray(new Long[companyIds.size()])); 
  24.     } 

 

04-06行会迭代所有的portlet,对于每一个portlet:

 

09-11行:

09-11行会根据这个portlet的id来获取这个portlet的model的名字列表,它会调用ResourceActionUtil类的getPortletModelResources方法:

  
  
  
  
  1. public static List<String> getPortletModelResources(String portletName) { 
  2.     return getResourceActions().getPortletModelResources(portletName); 

进而最终调用ResourceActionsImpl的getPortletModelResources方法:

  
  
  
  
  1. public List<String> getPortletModelResources(String portletName) { 
  2.         portletName = PortletConstants.getRootPortletId(portletName); 
  3.  
  4.         Set<String> resources = _portletModelResources.get(portletName); 
  5.  
  6.         if (resources == null) { 
  7.             return new UniqueList<String>(); 
  8.         } 
  9.         else { 
  10.             return Collections.list(Collections.enumeration(resources)); 
  11.         } 
  12.     } 

从这里我们可以看出,它是从_portletModelResources中获取与portletName对应的Model名字列表,怎么获得呢,见readModelResource方法:

  
  
  
  
  1. protected void readModelResource( 
  2.         String servletContextName, Element modelResourceElement) { 
  3.  
  4.         String name = modelResourceElement.elementTextTrim("model-name"); 
  5.  
  6.         Element portletRefElement = modelResourceElement.element("portlet-ref"); 
  7.  
  8.         for (Element portletNameElement : 
  9.                 portletRefElement.elements("portlet-name")) { 
  10.  
  11.             String portletName = portletNameElement.getTextTrim(); 
  12.  
  13.             if (servletContextName != null) { 
  14.                 portletName = 
  15.                     portletName.concat(PortletConstants.WAR_SEPARATOR).concat( 
  16.                         servletContextName); 
  17.             } 
  18.  
  19.             portletName = portal.getJsSafePortletId(portletName); 
  20.  
  21.             // Reference for a portlet to child models 
  22.  
  23.             Set<String> modelResources = _portletModelResources.get( 
  24.                 portletName); 
  25.  
  26.             if (modelResources == null) { 
  27.                 modelResources = new HashSet<String>(); 
  28.  
  29.                 _portletModelResources.put(portletName, modelResources); 
  30.             } 
  31.  
  32.             modelResources.add(name); 
  33.  
  34.             // Reference for a model to parent portlets 
  35.  
  36.             Set<String> portletResources = _modelPortletResources.get(name); 
  37.  
  38.             if (portletResources == null) { 
  39.                 portletResources = new HashSet<String>(); 
  40.  
  41.                 _modelPortletResources.put(name, portletResources); 
  42.             } 
  43.  
  44.             portletResources.add(portletName); 
  45.         } 
  46.  
  47.         List<String> supportsActions = readSupportsActions( 
  48.             modelResourceElement, _modelResourceActions, name); 
  49.  
  50.         checkModelActions(supportsActions); 
  51.  
  52.         setActions(_modelResourceActions, name, supportsActions); 
  53.  
  54.         readGroupDefaultActions( 
  55.             modelResourceElement, _modelResourceGroupDefaultActions, name); 
  56.  
  57.         List<String> guestDefaultActions = readGuestDefaultActions( 
  58.             modelResourceElement, _modelResourceGuestDefaultActions, name); 
  59.  
  60.         readGuestUnsupportedActions( 
  61.             modelResourceElement, _modelResourceGuestUnsupportedActions, name, 
  62.             guestDefaultActions); 
  63.  
  64.         setActions( 
  65.             _modelResourceGuestDefaultActions, name, guestDefaultActions); 
  66.  
  67.         readOwnerDefaultActions( 
  68.             modelResourceElement, _modelResourceOwnerDefaultActions, name); 
  69.     } 

 

分析第13-15行

它会遍历每一个companyId,然后调用ResourceCodeLocalServiceUtil的checkResourceCodes方法来检查Resouce codes:

  
  
  
  
  1. public static void checkResourceCodes(long companyId, java.lang.String name) 
  2.         throws com.liferay.portal.kernel.exception.SystemException { 
  3.         getService().checkResourceCodes(companyId, name); 
  4.     } 

它最终会委托ResouceCodeLocalServiceImpl的checkResourceCodes方法:

  
  
  
  
  1. @Transactional(propagation = Propagation.SUPPORTS, readOnly = true
  2.     public void checkResourceCodes(long companyId, String name) 
  3.         throws SystemException { 
  4.  
  5.         if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) { 
  6.             return
  7.         } 
  8.  
  9.         getResourceCode(companyId, name, ResourceConstants.SCOPE_COMPANY); 
  10.         getResourceCode(companyId, name, ResourceConstants.SCOPE_GROUP); 
  11.         getResourceCode( 
  12.             companyId, name, ResourceConstants.SCOPE_GROUP_TEMPLATE); 
  13.         getResourceCode(companyId, name, ResourceConstants.SCOPE_INDIVIDUAL); 
  14.     } 

它先依然检查user check permission,然后对各个scope (company,group等),分别发起数据库查询(封装在如下的fetchByC_N_S方法中)来获取响应的ResourceCode:

  
  
  
  
  1. public ResourceCode getResourceCode(long companyId, String name, int scope) 
  2.         throws SystemException { 
  3.  
  4.         // Always cache the resource code. This table exists to improve 
  5.         // performance. Create the resource code if one does not exist. 
  6.  
  7.         if (Validator.isNull(name)) { 
  8.             name = StringPool.BLANK; 
  9.         } 
  10.  
  11.         String key = encodeKey(companyId, name, scope); 
  12.  
  13.         ResourceCode resourceCode = _resourceCodes.get(key); 
  14.  
  15.         if (resourceCode == null) { 
  16.             resourceCode = resourceCodePersistence.fetchByC_N_S( 
  17.                 companyId, name, scope); 
  18.  
  19.             if (resourceCode == null) { 
  20.                 resourceCode = resourceCodeLocalService.addResourceCode( 
  21.                     companyId, name, scope); 
  22.             } 
  23.  
  24.             _resourceCodes.put(key, resourceCode); 
  25.         } 
  26.  
  27.         return resourceCode; 
  28.     } 

 

17-19行,最终也是执行数据库查询,这里不再展开。

你可能感兴趣的:(Liferay启动过程分析)