Skip to content

Commit

Permalink
CHE-6063. Improve mechanism of handling file watcher 'MODIFIED' events
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Nikitenko <[email protected]>
  • Loading branch information
RomanNikitenko committed Sep 13, 2017
1 parent 4a07971 commit 0157ffc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package org.eclipse.che.ide.editor.synchronization;

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.EMERGE_MODE;
import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.NOT_EMERGE_MODE;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;
Expand Down Expand Up @@ -144,7 +145,7 @@ public void onDocumentChanged(DocumentChangedEvent event) {
}

@Override
public void onFileContentUpdate(final FileContentUpdateEvent event) {
public void onFileContentUpdate(FileContentUpdateEvent event) {
if (synchronizedEditors.keySet().isEmpty()) {
return;
}
Expand All @@ -159,13 +160,54 @@ public void onFileContentUpdate(final FileContentUpdateEvent event) {
return;
}

if (!(virtualFile instanceof File)) {
updateContent(virtualFile, false);
return;
}

File file = (File) virtualFile;

String eventModificationStamp = event.getModificationStamp();
String currentModificationStamp = file.getModificationStamp();
if (isNullOrEmpty(currentModificationStamp) || isNullOrEmpty(eventModificationStamp)) {
updateContent(virtualFile, false);
return;
}

if (!Objects.equals(eventModificationStamp, currentModificationStamp)) {
updateContent(virtualFile, true);
}
}

private void updateContent(VirtualFile virtualFile, boolean externalOperation) {
final DocumentHandle documentHandle = getDocumentHandleFor(groupLeaderEditor);
if (documentHandle == null) {
return;
}

documentStorage.getDocument(
virtualFile,
new DocumentStorage.DocumentCallback() {

@Override
public void onDocumentReceived(final String content) {
updateContent(content, event.getModificationStamp(), virtualFile);
public void onDocumentReceived(String newContent) {
Document document = documentHandle.getDocument();

String oldContent = document.getContents();
if (Objects.equals(newContent, oldContent)) {
return;
}

TextPosition cursorPosition = document.getCursorPosition();
replaceContent(document, newContent, oldContent, cursorPosition);

if (externalOperation) {
notificationManager.notify(
"External operation",
"File '" + virtualFile.getName() + "' is updated",
SUCCESS,
NOT_EMERGE_MODE);
}
}

@Override
Expand All @@ -179,44 +221,6 @@ public void onDocumentLoadFailure(final Throwable caught) {
});
}

private void updateContent(
String newContent, String eventModificationStamp, VirtualFile virtualFile) {
final DocumentHandle documentHandle = getDocumentHandleFor(groupLeaderEditor);
if (documentHandle == null) {
return;
}

final Document document = documentHandle.getDocument();
final String oldContent = document.getContents();
if (Objects.equals(newContent, oldContent)) {
return;
}

final TextPosition cursorPosition = document.getCursorPosition();
if (!(virtualFile instanceof File)) {
replaceContent(document, newContent, oldContent, cursorPosition);
return;
}

final File file = (File) virtualFile;
final String currentStamp = file.getModificationStamp();

if (eventModificationStamp == null) {
replaceContent(document, newContent, oldContent, cursorPosition);
return;
}

if (!Objects.equals(eventModificationStamp, currentStamp)) {
replaceContent(document, newContent, oldContent, cursorPosition);

notificationManager.notify(
"External operation",
"File '" + file.getName() + "' is updated",
SUCCESS,
NOT_EMERGE_MODE);
}
}

private void replaceContent(
Document document, String newContent, String oldContent, TextPosition cursorPosition) {
document.replace(0, oldContent.length(), newContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public void shouldNotifyAboutExternalOperationAtUpdateContentWhenStampIsDifferen
reset(documentEventBus);
when(fileContentUpdateEvent.getFilePath()).thenReturn(FILE_LOCATION);
when(fileContentUpdateEvent.getModificationStamp()).thenReturn("some stamp");
when(((File) virtualFile).getModificationStamp()).thenReturn("current modification stamp");

editorGroupSynchronization.onFileContentUpdate(fileContentUpdateEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,8 @@ private File createTempIoFile(VirtualFile parent, String prefix, String suffix)

private void doUpdateContent(LocalVirtualFile virtualFile, InputStream content)
throws ServerException {
try (FileOutputStream fileOut = new FileOutputStream(virtualFile.toIoFile())) {
ByteStreams.copy(content, fileOut);
try {
Files.write(ByteStreams.toByteArray(content), virtualFile.toIoFile());
} catch (IOException e) {
String errorMessage = String.format("Unable set content of '%s'", virtualFile.getPath());
LOG.error(errorMessage + "\n" + e.getMessage(), e);
Expand Down

0 comments on commit 0157ffc

Please sign in to comment.