in util/src/main/java/com/epam/deltix/util/io/FileBasedRepository.java [148:288]
protected void update(File file, EventType event) {
checkHoldsLock();
final Path path = file.toPath();
if (logger.isDebugEnabled())
logger.debug("[%s] %s > %s").with(getClass().getSimpleName()).with(event).with(path);
if (event == EventType.DELETED) { // a file or folder is deleted
final FileItem fItem = items.remove(path);
if (fItem != null) { // if a file
for (RepositoryEventHandler<T> handler : getHandlers(SCMDRepositoryEvent.DELETED)) {
handler.onEvent(fItem.item, SCMDRepositoryEvent.DELETED);
}
} else {
// try to check all sub-items and remove them
for (Map.Entry<Path, FileItem> iKv : new HashMap<>(items).entrySet()) {
if (iKv.getKey().startsWith(path)) {
final FileItem deletedItem = items.remove(iKv.getKey());
for (RepositoryEventHandler<T> handler : getHandlers(SCMDRepositoryEvent.DELETED)) {
handler.onEvent(deletedItem.item, SCMDRepositoryEvent.DELETED);
}
}
}
}
} else if (file.isDirectory()) {
switch (event) {
case SCANNED:
case CREATED:
if (isSubscribableFolder(file)) {
for (Map.Entry<Path, FileItem> iKv : new HashMap<>(items).entrySet()) {
if (iKv.getKey().startsWith(path)) {
break; // already under watching
}
}
if (watchFs) {
try {
FileSystemWatcher.getInstance().subscribe(fsEventHandler, file, EventType.SCANNED, EventType.CREATED, EventType.MODIFIED, EventType.DELETED);
} catch (IOException e) {
logger.warn().append("An error while subscription to ").append(file).append(e).commit();
}
}
}
break;
}
} else if (isItemFile(file)) { // a file item
switch (event) {
case SCANNED:
case CREATED:
if (file.length() == 0) {
break;
}
if (IOUtil.copiedCompletely(file)) {
final SCMDRepositoryEvent e = event == EventType.SCANNED ? SCMDRepositoryEvent.SCANNED : SCMDRepositoryEvent.CREATED;
if (items.containsKey(path)) { // already exists
break;
}
try {
final FileItem fItem = new FileItem(file, prepareItem(file));
items.put(path, fItem);
for (RepositoryEventHandler<T> handler : getHandlers(e)) {
handler.onEvent(fItem.item, e);
}
} catch (Throwable t) {
logger.warn().append("An error while preparing item for ").append(path).append(t).commit();
}
}
break;
case MODIFIED:
FileItem fItem = items.get(path);
final boolean isNew = fItem == null;
final boolean isEmpty = file.length() == 0;
final long lastModified = file.lastModified();
if (isNew) {
if (!isEmpty) {
if (IOUtil.copiedCompletely(file)) {
// created
try {
fItem = new FileItem(file, prepareItem(file));
items.put(path, fItem);
for (RepositoryEventHandler<T> handler : getHandlers(SCMDRepositoryEvent.CREATED)) {
handler.onEvent(fItem.item, SCMDRepositoryEvent.CREATED);
}
} catch (Throwable t) {
logger.warn().append("An error while preparing item for ").append(path).append(t).commit();
}
}
}
} else {
if (isEmpty) {
// deleted
try {
items.remove(path);
for (RepositoryEventHandler<T> handler : getHandlers(SCMDRepositoryEvent.DELETED)) {
handler.onEvent(fItem.item, SCMDRepositoryEvent.DELETED);
}
} catch (Throwable t) {
logger.warn().append("An error while preparing item for ").append(path).append(t).commit();
}
} else if (lastModified != fItem.lastModified) {
// modified
if (IOUtil.copiedCompletely(file)) {
try {
fItem.lastModified = lastModified;
fItem.item = prepareItem(file);
for (RepositoryEventHandler<T> handler : getHandlers(SCMDRepositoryEvent.MODIFIED)) {
handler.onEvent(fItem.item, SCMDRepositoryEvent.MODIFIED);
}
} catch (Throwable t) {
logger.warn().append("An error while preparing item for ").append(path).append(t).commit();
}
}
}
}
break;
}
}
}