| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
package org.crosswire.jsword.book.sword.state; |
| 21 | |
|
| 22 | |
import java.util.HashMap; |
| 23 | |
import java.util.Iterator; |
| 24 | |
import java.util.Map; |
| 25 | |
import java.util.Queue; |
| 26 | |
import java.util.concurrent.ConcurrentLinkedQueue; |
| 27 | |
import java.util.concurrent.Executors; |
| 28 | |
import java.util.concurrent.ScheduledFuture; |
| 29 | |
import java.util.concurrent.ThreadFactory; |
| 30 | |
import java.util.concurrent.TimeUnit; |
| 31 | |
|
| 32 | |
import org.crosswire.jsword.book.BookException; |
| 33 | |
import org.crosswire.jsword.book.BookMetaData; |
| 34 | |
import org.crosswire.jsword.book.sword.BlockType; |
| 35 | |
import org.slf4j.Logger; |
| 36 | |
import org.slf4j.LoggerFactory; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | 0 | public final class OpenFileStateManager { |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | 0 | private OpenFileStateManager(final int cleanupIntervalSeconds, final int maxExpiry) { |
| 67 | |
|
| 68 | 0 | this.monitoringThread = Executors.newScheduledThreadPool(1, new ThreadFactory() { |
| 69 | |
public Thread newThread(Runnable r) { |
| 70 | 0 | Thread t = new Thread(r); |
| 71 | 0 | t.setDaemon(true); |
| 72 | 0 | return t; |
| 73 | |
} |
| 74 | |
|
| 75 | 0 | }).scheduleWithFixedDelay(new Runnable() { |
| 76 | |
public void run() { |
| 77 | |
|
| 78 | |
|
| 79 | 0 | long currentTime = System.currentTimeMillis(); |
| 80 | |
|
| 81 | 0 | for (Queue<OpenFileState> e : OpenFileStateManager.this.metaToStates.values()) { |
| 82 | 0 | for (Iterator<OpenFileState> iterator = e.iterator(); iterator.hasNext(); ) { |
| 83 | 0 | final OpenFileState state = iterator.next(); |
| 84 | 0 | if (state.getLastAccess() + maxExpiry * 1000 < currentTime) { |
| 85 | |
|
| 86 | 0 | state.releaseResources(); |
| 87 | |
|
| 88 | |
|
| 89 | 0 | iterator.remove(); |
| 90 | |
} |
| 91 | 0 | } |
| 92 | |
} |
| 93 | 0 | } |
| 94 | |
}, 0, cleanupIntervalSeconds, TimeUnit.SECONDS); |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public static synchronized void init(final int cleanupIntervalSeconds, final int maxExpiry) { |
| 105 | 0 | if (manager == null) { |
| 106 | 0 | manager = new OpenFileStateManager(cleanupIntervalSeconds, maxExpiry); |
| 107 | |
} else { |
| 108 | |
|
| 109 | 0 | LOGGER.warn("The OpenFileStateManager has already been initialised, potentially with its default settings. The following values were ignored: cleanUpInterval [{}], maxExpiry=[{}]", Integer.toString(cleanupIntervalSeconds), Integer.toString(maxExpiry)); |
| 110 | |
} |
| 111 | |
|
| 112 | 0 | } |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public static OpenFileStateManager instance() { |
| 119 | 0 | if (manager == null) { |
| 120 | 0 | synchronized (OpenFileStateManager.class) { |
| 121 | 0 | init(60, 60); |
| 122 | 0 | } |
| 123 | |
} |
| 124 | 0 | return manager; |
| 125 | |
} |
| 126 | |
|
| 127 | |
public RawBackendState getRawBackendState(BookMetaData metadata) throws BookException { |
| 128 | 0 | ensureNotShuttingDown(); |
| 129 | |
|
| 130 | 0 | RawBackendState state = getInstance(metadata); |
| 131 | 0 | if (state == null) { |
| 132 | 0 | LOGGER.trace("Initializing: {}", metadata.getInitials()); |
| 133 | 0 | return new RawBackendState(metadata); |
| 134 | |
} |
| 135 | |
|
| 136 | 0 | LOGGER.trace("Reusing: {}", metadata.getInitials()); |
| 137 | 0 | return state; |
| 138 | |
} |
| 139 | |
|
| 140 | |
public RawFileBackendState getRawFileBackendState(BookMetaData metadata) throws BookException { |
| 141 | 0 | ensureNotShuttingDown(); |
| 142 | |
|
| 143 | 0 | RawFileBackendState state = getInstance(metadata); |
| 144 | 0 | if (state == null) { |
| 145 | 0 | LOGGER.trace("Initializing: {}", metadata.getInitials()); |
| 146 | 0 | return new RawFileBackendState(metadata); |
| 147 | |
} |
| 148 | |
|
| 149 | 0 | LOGGER.trace("Reusing: {}", metadata.getInitials()); |
| 150 | 0 | return state; |
| 151 | |
} |
| 152 | |
|
| 153 | |
public GenBookBackendState getGenBookBackendState(BookMetaData metadata) throws BookException { |
| 154 | 0 | ensureNotShuttingDown(); |
| 155 | |
|
| 156 | 0 | GenBookBackendState state = getInstance(metadata); |
| 157 | 0 | if (state == null) { |
| 158 | 0 | LOGGER.trace("Initializing: {}", metadata.getInitials()); |
| 159 | 0 | return new GenBookBackendState(metadata); |
| 160 | |
} |
| 161 | |
|
| 162 | 0 | LOGGER.trace("Reusing: {}", metadata.getInitials()); |
| 163 | 0 | return state; |
| 164 | |
} |
| 165 | |
|
| 166 | |
public RawLDBackendState getRawLDBackendState(BookMetaData metadata) throws BookException { |
| 167 | 0 | ensureNotShuttingDown(); |
| 168 | |
|
| 169 | 0 | RawLDBackendState state = getInstance(metadata); |
| 170 | 0 | if (state == null) { |
| 171 | 0 | LOGGER.trace("Initializing: {}", metadata.getInitials()); |
| 172 | 0 | return new RawLDBackendState(metadata); |
| 173 | |
} |
| 174 | |
|
| 175 | 0 | LOGGER.trace("Reusing: {}", metadata.getInitials()); |
| 176 | 0 | return state; |
| 177 | |
} |
| 178 | |
|
| 179 | |
public ZLDBackendState getZLDBackendState(BookMetaData metadata) throws BookException { |
| 180 | 0 | ensureNotShuttingDown(); |
| 181 | |
|
| 182 | 0 | ZLDBackendState state = getInstance(metadata); |
| 183 | 0 | if (state == null) { |
| 184 | 0 | LOGGER.trace("Initializing: {}", metadata.getInitials()); |
| 185 | 0 | return new ZLDBackendState(metadata); |
| 186 | |
} |
| 187 | |
|
| 188 | 0 | LOGGER.trace("Reusing: {}", metadata.getInitials()); |
| 189 | 0 | return state; |
| 190 | |
} |
| 191 | |
|
| 192 | |
public ZVerseBackendState getZVerseBackendState(BookMetaData metadata, BlockType blockType) throws BookException { |
| 193 | 0 | ensureNotShuttingDown(); |
| 194 | |
|
| 195 | 0 | ZVerseBackendState state = getInstance(metadata); |
| 196 | 0 | if (state == null) { |
| 197 | 0 | LOGGER.trace("Initializing: {}", metadata.getInitials()); |
| 198 | 0 | return new ZVerseBackendState(metadata, blockType); |
| 199 | |
} |
| 200 | |
|
| 201 | 0 | LOGGER.trace("Reusing: {}", metadata.getInitials()); |
| 202 | 0 | return state; |
| 203 | |
} |
| 204 | |
|
| 205 | |
@SuppressWarnings("unchecked") |
| 206 | |
private <T extends OpenFileState> T getInstance(BookMetaData metadata) { |
| 207 | 0 | Queue<OpenFileState> availableStates = getQueueForMeta(metadata); |
| 208 | 0 | final T state = (T) availableStates.poll(); |
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | 0 | if (state != null) { |
| 215 | 0 | state.setLastAccess(System.currentTimeMillis()); |
| 216 | |
} |
| 217 | 0 | return state; |
| 218 | |
} |
| 219 | |
|
| 220 | |
private Queue<OpenFileState> getQueueForMeta(BookMetaData metadata) { |
| 221 | 0 | Queue<OpenFileState> availableStates = metaToStates.get(metadata); |
| 222 | 0 | if (availableStates == null) { |
| 223 | 0 | synchronized (OpenFileState.class) { |
| 224 | 0 | availableStates = new ConcurrentLinkedQueue<OpenFileState>(); |
| 225 | 0 | metaToStates.put(metadata, availableStates); |
| 226 | 0 | } |
| 227 | |
} |
| 228 | 0 | return availableStates; |
| 229 | |
} |
| 230 | |
|
| 231 | |
public void release(OpenFileState fileState) { |
| 232 | 0 | if (fileState == null) { |
| 233 | |
|
| 234 | |
|
| 235 | 0 | return; |
| 236 | |
} |
| 237 | |
|
| 238 | 0 | fileState.setLastAccess(System.currentTimeMillis()); |
| 239 | |
|
| 240 | |
|
| 241 | 0 | BookMetaData bmd = fileState.getBookMetaData(); |
| 242 | 0 | Queue<OpenFileState> queueForMeta = getQueueForMeta(bmd); |
| 243 | 0 | LOGGER.trace("Offering to releasing: {}", bmd.getInitials()); |
| 244 | 0 | boolean offered = queueForMeta.offer(fileState); |
| 245 | |
|
| 246 | |
|
| 247 | 0 | if (!offered) { |
| 248 | 0 | LOGGER.trace("Released: {}", bmd.getInitials()); |
| 249 | 0 | fileState.releaseResources(); |
| 250 | |
} |
| 251 | 0 | } |
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
public void shutDown() { |
| 257 | 0 | shuttingDown = true; |
| 258 | 0 | this.monitoringThread.cancel(true); |
| 259 | 0 | for (Queue<OpenFileState> e : metaToStates.values()) { |
| 260 | 0 | OpenFileState state = null; |
| 261 | 0 | while ((state = e.poll()) != null) { |
| 262 | 0 | state.releaseResources(); |
| 263 | |
} |
| 264 | 0 | } |
| 265 | 0 | } |
| 266 | |
|
| 267 | |
private void ensureNotShuttingDown() throws BookException { |
| 268 | 0 | if (shuttingDown) { |
| 269 | 0 | throw new BookException("Unable to read book, application is shutting down."); |
| 270 | |
} |
| 271 | 0 | } |
| 272 | |
|
| 273 | |
private final ScheduledFuture<?> monitoringThread; |
| 274 | 0 | private final Map<BookMetaData, Queue<OpenFileState>> metaToStates = new HashMap<BookMetaData, Queue<OpenFileState>>(); |
| 275 | |
private volatile boolean shuttingDown; |
| 276 | |
|
| 277 | |
private static volatile OpenFileStateManager manager; |
| 278 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(OpenFileStateManager.class); |
| 279 | |
} |