Coverage Report - org.crosswire.jsword.index.lucene.InstalledIndex
 
Classes in this File Line Coverage Branch Coverage Complexity
InstalledIndex
0%
0/53
0%
0/10
1.8
 
 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, 2007 - 2016
 18  
  *
 19  
  */
 20  
 package org.crosswire.jsword.index.lucene;
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.net.URI;
 24  
 
 25  
 import org.crosswire.common.util.CWProject;
 26  
 import org.crosswire.common.util.FileUtil;
 27  
 import org.crosswire.common.util.NetUtil;
 28  
 import org.crosswire.common.util.PropertyMap;
 29  
 import org.crosswire.jsword.book.Book;
 30  
 import org.slf4j.Logger;
 31  
 import org.slf4j.LoggerFactory;
 32  
 
 33  
 
 34  
 /*
 35  
  * TODO(Sijo): do we need method reindexAllInstalledBooks() ? :
 36  
  * If this succeeds, update Installed.Index.DefaultVersion prop on the client computer
 37  
  */
 38  
 /**
 39  
  * A singleton that Reads and Maintains Installed Index Metadata (for e.g.
 40  
  * version indexed on client machine) in properties file If file does not exist
 41  
  * on the client, it will be created File location:
 42  
  * {WritableProjectDir}/JSword/lucene
 43  
  * /org.crosswire.jsword.index.lucene.InstalledIndex.properties
 44  
  *
 45  
  *
 46  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 47  
  * @author Sijo Cherian
 48  
  */
 49  
 public final class InstalledIndex {
 50  
     public static final String INSTALLED_INDEX_DEFAULT_VERSION = "Installed.Index.DefaultVersion";
 51  
     // Book's property key format
 52  
     // Installed.Index.Version.Book.Initial[module-version]
 53  
     public static final String PREFIX_INSTALLED_INDEX_VERSION_BOOK_OVERRIDE = "Installed.Index.Version.Book.";
 54  
     // TODO(Sijo): change this value on lucene upgrade
 55  
     /** The Index version for new indexes */
 56  
     public static final float DEFAULT_INSTALLED_INDEX_VERSION = IndexMetadata.INDEX_VERSION_1_2;
 57  
 
 58  
     /**
 59  
      * All access through this single instance.
 60  
      *
 61  
      * @return the singleton instance
 62  
      */
 63  
     public static InstalledIndex instance() {
 64  0
         return myInstance;
 65  
     }
 66  
 
 67  
     public float getInstalledIndexDefaultVersion() {
 68  0
         float toReturn = DEFAULT_INSTALLED_INDEX_VERSION;
 69  0
         String value = props.get(INSTALLED_INDEX_DEFAULT_VERSION);
 70  0
         if (value != null) {
 71  0
             toReturn = Float.parseFloat(value);
 72  
         }
 73  0
         return toReturn;
 74  
     }
 75  
 
 76  
     public float getInstalledIndexVersion(Book b) {
 77  0
         if (b == null) {
 78  0
             return getInstalledIndexDefaultVersion();
 79  
         }
 80  
 
 81  
         // e.g. look for Installed.Index.Version.Book.ESV[1.0.1] , else use
 82  
         // Installed.Index.DefaultVersion
 83  0
         String value = props.get(PREFIX_INSTALLED_INDEX_VERSION_BOOK_OVERRIDE + IndexMetadata.getBookIdentifierPropSuffix(b.getBookMetaData()),
 84  
                 props.get(INSTALLED_INDEX_DEFAULT_VERSION));
 85  
 
 86  0
         if (value == null) {
 87  0
             return DEFAULT_INSTALLED_INDEX_VERSION;
 88  
         }
 89  0
         return Float.parseFloat(value);
 90  
     }
 91  
 
 92  
     // Store the LatestIndexVersion for a book in the metadata file : typically
 93  
     // after a new index creation
 94  
     public void storeLatestVersionAsInstalledIndexMetadata(Book b) throws IOException {
 95  
 
 96  0
         synchronized (writeLock) {
 97  
 
 98  0
             props.put(PREFIX_INSTALLED_INDEX_VERSION_BOOK_OVERRIDE + IndexMetadata.getBookIdentifierPropSuffix(b.getBookMetaData()),
 99  
                     String.valueOf(IndexMetadata.instance().getLatestIndexVersion(b)));
 100  
 
 101  0
             NetUtil.storeProperties(props, getPropertyFileURI(), metadataFileComment);
 102  0
         }
 103  0
     }
 104  
 
 105  
     public URI getPropertyFileURI() {
 106  0
         return CWProject.instance().getWritableURI(LuceneIndexManager.DIR_LUCENE + "/" + getClass().getName(), FileUtil.EXTENSION_PROPERTIES);
 107  
     }
 108  
 
 109  
     protected void storeInstalledIndexMetadata() throws IOException {
 110  0
         synchronized (writeLock) {
 111  0
             NetUtil.storeProperties(props, getPropertyFileURI(), metadataFileComment);
 112  0
         }
 113  0
     }
 114  
 
 115  0
     private InstalledIndex() {
 116  0
         props = new PropertyMap();
 117  0
         URI propURI = getPropertyFileURI();
 118  
         try {
 119  
             // props = ResourceUtil.getProperties(getClass());
 120  
 
 121  0
             if (NetUtil.canRead(propURI)) {
 122  0
                 props = NetUtil.loadProperties(propURI);
 123  
             }
 124  
 
 125  
             /* Initial values if prop file empty */
 126  0
             if (props.size() == 0) {
 127  0
                 props.put(INSTALLED_INDEX_DEFAULT_VERSION, String.valueOf(DEFAULT_INSTALLED_INDEX_VERSION));
 128  0
                 storeInstalledIndexMetadata();
 129  
             }
 130  
 
 131  0
         } catch (IOException e) {
 132  0
             log.error("Property file read error: " + propURI.toString(), e);
 133  0
         }
 134  0
     }
 135  
 
 136  
     /**
 137  
      * Use this method to add/update custom property in the metadata file. Note:
 138  
      * If all the installed books indices have been upgraded/downloaded, client
 139  
      * can pass in property InstalledIndex.INSTALLED_INDEX_DEFAULT_VERSION =
 140  
      * <VersionToStore>, for e.g for client managed downloadable index
 141  
      * 
 142  
      * @param updateProps
 143  
      * @throws IOException
 144  
      */
 145  
     public void storeInstalledIndexMetadata(PropertyMap updateProps) throws IOException {
 146  
 
 147  0
         synchronized (writeLock) {
 148  0
             props.putAll(updateProps);
 149  0
             NetUtil.storeProperties(props, getPropertyFileURI(), metadataFileComment);
 150  0
         }
 151  0
     }
 152  
 
 153  
     // Store a client specified IndexVersion in the metadata file: Can be used
 154  
     // for client managed downloadable index
 155  
     public void storeInstalledIndexMetadata(Book b, String installedIndexVersionToStore) throws IOException {
 156  
 
 157  0
         synchronized (writeLock) {
 158  
 
 159  0
             props.put(PREFIX_INSTALLED_INDEX_VERSION_BOOK_OVERRIDE + IndexMetadata.getBookIdentifierPropSuffix(b.getBookMetaData()),
 160  
                     installedIndexVersionToStore);
 161  
 
 162  0
             NetUtil.storeProperties(props, getPropertyFileURI(), metadataFileComment);
 163  0
         }
 164  0
     }
 165  
 
 166  
     public void removeFromInstalledIndexMetadata(Book b) throws IOException {
 167  
 
 168  0
         synchronized (writeLock) {
 169  
 
 170  0
             props.remove(PREFIX_INSTALLED_INDEX_VERSION_BOOK_OVERRIDE + IndexMetadata.getBookIdentifierPropSuffix(b.getBookMetaData()));
 171  
 
 172  0
             NetUtil.storeProperties(props, getPropertyFileURI(), metadataFileComment);
 173  0
         }
 174  0
     }
 175  
 
 176  
     private PropertyMap props;
 177  
 
 178  0
     private Object writeLock = new Object();
 179  
 
 180  0
     private static String metadataFileComment = "Search index properties that stay persistent on clients computer. Used during index upgrades."
 181  
             + "\nContains Default index version, used for all searchable books, if book specific over-ride is not found.\n"
 182  
             + "JSword adds a Book specific installed index version over-ride property, after an index creation. ";
 183  
 
 184  0
     private static InstalledIndex myInstance = new InstalledIndex();
 185  0
     private static final Logger log = LoggerFactory.getLogger(InstalledIndex.class);
 186  
 }