Skip to content

Commit 0157ffc

Browse files
CHE-6063. Improve mechanism of handling file watcher 'MODIFIED' events
Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>
1 parent 4a07971 commit 0157ffc

3 files changed

Lines changed: 48 additions & 43 deletions

File tree

ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/editor/synchronization/EditorGroupSynchronizationImpl.java

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
package org.eclipse.che.ide.editor.synchronization;
1212

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

146147
@Override
147-
public void onFileContentUpdate(final FileContentUpdateEvent event) {
148+
public void onFileContentUpdate(FileContentUpdateEvent event) {
148149
if (synchronizedEditors.keySet().isEmpty()) {
149150
return;
150151
}
@@ -159,13 +160,54 @@ public void onFileContentUpdate(final FileContentUpdateEvent event) {
159160
return;
160161
}
161162

163+
if (!(virtualFile instanceof File)) {
164+
updateContent(virtualFile, false);
165+
return;
166+
}
167+
168+
File file = (File) virtualFile;
169+
170+
String eventModificationStamp = event.getModificationStamp();
171+
String currentModificationStamp = file.getModificationStamp();
172+
if (isNullOrEmpty(currentModificationStamp) || isNullOrEmpty(eventModificationStamp)) {
173+
updateContent(virtualFile, false);
174+
return;
175+
}
176+
177+
if (!Objects.equals(eventModificationStamp, currentModificationStamp)) {
178+
updateContent(virtualFile, true);
179+
}
180+
}
181+
182+
private void updateContent(VirtualFile virtualFile, boolean externalOperation) {
183+
final DocumentHandle documentHandle = getDocumentHandleFor(groupLeaderEditor);
184+
if (documentHandle == null) {
185+
return;
186+
}
187+
162188
documentStorage.getDocument(
163189
virtualFile,
164190
new DocumentStorage.DocumentCallback() {
165191

166192
@Override
167-
public void onDocumentReceived(final String content) {
168-
updateContent(content, event.getModificationStamp(), virtualFile);
193+
public void onDocumentReceived(String newContent) {
194+
Document document = documentHandle.getDocument();
195+
196+
String oldContent = document.getContents();
197+
if (Objects.equals(newContent, oldContent)) {
198+
return;
199+
}
200+
201+
TextPosition cursorPosition = document.getCursorPosition();
202+
replaceContent(document, newContent, oldContent, cursorPosition);
203+
204+
if (externalOperation) {
205+
notificationManager.notify(
206+
"External operation",
207+
"File '" + virtualFile.getName() + "' is updated",
208+
SUCCESS,
209+
NOT_EMERGE_MODE);
210+
}
169211
}
170212

171213
@Override
@@ -179,44 +221,6 @@ public void onDocumentLoadFailure(final Throwable caught) {
179221
});
180222
}
181223

182-
private void updateContent(
183-
String newContent, String eventModificationStamp, VirtualFile virtualFile) {
184-
final DocumentHandle documentHandle = getDocumentHandleFor(groupLeaderEditor);
185-
if (documentHandle == null) {
186-
return;
187-
}
188-
189-
final Document document = documentHandle.getDocument();
190-
final String oldContent = document.getContents();
191-
if (Objects.equals(newContent, oldContent)) {
192-
return;
193-
}
194-
195-
final TextPosition cursorPosition = document.getCursorPosition();
196-
if (!(virtualFile instanceof File)) {
197-
replaceContent(document, newContent, oldContent, cursorPosition);
198-
return;
199-
}
200-
201-
final File file = (File) virtualFile;
202-
final String currentStamp = file.getModificationStamp();
203-
204-
if (eventModificationStamp == null) {
205-
replaceContent(document, newContent, oldContent, cursorPosition);
206-
return;
207-
}
208-
209-
if (!Objects.equals(eventModificationStamp, currentStamp)) {
210-
replaceContent(document, newContent, oldContent, cursorPosition);
211-
212-
notificationManager.notify(
213-
"External operation",
214-
"File '" + file.getName() + "' is updated",
215-
SUCCESS,
216-
NOT_EMERGE_MODE);
217-
}
218-
}
219-
220224
private void replaceContent(
221225
Document document, String newContent, String oldContent, TextPosition cursorPosition) {
222226
document.replace(0, oldContent.length(), newContent);

ide/che-core-ide-app/src/test/java/org/eclipse/che/ide/editor/synchronization/EditorGroupSynchronizationImplTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ public void shouldNotifyAboutExternalOperationAtUpdateContentWhenStampIsDifferen
158158
reset(documentEventBus);
159159
when(fileContentUpdateEvent.getFilePath()).thenReturn(FILE_LOCATION);
160160
when(fileContentUpdateEvent.getModificationStamp()).thenReturn("some stamp");
161+
when(((File) virtualFile).getModificationStamp()).thenReturn("current modification stamp");
161162

162163
editorGroupSynchronization.onFileContentUpdate(fileContentUpdateEvent);
163164

wsagent/che-core-api-project/src/main/java/org/eclipse/che/api/vfs/impl/file/LocalVirtualFileSystem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,8 @@ private File createTempIoFile(VirtualFile parent, String prefix, String suffix)
616616

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

0 commit comments

Comments
 (0)