From 4e28ed58b4e29d31d23f6a83f1510ab47ad1a02e Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Thu, 24 May 2018 13:21:47 +0200 Subject: [PATCH 1/5] Extract migrations --- src/main/java/org/jabref/JabRefMain.java | 47 +++++++++++++----------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/jabref/JabRefMain.java b/src/main/java/org/jabref/JabRefMain.java index 79291172919..1df0090fb76 100644 --- a/src/main/java/org/jabref/JabRefMain.java +++ b/src/main/java/org/jabref/JabRefMain.java @@ -36,16 +36,21 @@ * JabRef MainClass */ public class JabRefMain extends Application { - private static final Logger LOGGER = LoggerFactory.getLogger(JabRefMain.class); + private static String[] arguments; public static void main(String[] args) { arguments = args; - launch(arguments); } + @Override + public void start(Stage mainStage) throws Exception { + Platform.setImplicitExit(false); + SwingUtilities.invokeLater(() -> start(arguments)); + } + /** * Tests if we are running an acceptable Java and terminates JabRef when we are sure the version is not supported. * This test uses the requirements for the Java version as specified in gradle.build. It is possible to @@ -97,16 +102,7 @@ private static void start(String[] args) { JabRefPreferences preferences = JabRefPreferences.getInstance(); Globals.prefs = preferences; // Perform Migrations - // Perform checks and changes for users with a preference set from an older JabRef version. - PreferencesMigrations.upgradePrefsToOrgJabRef(); - PreferencesMigrations.upgradeSortOrder(); - PreferencesMigrations.upgradeFaultyEncodingStrings(); - PreferencesMigrations.upgradeLabelPatternToBibtexKeyPattern(); - PreferencesMigrations.upgradeImportFileAndDirePatterns(); - PreferencesMigrations.upgradeStoredCustomEntryTypes(); - PreferencesMigrations.upgradeKeyBindingsToJavaFX(); - PreferencesMigrations.addCrossRefRelatedFieldsForAutoComplete(); - PreferencesMigrations.upgradeObsoleteLookAndFeels(); + migratePreferences(); FallbackExceptionHandler.installExceptionHandler(); @@ -138,12 +134,12 @@ private static void start(String[] args) { preferences.loadCustomEntryTypes(BibDatabaseMode.BIBLATEX)); Globals.exportFactory = Globals.prefs.getExporterFactory(Globals.journalAbbreviationLoader); - // Process arguments - ArgumentProcessor argumentProcessor = new ArgumentProcessor(args, ArgumentProcessor.Mode.INITIAL_START); - // Initialize protected terms loader Globals.protectedTermsLoader = new ProtectedTermsLoader(Globals.prefs.getProtectedTermsPreferences()); + // Process arguments + ArgumentProcessor argumentProcessor = new ArgumentProcessor(args, ArgumentProcessor.Mode.INITIAL_START); + // Check for running JabRef RemotePreferences remotePreferences = Globals.prefs.getRemotePreferences(); if (remotePreferences.useRemoteServer()) { @@ -178,14 +174,21 @@ private static void start(String[] args) { // If not, start GUI SwingUtilities - .invokeLater(() -> new JabRefGUI(argumentProcessor.getParserResults(), - argumentProcessor.isBlank())); + .invokeLater(() -> new JabRefGUI(argumentProcessor.getParserResults(), argumentProcessor.isBlank())); } - @Override - public void start(Stage mainStage) throws Exception { - Platform.setImplicitExit(false); - SwingUtilities.invokeLater(() -> start(arguments) - ); + /** + * Perform checks and changes for users with a preference set from an older JabRef version. + */ + private static void migratePreferences() { + PreferencesMigrations.upgradePrefsToOrgJabRef(); + PreferencesMigrations.upgradeSortOrder(); + PreferencesMigrations.upgradeFaultyEncodingStrings(); + PreferencesMigrations.upgradeLabelPatternToBibtexKeyPattern(); + PreferencesMigrations.upgradeImportFileAndDirePatterns(); + PreferencesMigrations.upgradeStoredCustomEntryTypes(); + PreferencesMigrations.upgradeKeyBindingsToJavaFX(); + PreferencesMigrations.addCrossRefRelatedFieldsForAutoComplete(); + PreferencesMigrations.upgradeObsoleteLookAndFeels(); } } From 888aade35e69a5fa7e9cca87fdaa755f01183c4e Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Thu, 24 May 2018 13:38:38 +0200 Subject: [PATCH 2/5] Structure startup --- src/main/java/org/jabref/JabRefMain.java | 95 ++++++++++++++---------- 1 file changed, 54 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/jabref/JabRefMain.java b/src/main/java/org/jabref/JabRefMain.java index 1df0090fb76..2f3e152e8cc 100644 --- a/src/main/java/org/jabref/JabRefMain.java +++ b/src/main/java/org/jabref/JabRefMain.java @@ -98,49 +98,43 @@ private static void ensureCorrectJavaVersion() { } private static void start(String[] args) { + // Fail on unsupported Java versions + ensureCorrectJavaVersion(); + FallbackExceptionHandler.installExceptionHandler(); + // Init preferences - JabRefPreferences preferences = JabRefPreferences.getInstance(); + final JabRefPreferences preferences = JabRefPreferences.getInstance(); Globals.prefs = preferences; - // Perform Migrations + // Perform migrations migratePreferences(); - FallbackExceptionHandler.installExceptionHandler(); - - ensureCorrectJavaVersion(); - - ProxyPreferences proxyPreferences = preferences.getProxyPreferences(); - ProxyRegisterer.register(proxyPreferences); - if (proxyPreferences.isUseProxy() && proxyPreferences.isUseAuthentication()) { - Authenticator.setDefault(new ProxyAuthenticator()); - } + configureProxy(preferences.getProxyPreferences()); Globals.startBackgroundTasks(); - // Update handling of special fields based on preferences - InternalBibtexFields - .updateSpecialFields(Globals.prefs.getBoolean(JabRefPreferences.SERIALIZESPECIALFIELDS)); - // Update name of the time stamp field based on preferences - InternalBibtexFields.updateTimeStampField(Globals.prefs.getTimestampPreferences().getTimestampField()); - // Update which fields should be treated as numeric, based on preferences: - InternalBibtexFields.setNumericFields(Globals.prefs.getStringList(JabRefPreferences.NUMERIC_FIELDS)); - - // Read list(s) of journal names and abbreviations - Globals.journalAbbreviationLoader = new JournalAbbreviationLoader(); - - /* Build list of Import and Export formats */ - Globals.IMPORT_FORMAT_READER.resetImportFormats(Globals.prefs.getImportFormatPreferences(), - Globals.prefs.getXMPPreferences(), Globals.getFileUpdateMonitor()); - EntryTypes.loadCustomEntryTypes(preferences.loadCustomEntryTypes(BibDatabaseMode.BIBTEX), - preferences.loadCustomEntryTypes(BibDatabaseMode.BIBLATEX)); - Globals.exportFactory = Globals.prefs.getExporterFactory(Globals.journalAbbreviationLoader); - - // Initialize protected terms loader - Globals.protectedTermsLoader = new ProtectedTermsLoader(Globals.prefs.getProtectedTermsPreferences()); + applyPreferences(preferences); // Process arguments ArgumentProcessor argumentProcessor = new ArgumentProcessor(args, ArgumentProcessor.Mode.INITIAL_START); // Check for running JabRef + if (!allowMultipleAppInstances(args)) { + return; + } + + // See if we should shut down now + if (argumentProcessor.shouldShutDown()) { + Globals.shutdownThreadPools(); + Platform.exit(); + return; + } + + // If not, start GUI + SwingUtilities + .invokeLater(() -> new JabRefGUI(argumentProcessor.getParserResults(), argumentProcessor.isBlank())); + } + + private static boolean allowMultipleAppInstances(String[] args) { RemotePreferences remotePreferences = Globals.prefs.getRemotePreferences(); if (remotePreferences.useRemoteServer()) { Globals.REMOTE_LISTENER.open(new JabRefMessageHandler(), remotePreferences.getPort()); @@ -154,27 +148,46 @@ private static void start(String[] args) { Globals.shutdownThreadPools(); // needed to tell JavaFx to stop Platform.exit(); - return; + return false; } } // we are alone, we start the server Globals.REMOTE_LISTENER.start(); } + return true; + } + + private static void applyPreferences(JabRefPreferences preferences) { + // Update handling of special fields based on preferences + InternalBibtexFields.updateSpecialFields(Globals.prefs.getBoolean(JabRefPreferences.SERIALIZESPECIALFIELDS)); + // Update name of the time stamp field based on preferences + InternalBibtexFields.updateTimeStampField(Globals.prefs.getTimestampPreferences().getTimestampField()); + // Update which fields should be treated as numeric, based on preferences: + InternalBibtexFields.setNumericFields(Globals.prefs.getStringList(JabRefPreferences.NUMERIC_FIELDS)); + + // Read list(s) of journal names and abbreviations + Globals.journalAbbreviationLoader = new JournalAbbreviationLoader(); + + /* Build list of Import and Export formats */ + Globals.IMPORT_FORMAT_READER.resetImportFormats(Globals.prefs.getImportFormatPreferences(), + Globals.prefs.getXMPPreferences(), Globals.getFileUpdateMonitor()); + EntryTypes.loadCustomEntryTypes(preferences.loadCustomEntryTypes(BibDatabaseMode.BIBTEX), + preferences.loadCustomEntryTypes(BibDatabaseMode.BIBLATEX)); + Globals.exportFactory = Globals.prefs.getExporterFactory(Globals.journalAbbreviationLoader); + + // Initialize protected terms loader + Globals.protectedTermsLoader = new ProtectedTermsLoader(Globals.prefs.getProtectedTermsPreferences()); // override used newline character with the one stored in the preferences // The preferences return the system newline character sequence as default OS.NEWLINE = Globals.prefs.get(JabRefPreferences.NEWLINE); + } - // See if we should shut down now - if (argumentProcessor.shouldShutDown()) { - Globals.shutdownThreadPools(); - Platform.exit(); - return; + private static void configureProxy(ProxyPreferences proxyPreferences) { + ProxyRegisterer.register(proxyPreferences); + if (proxyPreferences.isUseProxy() && proxyPreferences.isUseAuthentication()) { + Authenticator.setDefault(new ProxyAuthenticator()); } - - // If not, start GUI - SwingUtilities - .invokeLater(() -> new JabRefGUI(argumentProcessor.getParserResults(), argumentProcessor.isBlank())); } /** From ad8d3f07bd6ff4fd6ee34f9669623e00c621a8e5 Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Fri, 25 May 2018 09:28:02 +0200 Subject: [PATCH 3/5] Move migrations to PreferencesMigrations --- src/main/java/org/jabref/JabRefMain.java | 17 +-------- .../migrations/PreferencesMigrations.java | 38 ++++++++++++------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/jabref/JabRefMain.java b/src/main/java/org/jabref/JabRefMain.java index 2f3e152e8cc..8eeda32c3d4 100644 --- a/src/main/java/org/jabref/JabRefMain.java +++ b/src/main/java/org/jabref/JabRefMain.java @@ -106,7 +106,7 @@ private static void start(String[] args) { final JabRefPreferences preferences = JabRefPreferences.getInstance(); Globals.prefs = preferences; // Perform migrations - migratePreferences(); + PreferencesMigrations.runMigrations(); configureProxy(preferences.getProxyPreferences()); @@ -189,19 +189,4 @@ private static void configureProxy(ProxyPreferences proxyPreferences) { Authenticator.setDefault(new ProxyAuthenticator()); } } - - /** - * Perform checks and changes for users with a preference set from an older JabRef version. - */ - private static void migratePreferences() { - PreferencesMigrations.upgradePrefsToOrgJabRef(); - PreferencesMigrations.upgradeSortOrder(); - PreferencesMigrations.upgradeFaultyEncodingStrings(); - PreferencesMigrations.upgradeLabelPatternToBibtexKeyPattern(); - PreferencesMigrations.upgradeImportFileAndDirePatterns(); - PreferencesMigrations.upgradeStoredCustomEntryTypes(); - PreferencesMigrations.upgradeKeyBindingsToJavaFX(); - PreferencesMigrations.addCrossRefRelatedFieldsForAutoComplete(); - PreferencesMigrations.upgradeObsoleteLookAndFeels(); - } } diff --git a/src/main/java/org/jabref/migrations/PreferencesMigrations.java b/src/main/java/org/jabref/migrations/PreferencesMigrations.java index e21a0fab82a..c3768f51cdd 100644 --- a/src/main/java/org/jabref/migrations/PreferencesMigrations.java +++ b/src/main/java/org/jabref/migrations/PreferencesMigrations.java @@ -22,17 +22,30 @@ import org.slf4j.LoggerFactory; public class PreferencesMigrations { - private static final Logger LOGGER = LoggerFactory.getLogger(PreferencesMigrations.class); private PreferencesMigrations() { } /** - * Migrate all preferences from net/sf/jabref to org/jabref + * Perform checks and changes for users with a preference set from an older JabRef version. */ - public static void upgradePrefsToOrgJabRef() { + public static void runMigrations() { + upgradePrefsToOrgJabRef(); + upgradeSortOrder(); + upgradeFaultyEncodingStrings(); + upgradeLabelPatternToBibtexKeyPattern(); + upgradeImportFileAndDirePatterns(); + upgradeStoredCustomEntryTypes(); + upgradeKeyBindingsToJavaFX(); + addCrossRefRelatedFieldsForAutoComplete(); + upgradeObsoleteLookAndFeels(); + } + /** + * Migrate all preferences from net/sf/jabref to org/jabref + */ + private static void upgradePrefsToOrgJabRef() { JabRefPreferences prefs = Globals.prefs; Preferences mainPrefsNode = Preferences.userNodeForPackage(JabRefMain.class); try { @@ -69,7 +82,7 @@ private static void copyPrefsRecursively(Preferences from, Preferences to) throw /** * Added from Jabref 2.11 beta 4 onwards to fix wrong encoding names */ - public static void upgradeFaultyEncodingStrings() { + private static void upgradeFaultyEncodingStrings() { JabRefPreferences prefs = Globals.prefs; String defaultEncoding = prefs.get(JabRefPreferences.DEFAULT_ENCODING); if (defaultEncoding == null) { @@ -110,7 +123,7 @@ public static void upgradeFaultyEncodingStrings() { * these preferences, but it is only used when the new preference does not * exist */ - public static void upgradeSortOrder() { + private static void upgradeSortOrder() { JabRefPreferences prefs = Globals.prefs; if (prefs.get(JabRefPreferences.EXPORT_IN_SPECIFIED_ORDER, null) == null) { @@ -138,8 +151,7 @@ public static void upgradeSortOrder() { /** * Migrate all customized entry types from versions <=3.7 */ - public static void upgradeStoredCustomEntryTypes() { - + private static void upgradeStoredCustomEntryTypes() { JabRefPreferences prefs = Globals.prefs; Preferences mainPrefsNode = Preferences.userNodeForPackage(JabRefMain.class); @@ -159,8 +171,7 @@ public static void upgradeStoredCustomEntryTypes() { /** * Migrate LabelPattern configuration from versions <=3.5 to new BibtexKeyPatterns */ - public static void upgradeLabelPatternToBibtexKeyPattern() { - + private static void upgradeLabelPatternToBibtexKeyPattern() { JabRefPreferences prefs = Globals.prefs; try { @@ -223,7 +234,7 @@ private static void migrateFileImportPattern(String oldStylePattern, String newS } } - public static void upgradeImportFileAndDirePatterns() { + private static void upgradeImportFileAndDirePatterns() { JabRefPreferences prefs = Globals.prefs; Preferences mainPrefsNode = Preferences.userNodeForPackage(JabRefMain.class); @@ -244,7 +255,7 @@ public static void upgradeImportFileAndDirePatterns() { // the user defined old-style patterns, and the default pattern is "". } - public static void upgradeKeyBindingsToJavaFX() { + private static void upgradeKeyBindingsToJavaFX() { UnaryOperator replaceKeys = (str) -> { String result = str.replace("ctrl ", "ctrl+"); result = result.replace("shift ", "shift+"); @@ -261,7 +272,7 @@ public static void upgradeKeyBindingsToJavaFX() { } - public static void addCrossRefRelatedFieldsForAutoComplete() { + private static void addCrossRefRelatedFieldsForAutoComplete() { JabRefPreferences prefs = Globals.prefs; //LinkedHashSet because we want to retain the order and add new fields to the end Set keys = new LinkedHashSet<>(prefs.getStringList(JabRefPreferences.AUTOCOMPLETER_COMPLETE_FIELDS)); @@ -283,7 +294,7 @@ private static void migrateTypedKeyPrefs(JabRefPreferences prefs, Preferences ol prefs.putKeyPattern(keyPattern); } - public static void upgradeObsoleteLookAndFeels() { + private static void upgradeObsoleteLookAndFeels() { JabRefPreferences prefs = Globals.prefs; String currentLandF = prefs.get(JabRefPreferences.WIN_LOOK_AND_FEEL); @@ -304,5 +315,4 @@ public static void upgradeObsoleteLookAndFeels() { } }); } - } From 9181257291db13a9f53439d08f5a11446f3d35d8 Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Fri, 25 May 2018 09:37:00 +0200 Subject: [PATCH 4/5] Shutdown duplicate code --- src/main/java/org/jabref/JabRefMain.java | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/jabref/JabRefMain.java b/src/main/java/org/jabref/JabRefMain.java index 8eeda32c3d4..4654655e860 100644 --- a/src/main/java/org/jabref/JabRefMain.java +++ b/src/main/java/org/jabref/JabRefMain.java @@ -118,14 +118,8 @@ private static void start(String[] args) { ArgumentProcessor argumentProcessor = new ArgumentProcessor(args, ArgumentProcessor.Mode.INITIAL_START); // Check for running JabRef - if (!allowMultipleAppInstances(args)) { - return; - } - - // See if we should shut down now - if (argumentProcessor.shouldShutDown()) { - Globals.shutdownThreadPools(); - Platform.exit(); + if (!handleMultipleAppInstances(args) || argumentProcessor.shouldShutDown()) { + shutdownCurrentInstance(); return; } @@ -134,7 +128,7 @@ private static void start(String[] args) { .invokeLater(() -> new JabRefGUI(argumentProcessor.getParserResults(), argumentProcessor.isBlank())); } - private static boolean allowMultipleAppInstances(String[] args) { + private static boolean handleMultipleAppInstances(String[] args) { RemotePreferences remotePreferences = Globals.prefs.getRemotePreferences(); if (remotePreferences.useRemoteServer()) { Globals.REMOTE_LISTENER.open(new JabRefMessageHandler(), remotePreferences.getPort()); @@ -145,9 +139,6 @@ private static boolean allowMultipleAppInstances(String[] args) { // We have successfully sent our command line options through the socket to another JabRef instance. // So we assume it's all taken care of, and quit. LOGGER.info(Localization.lang("Arguments passed on to running JabRef instance. Shutting down.")); - Globals.shutdownThreadPools(); - // needed to tell JavaFx to stop - Platform.exit(); return false; } } @@ -157,6 +148,12 @@ private static boolean allowMultipleAppInstances(String[] args) { return true; } + private static void shutdownCurrentInstance() { + Globals.shutdownThreadPools(); + // needed to tell JavaFx to stop + Platform.exit(); + } + private static void applyPreferences(JabRefPreferences preferences) { // Update handling of special fields based on preferences InternalBibtexFields.updateSpecialFields(Globals.prefs.getBoolean(JabRefPreferences.SERIALIZESPECIALFIELDS)); From 0776ac25d22297376932a758f4894be0f2aa3965 Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Fri, 25 May 2018 09:41:15 +0200 Subject: [PATCH 5/5] Package private for tests --- src/main/java/org/jabref/migrations/PreferencesMigrations.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/migrations/PreferencesMigrations.java b/src/main/java/org/jabref/migrations/PreferencesMigrations.java index c3768f51cdd..736cde821ec 100644 --- a/src/main/java/org/jabref/migrations/PreferencesMigrations.java +++ b/src/main/java/org/jabref/migrations/PreferencesMigrations.java @@ -234,7 +234,7 @@ private static void migrateFileImportPattern(String oldStylePattern, String newS } } - private static void upgradeImportFileAndDirePatterns() { + static void upgradeImportFileAndDirePatterns() { JabRefPreferences prefs = Globals.prefs; Preferences mainPrefsNode = Preferences.userNodeForPackage(JabRefMain.class);