Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure startup #4054

Merged
merged 5 commits into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 62 additions & 64 deletions src/main/java/org/jabref/JabRefMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>gradle.build</code>. It is possible to
Expand Down Expand Up @@ -93,58 +98,37 @@ private static void ensureCorrectJavaVersion() {
}

private static void start(String[] args) {
// Init preferences
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();

// Fail on unsupported Java versions
ensureCorrectJavaVersion();
FallbackExceptionHandler.installExceptionHandler();

ensureCorrectJavaVersion();
// Init preferences
final JabRefPreferences preferences = JabRefPreferences.getInstance();
Globals.prefs = preferences;
// Perform migrations
PreferencesMigrations.runMigrations();

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);
applyPreferences(preferences);

// Process arguments
ArgumentProcessor argumentProcessor = new ArgumentProcessor(args, ArgumentProcessor.Mode.INITIAL_START);

// Initialize protected terms loader
Globals.protectedTermsLoader = new ProtectedTermsLoader(Globals.prefs.getProtectedTermsPreferences());

// Check for running JabRef
if (!handleMultipleAppInstances(args) || argumentProcessor.shouldShutDown()) {
shutdownCurrentInstance();
return;
}

// If not, start GUI
SwingUtilities
.invokeLater(() -> new JabRefGUI(argumentProcessor.getParserResults(), argumentProcessor.isBlank()));
}

private static boolean handleMultipleAppInstances(String[] args) {
RemotePreferences remotePreferences = Globals.prefs.getRemotePreferences();
if (remotePreferences.useRemoteServer()) {
Globals.REMOTE_LISTENER.open(new JabRefMessageHandler(), remotePreferences.getPort());
Expand All @@ -155,37 +139,51 @@ private static void start(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;
return false;
}
}
// we are alone, we start the server
Globals.REMOTE_LISTENER.start();
}
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));
// 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;
}

// If not, start GUI
SwingUtilities
.invokeLater(() -> new JabRefGUI(argumentProcessor.getParserResults(),
argumentProcessor.isBlank()));
}

@Override
public void start(Stage mainStage) throws Exception {
Platform.setImplicitExit(false);
SwingUtilities.invokeLater(() -> start(arguments)
);
private static void configureProxy(ProxyPreferences proxyPreferences) {
ProxyRegisterer.register(proxyPreferences);
if (proxyPreferences.isUseProxy() && proxyPreferences.isUseAuthentication()) {
Authenticator.setDefault(new ProxyAuthenticator());
}
}
}
38 changes: 24 additions & 14 deletions src/main/java/org/jabref/migrations/PreferencesMigrations.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -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 {
Expand Down Expand Up @@ -223,7 +234,7 @@ private static void migrateFileImportPattern(String oldStylePattern, String newS
}
}

public static void upgradeImportFileAndDirePatterns() {
static void upgradeImportFileAndDirePatterns() {
JabRefPreferences prefs = Globals.prefs;

Preferences mainPrefsNode = Preferences.userNodeForPackage(JabRefMain.class);
Expand All @@ -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<String> replaceKeys = (str) -> {
String result = str.replace("ctrl ", "ctrl+");
result = result.replace("shift ", "shift+");
Expand All @@ -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<String> keys = new LinkedHashSet<>(prefs.getStringList(JabRefPreferences.AUTOCOMPLETER_COMPLETE_FIELDS));
Expand All @@ -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);

Expand All @@ -304,5 +315,4 @@ public static void upgradeObsoleteLookAndFeels() {
}
});
}

}