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, 2013 - 2016
18   *
19   */
20  package org.crosswire.jsword.book.sword.state;
21  
22  import java.io.File;
23  import java.io.FileNotFoundException;
24  import java.io.RandomAccessFile;
25  import java.net.URI;
26  
27  import org.crosswire.common.util.FileUtil;
28  import org.crosswire.common.util.IOUtil;
29  import org.crosswire.common.util.NetUtil;
30  import org.crosswire.jsword.book.BookException;
31  import org.crosswire.jsword.book.BookMetaData;
32  import org.crosswire.jsword.book.sword.BlockType;
33  import org.crosswire.jsword.book.sword.SwordConstants;
34  import org.crosswire.jsword.book.sword.SwordUtil;
35  import org.crosswire.jsword.versification.Testament;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   * Stores the random access files required for processing the passage request.
41   * 
42   * The caller is required to close to correctly free resources and avoid File
43   * pointer leaks.
44   * 
45   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
46   * @author DM Smith
47   */
48  public class ZVerseBackendState extends AbstractOpenFileState {
49      /**
50       * This is default package access for forcing the use of the
51       * OpenFileStateManager to manage the creation. Not doing so may result in
52       * new instances of OpenFileState being created for no reason, and as a
53       * result, if they are released to the OpenFileStateManager by mistake this
54       * would result in leakage
55       * 
56       * @param bookMetaData the appropriate metadata for the book
57       */
58      ZVerseBackendState(BookMetaData bookMetaData, BlockType blockType) throws BookException {
59          super(bookMetaData);
60          URI path = SwordUtil.getExpandedDataPath(bookMetaData);
61          String otAllButLast = NetUtil.lengthenURI(path, File.separator + SwordConstants.FILE_OT + '.' + blockType.getIndicator() + SUFFIX_PART1).getPath();
62          File otIdxFile = new File(otAllButLast + SUFFIX_INDEX);
63          File otTextFile = new File(otAllButLast + SUFFIX_TEXT);
64          File otCompFile = new File(otAllButLast + SUFFIX_COMP);
65  
66          String ntAllButLast = NetUtil.lengthenURI(path, File.separator + SwordConstants.FILE_NT + '.' + blockType.getIndicator() + SUFFIX_PART1).getPath();
67          File ntIdxFile = new File(ntAllButLast + SUFFIX_INDEX);
68          File ntTextFile = new File(ntAllButLast + SUFFIX_TEXT);
69          File ntCompFile = new File(ntAllButLast + SUFFIX_COMP);
70  
71          // check whether exists to swallow any exception as before
72          if (otIdxFile.canRead()) {
73              try {
74                  otCompRaf = new RandomAccessFile(otIdxFile, FileUtil.MODE_READ);
75                  otTextRaf = new RandomAccessFile(otTextFile, FileUtil.MODE_READ);
76                  otIdxRaf = new RandomAccessFile(otCompFile, FileUtil.MODE_READ);
77              } catch (FileNotFoundException ex) {
78                  //failed to open the files, so close them now
79                  IOUtil.close(otCompRaf);
80                  IOUtil.close(otTextRaf);
81                  IOUtil.close(otIdxRaf);
82  
83                  assert false : ex;
84                  LOGGER.error("Could not open OT", ex);
85              }
86          }
87  
88          // why do swallow the exception and log. Can Books have one testament
89          // without the other.
90          if (ntIdxFile.canRead()) {
91              try {
92                  ntCompRaf = new RandomAccessFile(ntIdxFile, FileUtil.MODE_READ);
93                  ntTextRaf = new RandomAccessFile(ntTextFile, FileUtil.MODE_READ);
94                  ntIdxRaf = new RandomAccessFile(ntCompFile, FileUtil.MODE_READ);
95              } catch (FileNotFoundException ex) {
96                  //failed to open the files, so close them now
97                  IOUtil.close(ntCompRaf);
98                  IOUtil.close(ntTextRaf);
99                  IOUtil.close(ntIdxRaf);
100 
101                 assert false : ex;
102                 LOGGER.error("Could not open OT", ex);
103             }
104         }
105     }
106 
107     public void releaseResources() {
108         IOUtil.close(ntCompRaf);
109         IOUtil.close(ntTextRaf);
110         IOUtil.close(ntIdxRaf);
111         IOUtil.close(otCompRaf);
112         IOUtil.close(otTextRaf);
113         IOUtil.close(otIdxRaf);
114         ntCompRaf = null;
115         ntTextRaf = null;
116         ntIdxRaf = null;
117         otCompRaf = null;
118         otTextRaf = null;
119         otIdxRaf = null;
120     }
121 
122     /**
123      * Get the compression file for the given testament.
124      * 
125      * @param testament the testament for the index
126      * @return the index for the testament
127      */
128     public RandomAccessFile getCompRaf(Testament testament) {
129         return testament == Testament.NEW ? ntCompRaf : otCompRaf;
130     }
131 
132     /**
133      * Get the text file for the given testament.
134      * 
135      * @param testament the testament for the index
136      * @return the index for the testament
137      */
138     public RandomAccessFile getTextRaf(Testament testament) {
139         return testament == Testament.NEW ? ntTextRaf : otTextRaf;
140     }
141 
142     /**
143      * Get the index file for the given testament.
144      * 
145      * @param testament the testament for the index
146      * @return the index for the testament
147      */
148     public RandomAccessFile getIdxRaf(Testament testament) {
149         return testament == Testament.NEW ? ntIdxRaf : otIdxRaf;
150     }
151 
152     /**
153      * @return the lastTestament
154      */
155     public Testament getLastTestament() {
156         return lastTestament;
157     }
158 
159     /**
160      * @return the lastBlockNum
161      */
162     public long getLastBlockNum() {
163         return lastBlockNum;
164     }
165 
166     /**
167      * @return the lastUncompressed
168      */
169     public byte[] getLastUncompressed() {
170         return lastUncompressed;
171     }
172 
173     /**
174      * @param lastTestament
175      *            the lastTestament to set
176      */
177     public void setLastTestament(Testament lastTestament) {
178         this.lastTestament = lastTestament;
179     }
180 
181     /**
182      * @param lastBlockNum
183      *            the lastBlockNum to set
184      */
185     public void setLastBlockNum(long lastBlockNum) {
186         this.lastBlockNum = lastBlockNum;
187     }
188 
189     /**
190      * @param lastUncompressed
191      *            the lastUncompressed to set
192      */
193     public void setLastUncompressed(byte[] lastUncompressed) {
194         this.lastUncompressed = lastUncompressed;
195     }
196 
197     private static final String SUFFIX_COMP = "v";
198     private static final String SUFFIX_INDEX = "s";
199     private static final String SUFFIX_PART1 = "z";
200     private static final String SUFFIX_TEXT = "z";
201 
202     /**
203      * The compressed random access files
204      */
205     private RandomAccessFile otCompRaf;
206     private RandomAccessFile ntCompRaf;
207 
208     /**
209      * The data random access files
210      */
211     private RandomAccessFile otTextRaf;
212     private RandomAccessFile ntTextRaf;
213 
214     /**
215      * The index random access files
216      */
217     private RandomAccessFile otIdxRaf;
218     private RandomAccessFile ntIdxRaf;
219     private Testament lastTestament;
220     private long lastBlockNum = -1;
221     private byte[] lastUncompressed;
222 
223     /**
224      * The log stream
225      */
226     private static final Logger LOGGER = LoggerFactory.getLogger(ZVerseBackendState.class);
227 }
228