cmssearchmanager.java

来自「找了很久才找到到源代码」· Java 代码 · 共 1,663 行 · 第 1/5 页

JAVA
1,663
字号
    }

    /**
     * Sets the maximum age a text extraction result is kept in the cache (in hours) as a String.<p>
     *
     * @param extractionCacheMaxAge the maximum age for a text extraction result to set
     */
    public void setExtractionCacheMaxAge(String extractionCacheMaxAge) {

        try {
            setExtractionCacheMaxAge(Float.parseFloat(extractionCacheMaxAge));
        } catch (NumberFormatException e) {
            LOG.error(Messages.get().getBundle().key(
                Messages.LOG_PARSE_EXTRACTION_CACHE_AGE_FAILED_2,
                extractionCacheMaxAge,
                new Float(DEFAULT_EXTRACTION_CACHE_MAX_AGE)), e);
            setExtractionCacheMaxAge(DEFAULT_EXTRACTION_CACHE_MAX_AGE);
        }
    }

    /**
     * Sets the unlock mode during indexing.<p>
     * 
     * @param value the value 
     */
    public void setForceunlock(String value) {

        m_forceUnlockMode = CmsSearchForceUnlockMode.valueOf(value);
    }

    /**
     * Sets the highlighter.<p>
     *
     * A highlighter is a class implementing org.opencms.search.documents.I_TermHighlighter.<p>
     *
     * @param highlighter the package/class name of the highlighter
     */
    public void setHighlighter(String highlighter) {

        try {
            m_highlighter = (I_CmsTermHighlighter)Class.forName(highlighter).newInstance();
        } catch (Exception exc) {
            m_highlighter = null;
        }
    }

    /**
     * Sets the seconds to wait for an index lock during an update operation.<p>
     * 
     * @param value the seconds to wait for an index lock during an update operation
     */
    public void setIndexLockMaxWaitSeconds(int value) {

        m_indexLockMaxWaitSeconds = value;
    }

    /**
     * Sets the max. excerpt length.<p>
     *
     * @param maxExcerptLength the max. excerpt length to set
     */
    public void setMaxExcerptLength(int maxExcerptLength) {

        m_maxExcerptLength = maxExcerptLength;
    }

    /**
     * Sets the max. excerpt length as a String.<p>
     *
     * @param maxExcerptLength the max. excerpt length to set
     */
    public void setMaxExcerptLength(String maxExcerptLength) {

        try {
            setMaxExcerptLength(Integer.parseInt(maxExcerptLength));
        } catch (Exception e) {
            LOG.error(Messages.get().getBundle().key(
                Messages.LOG_PARSE_EXCERPT_LENGTH_FAILED_2,
                maxExcerptLength,
                new Integer(DEFAULT_EXCERPT_LENGTH)), e);
            setMaxExcerptLength(DEFAULT_EXCERPT_LENGTH);
        }
    }

    /**
     * Sets the timeout to abandon threads indexing a resource.<p>
     * 
     * @param value the timeout in milliseconds
     */
    public void setTimeout(long value) {

        m_timeout = value;
    }

    /**
     * Sets the timeout to abandon threads indexing a resource as a String.<p>
     * 
     * @param value the timeout in milliseconds
     */
    public void setTimeout(String value) {

        try {
            setTimeout(Long.parseLong(value));
        } catch (Exception e) {
            LOG.error(Messages.get().getBundle().key(
                Messages.LOG_PARSE_TIMEOUT_FAILED_2,
                value,
                new Long(DEFAULT_TIMEOUT)), e);
            setTimeout(DEFAULT_TIMEOUT);
        }
    }

    /**
     * Proceed the unlocking of the given index depending on the setting of <code>m_forceUnlockMode</code> and the given mode.<p>
     * 
     * @param index the index to check the lock for
     * @param report the report to write error messages on
     * @param mode the mode of the index process if true the index is updated otherwise it is rebuild completely
     * 
     * @throws CmsIndexException if unlocking of the index is impossible for some reasons
     */
    protected void forceIndexUnlock(CmsSearchIndex index, I_CmsReport report, boolean mode) throws CmsIndexException {

        File indexPath = new File(index.getPath());
        boolean indexLocked = true;
        // check if the target index path already exists
        if (indexPath.exists()) {
            // get the lock state of the given index
            try {
                indexLocked = IndexReader.isLocked(index.getPath());
            } catch (Exception e) {
                LOG.error(Messages.get().getBundle().key(
                    Messages.LOG_IO_INDEX_READER_OPEN_2,
                    index.getPath(),
                    index.getName()), e);
            }

            // if index is unlocked do nothing
            if (indexLocked) {
                if ((m_forceUnlockMode != null) && m_forceUnlockMode.equals(CmsSearchForceUnlockMode.ALWAYS)) {
                    try {
                        // try to force unlock on the index
                        IndexReader.unlock(FSDirectory.getDirectory(index.getPath()));
                    } 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);
                    }
                } else if ((m_forceUnlockMode != null) && m_forceUnlockMode.equals(CmsSearchForceUnlockMode.NEVER)) {
                    // wait if index will be unlocked during waiting
                    indexLocked = waitIndexLock(index, report, indexLocked);
                    // if index is still locked throw an exception
                    if (indexLocked) {
                        CmsMessageContainer msg = Messages.get().container(
                            Messages.ERR_INDEX_LOCK_FAILED_1,
                            index.getName());
                        report.println(msg, I_CmsReport.FORMAT_ERROR);
                        throw new CmsIndexException(msg);
                    }
                } else {
                    if (mode) {
                        // if index has to be updated wait if index will be unlocked during waiting
                        indexLocked = waitIndexLock(index, report, indexLocked);
                    }
                    // check if the index is locked
                    if (indexLocked) {
                        // mode equals update throw exception
                        if (mode) {
                            // 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);
                        } else {
                            try {
                                // try to force unlock on the index
                                IndexReader.unlock(FSDirectory.getDirectory(index.getPath()));
                            } 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);
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * Returns an analyzer for the given language.<p>
     * 
     * The analyzer is selected according to the analyzer configuration.<p>
     * 
     * @param locale the locale to get the analyzer for
     * @return the appropriate lucene analyzer
     * @throws CmsIndexException if something goes wrong
     */
    protected Analyzer getAnalyzer(Locale locale) throws CmsIndexException {

        Analyzer analyzer = null;
        String className = null;

        CmsSearchAnalyzer analyzerConf = (CmsSearchAnalyzer)m_analyzers.get(locale);
        if (analyzerConf == null) {
            throw new CmsIndexException(Messages.get().container(Messages.ERR_ANALYZER_NOT_FOUND_1, locale));
        }

        try {
            className = analyzerConf.getClassName();
            Class analyzerClass = Class.forName(className);

            // added param for snowball analyzer
            String stemmerAlgorithm = analyzerConf.getStemmerAlgorithm();
            if (stemmerAlgorithm != null) {
                analyzer = (Analyzer)analyzerClass.getDeclaredConstructor(new Class[] {String.class}).newInstance(
                    new Object[] {stemmerAlgorithm});
            } else {
                analyzer = (Analyzer)analyzerClass.newInstance();
            }

        } catch (Exception e) {
            throw new CmsIndexException(Messages.get().container(Messages.ERR_LOAD_ANALYZER_1, className), e);
        }

        // change analyzer for root path field to Whitespace analyzer
        PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(analyzer);
        wrapper.addAnalyzer(CmsSearchField.FIELD_ROOT, new WhitespaceAnalyzer());

        return wrapper;
    }

    /**
     * Returns a lucene document factory for given resource.<p>
     * 
     * The type of the document factory is selected by the type of the resource
     * and the MIME type of the resource content, according to the configuration in <code>opencms-search.xml</code>.<p>
     * 
     * @param resource a cms resource
     * @return a lucene document factory or null
     */
    protected I_CmsDocumentFactory getDocumentFactory(CmsResource resource) {

        // first get the MIME type of the resource
        String mimeType = OpenCms.getResourceManager().getMimeType(
            resource.getRootPath(),
            null,
            CmsResourceManager.MIMETYPE_TEXT);
        I_CmsDocumentFactory result = null;
        String typeName = null;
        try {
            typeName = OpenCms.getResourceManager().getResourceType(resource.getTypeId()).getTypeName();
        } catch (CmsLoaderException e) {
            // ignore, unknown resource type, resource can not be indexed
        }
        if (typeName != null) {
            // create the factory lookup key for the document
            String documentTypeKey = A_CmsVfsDocument.getDocumentKey(typeName, mimeType);
            // check if a setting is available for this specific MIME type
            result = (I_CmsDocumentFactory)m_documentTypes.get(documentTypeKey);
            if (result == null) {
                // no setting is available, try to use a generic setting without MIME type
                result = (I_CmsDocumentFactory)m_documentTypes.get(A_CmsVfsDocument.getDocumentKey(typeName, null));
                // please note: the result may still be null
            }
        }
        return result;
    }

    /**
     * Returns the set of names of all configured documenttypes.<p>
     * 
     * @return the set of names of all configured documenttypes
     */
    protected List getDocumentTypes() {

        List names = new ArrayList();
        for (Iterator i = m_documentTypes.values().iterator(); i.hasNext();) {
            I_CmsDocumentFactory factory = (I_CmsDocumentFactory)i.next();
            names.add(factory.getName());
        }

        return names;
    }

    /**
     * Initializes the available Cms resource types to be indexed.<p>
     * 
     * A map stores document factories keyed by a string representing
     * a colon separated list of Cms resource types and/or mimetypes.<p>
     * 
     * The keys of this map are used to trigger a document factory to convert 
     * a Cms resource into a Lucene index document.<p>
     * 
     * A document factory is a class implementing the interface
     * {@link org.opencms.search.documents.I_CmsDocumentFactory}.<p>
     */
    protected void initAvailableDocumentTypes() {

        CmsSearchDocumentType documenttype = null;
        String className = null;
        String name = null;
        I_CmsDocumentFactory documentFactory = null;
        List resourceTypes = null;
        List mimeTypes = null;
        Class c = null;

        m_documentTypes = new HashMap();

        for (int i = 0, n = m_documentTypeConfigs.size(); i < n; i++) {

            documenttype = (CmsSearchDocumentType)m_documentTypeConfigs.get(i);
            name = documenttype.getName();

            try {
                className = documenttype.getClassName();
                resourceTypes = documenttype.getResourceTypes();
                mimeTypes = documenttype.getMimeTypes();

                if (name == null) {
                    throw new CmsIndexException(Messages.get().container(Messages.ERR_DOCTYPE_NO_NAME_0));
                }
                if (className == null) {
                    throw new CmsIndexException(Messages.get().container(Messages.ERR_DOCTYPE_NO_CLASS_DEF_0));
                }
                if (resourceTypes.size() == 0) {

⌨️ 快捷键说明

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