use of com.helger.commons.io.watchdir.IWatchDirCallback in project phase4 by phax.
the class DropFolderUserMessage method init.
public static void init(@Nonnull final IAS4CryptoFactory aCryptoFactory) {
if (s_aWatch != null)
throw new IllegalStateException("Already inited!");
final IConfig aConfig = AS4Configuration.getConfig();
final Path aOutgoingDir = Paths.get(aConfig.getAsString("server.directory.outgoing", "out"));
final Path aIncomingDir = Paths.get(aConfig.getAsString("server.directory.incoming", "in"));
try {
// Ensure directories are present
Files.createDirectories(aOutgoingDir.resolve(PATH_DONE));
Files.createDirectories(aOutgoingDir.resolve(PATH_ERROR));
Files.createDirectories(aIncomingDir);
// Start watching directory for changes
final IWatchDirCallback aCB = (eAction, aCurFile) -> {
if (LOGGER.isDebugEnabled())
LOGGER.debug("WatchEvent " + eAction + " - " + aCurFile);
if (!eAction.equals(EWatchDirAction.DELETE) && aCurFile.toFile().isFile() && aCurFile.getFileName() != null && aCurFile.getFileName().toString().endsWith(".xml")) {
_send(aCryptoFactory, aCurFile, aIncomingDir);
}
};
s_aWatch = WatchDir.createAsyncRunningWatchDir(aOutgoingDir, false, aCB);
// Send initially for all existing files
try (final DirectoryStream<Path> aStream = Files.newDirectoryStream(aOutgoingDir, x -> x.toFile().isFile() && x.getFileName() != null && x.getFileName().toString().endsWith(".xml"))) {
for (final Path aCur : aStream) _send(aCryptoFactory, aCur, aIncomingDir);
}
} catch (final IOException ex) {
// Checked to unchecked conversion
throw new UncheckedIOException(ex);
}
}
Aggregations