Ejemplo n.º 1
0
  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);
      }
    }

    // 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;
  }
  protected void reindex(ActionRequest actionRequest) throws Exception {
    String portletId = ParamUtil.getString(actionRequest, "portletId");

    long[] companyIds = PortalInstances.getCompanyIds();

    if (LuceneHelperUtil.isLoadIndexFromClusterEnabled()) {
      MessageValuesThreadLocal.setValue(ClusterLink.CLUSTER_FORWARD_MESSAGE, true);
    }

    Set<String> usedSearchEngineIds = new HashSet<String>();

    if (Validator.isNull(portletId)) {
      for (long companyId : companyIds) {
        try {
          LuceneIndexer luceneIndexer = new LuceneIndexer(companyId);

          luceneIndexer.reindex();

          usedSearchEngineIds.addAll(luceneIndexer.getUsedSearchEngineIds());
        } catch (Exception e) {
          _log.error(e, e);
        }
      }
    } else {
      Portlet portlet = PortletLocalServiceUtil.getPortletById(companyIds[0], portletId);

      if (portlet == null) {
        return;
      }

      List<Indexer> indexers = portlet.getIndexerInstances();

      if (indexers == null) {
        return;
      }

      Set<String> searchEngineIds = new HashSet<String>();

      for (Indexer indexer : indexers) {
        searchEngineIds.add(indexer.getSearchEngineId());
      }

      for (String searchEngineId : searchEngineIds) {
        for (long companyId : companyIds) {
          SearchEngineUtil.deletePortletDocuments(searchEngineId, companyId, portletId);
        }
      }

      for (Indexer indexer : indexers) {
        for (long companyId : companyIds) {
          ShardUtil.pushCompanyService(companyId);

          try {
            indexer.reindex(new String[] {String.valueOf(companyId)});

            usedSearchEngineIds.add(indexer.getSearchEngineId());
          } catch (Exception e) {
            _log.error(e, e);
          } finally {
            ShardUtil.popCompanyService();
          }
        }
      }
    }

    if (LuceneHelperUtil.isLoadIndexFromClusterEnabled()) {
      Set<BaseAsyncDestination> searchWriterDestinations = new HashSet<BaseAsyncDestination>();

      MessageBus messageBus = MessageBusUtil.getMessageBus();

      for (String usedSearchEngineId : usedSearchEngineIds) {
        String searchWriterDestinationName =
            SearchEngineUtil.getSearchWriterDestinationName(usedSearchEngineId);

        Destination destination = messageBus.getDestination(searchWriterDestinationName);

        if (destination instanceof BaseAsyncDestination) {
          BaseAsyncDestination baseAsyncDestination = (BaseAsyncDestination) destination;

          searchWriterDestinations.add(baseAsyncDestination);
        }
      }

      submitClusterIndexLoadingSyncJob(searchWriterDestinations, companyIds);
    }
  }