| IndexDownloader.java |
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 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * 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 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: IndexDownloader.java 2021 2010-11-25 03:31:42Z dmsmith $
21 */
22 package org.crosswire.jsword.util;
23
24 import java.io.IOException;
25 import java.net.URI;
26
27 import org.crosswire.common.util.NetUtil;
28 import org.crosswire.jsword.book.Book;
29 import org.crosswire.jsword.book.BookException;
30 import org.crosswire.jsword.book.install.InstallException;
31 import org.crosswire.jsword.book.install.Installer;
32 import org.crosswire.jsword.index.IndexManager;
33 import org.crosswire.jsword.index.IndexManagerFactory;
34 import org.crosswire.jsword.index.IndexStatus;
35
36 /**
37 * .
38 *
39 * @see gnu.lgpl.License for license details.<br>
40 * The copyright to this program is held by it's authors.
41 * @author Joe Walker [joe at eireneh dot com]
42 */
43 public final class IndexDownloader {
44 /**
45 * Prevent instantiation
46 */
47 private IndexDownloader() {
48 }
49
50 /**
51 * Download and install a search index
52 *
53 * @param book
54 * The book to get an index for
55 */
56 public static void downloadIndex(Book book, Installer installer) throws IOException, InstallException, BookException {
57 // Get a temp home
58 URI tempDownload = NetUtil.getTemporaryURI(TEMP_PREFIX, TEMP_SUFFIX);
59
60 IndexStatus finalStatus = IndexStatus.UNDONE;
61 try {
62 // Now we know what installer to use, download to the temp file
63 installer.downloadSearchIndex(book, tempDownload);
64
65 // And install from that file.
66 IndexManager idxman = IndexManagerFactory.getIndexManager();
67 book.setIndexStatus(IndexStatus.CREATING);
68 idxman.installDownloadedIndex(book, tempDownload);
69 finalStatus = IndexStatus.DONE;
70 } finally {
71 book.setIndexStatus(finalStatus);
72 // tidy up after ourselves
73 if (tempDownload != null) {
74 NetUtil.delete(tempDownload);
75 }
76 }
77 }
78
79 /**
80 * Temp file prefix
81 */
82 private static final String TEMP_PREFIX = "jsword-index";
83
84 /**
85 * Temp file suffix
86 */
87 private static final String TEMP_SUFFIX = "dat";
88 }
89