在MainServlet中,当初始化Resource Code完毕后,就开始初始化公司信息,对应代码如下:
- if (_log.isDebugEnabled()) {
- _log.debug("Initialize companies");
- }
- try {
- initCompanies();
- }
- ..
它会调用initCompanies()方法:
- protected void initCompanies() throws Exception {
- ServletContext servletContext = getServletContext();
- String[] webIds = PortalInstances.getWebIds();
- for (int i = 0; i < webIds.length; i++) {
- PortalInstances.initCompany(servletContext, webIds[i]);
- }
- }
从这里可以看出,它先02行获得Servlet上下文,然后04行获得webId的数组:
分析04行:
我们跟进到PortalInstances的getWebIds方法:
- public static String[] getWebIds() {
- return _instance._getWebIds();
- }
它最终是调用_getWebIds()方法:
- private String[] _getWebIds() {
- if (_webIds != null) {
- return _webIds;
- }
- if (Validator.isNull(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
- throw new RuntimeException("Default web id must not be null");
- }
- try {
- List<Company> companies = CompanyLocalServiceUtil.getCompanies(
- false);
- List<String> webIdsList = new ArrayList<String>(companies.size());
- for (Company company : companies) {
- String webId = company.getWebId();
- if (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
- webIdsList.add(0, webId);
- }
- else {
- webIdsList.add(webId);
- }
- }
- _webIds = webIdsList.toArray(new String[webIdsList.size()]);
- }
- catch (Exception e) {
- _log.error(e, e);
- }
- if ((_webIds == null) || (_webIds.length == 0)) {
- _webIds = new String[] {PropsValues.COMPANY_DEFAULT_WEB_ID};
- }
- return _webIds;
- }
在这个方法中,首先06行校验是否设置了company的default web id,这个值最终在portal.propeties中获得:
- #
- # This sets the default web id. Omniadmin users must belong to the company
- # with this web id.
- #
- company.default.web.id=liferay.com
然后第11行会调用 CompanyLocalServiceUtil类的getCompanies方法来获取Company列表,它最终会在CompanyLocalServiceImpl中发起一个数据库查询,见getCompanies(boolean)方法:
- public List<Company> getCompanies(boolean system) throws SystemException {
- return companyPersistence.findBySystem(system);
- }
然后后面代码就是遍历所有的Company对象,分离出webId,然后填入数组中,并且返回
分析06-08行:
既然04行已经获得了数据库中所有Company的webId,那么现在就是遍历webId,用这些webId来初始化Company.(从数据库信息构建一个一个的服务器内存中的对象):
我们跟进到PortalInstances的initCompany方法,它最终会调用_initCompany方法:
- private long _initCompany(ServletContext servletContext, String webId) {
- // Begin initializing company
- if (_log.isDebugEnabled()) {
- _log.debug("Begin initializing company with web id " + webId);
- }
- long companyId = 0;
- try {
- Company company = CompanyLocalServiceUtil.checkCompany(webId);
- companyId = company.getCompanyId();
- }
- catch (Exception e) {
- _log.error(e, e);
- }
- CompanyThreadLocal.setCompanyId(companyId);
- // Lucene
- LuceneHelperUtil.startup(companyId);
- // Initialize display
- if (_log.isDebugEnabled()) {
- _log.debug("Initialize display");
- }
- try {
- String xml = HttpUtil.URLtoString(servletContext.getResource(
- "/WEB-INF/liferay-display.xml"));
- PortletCategory portletCategory = (PortletCategory)WebAppPool.get(
- companyId, WebKeys.PORTLET_CATEGORY);
- if (portletCategory == null) {
- portletCategory = new PortletCategory();
- }
- PortletCategory newPortletCategory =
- PortletLocalServiceUtil.getEARDisplay(xml);
- portletCategory.merge(newPortletCategory);
- for (int i = 0; i < _companyIds.length; i++) {
- long currentCompanyId = _companyIds[i];
- PortletCategory currentPortletCategory =
- (PortletCategory)WebAppPool.get(
- currentCompanyId, WebKeys.PORTLET_CATEGORY);
- if (currentPortletCategory != null) {
- portletCategory.merge(currentPortletCategory);
- }
- }
- WebAppPool.put(
- companyId, WebKeys.PORTLET_CATEGORY, portletCategory);
- }
- catch (Exception e) {
- _log.error(e, e);
- }
- // Check journal content search
- if (_log.isDebugEnabled()) {
- _log.debug("Check journal content search");
- }
- if (GetterUtil.getBoolean(
- PropsUtil.get(
- PropsKeys.JOURNAL_SYNC_CONTENT_SEARCH_ON_STARTUP))) {
- try {
- JournalContentSearchLocalServiceUtil.checkContentSearches(
- companyId);
- }
- catch (Exception e) {
- _log.error(e, e);
- }
- }
- // LDAP Import
- try {
- if (LDAPSettingsUtil.isImportOnStartup(companyId)) {
- PortalLDAPImporterUtil.importFromLDAP(companyId);
- }
- }
- catch (Exception e) {
- _log.error(e, e);
- }
- // Process application startup events
- if (_log.isDebugEnabled()) {
- _log.debug("Process application startup events");
- }
- try {
- EventsProcessorUtil.process(
- PropsKeys.APPLICATION_STARTUP_EVENTS,
- PropsValues.APPLICATION_STARTUP_EVENTS,
- new String[] {String.valueOf(companyId)});
- }
- catch (Exception e) {
- _log.error(e, e);
- }
- // End initializing company
- if (_log.isDebugEnabled()) {
- _log.debug(
- "End initializing company with web id " + webId +
- " and company id " + companyId);
- }
- addCompanyId(companyId);
- return companyId;
- }
这里又做了很多事情:
第12行做了许多检查,确保company包含默认的用户和组。
第22-24行中为companyId建立Lucene索引。
第26-65行读取liferay-display.xml,并且吧companyId,以及每个portlet分类都放入WebAppPool中。
其他略。
所以,在这一步,所有的company信息都从数据库读出,并且正确的初始化了。