| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| IndexDownloader |
|
| 1.5;1.5 |
| 1 | /** | |
| 2 | * Distribution License: | |
| 3 | * JSword is free software; you can redistribute it and/or modify it under | |
| 4 | * the terms of the GNU Lesser General Public License, version 2.1 or later | |
| 5 | * as published by the Free Software Foundation. This program is distributed | |
| 6 | * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
| 7 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 8 | * See the GNU Lesser General Public License for more details. | |
| 9 | * | |
| 10 | * The License is available on the internet at: | |
| 11 | * http://www.gnu.org/copyleft/lgpl.html | |
| 12 | * or by writing to: | |
| 13 | * Free Software Foundation, Inc. | |
| 14 | * 59 Temple Place - Suite 330 | |
| 15 | * Boston, MA 02111-1307, USA | |
| 16 | * | |
| 17 | * © CrossWire Bible Society, 2005 - 2016 | |
| 18 | * | |
| 19 | */ | |
| 20 | package org.crosswire.jsword.util; | |
| 21 | ||
| 22 | import java.io.IOException; | |
| 23 | import java.net.URI; | |
| 24 | ||
| 25 | import org.crosswire.common.util.NetUtil; | |
| 26 | import org.crosswire.jsword.book.Book; | |
| 27 | import org.crosswire.jsword.book.BookException; | |
| 28 | import org.crosswire.jsword.book.install.InstallException; | |
| 29 | import org.crosswire.jsword.book.install.Installer; | |
| 30 | import org.crosswire.jsword.index.IndexManager; | |
| 31 | import org.crosswire.jsword.index.IndexManagerFactory; | |
| 32 | import org.crosswire.jsword.index.IndexStatus; | |
| 33 | ||
| 34 | /** | |
| 35 | * . | |
| 36 | * | |
| 37 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
| 38 | * @author Joe Walker | |
| 39 | */ | |
| 40 | public final class IndexDownloader { | |
| 41 | /** | |
| 42 | * Prevent instantiation | |
| 43 | */ | |
| 44 | 0 | private IndexDownloader() { |
| 45 | 0 | } |
| 46 | ||
| 47 | /** | |
| 48 | * Download and install a search index | |
| 49 | * | |
| 50 | * @param book | |
| 51 | * The book to get an index for | |
| 52 | * @param installer | |
| 53 | * @throws IOException | |
| 54 | * @throws InstallException | |
| 55 | * @throws BookException | |
| 56 | */ | |
| 57 | public static void downloadIndex(Book book, Installer installer) throws IOException, InstallException, BookException { | |
| 58 | // Get a temp home | |
| 59 | 0 | URI tempDownload = NetUtil.getTemporaryURI(TEMP_PREFIX, TEMP_SUFFIX); |
| 60 | ||
| 61 | 0 | IndexStatus finalStatus = IndexStatus.UNDONE; |
| 62 | try { | |
| 63 | // Now we know what installer to use, download to the temp file | |
| 64 | 0 | installer.downloadSearchIndex(book, tempDownload); |
| 65 | ||
| 66 | // And install from that file. | |
| 67 | 0 | IndexManager idxman = IndexManagerFactory.getIndexManager(); |
| 68 | 0 | book.setIndexStatus(IndexStatus.CREATING); |
| 69 | 0 | idxman.installDownloadedIndex(book, tempDownload); |
| 70 | 0 | finalStatus = IndexStatus.DONE; |
| 71 | } finally { | |
| 72 | 0 | book.setIndexStatus(finalStatus); |
| 73 | // tidy up after ourselves | |
| 74 | 0 | if (tempDownload != null) { |
| 75 | 0 | NetUtil.delete(tempDownload); |
| 76 | } | |
| 77 | } | |
| 78 | 0 | } |
| 79 | ||
| 80 | /** | |
| 81 | * Temp file prefix | |
| 82 | */ | |
| 83 | private static final String TEMP_PREFIX = "jsword-index"; | |
| 84 | ||
| 85 | /** | |
| 86 | * Temp file suffix | |
| 87 | */ | |
| 88 | private static final String TEMP_SUFFIX = "dat"; | |
| 89 | } |