| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 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 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public final class InstalledIndex { |
| 50 | |
public static final String INSTALLED_INDEX_DEFAULT_VERSION = "Installed.Index.DefaultVersion"; |
| 51 | |
|
| 52 | |
|
| 53 | |
public static final String PREFIX_INSTALLED_INDEX_VERSION_BOOK_OVERRIDE = "Installed.Index.Version.Book."; |
| 54 | |
|
| 55 | |
|
| 56 | |
public static final float DEFAULT_INSTALLED_INDEX_VERSION = IndexMetadata.INDEX_VERSION_1_2; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 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 | |
|
| 82 | |
|
| 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 | |
|
| 93 | |
|
| 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 | |
|
| 120 | |
|
| 121 | 0 | if (NetUtil.canRead(propURI)) { |
| 122 | 0 | props = NetUtil.loadProperties(propURI); |
| 123 | |
} |
| 124 | |
|
| 125 | |
|
| 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 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 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 | |
|
| 154 | |
|
| 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 | |
} |