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

📄 collectionmanagerimpl.java

📁 很好的搜索代码,大家都很难下载!抓紧时间啊!不要错过!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                if (c != null) {
                    log.debug("Returning Analyzer: " + analyzerClassName);
                    analyzerObject = (Analyzer) c.newInstance();
                }
            }
        }
        catch (InstantiationException e1) {
            log.debug("Can not initiate Analyzer '" + analyzerClassName, e1);
        }
        catch (IllegalAccessException e1) {
            log.debug("Can not access Analyzer " + analyzerClassName, e1);
        }
        catch (ClassNotFoundException e) {
            log.debug("Class not found: " + analyzerClassName, e);
        }
    }

    /**
     * The default cache base directory for all collections. The cache is the directory on disk where zipped content is unzipped for
     * indexing.
     * 
     * @param thisDir the directory on disk
     */
    public void setCacheBaseDir(final File thisDir) {
        cacheBaseDir = thisDir;
    }

    /**
     * The default index base directory for all collections. The index is the directory on disk where a Lucene index is stored.
     * 
     * @param thisDir the directory on disk
     * 
     * @see org.apache.lucene.index.IndexReader
     */
    public void setIndexBaseDir(final File thisDir) {
        indexBaseDir = thisDir;
    }

    /**
     * Indicates whether a Collection cache should be kept after indexing. The value of this CollectionManagerImpl functions as
     * default for all Collections.
     * 
     * @param b keep cache or not.
     */
    public void setKeepCache(final boolean b) {
        keepCache = b;
    }

    /**
     * @return Returns the allAnalyzers.
     */
    public String[] getAllAnalyzers() {
        return allAnalyzers;
    }

    /**
     * get the ArchiveHandler, which contains the mappings for unArchiving archives.
     * 
     * @return object containing mappings for handling archives
     */
    public Handler getArchiveHandler() {
        return archiveHandler;
    }

    /**
     * @return Returns the factory.
     */
    public ExtractorFactory getFactory() {
        return factory;
    }

    /**
     * Set the ArchiveHandler.
     * 
     * @param handler object containing mappings for handling archives
     */
    public void setArchiveHandler(final Handler handler) {
        archiveHandler = handler;
    }

    /**
     * @param thatFactory The factory to set.
     */
    public void setFactory(final ExtractorFactory thatFactory) {
        this.factory = thatFactory;
    }

    /**
     * Expands Archives to disk. This is used is 'on-the-fly' extraction from cache
     * 
     * @param col the Collection to which cache this archive is extracted
     * @param zip the archive or directory that might contain archives
     * 
     * @return true if archive(s) could be extracted
     * 
     * @throws IndexException on error
     * 
     * @see org.zilverline.web.CacheController
     */
    public boolean expandArchive(final FileSystemCollection col, final File zip) throws IndexException {
        log.debug("getFromCache: document " + zip + " from : " + col.getName());

        if (!zip.exists()) {
            log.warn(zip + " does not exist.");

            return false;
        }

        // in the recursion we could have a directory
        if (zip.isDirectory()) {
            File[] files = zip.listFiles();

            for (int i = 0; i < files.length; i++) {
                expandArchive(col, files[i]);
            }
        } else {
            String extension = FileUtils.getExtension(zip);

            if ((archiveHandler != null) && archiveHandler.canUnPack(extension)) {
                // we have an archive
                log.debug(zip + " is an archive");

                File dir = null;

                if (StringUtils.hasText(archiveHandler.getUnArchiveCommand(extension))) {
                    // this is a zip: handle with java's zip capabilities
                    log.debug(zip + " is a zip file");
                    dir = unZip(zip, col);
                } else {
                    log.debug(zip + " is a external archive file");
                    dir = unPack(zip, col);
                }

                // recursively handle all archives in this one
                log.debug("Recurse into " + dir);
                File[] files = dir.listFiles();

                for (int i = 0; i < files.length; i++) {
                    expandArchive(col, files[i]);
                }

                return true;
            } else {
                if (archiveHandler == null || archiveHandler.getMappings() == null) {
                    log.warn("Can't extract this type, no archiveHandler");
                } else {
                    log.warn("Can't extract this type, not a supported extension: " + extension);
                }
            }
        }

        return false;
    }

    /**
     * 'unpacks' a given archive file into cache directory with derived name. e.g. c:\temp\file.chm wil be unpacked into
     * [cacheDir]\file_chm\.
     * 
     * @param sourceFile the Archive file to be unpacked
     * @param thisCollection the collection whose cache and contenDir is used
     * 
     * @return File (new) directory containing unpacked file, null if unknown Archive
     */
    public File unPack(final File sourceFile, final FileSystemCollection thisCollection) {
        File unPackDestinationDirectory = null;
        // based on file extension, lookup in the archiveHandler whether this is a known archive
        // we don't really have to do this, since it is already been checked in the calling indexDocs(), but better safe then sorry

        if (archiveHandler == null) {
            // we have an unknown archive
            log.warn("No archiveHandler found while trying to unPack " + sourceFile);
            return null;
        }

        String extension = FileUtils.getExtension(sourceFile);

        if (!archiveHandler.canUnPack(extension)) {
            // we have an unknown archive
            log.warn("No archiveHandler found for " + sourceFile);
            return null;
        }

        // Create destination where file will be unpacked
        unPackDestinationDirectory = file2CacheDir(sourceFile, thisCollection);
        log.debug("unpacking " + sourceFile + " into " + unPackDestinationDirectory);

        // get the command from the map by supplying the file extension
        String unArchiveCommand = archiveHandler.getUnArchiveCommand(extension);

        if (SysUtils.execute(unArchiveCommand, sourceFile, unPackDestinationDirectory)) {
            log.info("Executed: " + unArchiveCommand + " " + sourceFile + " in " + unPackDestinationDirectory);
        } else {
            log.warn("Can not execute " + unArchiveCommand + " " + sourceFile + " in " + unPackDestinationDirectory);
        }
        // delete the archive file if it is in the cache, we don't need to
        // store it, since we've extracted the contents
        if (FileUtils.isIn(sourceFile, thisCollection.getCacheDirWithManagerDefaults())) {
            sourceFile.delete();
        }

        return unPackDestinationDirectory;
    }

    /**
     * Takes a file and creates a directory with a derived name in the cacheDir. If the file was not already in cache, and sits in
     * contentDir it is mapped to cache, otherwise it stays within cache.
     * 
     * <p>
     * e.g. given cachedir <code>c:\temp\</code> and contentdir <code>e:\docs\Projects\lucene\content\</code>,
     * <code>e:\docs\Projects\lucene\content\books.zip</code> yields <code>c:\temp\books_zip\</code>
     * </p>
     * 
     * <p>
     * <code>c:\temp\books.zip</code> yields <code>c:\temp\books_zip\</code>
     * </p>
     * 
     * @param sourceFile the file to be used as name for the directory
     * @param thisCollection the collection (not null) whose cache and contenDir is used
     * 
     * @return File the (newly created) directory
     */
    public static File file2CacheDir(final File sourceFile, final FileSystemCollection thisCollection) {
        log.debug("Entering file2Dir, with " + sourceFile + ", for collection:" + thisCollection.getName());

        File unZipDestinationDirectory = null;

        try {
            File cacheDir = thisCollection.getCacheDirWithManagerDefaults();
            // just to be sure the cacheDir exists
            if (!cacheDir.isDirectory()) {
                if (!cacheDir.mkdirs()) {
                    log.warn("Can't create cache directory " + cacheDir);
                    return null;
                }
            }
            // get the full path (not just the name, since we could have recursed into newly created directory
            String destinationDirectory = sourceFile.getCanonicalPath();

            // change extension into _
            int index = destinationDirectory.lastIndexOf('.');
            String extension;

            if (index != -1) {
                extension = destinationDirectory.substring(index + 1);
                destinationDirectory = destinationDirectory.substring(0, index) + '_' + extension;
            }

            // if sourceFile still sits in contentdir it must be mapped to cache
            String collectionPath = thisCollection.getContentDir().getCanonicalPath();

            if (destinationDirectory.startsWith(collectionPath)) {
                // chop off the first part (collectionPath)
                String relativePath = destinationDirectory.substring(collectionPath.length());

                unZipDestinationDirectory = new File(thisCollection.getCacheDirWithManagerDefaults(), relativePath);
                log.debug("Mapped " + relativePath + " to cache: " + thisCollection.getCacheDirWithManagerDefaults());
            } else {
                unZipDestinationDirectory = new File(destinationDirectory);
            }

            // actually create the directory
            boolean canCreate = unZipDestinationDirectory.mkdirs();

            if (!canCreate) {
                log.warn("Could not create: " + unZipDestinationDirectory);
            }

            log.debug("Created: " + unZipDestinationDirectory + " from File: " + sourceFile);
        }
        catch (Exception e) {
            log.error("error creating directory from file: " + sourceFile, e);
        }

        return unZipDestinationDirectory;
    }

    /**
     * unZips a given zip file into cache directory with derived name. e.g. c:\temp\file.zip wil be unziiped into
     * [cacheDir]\file_zip\.
     * 
     * @param sourceZipFile the ZIP file to be unzipped
     * @param thisCollection the collection whose cache and contenDir is used
     * 
     * @return File (new) directory containing zip file
     */
    public static File unZip(final File sourceZipFile, final FileSystemCollection thisCollection) {
        // specify buffer size for extraction
        final int aBUFFER = 2048;
        File unzipDestinationDirectory = null;
        ZipFile zipFile = null;
        FileOutputStream fos = null;
        BufferedOutputStream dest = null;
        BufferedInputStream bis = null;

        try {
            // Specify destination where file will be unzipped
            unzipDestinationDirectory = file2CacheDir(sourceZipFile, thisCollection);
            log.info("unzipping " + sourceZipFile + " into " + unzipDestinationDirectory);
            // Open Zip file for reading
            zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
            // Create an enumeration of the entries in the zip file
            Enumeration zipFileEntries = zipFile.entries();
            while (zipFileEntries.hasMoreElements()) {
                // grab a zip file entry
                ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
                String currentEntry = entry.getName();
                log.debug("Extracting: " + entry);
                File destFile = new File(unzipDestinationDirectory, currentEntry);
                // grab file's parent directory structure
                File destinationParent = destFile.getParentFile();
                // create the parent directory structure if needed
                destinationParent.mkdirs();
                // extract file if not a directory
                if (!entry.isDirectory()) {
                    bis = new BufferedInputStream(zipFile.getInputStream(entry));
                    int currentByte;
                    // establish buffer for writing file
                    byte[] data = new byte[aBUFFER];
                    // write the current file to disk
                    fos = new FileOutputStream(destFile);
                    dest = new BufferedOutputStream(fos, aBUFFER);
                    // read and write until last byte is encountered
                    while ((currentByte = bis.read(data, 0, aBUFFER)) != -1) {
                        dest.write(data, 0, currentByte);
                    }
                    dest.flush();
                    dest.close();
                    bis.close();
                }
            }
            zipFile.close();
            // delete the zip file if it is in the cache, we don't need to store
            // it, since we've extracted the contents
            if (FileUtils.isIn(sourceZipFile, thisCollection.getCacheDirWithManagerDefaults())) {
                sourceZipFile.delete();
            }
        }
        catch (Exception e) {
            log.error("Can't unzip: " + sourceZipFile, e);
        }
        finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (dest != null) {
                    dest.close();
                }
                if (bis != null) {
                    bis.close();
                }
            }
            catch (IOException e1) {
                log.error("Error closing files", e1);
            }
        }

        return unzipDestinationDirectory;
    }

    /**
     * @return Returns the allExtractors.
     */
    public String[] getAllExtractors() {
        return allExtractors;
    }

    /**
     * @return Returns the mergeFactor.
     */
    public Integer getMergeFactor() {
        return mergeFactor;
    }

    /**
     * @param mergeFactor The mergeFactor to set.
     */
    public void setMergeFactor(Integer mergeFactor) {
        this.mergeFactor = mergeFactor;
    }

    /**
     * @return Returns the priority.
     */
    public Integer getPriority() {
        return priority;
    }

    /**
     * @param priority The priority to set.
     */
    public void setPriority(Integer priority) {
        this.priority = priority;
    }

    /**
     * @return Returns the maxMergeDocs.
     */
    public Integer getMaxMergeDocs() {
        return maxMergeDocs;
    }

    /**
     * @param maxMergeDocs The maxMergeDocs to set.
     */
    public void setMaxMergeDocs(Integer maxMergeDocs) {
        this.maxMergeDocs = maxMergeDocs;
    }

    /**
     * @return Returns the minMergeDocs.
     */
    public Integer getMinMergeDocs() {
        return minMergeDocs;
    }

    /**
     * @param minMergeDocs The minMergeDocs to set.
     */
    public void setMinMergeDocs(Integer minMergeDocs) {
        this.minMergeDocs = minMergeDocs;
    }
}

⌨️ 快捷键说明

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