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

📄 collectionmanagerimpl.java

📁 很好的搜索代码,大家都很难下载!抓紧时间啊!不要错过!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2003-2004 Michael Franken, Zilverline.
 *
 * The contents of this file, or the files included with this file, are subject to
 * the current version of ZILVERLINE Collaborative Source License for the
 * Zilverline Search Engine (the "License"); You may not use this file except in
 * compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 *     http://www.zilverline.org.
 *
 * See the License for the rights, obligations and
 * limitations governing use of the contents of the file.
 *
 * The Original and Upgraded Code is the Zilverline Search Engine. The developer of
 * the Original and Upgraded Code is Michael Franken. Michael Franken owns the
 * copyrights in the portions it created. All Rights Reserved.
 *
 */

package org.zilverline.service;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.springframework.util.StringUtils;
import org.zilverline.core.DocumentCollection;
import org.zilverline.core.ExtractorFactory;
import org.zilverline.core.FileSystemCollection;
import org.zilverline.core.Handler;
import org.zilverline.core.IndexException;
import org.zilverline.dao.CollectionManagerDAO;
import org.zilverline.dao.DAOException;
import org.zilverline.util.FileUtils;
import org.zilverline.util.SysUtils;

/**
 * The CollectionManagerImpl holds all collections, and base values for them.
 * 
 * <p>
 * NB. This Bean gets instantiated in web-servlet.xml.
 * </p>
 * 
 * @author Michael Franken
 * @version $Revision: 1.28 $
 * 
 * @see org.zilverline.core.FileSystemCollection
 */
public class CollectionManagerImpl implements CollectionManager {

    /**
     * @return Returns the analyzer.
     */
    public String getAnalyzer() {
        return analyzer;
    }

    /**
     * Gets a collection by id.
     * 
     * @param theId The id of the collection
     * 
     * @return Collection or null if not found
     */
    public DocumentCollection getCollection(final Long theId) {
        Iterator li = collections.iterator();
        while (li.hasNext()) {
            DocumentCollection c = (DocumentCollection) li.next();
            if (theId.equals(c.getId())) {
                return c;
            }
        }
        return null;
    }

    /**
     * DAO object taking care of persistence for CollectionManager and its collections.
     */
    private transient CollectionManagerDAO dao;

    /**
     * MergeFactor for indexing process.
     */
    private Integer mergeFactor;

    /**
     * minMergeDocs for indexing process.
     */
    private Integer minMergeDocs;

    /**
     * maxMergeDocs for indexing process.
     */
    private Integer maxMergeDocs;

    /**
     * priority for indexing process.
     */
    private Integer priority = new Integer(2);

    /**
     * @return Returns the dao.
     */
    public CollectionManagerDAO getDao() {
        return dao;
    }

    /**
     * Set the DAO for this CollectionManager.
     * 
     * @param thisDao The dao to set.
     */
    public void setDao(final CollectionManagerDAO thisDao) {
        this.dao = thisDao;
    }

    /** logger for Commons logging. */
    private static Log log = LogFactory.getLog(CollectionManagerImpl.class);

    /**
     * String representation of Analyzer. Stored to present to user for selection.
     */
    private String analyzer = "org.apache.lucene.analysis.standard.StandardAnalyzer";

    /** Array containing all available Analyzers. */
    private transient String[] allAnalyzers;

    /**
     * The Analyzer to be used in indexing and searching. StandardAnalyzer by default.
     */
    private transient Analyzer analyzerObject = new StandardAnalyzer();

    /**
     * The default cache base directory for all collections. The cache is the directory on disk where zipped content is unzipped for
     * indexing. By default use WEB-INF/cache
     */
    private File cacheBaseDir = new File(new File(this.getClass().getResource("/").getFile()).getParentFile(), "cache");

    /** The set of collections this CollectionManagerImpl manages. */
    // TODO refactor this into a Set
    private List collections = new ArrayList();

