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

DI-1 #687

Closed
wants to merge 32 commits into from
Closed

DI-1 #687

Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
418abad
Migrate BibEntryTypesManager
calixtus May 22, 2024
a72a5ab
Fix deprecation
calixtus May 23, 2024
fcc1b86
Migrate UndoManager
calixtus May 23, 2024
da9b991
Remove BibEntryTypesManager from Globals
calixtus May 23, 2024
f174227
Migrate PreferencesService
calixtus May 23, 2024
e358d17
Migrate DialogService
calixtus May 23, 2024
7bae500
Migrate ThemeManager
calixtus May 23, 2024
6d34bae
Migrate StateManager
calixtus May 23, 2024
3d58c7f
Migrate TaskExecutor
calixtus May 23, 2024
513d577
Migrate KeyBindingRepository WIP
calixtus May 23, 2024
b1a7a68
Fix StringToList conversion
calixtus May 23, 2024
f11d97c
Migrate KeyBindingRepository
calixtus May 23, 2024
5530353
Fix some tests
calixtus May 23, 2024
37fa15b
Checkstyle
calixtus May 23, 2024
a5674f4
Rewrite
calixtus May 23, 2024
1a572c9
Checkstyle
calixtus May 23, 2024
7ec1dbc
Fix tests
calixtus May 24, 2024
4b5b72e
Migrate ProtectedTermsLoader
calixtus May 24, 2024
7d93569
Refactor Launcher and extract logging initialization
calixtus May 24, 2024
555824c
Migrate JournalAbbreviationsRepository
calixtus May 24, 2024
fd17b7a
Move initialization to initialize
calixtus May 26, 2024
efc6767
Migrate ClipBoardManager
calixtus May 26, 2024
103a718
Migrate RemoteListenerServerManager
calixtus May 26, 2024
8bac78a
Migrate BuildInfo
calixtus May 26, 2024
55a1345
Merge remote-tracking branch 'refs/remotes/upstream/main' into di
calixtus May 27, 2024
9aa8cc4
Fix merge errors
calixtus May 27, 2024
5a085e1
Add some text on DI
koppor May 27, 2024
5d55857
Migrate FileUpdateMonitor and DirectoryMonitor, Remove Globals
calixtus May 27, 2024
7154f80
Remove DefaultInjector
calixtus May 27, 2024
3f0a318
Merge remote-tracking branch 'koppor/di' into di
calixtus May 27, 2024
56bc88a
Fix JavaDoc error
calixtus May 27, 2024
1c72950
Fix JavaDoc comment
calixtus May 27, 2024
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
8 changes: 5 additions & 3 deletions src/jmh/java/org/jabref/benchmarks/Benchmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Random;
import java.util.stream.Collectors;

