private boolean invokeExistingSagas( EventProxy<?> event, Class<? extends Saga> sagaType, Collection<AssociationValue> associationValues) { Set<String> sagas = new TreeSet<>(); for (AssociationValue associationValue : associationValues) { sagas.addAll(sagaRepository.find(sagaType, associationValue)); } for (Saga sagaInCreation : sagasInCreation.values()) { if (sagaType.isInstance(sagaInCreation) && containsAny(sagaInCreation.getAssociationValues(), associationValues)) { sagas.add(sagaInCreation.getSagaIdentifier()); } } boolean sagaOfTypeInvoked = false; for (final String sagaId : sagas) { if (synchronizeSagaAccess) { lock.obtainLock(sagaId); Saga invokedSaga = null; try { invokedSaga = loadAndInvoke(event, sagaId, associationValues); if (invokedSaga != null) { sagaOfTypeInvoked = true; } } finally { doReleaseLock(sagaId, invokedSaga); } } else { loadAndInvoke(event, sagaId, associationValues); } } return sagaOfTypeInvoked; }
/** * Gets all settlement IDs in String form for reading. * * @return all filenames */ public static String[] getAllChunkGroupIds() { File directory = new File(WriteReadType.SETTLEMENT_NORMAL.getDirectory()); FilenameFilter filter = new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(IOConstants.FILE_EXTENTENSION); } }; if (!directory.exists()) { directory.mkdirs(); Saga.info("Creating " + directory + " directory."); } String[] names = directory.list(filter); if (names == null) { Saga.severe("Could not retrieve faction names."); names = new String[0]; } // Remove extensions: for (int i = 0; i < names.length; i++) { names[i] = names[i].replaceAll(IOConstants.FILE_EXTENTENSION, ""); } return names; }
/** * Moves the chunk group file to the folder for deleted factions. * * @param groupId */ public static void deleteChunkGroup(String groupId) { // Create folders: File deletedDirectory = new File(WriteReadType.SETTLEMENT_DELETED.getDirectory()); File factionDirectory = new File(WriteReadType.SETTLEMENT_NORMAL.getDirectory()); File deletedFile = new File( WriteReadType.SETTLEMENT_DELETED.getDirectory() + groupId + IOConstants.FILE_EXTENTENSION); File factionFile = new File( WriteReadType.SETTLEMENT_NORMAL.getDirectory() + groupId + IOConstants.FILE_EXTENTENSION); if (!deletedDirectory.exists()) { deletedDirectory.mkdirs(); Saga.info("Creating " + deletedDirectory + " directory."); } if (!factionDirectory.exists()) { factionDirectory.mkdirs(); Saga.info("Creating " + factionDirectory + " directory."); } // Check if exists. if (!factionFile.exists()) { Saga.severe("Cant move " + factionFile + ", because it doesent exist."); } // Rename if target exists: for (int i = 1; i < 1000; i++) { if (deletedFile.exists()) { deletedFile.renameTo( new File( WriteReadType.SETTLEMENT_DELETED.getDirectory() + groupId + "(" + i + ")" + IOConstants.FILE_EXTENTENSION)); } else { break; } } // Move file to deleted folder: boolean success = factionFile.renameTo(deletedFile); // Notify on failure: if (success) { Saga.info("Moved " + factionFile + " file to deleted factions folder."); } else { Saga.severe("Failed to move " + factionFile + " to deleted factions folder."); } }
private void doInvokeSaga(EventProxy<?> event, Saga saga) { try { saga.handle(event); } catch (RuntimeException e) { if (suppressExceptions) { logger.error( format( "An exception occurred while a Saga [%s] was handling an Event [%s]:", saga.getClass().getSimpleName(), event.getPayloadType().getSimpleName()), e); } else { throw e; } } }
private Saga loadAndInvoke( EventProxy<?> event, String sagaId, Collection<AssociationValue> associations) { Saga saga = sagasInCreation.get(sagaId); if (saga == null) { saga = sagaRepository.load(sagaId); } if (saga == null || !saga.isActive() || !containsAny(saga.getAssociationValues(), associations)) { return null; } preProcessSaga(saga); try { doInvokeSaga(event, saga); } finally { commit(saga); } return saga; }
/** * Writes configuration. * * @param config configuration String * @param writeType write type * @param configType configuration type * @throws IOException thrown when read fails */ private static void writeConfig(String config, WriteReadType writeType, String fileName) throws IOException { File directory = new File(writeType.getDirectory()); File file = new File(writeType.getDirectory() + fileName); if (!directory.exists()) { directory.mkdirs(); Saga.info("Creating " + directory + " directory."); } if (!file.exists()) { file.createNewFile(); Saga.info("Creating " + file + " file."); } BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write(config); out.close(); }
private void startNewSaga( EventProxy<?> event, Class<? extends Saga> sagaType, AssociationValue associationValue) { Saga newSaga = sagaFactory.createSaga(sagaType); newSaga.getAssociationValues().add(associationValue); preProcessSaga(newSaga); sagasInCreation.put(newSaga.getSagaIdentifier(), newSaga); try { if (synchronizeSagaAccess) { lock.obtainLock(newSaga.getSagaIdentifier()); try { doInvokeSaga(event, newSaga); } finally { try { sagaRepository.add(newSaga); } finally { doReleaseLock(newSaga.getSagaIdentifier(), newSaga); } } } else { try { doInvokeSaga(event, newSaga); } finally { sagaRepository.add(newSaga); } } } finally { removeEntry(newSaga.getSagaIdentifier(), sagasInCreation); } }
/** * Reads configuration. * * @param writeType write type * @param configType configuration type * @throws IOException thrown when read fails */ private static String readConfig(WriteReadType writeType, String fileName) throws IOException { // Add directory if missing: File directory = new File(writeType.getDirectory()); if (!directory.exists()) { directory.mkdirs(); Saga.info("Creating " + directory + " directory."); } File file = new File(writeType.getDirectory() + fileName); int ch; StringBuffer strContent = new StringBuffer(""); FileInputStream fin = null; fin = new FileInputStream(file); while ((ch = fin.read()) != -1) { strContent.append((char) ch); } fin.close(); return strContent.toString(); }