    /**
     * The default index base directory for all collections. The index is the directory on disk where a Lucene index is stored. By
     * default use WEB-INF/index
     * 
     * @see org.apache.lucene.index.IndexReader
     */
    private File indexBaseDir = new File(new File(this.getClass().getResource("/").getFile()).getParentFile(), "index");

    /** The default for all collections whether to keep cache dir after indexing. */
    private boolean keepCache = false;

    /**
     * The handler of archives, contains mappings for file extension to unarchiving programs.
     */
    private Handler archiveHandler;

    /**
     * Factory containing Extractor mapppings.
     */
    private ExtractorFactory factory = new ExtractorFactory();

    /**
     * Array containing all Extractors by name.
     */
    private transient String[] allExtractors;

    /**
     * Add or updates collection to list of collections, and sets the manager.
     * 
     * Update occurs when a collection with the same id is found in the collections. Assigns an ID to a new Collection.
     * 
     * @param col Collection containing documents
     */
    public void addCollection(final DocumentCollection col) {
        long maxId = 0L;
        // TODO: Not so efficient and clean
        if (col.getId() != null) {
            // try to find previous occurence in list
            for (int i = 0; i < collections.size(); i++) {
                DocumentCollection thisCollection = (DocumentCollection) collections.get(i);
                maxId = Math.max(maxId, thisCollection.getId().longValue());
                log.debug("max ID: " + maxId);
                if (col.getId().equals(thisCollection.getId())) {
                    log.debug("Updating collection " + col.getId() + ", " + col.getName() + " to Manager at location " + i);
                    collections.set(i, col);
                    thisCollection.setManager(this);
                    return;
                }
            }
        } else {
            for (int i = 0; i < collections.size(); i++) {
                DocumentCollection thisCollection = (DocumentCollection) collections.get(i);
                maxId = Math.max(maxId, thisCollection.getId().longValue());
                log.debug("max ID: " + maxId);
            }
        }
        // it must be a new, or non existing collection, add it
        col.setId(new Long(maxId + 1L));
        log.debug("Adding collection " + col.getId() + ", " + col.getName() + " to Manager at end");
        collections.add(col);
        col.setManager(this);
    }

    /**
     * Deletes collection from list of collections.
     * 
     * @param col Collection containing documents
     */
    public void deleteCollection(final DocumentCollection col) {
        for (int i = 0; i < collections.size(); i++) {
            DocumentCollection thisCollection = (DocumentCollection) collections.get(i);
            if (col.getId().equals(thisCollection.getId())) {
                collections.remove(i);
                break;
            }
        }
    }

    /**
     * Indicates whether any indexing is going on.
     * 
     * @return true if so.
     */
    public boolean isIndexingInProgress() {
        for (int i = 0; i < collections.size(); i++) {
            DocumentCollection thisCollection = (DocumentCollection) collections.get(i);
            if (thisCollection.isIndexingInProgress()) {
                return true;
            }
        }
        return false;
    }

    /**
     * Store the CollectionManager to store.
     * 
     * @throws IndexException when collectionManager can not be saved to underlying store
     */
    public void store() throws IndexException {
        if (dao != null) {
            try {
                dao.store(this);
            }
            catch (DAOException e) {
                throw new IndexException("Can not save IndexService", e);
            }
        } else {
            log.error("No DAO set for IndexService");
        }
    }

    /**
     * Returns an Analyzer for this collection based on configuration.
     * 
     * @return the Analyzer used to index and search this collection
     */

    // TODO: rework this, this should actually be getAnalyzer, but that doesn't
    // work because of hack above.
    public Analyzer createAnalyzer() {
        return analyzerObject;
    }

    /**
     * Get the cache base directory.
     * 
     * @return String the directory where the cache sits
     */
    public File getCacheBaseDir() {
        return cacheBaseDir;
    }