import org.jabref.gui.Globals;
import org.jabref.logic.bibtex.FieldPreferences;
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences;
import org.jabref.logic.exporter.BibWriter;
Expand All @@ -35,7 +34,9 @@
import org.jabref.model.metadata.MetaData;
import org.jabref.model.search.rules.SearchRules.SearchFlags;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
import org.openjdk.jmh.Main;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
Expand All @@ -55,7 +56,7 @@ public class Benchmarks {

@Setup
public void init() throws Exception {
Globals.prefs = JabRefPreferences.getInstance();
Injector.setModelOrService(PreferencesService.class, JabRefPreferences.getInstance());

Random randomizer = new Random();
for (int i = 0; i < 1000; i++) {
Expand Down Expand Up @@ -92,7 +93,8 @@ private StringWriter getOutputWriter() throws IOException {

@Benchmark
public ParserResult parse() throws IOException {
BibtexParser parser = new BibtexParser(Globals.prefs.getImportFormatPreferences());
PreferencesService preferencesService = Injector.instantiateModelOrService(PreferencesService.class);
BibtexParser parser = new BibtexParser(preferencesService.getImportFormatPreferences());
return parser.parse(new StringReader(bibtexString));
}

Expand Down
71 changes: 32 additions & 39 deletions src/main/java/org/jabref/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.jabref.gui.JabRefGUI;
import org.jabref.logic.UiCommand;
import org.jabref.logic.journals.JournalAbbreviationLoader;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.net.ProxyAuthenticator;
import org.jabref.logic.net.ProxyPreferences;
import org.jabref.logic.net.ProxyRegisterer;
Expand All @@ -25,13 +26,15 @@
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
import org.jabref.logic.remote.RemotePreferences;
import org.jabref.logic.remote.client.RemoteClient;
import org.jabref.logic.util.BuildInfo;
import org.jabref.logic.util.OS;
import org.jabref.migrations.PreferencesMigrations;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -47,41 +50,33 @@
*/
public class Launcher {
private static Logger LOGGER;
private static boolean isDebugEnabled;

public static void main(String[] args) {
routeLoggingToSlf4J();
initLogging(args);

// We must configure logging as soon as possible, which is why we cannot wait for the usual
// argument parsing workflow to parse logging options .e.g. --debug
JabRefCLI jabRefCLI;
try {
jabRefCLI = new JabRefCLI(args);
isDebugEnabled = jabRefCLI.isDebugLogging();
} catch (ParseException e) {
isDebugEnabled = false;
}

addLogToDisk();
try {
BibEntryTypesManager entryTypesManager = new BibEntryTypesManager();
Globals.entryTypesManager = entryTypesManager;
Injector.setModelOrService(BuildInfo.class, new BuildInfo());

// Initialize preferences
final JabRefPreferences preferences = JabRefPreferences.getInstance();
Injector.setModelOrService(PreferencesService.class, preferences);

// Early exit in case another instance is already running
if (!handleMultipleAppInstances(args, preferences.getRemotePreferences())) {
return;
}

Globals.prefs = preferences;
BibEntryTypesManager entryTypesManager = preferences.getCustomEntryTypesRepository();
Injector.setModelOrService(BibEntryTypesManager.class, entryTypesManager);

PreferencesMigrations.runMigrations(preferences, entryTypesManager);

// Initialize rest of preferences
Injector.setModelOrService(JournalAbbreviationRepository.class, JournalAbbreviationLoader.loadRepository(preferences.getJournalAbbreviationPreferences()));
Injector.setModelOrService(ProtectedTermsLoader.class, new ProtectedTermsLoader(preferences.getProtectedTermsPreferences()));

configureProxy(preferences.getProxyPreferences());
configureSSL(preferences.getSSLPreferences());
initGlobals(preferences);

clearOldSearchIndices();

try {
Expand Down Expand Up @@ -114,25 +109,35 @@ public static void main(String[] args) {
}
}

private static void routeLoggingToSlf4J() {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}

/**
* This needs to be called as early as possible. After the first log write, it
* is not possible to alter
* the log configuration programmatically anymore.
* is not possible to alter the log configuration programmatically anymore.
*/
private static void addLogToDisk() {
private static void initLogging(String[] args) {
// routeLoggingToSlf4J
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();

// We must configure logging as soon as possible, which is why we cannot wait for the usual
// argument parsing workflow to parse logging options .e.g. --debug
boolean isDebugEnabled;
try {
JabRefCLI jabRefCLI = new JabRefCLI(args);
isDebugEnabled = jabRefCLI.isDebugLogging();
} catch (ParseException e) {
isDebugEnabled = false;
}

// addLogToDisk
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment no action needed:

Here, we see different coding styles. I used method names to avoid comments and to enable grouping of related statements. For you, it seemed that grouping is too fine grained and you wanted to group using lines. -- For me having the single line comment // addLogToDisk spanning statements separated by empty lines was unclear (also because there is the comment The "Shared File Writer" ....

Path directory = OS.getNativeDesktop().getLogDirectory();
try {
Files.createDirectories(directory);
} catch (IOException e) {
initializeLogger();
LOGGER = LoggerFactory.getLogger(Launcher.class);
LOGGER.error("Could not create log directory {}", directory, e);
return;
}

// The "Shared File Writer" is explained at
// https://tinylog.org/v2/configuration/#shared-file-writer
Map<String, String> configuration = Map.of(
Expand All @@ -144,12 +149,8 @@ private static void addLogToDisk() {
"writerFile.charset", "UTF-8",
"writerFile.policies", "startup",
"writerFile.backups", "30");

configuration.forEach(Configuration::set);
initializeLogger();
}

private static void initializeLogger() {
LOGGER = LoggerFactory.getLogger(Launcher.class);
}

Expand Down Expand Up @@ -183,14 +184,6 @@ private static boolean handleMultipleAppInstances(String[] args, RemotePreferenc
return true;
}

private static void initGlobals(PreferencesService preferences) {
// Read list(s) of journal names and abbreviations
Globals.journalAbbreviationRepository = JournalAbbreviationLoader
.loadRepository(preferences.getJournalAbbreviationPreferences());
Globals.entryTypesManager = preferences.getCustomEntryTypesRepository();
Globals.protectedTermsLoader = new ProtectedTermsLoader(preferences.getProtectedTermsPreferences());
}

private static void configureProxy(ProxyPreferences proxyPreferences) {
ProxyRegisterer.register(proxyPreferences);
if (proxyPreferences.shouldUseProxy() && proxyPreferences.shouldUseAuthentication()) {
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Set;
import java.util.prefs.BackingStoreException;

import org.jabref.gui.Globals;
import org.jabref.gui.externalfiles.AutoSetFileLinksUtil;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.logic.JabRefException;
Expand Down Expand Up @@ -59,6 +58,7 @@
import org.jabref.preferences.PreferencesService;
import org.jabref.preferences.SearchPreferences;

import com.airhacks.afterburner.injection.Injector;
import com.google.common.base.Throwables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -250,9 +250,9 @@ public void processArguments() {
preferencesService.getXmpPreferences(),
preferencesService.getFilePreferences(),
preferencesService.getLibraryPreferences().getDefaultBibDatabaseMode(),
Globals.entryTypesManager,
Injector.instantiateModelOrService(BibEntryTypesManager.class),
preferencesService.getFieldPreferences(),
Globals.journalAbbreviationRepository,
Injector.instantiateModelOrService(JournalAbbreviationRepository.class),
cli.isWriteXMPtoPdf() || cli.isWriteMetadatatoPdf(),
cli.isEmbeddBibfileInPdf() || cli.isWriteMetadatatoPdf());
}
Expand Down Expand Up @@ -486,15 +486,20 @@ private boolean exportMatches(List<ParserResult> loaded) {
// export new database
ExporterFactory exporterFactory = ExporterFactory.create(
preferencesService,
Globals.entryTypesManager);
Injector.instantiateModelOrService(BibEntryTypesManager.class));
Optional<Exporter> exporter = exporterFactory.getExporterByName(formatName);
if (exporter.isEmpty()) {
System.err.println(Localization.lang("Unknown export format %0", formatName));
} else {
// We have an TemplateExporter instance:
try {
System.out.println(Localization.lang("Exporting %0", data[1]));
exporter.get().export(databaseContext, Path.of(data[1]), matches, Collections.emptyList(), Globals.journalAbbreviationRepository);
exporter.get().export(
databaseContext,
Path.of(data[1]),
matches,
Collections.emptyList(),
Injector.instantiateModelOrService(JournalAbbreviationRepository.class));
} catch (Exception ex) {
System.err.println(Localization.lang("Could not export file '%0' (reason: %1)", data[1], Throwables.getStackTraceAsString(ex)));
}
Expand Down Expand Up @@ -661,7 +666,7 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
System.out.println(Localization.lang("Exporting %0", data[0]));
ExporterFactory exporterFactory = ExporterFactory.create(
preferencesService,
Globals.entryTypesManager);
Injector.instantiateModelOrService(BibEntryTypesManager.class));
Optional<Exporter> exporter = exporterFactory.getExporterByName(data[1]);
if (exporter.isEmpty()) {
System.err.println(Localization.lang("Unknown export format %0", data[1]));
Expand All @@ -673,7 +678,7 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
Path.of(data[0]),
parserResult.getDatabaseContext().getDatabase().getEntries(),
fileDirForDatabase,
Globals.journalAbbreviationRepository);
Injector.instantiateModelOrService(JournalAbbreviationRepository.class));
} catch (Exception ex) {
System.err.println(Localization.lang("Could not export file '%0' (reason: %1)", data[0], Throwables.getStackTraceAsString(ex)));
}
Expand All @@ -684,7 +689,7 @@ private void exportFile(List<ParserResult> loaded, String[] data) {
private void importPreferences() {
try {
preferencesService.importPreferences(Path.of(cli.getPreferencesImport()));
Globals.entryTypesManager = preferencesService.getCustomEntryTypesRepository();
Injector.setModelOrService(BibEntryTypesManager.class, preferencesService.getCustomEntryTypesRepository());
} catch (JabRefException ex) {
LOGGER.error("Cannot import preferences", ex);
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/jabref/cli/JabRefCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@

import javafx.util.Pair;

import org.jabref.gui.Globals;
import org.jabref.logic.exporter.ExporterFactory;
import org.jabref.logic.importer.ImportFormatReader;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.BuildInfo;
import org.jabref.logic.util.OS;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.strings.StringUtil;
import org.jabref.model.util.DummyFileUpdateMonitor;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
Expand Down Expand Up @@ -326,7 +328,7 @@ public static void printUsage(PreferencesService preferencesService) {

ExporterFactory exporterFactory = ExporterFactory.create(
preferencesService,
Globals.entryTypesManager);
Injector.instantiateModelOrService(BibEntryTypesManager.class));
List<Pair<String, String>> exportFormats = exporterFactory
.getExporters().stream()
.map(format -> new Pair<>(format.getName(), format.getId()))
Expand All @@ -341,7 +343,8 @@ public static void printUsage(PreferencesService preferencesService) {
}

private String getVersionInfo() {
return "JabRef %s".formatted(Globals.BUILD_INFO.version);
BuildInfo buildInfo = Injector.instantiateModelOrService(BuildInfo.class);
return "JabRef %s".formatted(buildInfo.version);
}

public List<String> getLeftOver() {
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jabref.model.entry.BibtexString;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -37,16 +38,13 @@ public class ClipBoardManager {
private static Clipboard clipboard;
private static java.awt.datatransfer.Clipboard primary;

private final PreferencesService preferencesService;

public ClipBoardManager(PreferencesService preferencesService) {
this(Clipboard.getSystemClipboard(), Toolkit.getDefaultToolkit().getSystemSelection(), preferencesService);
public ClipBoardManager() {
this(Clipboard.getSystemClipboard(), Toolkit.getDefaultToolkit().getSystemSelection());
}

public ClipBoardManager(Clipboard clipboard, java.awt.datatransfer.Clipboard primary, PreferencesService preferencesService) {
public ClipBoardManager(Clipboard clipboard, java.awt.datatransfer.Clipboard primary) {
ClipBoardManager.clipboard = clipboard;
ClipBoardManager.primary = primary;
this.preferencesService = preferencesService;
}

/**
Expand Down Expand Up @@ -169,6 +167,7 @@ public void setContent(List<BibEntry> entries, BibEntryTypesManager entryTypesMa
}

private String serializeEntries(List<BibEntry> entries, BibEntryTypesManager entryTypesManager) throws IOException {
PreferencesService preferencesService = Injector.instantiateModelOrService(PreferencesService.class);
// BibEntry is not Java serializable. Thus, we need to do the serialization manually
// At reading of the clipboard in JabRef, we parse the plain string in all cases, so we don't need to flag we put BibEntries here
// Furthermore, storing a string also enables other applications to work with the data
Expand Down
39 changes: 4 additions & 35 deletions src/main/java/org/jabref/gui/DefaultInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

import java.util.function.Function;

import javax.swing.undo.UndoManager;

import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.PreferencesService;

import com.airhacks.afterburner.injection.Injector;
import com.airhacks.afterburner.injection.PresenterFactory;
Expand All @@ -28,35 +19,13 @@ public class DefaultInjector implements PresenterFactory {
* Dependencies without default constructor are constructed by hand.
*/
private static Object createDependency(Class<?> clazz) {
if (clazz == DialogService.class) {
return JabRefGUI.getDialogService();
} else if (clazz == TaskExecutor.class) {
return Globals.TASK_EXECUTOR;
} else if (clazz == PreferencesService.class) {
return Globals.prefs;
} else if (clazz == KeyBindingRepository.class) {
return Globals.getKeyPrefs();
} else if (clazz == JournalAbbreviationRepository.class) {
return Globals.journalAbbreviationRepository;
} else if (clazz == StateManager.class) {
return Globals.stateManager;
} else if (clazz == ThemeManager.class) {
return JabRefGUI.getThemeManager();
} else if (clazz == FileUpdateMonitor.class) {
if (clazz == FileUpdateMonitor.class) {
return Globals.getFileUpdateMonitor();
} else if (clazz == ProtectedTermsLoader.class) {
return Globals.protectedTermsLoader;
} else if (clazz == ClipBoardManager.class) {
return Globals.getClipboardManager();
} else if (clazz == UndoManager.class) {
return Globals.undoManager;
} else if (clazz == BibEntryTypesManager.class) {
return Globals.entryTypesManager;
} else {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
LOGGER.error("Cannot instantiate dependency: " + clazz, ex);
return clazz.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
LOGGER.error("Cannot instantiate dependency: {}", clazz, ex);
return null;
}
}
Expand Down
Loading
Loading