@Override protected void doStore(String id, T value) throws ObjectStoreException { if (id == null) { throw new ObjectStoreException(CoreMessages.objectIsNull("id")); } // this block is unfortunately necessary to counter a possible race condition // between multiple nonatomic calls to containsObject/storeObject StoredObject<T> obj = new StoredObject<>(id, value); synchronized (store) { if (store.values().contains(obj)) { throw new ObjectAlreadyExistsException(); } boolean written = false; while (!written) { Long key = Long.valueOf(System.nanoTime()); written = (store.put(key, obj) == null); } } }
@Override protected void doStore(String key, String value) throws ObjectStoreException { boolean wasAdded; if (key == null) { throw new ObjectStoreException(); } synchronized (this) // map is shared { wasAdded = map.containsKey(key); map.put(key, value); } barrier.countDown(); try { barrier.await(); } catch (Exception e) { synchronized (errorHappenedInChildThreads) { errorHappenedInChildThreads = true; } } if (wasAdded) { throw new ObjectAlreadyExistsException(); } }
private PersistentObjectStorePartition<T> getPartitionObjectStore(String partitionName) throws ObjectStoreException { if (!partitionsByName.containsKey(partitionName)) { throw new ObjectStoreException(CoreMessages.createStaticMessage("No partition named: " + partitionName)); } return partitionsByName.get(partitionName); }
protected void serialize(File outputFile, StoreValue<T> storeValue) throws ObjectStoreException { try ( FileOutputStream fileOutputStream = new FileOutputStream(outputFile); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); ObjectOutputStream objectOutputStream = new ObjectOutputStream(bufferedOutputStream)) { serializer.getInternalProtocol().serialize(storeValue, objectOutputStream); objectOutputStream.flush(); } catch (Exception se) { throw new ObjectStoreException(se); } }
protected File createOrRetrievePartitionDescriptorFile() throws ObjectStoreException { try { File partitionDescriptorFile = new File(partitionDirectory, PARTITION_DESCRIPTOR_FILE); if (partitionDescriptorFile.exists()) { this.partitionName = readPartitionFileName(partitionDirectory); return partitionDescriptorFile; } try (FileWriter fileWriter = new FileWriter(partitionDescriptorFile.getAbsolutePath(), false)) { fileWriter.write(partitionName); fileWriter.flush(); } return partitionDescriptorFile; } catch (Exception e) { throw new ObjectStoreException(e); } }
protected void createDirectory(File directory) throws ObjectStoreException { try { // To support concurrency we need to check if directory exists again // inside // synchronized method if (!directory.exists() && !directory.mkdirs()) { throw new MuleRuntimeException(failedToCreate("object store directory " + directory.getAbsolutePath())); } } catch (Exception e) { throw new ObjectStoreException(e); } }
protected void validateKeyAndPartitionName(String key, String partitionName) throws ObjectStoreException { validateKey(key); if (partitionName == null || partitionName.trim().length() == 0) { throw new ObjectStoreException(createStaticMessage("partition name cannot be null or blank")); } }
protected void deleteStoreFile(File file) throws ObjectStoreException { if (file.exists()) { if (!file.delete()) { throw new ObjectStoreException(createStaticMessage("Deleting " + file.getAbsolutePath() + " failed")); } realKeyToUUIDIndex.removeValue(file.getName()); } else { throw new ObjectDoesNotExistException(); } }
protected File createFileToStoreObject() throws ObjectStoreException { String filename = UUID.getUUID() + OBJECT_FILE_EXTENSION; try { return newFile(partitionDirectory, filename); } catch (MuleRuntimeException mre) { throw new ObjectStoreException(mre); } }
@Override protected void doStore(String id, T value) throws ObjectStoreException { if (id == null) { throw new ObjectStoreException(CoreMessages.objectIsNull("id")); } // this block is unfortunately necessary to counter a possible race condition // between multiple nonatomic calls to containsObject/storeObject StoredObject<T> obj = new StoredObject<>(id, value); synchronized (store) { if (store.values().contains(obj)) { throw new ObjectAlreadyExistsException(); } boolean written = false; while (!written) { Long key = Long.valueOf(System.nanoTime()); written = (store.put(key, obj) == null); } } }
@Override public void clear() throws ObjectStoreException { synchronized (realKeyToUUIDIndex) { try { cleanDirectory(this.partitionDirectory); } catch (IOException e) { throw new ObjectStoreException(createStaticMessage("Could not clear ObjectStore"), e); } realKeyToUUIDIndex.clear(); } }
@SuppressWarnings("unchecked") protected StoreValue<T> deserialize(File file) throws ObjectStoreException { try ( FileInputStream fileInputStream = new FileInputStream(file); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); ObjectInputStream objectInputStream = new ObjectInputStream(bufferedInputStream)) { StoreValue<T> storedValue = serializer.getInternalProtocol().deserialize(objectInputStream); if (storedValue.getValue() instanceof DeserializationPostInitialisable) { DeserializationPostInitialisable.Implementation.init(storedValue.getValue(), muleContext); } return storedValue; } catch (Exception e) { throw new ObjectStoreException(e); } }
throw new ObjectStoreException(createStaticMessage(format("Could not restore object store data from %1s", partitionDirectory.getAbsolutePath())));
@Override protected void doStore(String key, String value) throws ObjectStoreException { boolean wasAdded; if (key == null) { throw new ObjectStoreException(); } synchronized (this) // map is shared { wasAdded = map.containsKey(key); map.put(key, value); } barrier.countDown(); try { barrier.await(); } catch (Exception e) { synchronized (errorHappenedInChildThreads) { errorHappenedInChildThreads = true; } } if (wasAdded) { throw new ObjectAlreadyExistsException(); } }
protected EventGroup getEventGroup(String groupId) throws ObjectStoreException { try { EventGroup eventGroup = (EventGroup) correlatorStore.retrieve(groupId, getEventGroupsPartitionKey()); if (!eventGroup.isInitialised()) { try { DeserializationPostInitialisable.Implementation.init(eventGroup, muleContext); } catch (Exception e) { throw new ObjectStoreException(e); } } eventGroup.initEventsStore(correlatorStore); return eventGroup; } catch (ObjectDoesNotExistException e) { return null; } }