public void open()
{
if (!_storeFile.exists())
{
throw new IllegalConfigurationException("Preferences file does not exist");
}
if (_storeLock != null)
{
throw new IllegalStateException("Preferences store is already opened");
}
try
{
_storeRAF = new RandomAccessFile(_storeFile, "rw");
FileChannel fileChannel = _storeRAF.getChannel();
try
{
_storeLock = fileChannel.tryLock();
}
catch (OverlappingFileLockException e)
{
_storeLock = null;
}
if (_storeLock == null)
{
throw new IllegalConfigurationException("Cannot get lock on store file " + _storeFile.getName()
+ " is another instance running?");
}
long fileSize = fileChannel.size();
if (fileSize > 0)
{
ByteBuffer buffer = ByteBuffer.allocate((int) fileSize);
fileChannel.read(buffer);
buffer.rewind();
buffer.flip();
byte[] data = buffer.array();
try
{
Map<String, Map<String, Object>> preferencesMap = _objectMapper.readValue(data,
new TypeReference<Map<String, Map<String, Object>>>()
{
});
_preferences.putAll(preferencesMap);
}
catch (JsonProcessingException e)
{
throw new IllegalConfigurationException("Cannot parse preferences json in " + _storeFile.getName(), e);
}
}
}
catch (IOException e)
{
throw new IllegalConfigurationException("Cannot load preferences from " + _storeFile.getName(), e);
}
}