From eb0115f8b4a637275581e3d742e8b59c6056677e Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Tue, 24 Jan 2017 14:53:36 +0100 Subject: [PATCH 1/3] Make sure that unregistered event sources do not stop JabRef from shutting down --- CHANGELOG.md | 3 ++- .../logic/autosaveandbackup/AutosaveManager.java | 14 ++++++++++++-- .../logic/autosaveandbackup/BackupManager.java | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12e85e98aa8..3de7f58fbde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,8 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - The [ACM fetcher](https://help.jabref.org/en/ACMPortal) does no longer add HTML code to the bib-file. Fixes [#2472](https://github.com/JabRef/jabref/issues/2472). - When [finding unlinked files](https://help.jabref.org/en/FindUnlinkedFiles), JabRef does not freeze any more. Fixes [#2309]()https://github.com/JabRef/jabref/issues/2309). - Collapse and expand all buttons in the group assignment dialog no longer lead to a crash of JabRef. -- The aux export command line function does no longer add duplicates of references that were resolved via `crossref`. Fixes [#2475](https://github.com/JabRef/jabref/issues/2475). +- The aux export command line function does no longer add duplicates of references that were resolved via `crossref`. Fixes [#2475](https://github.com/JabRef/jabref/issues/2475). +- When the database is changed external, JabRef is no longer prevented from an orderly shutdown. Fixes [#2486](https://github.com/JabRef/jabref/issues/2486). ### Removed diff --git a/src/main/java/net/sf/jabref/logic/autosaveandbackup/AutosaveManager.java b/src/main/java/net/sf/jabref/logic/autosaveandbackup/AutosaveManager.java index 65908e2c971..b04f188e116 100644 --- a/src/main/java/net/sf/jabref/logic/autosaveandbackup/AutosaveManager.java +++ b/src/main/java/net/sf/jabref/logic/autosaveandbackup/AutosaveManager.java @@ -53,8 +53,18 @@ public synchronized void listen(@SuppressWarnings("unused") BibDatabaseContextCh } private void shutdown() { - bibDatabaseContext.getDatabase().unregisterListener(this); - bibDatabaseContext.getMetaData().unregisterListener(this); + try { + bibDatabaseContext.getDatabase().unregisterListener(this); + } catch(IllegalArgumentException e) { + // occurs if the event source has not been registered, should not prevent shutdown + LOGGER.debug(e); + } + try { + bibDatabaseContext.getMetaData().unregisterListener(this); + } catch(IllegalArgumentException e) { + // occurs if the event source has not been registered, should not prevent shutdown + LOGGER.debug(e); + } executor.shutdown(); } diff --git a/src/main/java/net/sf/jabref/logic/autosaveandbackup/BackupManager.java b/src/main/java/net/sf/jabref/logic/autosaveandbackup/BackupManager.java index ce24b0c4ca4..c117e0019ea 100644 --- a/src/main/java/net/sf/jabref/logic/autosaveandbackup/BackupManager.java +++ b/src/main/java/net/sf/jabref/logic/autosaveandbackup/BackupManager.java @@ -145,8 +145,18 @@ private void startBackupTask() { * This method should only be used when closing a database/JabRef legally. */ private void shutdown() { - bibDatabaseContext.getDatabase().unregisterListener(this); - bibDatabaseContext.getMetaData().unregisterListener(this); + try { + bibDatabaseContext.getDatabase().unregisterListener(this); + } catch(IllegalArgumentException e) { + // occurs if the event source has not been registered, should not prevent shutdown + LOGGER.debug(e); + } + try { + bibDatabaseContext.getMetaData().unregisterListener(this); + } catch(IllegalArgumentException e) { + // occurs if the event source has not been registered, should not prevent shutdown + LOGGER.debug(e); + } executor.shutdown(); determineBackupPath().ifPresent(this::deleteBackupFile); } From 4cc46f43aeb9b17ce19e7f0668191fef7a5a8413 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Wed, 25 Jan 2017 15:50:44 +0100 Subject: [PATCH 2/3] Catch unregistration exception at lowest possible level --- .../autosaveandbackup/AutosaveManager.java | 21 +++++++------------ .../autosaveandbackup/BackupManager.java | 14 ++----------- .../SearchQueryHighlightObservable.java | 6 +++++- .../sf/jabref/model/database/BibDatabase.java | 7 ++++++- .../net/sf/jabref/model/entry/BibEntry.java | 7 ++++++- .../sf/jabref/model/metadata/MetaData.java | 6 +++++- 6 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/autosaveandbackup/AutosaveManager.java b/src/main/java/net/sf/jabref/logic/autosaveandbackup/AutosaveManager.java index b04f188e116..5948766ebbb 100644 --- a/src/main/java/net/sf/jabref/logic/autosaveandbackup/AutosaveManager.java +++ b/src/main/java/net/sf/jabref/logic/autosaveandbackup/AutosaveManager.java @@ -53,18 +53,8 @@ public synchronized void listen(@SuppressWarnings("unused") BibDatabaseContextCh } private void shutdown() { - try { - bibDatabaseContext.getDatabase().unregisterListener(this); - } catch(IllegalArgumentException e) { - // occurs if the event source has not been registered, should not prevent shutdown - LOGGER.debug(e); - } - try { - bibDatabaseContext.getMetaData().unregisterListener(this); - } catch(IllegalArgumentException e) { - // occurs if the event source has not been registered, should not prevent shutdown - LOGGER.debug(e); - } + bibDatabaseContext.getDatabase().unregisterListener(this); + bibDatabaseContext.getMetaData().unregisterListener(this); executor.shutdown(); } @@ -99,6 +89,11 @@ public void registerListener(Object listener) { } public void unregisterListener(Object listener) { - eventBus.unregister(listener); + try { + eventBus.unregister(listener); + } catch(IllegalArgumentException e) { + // occurs if the event source has not been registered, should not prevent shutdown + LOGGER.debug(e); + } } } diff --git a/src/main/java/net/sf/jabref/logic/autosaveandbackup/BackupManager.java b/src/main/java/net/sf/jabref/logic/autosaveandbackup/BackupManager.java index c117e0019ea..ce24b0c4ca4 100644 --- a/src/main/java/net/sf/jabref/logic/autosaveandbackup/BackupManager.java +++ b/src/main/java/net/sf/jabref/logic/autosaveandbackup/BackupManager.java @@ -145,18 +145,8 @@ private void startBackupTask() { * This method should only be used when closing a database/JabRef legally. */ private void shutdown() { - try { - bibDatabaseContext.getDatabase().unregisterListener(this); - } catch(IllegalArgumentException e) { - // occurs if the event source has not been registered, should not prevent shutdown - LOGGER.debug(e); - } - try { - bibDatabaseContext.getMetaData().unregisterListener(this); - } catch(IllegalArgumentException e) { - // occurs if the event source has not been registered, should not prevent shutdown - LOGGER.debug(e); - } + bibDatabaseContext.getDatabase().unregisterListener(this); + bibDatabaseContext.getMetaData().unregisterListener(this); executor.shutdown(); determineBackupPath().ifPresent(this::deleteBackupFile); } diff --git a/src/main/java/net/sf/jabref/logic/search/SearchQueryHighlightObservable.java b/src/main/java/net/sf/jabref/logic/search/SearchQueryHighlightObservable.java index d7e4144e84a..90ffbd79cf2 100644 --- a/src/main/java/net/sf/jabref/logic/search/SearchQueryHighlightObservable.java +++ b/src/main/java/net/sf/jabref/logic/search/SearchQueryHighlightObservable.java @@ -31,7 +31,11 @@ public void addSearchListener(SearchQueryHighlightListener newListener) { public void removeSearchListener(SearchQueryHighlightListener listener) { Objects.requireNonNull(listener); - eventBus.unregister(listener); + try { + eventBus.unregister(listener); + } catch(IllegalArgumentException e) { + // occurs if the event source has not been registered, should not prevent shutdown + } } /** * Fires an event if a search was started (or cleared) diff --git a/src/main/java/net/sf/jabref/model/database/BibDatabase.java b/src/main/java/net/sf/jabref/model/database/BibDatabase.java index b19e3a66523..9e1fb0ac7c4 100644 --- a/src/main/java/net/sf/jabref/model/database/BibDatabase.java +++ b/src/main/java/net/sf/jabref/model/database/BibDatabase.java @@ -530,7 +530,12 @@ public void registerListener(Object listener) { * @param listener listener (subscriber) to remove */ public void unregisterListener(Object listener) { - this.eventBus.unregister(listener); + try { + this.eventBus.unregister(listener); + } catch(IllegalArgumentException e) { + // occurs if the event source has not been registered, should not prevent shutdown + LOGGER.debug(e); + } } @Subscribe diff --git a/src/main/java/net/sf/jabref/model/entry/BibEntry.java b/src/main/java/net/sf/jabref/model/entry/BibEntry.java index f1e55b73751..14c7aa6a364 100644 --- a/src/main/java/net/sf/jabref/model/entry/BibEntry.java +++ b/src/main/java/net/sf/jabref/model/entry/BibEntry.java @@ -764,7 +764,12 @@ public void registerListener(Object object) { } public void unregisterListener(Object object) { - this.eventBus.unregister(object); + try { + this.eventBus.unregister(object); + } catch(IllegalArgumentException e) { + // occurs if the event source has not been registered, should not prevent shutdown + LOGGER.debug(e); + } } public BibEntry withField(String field, String value) { diff --git a/src/main/java/net/sf/jabref/model/metadata/MetaData.java b/src/main/java/net/sf/jabref/model/metadata/MetaData.java index c55e3d52718..525fbf81ee3 100644 --- a/src/main/java/net/sf/jabref/model/metadata/MetaData.java +++ b/src/main/java/net/sf/jabref/model/metadata/MetaData.java @@ -254,7 +254,11 @@ public void registerListener(Object listener) { } public void unregisterListener(Object listener) { - this.eventBus.unregister(listener); + try { + this.eventBus.unregister(listener); + } catch(IllegalArgumentException e) { + // occurs if the event source has not been registered, should not prevent shutdown + } } private Optional getDefaultCiteKeyPattern() { From e0d59c277747a7c4a3100ca53b71584d8bac3299 Mon Sep 17 00:00:00 2001 From: Joerg Lenhard Date: Wed, 25 Jan 2017 16:59:36 +0100 Subject: [PATCH 3/3] Minor formatting improvements --- .../sf/jabref/logic/autosaveandbackup/AutosaveManager.java | 2 +- .../jabref/logic/search/SearchQueryHighlightObservable.java | 2 +- src/main/java/net/sf/jabref/model/database/BibDatabase.java | 4 ++-- src/main/java/net/sf/jabref/model/entry/BibEntry.java | 2 +- src/main/java/net/sf/jabref/model/metadata/MetaData.java | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/autosaveandbackup/AutosaveManager.java b/src/main/java/net/sf/jabref/logic/autosaveandbackup/AutosaveManager.java index 5948766ebbb..323358afa9b 100644 --- a/src/main/java/net/sf/jabref/logic/autosaveandbackup/AutosaveManager.java +++ b/src/main/java/net/sf/jabref/logic/autosaveandbackup/AutosaveManager.java @@ -91,7 +91,7 @@ public void registerListener(Object listener) { public void unregisterListener(Object listener) { try { eventBus.unregister(listener); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { // occurs if the event source has not been registered, should not prevent shutdown LOGGER.debug(e); } diff --git a/src/main/java/net/sf/jabref/logic/search/SearchQueryHighlightObservable.java b/src/main/java/net/sf/jabref/logic/search/SearchQueryHighlightObservable.java index 90ffbd79cf2..2902fa749d5 100644 --- a/src/main/java/net/sf/jabref/logic/search/SearchQueryHighlightObservable.java +++ b/src/main/java/net/sf/jabref/logic/search/SearchQueryHighlightObservable.java @@ -33,7 +33,7 @@ public void removeSearchListener(SearchQueryHighlightListener listener) { try { eventBus.unregister(listener); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { // occurs if the event source has not been registered, should not prevent shutdown } } diff --git a/src/main/java/net/sf/jabref/model/database/BibDatabase.java b/src/main/java/net/sf/jabref/model/database/BibDatabase.java index 9e1fb0ac7c4..fdd4e1e3cda 100644 --- a/src/main/java/net/sf/jabref/model/database/BibDatabase.java +++ b/src/main/java/net/sf/jabref/model/database/BibDatabase.java @@ -532,8 +532,8 @@ public void registerListener(Object listener) { public void unregisterListener(Object listener) { try { this.eventBus.unregister(listener); - } catch(IllegalArgumentException e) { - // occurs if the event source has not been registered, should not prevent shutdown + } catch (IllegalArgumentException e) { + // occurs if the event source has not been registered, should not prevent shutdown LOGGER.debug(e); } } diff --git a/src/main/java/net/sf/jabref/model/entry/BibEntry.java b/src/main/java/net/sf/jabref/model/entry/BibEntry.java index 14c7aa6a364..7656456ff28 100644 --- a/src/main/java/net/sf/jabref/model/entry/BibEntry.java +++ b/src/main/java/net/sf/jabref/model/entry/BibEntry.java @@ -766,7 +766,7 @@ public void registerListener(Object object) { public void unregisterListener(Object object) { try { this.eventBus.unregister(object); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { // occurs if the event source has not been registered, should not prevent shutdown LOGGER.debug(e); } diff --git a/src/main/java/net/sf/jabref/model/metadata/MetaData.java b/src/main/java/net/sf/jabref/model/metadata/MetaData.java index 525fbf81ee3..c9be65a839f 100644 --- a/src/main/java/net/sf/jabref/model/metadata/MetaData.java +++ b/src/main/java/net/sf/jabref/model/metadata/MetaData.java @@ -256,7 +256,7 @@ public void registerListener(Object listener) { public void unregisterListener(Object listener) { try { this.eventBus.unregister(listener); - } catch(IllegalArgumentException e) { + } catch (IllegalArgumentException e) { // occurs if the event source has not been registered, should not prevent shutdown } }