    /**
     * Gets a collection by name.
     * 
     * @param theName The name of the collection
     * 
     * @return Collection or null if not found
     */
    public DocumentCollection getCollectionByName(final String theName) {
        if (theName == null) {
            return null;
        }

        Iterator li = collections.iterator();

        while (li.hasNext()) {
            DocumentCollection c = (DocumentCollection) li.next();

            if (theName.equals(c.getName())) {
                return c;
            }
        }

        return null;
    }

    /**
     * Get all collections.
     * 
     * @return collections List of collections
     */
    public List getCollections() {
        return collections;
    }

    /**
     * Get the base directory for the index.
     * 
     * @return the directory
     */
    public File getIndexBaseDir() {
        return indexBaseDir;
    }

    /**
     * Initializes all collections.
     * 
     * @throws IndexException if one of the collections can not be initialized.
     */
    public void init() throws IndexException {
        allExtractors = ExtractorFactory.findExtractorsOnClasspath();
        allAnalyzers = Handler.findAnalyzersOnClasspath();

        CollectionManager thatManager = dao.load();
        if (thatManager != null) {
            this.cacheBaseDir = thatManager.getCacheBaseDir();
            this.indexBaseDir = thatManager.getIndexBaseDir();
            this.keepCache = thatManager.isKeepCache();
            this.analyzer = thatManager.getAnalyzer();
            this.archiveHandler = thatManager.getArchiveHandler();
            this.factory = thatManager.getFactory();
            this.priority = thatManager.getPriority();
            this.mergeFactor = thatManager.getMergeFactor();
            this.maxMergeDocs = thatManager.getMaxMergeDocs();
            this.minMergeDocs = thatManager.getMinMergeDocs();
            // if there is nothing, probably first time Zilverline runs

            collections.clear();
            Iterator li = thatManager.getCollections().iterator();
            try {
                while (li.hasNext()) {
                    DocumentCollection c = (DocumentCollection) li.next();
                    log.debug("Adding collection to manager: " + c.getName());
                    this.addCollection(c);
                    c.init();
                }
            }
            catch (IndexException e) {
                throw new IndexException("Error initializing all indexes in CollectionManagerImpl", e);
            }
        } else {
            // possibly first time Zilverline runs
            setFactory(new ExtractorFactory());
            setArchiveHandler(new Handler());

            if (getCollections().isEmpty()) {
                // try {
                // // make an initial Collection
                // Properties props = System.getProperties();
                // log.debug(props.toString());
                // String profile = System.getProperty("user.home");
                // if (profile != null) {
                // // probably on Windows
                // File myDocs = new File(profile);
                // if (myDocs.isDirectory()) {
                // log.info("Welcome to Zilverline. Creating initial collection at: " + myDocs);
                // // create a Collection
                // FileSystemCollection myCollection = new FileSystemCollection();
                // myCollection.setName("My Documents");
                // myCollection.setContentDir(myDocs);
                // addCollection(myCollection);
                // myCollection.init();
                // myCollection.indexInThread(true);
                // } else {
                // log.debug("Can't find " + myDocs + ", skip creating default collection");
                // }
                // } else {
                // log.debug("Can't find profile, skip creating default collection");
                // }
                //
                // } catch (IndexException e) {
                // log.warn("Can't initialize, skip creating default collection", e);
                // }
            }
        }
    }

    /**
     * The default for all collections whether to keep cache dir after indexing.
     * 
     * @return whether to keep the cache or not.
     */
    public boolean isKeepCache() {
        return keepCache;
    }

    /**
     * Create an Analyzer as specified by the given String.
     * 
     * @param analyzerClassName the name of the class. The class needs to be available on the classpath.
     */
    public void setAnalyzer(final String analyzerClassName) {
        try {
            if (analyzerClassName != null) {
                analyzer = analyzerClassName;

                Class c = Class.forName(analyzerClassName);

⌨️ 快捷键说明

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