在MainServlet中,当初始化resource actions之后,就开始初始化resource code ,这是由以下代码来完成的:
- if (_log.isDebugEnabled()) {
- _log.debug("Initialize resource codes");
- }
- try {
- initResourceCodes(portlets);
- }
- ..
它会去调用initResourceCodes方法:
- protected void initResourceCodes(List<Portlet> portlets) throws Exception {
- long[] companyIds = PortalInstances.getCompanyIdsBySQL();
- Iterator<Portlet> itr = portlets.iterator();
- while (itr.hasNext()) {
- Portlet portlet = itr.next();
- List<String> modelNames =
- ResourceActionsUtil.getPortletModelResources(
- portlet.getPortletId());
- for (long companyId : companyIds) {
- ResourceCodeLocalServiceUtil.checkResourceCodes(
- companyId, portlet.getPortletId());
- for (String modelName : modelNames) {
- ResourceCodeLocalServiceUtil.checkResourceCodes(
- companyId, modelName);
- }
- }
- }
- }
我们逐行分析:
02行:
02行会发起一个数据库的查询,来获取Liferay实例对应的数据库中存放的所有company的id,这个方法在PortalInstances类中:
- private long[] _getCompanyIdsBySQL() throws SQLException {
- List<Long> companyIds = new ArrayList<Long>();
- ...
- try {
- con = DataAccess.getConnection();
- ps = con.prepareStatement(_GET_COMPANY_IDS);
- rs = ps.executeQuery();
- while (rs.next()) {
- long companyId = rs.getLong("companyId");
- companyIds.add(companyId);
- }
- }
- finally {
- DataAccess.cleanUp(con, ps, rs);
- }
- return ArrayUtil.toArray(
- companyIds.toArray(new Long[companyIds.size()]));
- }
04-06行会迭代所有的portlet,对于每一个portlet:
09-11行:
09-11行会根据这个portlet的id来获取这个portlet的model的名字列表,它会调用ResourceActionUtil类的getPortletModelResources方法:
- public static List<String> getPortletModelResources(String portletName) {
- return getResourceActions().getPortletModelResources(portletName);
- }
进而最终调用ResourceActionsImpl的getPortletModelResources方法:
- public List<String> getPortletModelResources(String portletName) {
- portletName = PortletConstants.getRootPortletId(portletName);
- Set<String> resources = _portletModelResources.get(portletName);
- if (resources == null) {
- return new UniqueList<String>();
- }
- else {
- return Collections.list(Collections.enumeration(resources));
- }
- }
从这里我们可以看出,它是从_portletModelResources中获取与portletName对应的Model名字列表,怎么获得呢,见readModelResource方法:
- protected void readModelResource(
- String servletContextName, Element modelResourceElement) {
- String name = modelResourceElement.elementTextTrim("model-name");
- Element portletRefElement = modelResourceElement.element("portlet-ref");
- for (Element portletNameElement :
- portletRefElement.elements("portlet-name")) {
- String portletName = portletNameElement.getTextTrim();
- if (servletContextName != null) {
- portletName =
- portletName.concat(PortletConstants.WAR_SEPARATOR).concat(
- servletContextName);
- }
- portletName = portal.getJsSafePortletId(portletName);
- // Reference for a portlet to child models
- Set<String> modelResources = _portletModelResources.get(
- portletName);
- if (modelResources == null) {
- modelResources = new HashSet<String>();
- _portletModelResources.put(portletName, modelResources);
- }
- modelResources.add(name);
- // Reference for a model to parent portlets
- Set<String> portletResources = _modelPortletResources.get(name);
- if (portletResources == null) {
- portletResources = new HashSet<String>();
- _modelPortletResources.put(name, portletResources);
- }
- portletResources.add(portletName);
- }
- List<String> supportsActions = readSupportsActions(
- modelResourceElement, _modelResourceActions, name);
- checkModelActions(supportsActions);
- setActions(_modelResourceActions, name, supportsActions);
- readGroupDefaultActions(
- modelResourceElement, _modelResourceGroupDefaultActions, name);
- List<String> guestDefaultActions = readGuestDefaultActions(
- modelResourceElement, _modelResourceGuestDefaultActions, name);
- readGuestUnsupportedActions(
- modelResourceElement, _modelResourceGuestUnsupportedActions, name,
- guestDefaultActions);
- setActions(
- _modelResourceGuestDefaultActions, name, guestDefaultActions);
- readOwnerDefaultActions(
- modelResourceElement, _modelResourceOwnerDefaultActions, name);
- }
分析第13-15行:
它会遍历每一个companyId,然后调用ResourceCodeLocalServiceUtil的checkResourceCodes方法来检查Resouce codes:
- public static void checkResourceCodes(long companyId, java.lang.String name)
- throws com.liferay.portal.kernel.exception.SystemException {
- getService().checkResourceCodes(companyId, name);
- }
它最终会委托ResouceCodeLocalServiceImpl的checkResourceCodes方法:
- @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
- public void checkResourceCodes(long companyId, String name)
- throws SystemException {
- if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
- return;
- }
- getResourceCode(companyId, name, ResourceConstants.SCOPE_COMPANY);
- getResourceCode(companyId, name, ResourceConstants.SCOPE_GROUP);
- getResourceCode(
- companyId, name, ResourceConstants.SCOPE_GROUP_TEMPLATE);
- getResourceCode(companyId, name, ResourceConstants.SCOPE_INDIVIDUAL);
- }
它先依然检查user check permission,然后对各个scope (company,group等),分别发起数据库查询(封装在如下的fetchByC_N_S方法中)来获取响应的ResourceCode:
- public ResourceCode getResourceCode(long companyId, String name, int scope)
- throws SystemException {
- // Always cache the resource code. This table exists to improve
- // performance. Create the resource code if one does not exist.
- if (Validator.isNull(name)) {
- name = StringPool.BLANK;
- }
- String key = encodeKey(companyId, name, scope);
- ResourceCode resourceCode = _resourceCodes.get(key);
- if (resourceCode == null) {
- resourceCode = resourceCodePersistence.fetchByC_N_S(
- companyId, name, scope);
- if (resourceCode == null) {
- resourceCode = resourceCodeLocalService.addResourceCode(
- companyId, name, scope);
- }
- _resourceCodes.put(key, resourceCode);
- }
- return resourceCode;
- }
17-19行,最终也是执行数据库查询,这里不再展开。