From c68b6fbaa8675b7ee1cb2697b5d0b39970188724 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Tue, 16 May 2023 19:53:04 +0200 Subject: [PATCH 1/3] Make logger instances vars in linux Fixes #9908 --- .../jabref/gui/desktop/os/DefaultDesktop.java | 4 +-- .../java/org/jabref/gui/desktop/os/Linux.java | 26 +++++++++---------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java b/src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java index 86d97fb45ae..e33180068d1 100644 --- a/src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java @@ -8,12 +8,10 @@ import org.jabref.architecture.AllowedToUseAwt; import org.jabref.gui.DialogService; -import org.slf4j.Logger; import org.slf4j.LoggerFactory; @AllowedToUseAwt("Requires AWT to open a file") public class DefaultDesktop implements NativeDesktop { - private static final Logger LOGGER = LoggerFactory.getLogger(NativeDesktop.class); @Override public void openFile(String filePath, String fileType) throws IOException { @@ -33,7 +31,7 @@ public void openFolderAndSelectFile(Path filePath) throws IOException { @Override public void openConsole(String absolutePath, DialogService dialogService) throws IOException { - LOGGER.error("This feature is not supported by your Operating System."); + LoggerFactory.getLogger(DefaultDesktop.class).error("This feature is not supported by your Operating System."); } @Override diff --git a/src/main/java/org/jabref/gui/desktop/os/Linux.java b/src/main/java/org/jabref/gui/desktop/os/Linux.java index f3c3c3e3b4f..c1b77645c05 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Linux.java +++ b/src/main/java/org/jabref/gui/desktop/os/Linux.java @@ -20,30 +20,28 @@ import org.jabref.gui.util.StreamGobbler; import org.jabref.logic.l10n.Localization; -import org.slf4j.Logger; import org.slf4j.LoggerFactory; @AllowedToUseAwt("Requires AWT to open a file with the native method") public class Linux implements NativeDesktop { - private static final Logger LOGGER = LoggerFactory.getLogger(Linux.class); private void nativeOpenFile(String filePath) { JabRefExecutorService.INSTANCE.execute(() -> { try { File file = new File(filePath); Desktop.getDesktop().open(file); - LOGGER.debug("Open file in default application with Desktop integration"); + LoggerFactory.getLogger(Linux.class).debug("Open file in default application with Desktop integration"); } catch (IllegalArgumentException e) { - LOGGER.debug("Fail back to xdg-open"); + LoggerFactory.getLogger(Linux.class).debug("Fail back to xdg-open"); try { String[] cmd = {"xdg-open", filePath}; Runtime.getRuntime().exec(cmd); } catch (Exception e2) { - LOGGER.warn("Open operation not successful: " + e2); + LoggerFactory.getLogger(Linux.class).warn("Open operation not successful: ", e2); } } catch (IOException e) { - LOGGER.warn("Native open operation not successful: " + e); + LoggerFactory.getLogger(Linux.class).warn("Native open operation not successful: ", e); } }); } @@ -57,8 +55,8 @@ public void openFile(String filePath, String fileType) throws IOException { viewer = type.get().getOpenWithApplication(); ProcessBuilder processBuilder = new ProcessBuilder(viewer, filePath); Process process = processBuilder.start(); - StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug); - StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug); + StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LoggerFactory.getLogger(Linux.class)::debug); + StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LoggerFactory.getLogger(Linux.class)::debug); JabRefExecutorService.INSTANCE.execute(streamGobblerInput); JabRefExecutorService.INSTANCE.execute(streamGobblerError); @@ -80,8 +78,8 @@ public void openFileWithApplication(String filePath, String application) throws ProcessBuilder processBuilder = new ProcessBuilder(cmdArray); Process process = processBuilder.start(); - StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug); - StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug); + StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LoggerFactory.getLogger(Linux.class)::debug); + StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LoggerFactory.getLogger(Linux.class)::debug); JabRefExecutorService.INSTANCE.execute(streamGobblerInput); JabRefExecutorService.INSTANCE.execute(streamGobblerError); @@ -112,8 +110,8 @@ public void openFolderAndSelectFile(Path filePath) throws IOException { ProcessBuilder processBuilder = new ProcessBuilder(cmd); Process process = processBuilder.start(); - StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug); - StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug); + StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LoggerFactory.getLogger(Linux.class)::debug); + StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LoggerFactory.getLogger(Linux.class)::debug); JabRefExecutorService.INSTANCE.execute(streamGobblerInput); JabRefExecutorService.INSTANCE.execute(streamGobblerError); @@ -150,8 +148,8 @@ public void openConsole(String absolutePath, DialogService dialogService) throws builder.directory(new File(absolutePath)); Process processTerminal = builder.start(); - StreamGobbler streamGobblerInput = new StreamGobbler(processTerminal.getInputStream(), LOGGER::debug); - StreamGobbler streamGobblerError = new StreamGobbler(processTerminal.getErrorStream(), LOGGER::debug); + StreamGobbler streamGobblerInput = new StreamGobbler(processTerminal.getInputStream(), LoggerFactory.getLogger(Linux.class)::debug); + StreamGobbler streamGobblerError = new StreamGobbler(processTerminal.getErrorStream(), LoggerFactory.getLogger(Linux.class)::debug); JabRefExecutorService.INSTANCE.execute(streamGobblerInput); JabRefExecutorService.INSTANCE.execute(streamGobblerError); From b1eea07dec26ed5ff370e2fd2ecded2be1238433 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Tue, 16 May 2023 20:00:48 +0200 Subject: [PATCH 2/3] checkstyle --- src/main/java/org/jabref/gui/desktop/os/Linux.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/jabref/gui/desktop/os/Linux.java b/src/main/java/org/jabref/gui/desktop/os/Linux.java index c1b77645c05..aee7ed57fc3 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Linux.java +++ b/src/main/java/org/jabref/gui/desktop/os/Linux.java @@ -25,7 +25,6 @@ @AllowedToUseAwt("Requires AWT to open a file with the native method") public class Linux implements NativeDesktop { - private void nativeOpenFile(String filePath) { JabRefExecutorService.INSTANCE.execute(() -> { try { From 09eb36d0882b964cb3986eeeced349ed79dead5b Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Tue, 16 May 2023 21:04:15 +0200 Subject: [PATCH 3/3] add javadoc --- .../java/org/jabref/gui/desktop/os/DefaultDesktop.java | 7 +++++++ src/main/java/org/jabref/gui/desktop/os/Linux.java | 7 +++++++ src/main/java/org/jabref/gui/desktop/os/OSX.java | 7 +++++++ src/main/java/org/jabref/gui/desktop/os/Windows.java | 7 +++++++ 4 files changed, 28 insertions(+) diff --git a/src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java b/src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java index e33180068d1..30f651bbbbd 100644 --- a/src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java @@ -6,10 +6,17 @@ import java.nio.file.Path; import org.jabref.architecture.AllowedToUseAwt; +import org.jabref.cli.Launcher; import org.jabref.gui.DialogService; import org.slf4j.LoggerFactory; +/** + * This class contains some default implementations (if OS is neither linux, windows or osx) file directories and file/application open handling methods
+ * We cannot use a static logger instance here in this class as the Logger first needs to be configured in the {@link Launcher#addLogToDisk} + * The configuration of tinylog will become immutable as soon as the first log entry is issued. + * https://tinylog.org/v2/configuration/ + **/ @AllowedToUseAwt("Requires AWT to open a file") public class DefaultDesktop implements NativeDesktop { diff --git a/src/main/java/org/jabref/gui/desktop/os/Linux.java b/src/main/java/org/jabref/gui/desktop/os/Linux.java index aee7ed57fc3..a81a81bca55 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Linux.java +++ b/src/main/java/org/jabref/gui/desktop/os/Linux.java @@ -12,6 +12,7 @@ import java.util.Optional; import org.jabref.architecture.AllowedToUseAwt; +import org.jabref.cli.Launcher; import org.jabref.gui.DialogService; import org.jabref.gui.Globals; import org.jabref.gui.JabRefExecutorService; @@ -22,6 +23,12 @@ import org.slf4j.LoggerFactory; +/** + * This class contains Linux specific implementations for file directories and file/application open handling methods
+ * We cannot use a static logger instance here in this class as the Logger first needs to be configured in the {@link Launcher#addLogToDisk} + * The configuration of tinylog will become immutable as soon as the first log entry is issued. + * https://tinylog.org/v2/configuration/ + **/ @AllowedToUseAwt("Requires AWT to open a file with the native method") public class Linux implements NativeDesktop { diff --git a/src/main/java/org/jabref/gui/desktop/os/OSX.java b/src/main/java/org/jabref/gui/desktop/os/OSX.java index ece1d553d56..ccd0ca56f92 100644 --- a/src/main/java/org/jabref/gui/desktop/os/OSX.java +++ b/src/main/java/org/jabref/gui/desktop/os/OSX.java @@ -5,11 +5,18 @@ import java.util.Optional; import org.jabref.architecture.AllowedToUseAwt; +import org.jabref.cli.Launcher; import org.jabref.gui.DialogService; import org.jabref.gui.Globals; import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.gui.externalfiletype.ExternalFileTypes; +/** + * This class contains macOS (OSX) specific implementations for file directories and file/application open handling methods
+ * We cannot use a static logger instance here in this class as the Logger first needs to be configured in the {@link Launcher#addLogToDisk} + * The configuration of tinylog will become immutable as soon as the first log entry is issued. + * https://tinylog.org/v2/configuration/ + **/ @AllowedToUseAwt("Requires AWT to open a file") public class OSX implements NativeDesktop { diff --git a/src/main/java/org/jabref/gui/desktop/os/Windows.java b/src/main/java/org/jabref/gui/desktop/os/Windows.java index 7e5c2069703..e559f7dd033 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Windows.java +++ b/src/main/java/org/jabref/gui/desktop/os/Windows.java @@ -6,6 +6,7 @@ import java.nio.file.Path; import java.util.Optional; +import org.jabref.cli.Launcher; import org.jabref.gui.DialogService; import org.jabref.gui.Globals; import org.jabref.gui.externalfiletype.ExternalFileType; @@ -17,6 +18,12 @@ import com.sun.jna.platform.win32.Win32Exception; import org.slf4j.LoggerFactory; +/** + * This class contains Windows specific implementations for file directories and file/application open handling methods
+ * We cannot use a static logger instance here in this class as the Logger first needs to be configured in the {@link Launcher#addLogToDisk} + * The configuration of tinylog will become immutable as soon as the first log entry is issued. + * https://tinylog.org/v2/configuration/ + **/ public class Windows implements NativeDesktop { private static final String DEFAULT_EXECUTABLE_EXTENSION = ".exe";