⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmssearchmanager.java

📁 一个cms内容管理平台
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            publishedResources = adminCms.readPublishedResources(publishHistoryId);
        } catch (CmsException e) {
            LOG.error(
                Messages.get().getBundle().key(Messages.LOG_READING_CHANGED_RESOURCES_FAILED_1, publishHistoryId),
                e);
            return;
        }

        List updateResources = new ArrayList();
        Iterator itPubRes = publishedResources.iterator();
        while (itPubRes.hasNext()) {
            CmsPublishedResource res = (CmsPublishedResource)itPubRes.next();
            if (res.isFolder() || res.isUnChanged() || !res.isVfsResource()) {
                // folders, unchanged resources and non vfs resources don't need to be indexed after publish
                continue;
            }
            if (res.isDeleted() || res.isNew() || res.isChanged()) {
                if (updateResources.contains(res)) {
                    // resource may have been added as a sibling of another resource
                    // in this case we make sure to use the value from the publih list because of the "deleted" flag
                    updateResources.remove(res);
                    // "equals()" implementation of published resource only checks for path, 
                    // so the removed value may have a different "deleted" or "modified" status value
                    updateResources.add(res);
                } else {
                    // resource not yet contained in the list
                    updateResources.add(res);
                    // check for the siblings (not for deleted resources, these are already gone)
                    if (!res.isDeleted() && (res.getSiblingCount() > 1)) {
                        // this resource has siblings                    
                        try {
                            // read siblings from the online project
                            List siblings = adminCms.readSiblings(res.getRootPath(), CmsResourceFilter.ALL);
                            Iterator itSib = siblings.iterator();
                            while (itSib.hasNext()) {
                                // check all siblings
                                CmsResource sibling = (CmsResource)itSib.next();
                                CmsPublishedResource sib = new CmsPublishedResource(sibling);
                                if (!updateResources.contains(sib)) {
                                    // ensure sibling is added only once
                                    updateResources.add(sib);
                                }
                            }
                        } catch (CmsException e) {
                            // ignore, just use the original resource
                            if (LOG.isWarnEnabled()) {
                                LOG.warn(Messages.get().getBundle().key(
                                    Messages.LOG_UNABLE_TO_READ_SIBLINGS_1,
                                    res.getRootPath()), e);
                            }
                        }
                    }
                }
            }
        }

        // cache for the generated documents (to avoid multiple text extraction in case of overlapping indexes)
        Map documentCache = Collections.synchronizedMap(new LRUMap(256));

        if (!updateResources.isEmpty()) {
            // sort the resource to update
            Collections.sort(updateResources);
            // only update the indexes if the list of remaining published resources is not empty
            Iterator i = m_indexes.iterator();
            while (i.hasNext()) {
                CmsSearchIndex index = (CmsSearchIndex)i.next();
                if (CmsSearchIndex.REBUILD_MODE_AUTO.equals(index.getRebuildMode())) {
                    // only update indexes which have the rebuild mode set to "auto"
                    try {
                        updateIndex(index, report, false, updateResources, documentCache);
                    } catch (CmsException e) {
                        LOG.error(
                            Messages.get().getBundle().key(Messages.LOG_UPDATE_INDEX_FAILED_1, index.getName()),
                            e);
                    }
                }
            }
        }
    }

    /**
     * Updates (if required creates) the index with the given name.<p>
     * 
     * If the optional List of <code>{@link CmsPublishedResource}</code> instances is provided, the index will be 
     * incrementally updated for these resources only. If this List is <code>null</code> or empty, 
     * the index will be fully rebuild.<p>
     * 
     * @param index the index to update or rebuild
     * @param report the report to write output messages to 
     * @param wait signals to wait until all the indexing threads are finished
     * @param resourcesToIndex an (optional) list of <code>{@link CmsPublishedResource}</code> objects to update in the index
     * @param documentCache a cache for the created search documents, to avoid multiple text extraction
     * 
     * @throws CmsException if something goes wrong
     */
    protected synchronized void updateIndex(
        CmsSearchIndex index,
        I_CmsReport report,
        boolean wait,
        List resourcesToIndex,
        Map documentCache) throws CmsException {

        // copy the stored admin context for the indexing
        CmsObject cms = OpenCms.initCmsObject(m_adminCms);
        // make sure a report is available
        if (report == null) {
            report = new CmsLogReport(cms.getRequestContext().getLocale(), CmsSearchManager.class);
        }

        // check if the index has been configured correctly
        if (!index.checkConfiguration(cms)) {
            // the index is disabled
            return;
        }

        // set site root and project for this index
        cms.getRequestContext().setSiteRoot("/");
        // switch to the index project
        cms.getRequestContext().setCurrentProject(cms.readProject(index.getProject()));

        if ((resourcesToIndex == null) || resourcesToIndex.isEmpty()) {
            // rebuild the complete index

            if (checkIndexLock(index, report)) {
                // unable to lock the index for updating
                try {
                    // try to force unlock on the index (full rebuild is done anyway)
                    IndexReader.unlock(FSDirectory.getDirectory(index.getPath(), false));
                } catch (Exception e) {
                    // unable to force unlock of Lucene index, we can't continue this way
                    CmsMessageContainer msg = Messages.get().container(
                        Messages.ERR_INDEX_LOCK_FAILED_1,
                        index.getName());
                    report.println(msg, I_CmsReport.FORMAT_ERROR);
                    throw new CmsIndexException(msg, e);
                }
            }

            // create a new thread manager for the indexing threads
            // please note: document cache _must_ be null for full rebuild 
            //              since there may be diffeences between online and offline projects,
            //              which can only be ignored if a resource has just been published (then online=offline)
            CmsIndexingThreadManager threadManager = new CmsIndexingThreadManager(
                report,
                Long.parseLong(m_timeout),
                index.getName(),
                null);

            IndexWriter writer = null;
            try {
                // create a new index writer
                writer = index.getIndexWriter(true);

                // ouput start information on the report
                report.println(
                    Messages.get().container(Messages.RPT_SEARCH_INDEXING_REBUILD_BEGIN_1, index.getName()),
                    I_CmsReport.FORMAT_HEADLINE);

                // iterate all configured index sources of this index
                Iterator sources = index.getSources().iterator();
                while (sources.hasNext()) {
                    // get the next index source
                    CmsSearchIndexSource source = (CmsSearchIndexSource)sources.next();
                    // create the indexer
                    I_CmsIndexer indexer = source.getIndexer().newInstance(cms, report, index);
                    // new index creation, use all resources from the index source
                    indexer.rebuildIndex(writer, threadManager, source);
                }

                // wait for indexing threads to finish
                while (wait && threadManager.isRunning()) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // just continue with the loop after interruption
                    }
                }
                // optimize the generated index
                try {
                    writer.optimize();
                } catch (IOException e) {
                    if (LOG.isWarnEnabled()) {
                        LOG.warn(Messages.get().getBundle().key(
                            Messages.LOG_IO_INDEX_WRITER_OPTIMIZE_1,
                            index.getPath(),
                            index.getName()), e);
                    }
                }

                // ouput finish information on the report
                report.println(
                    Messages.get().container(Messages.RPT_SEARCH_INDEXING_REBUILD_END_1, index.getName()),
                    I_CmsReport.FORMAT_HEADLINE);

            } finally {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException e) {
                        if (LOG.isWarnEnabled()) {
                            LOG.warn(Messages.get().getBundle().key(
                                Messages.LOG_IO_INDEX_WRITER_CLOSE_2,
                                index.getPath(),
                                index.getName()), e);
                        }
                    }
                }
            }

            // show information about indexing runtime
            threadManager.reportStatistics();

        } else {
            // update the existing index

            List updateCollections = new ArrayList();

            boolean hasResourcesToDelete = false;
            boolean hasResourcesToUpdate = false;

            // iterate all configured index sources of this index
            Iterator sources = index.getSources().iterator();
            while (sources.hasNext()) {
                // get the next index source
                CmsSearchIndexSource source = (CmsSearchIndexSource)sources.next();
                // create the indexer
                I_CmsIndexer indexer = source.getIndexer().newInstance(cms, report, index);
                // collect the resources to update
                CmsSearchIndexUpdateData updateData = indexer.getUpdateData(source, resourcesToIndex);
                if (!updateData.isEmpty()) {
                    // add the update collection to the internal pipeline
                    updateCollections.add(updateData);
                    hasResourcesToDelete = hasResourcesToDelete | updateData.hasResourcesToDelete();
                    hasResourcesToUpdate = hasResourcesToUpdate | updateData.hasResourceToUpdate();
                }
            }

            if (hasResourcesToDelete || hasResourcesToUpdate) {
                // ouput start information on the report
                report.println(
                    Messages.get().container(Messages.RPT_SEARCH_INDEXING_UPDATE_BEGIN_1, index.getName()),
                    I_CmsReport.FORMAT_HEADLINE);
            }

            if (checkIndexLock(index, report)) {
                // unable to lock the index for updating
                CmsMessageContainer msg = Messages.get().container(Messages.ERR_INDEX_LOCK_FAILED_1, index.getName());
                report.println(msg, I_CmsReport.FORMAT_ERROR);
                throw new CmsIndexException(msg);
            }

            if (hasResourcesToDelete) {
                // delete the resource from the index
                IndexReader reader = null;
                try {
                    reader = IndexReader.open(index.getPath());
                } catch (IOException e) {
                    LOG.error(Messages.get().getBundle().key(
                        Messages.LOG_IO_INDEX_READER_OPEN_2,
                        index.getPath(),
                        index.getName()), e);
                }
                if (reader != null) {
                    try {
                        Iterator i = updateCollections.iterator();
                        while (i.hasNext()) {
                            CmsSearchIndexUpdateData updateCollection = (CmsSearchIndexUpdateData)i.next();
                            if (updateCollection.hasResourcesToDelete()) {
                                updateCollection.getIndexer().deleteResources(
                                    reader,
                                    updateCollection.getResourcesToDelete());
                            }
                        }
                    } finally {
                        try {
                            // close the reader after all resources have been deleted
                            reader.close();
                        } catch (IOException e) {
                            LOG.error(Messages.get().getBundle().key(
                                Messages.LOG_IO_INDEX_READER_CLOSE_2,
                                index.getPath(),
                                index.getName()), e);
                        }
                    }
                }
            }

            if (hasResourcesToUpdate) {

                // create a new thread manager
                CmsIndexingThreadManager threadManager = new CmsIndexingThreadManager(
                    report,
                    Long.parseLong(m_timeout),
                    index.getName(),
                    documentCache);

                IndexWriter writer = null;
                try {

                    // create an index writer that updates the current index
                    writer = index.getIndexWriter(false);

                    Iterator i = updateCollections.iterator();
                    while (i.hasNext()) {
                        CmsSearchIndexUpdateData updateCollection = (CmsSearchIndexUpdateData)i.next();
                        if (updateCollection.hasResourceToUpdate()) {
                            updateCollection.getIndexer().updateResources(
                                writer,
                                threadManager,
                                updateCollection.getResourcesToUpdate());
                        }
                    }

                    // wait for indexing threads to finish
                    while (wait && threadManager.isRunning()) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // just continue with the loop after interruption
                        }
                    }

                } finally {
                    if (writer != null) {
                        try {
                            writer.close();
                        } catch (IOException e) {
                            LOG.error(Messages.get().getBundle().key(
                                Messages.LOG_IO_INDEX_WRITER_CLOSE_2,
                                index.getPath(),
                                index.getName()), e);
                        }
                    }
                }
            }

            if (hasResourcesToDelete || hasResourcesToUpdate) {
                // ouput finish information on the report
                report.println(
                    Messages.get().container(Messages.RPT_SEARCH_INDEXING_UPDATE_END_1, index.getName()),
                    I_CmsReport.FORMAT_HEADLINE);
            }
        }

        // clear the cache for search results
        m_resultCache.clear();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -