Skip to content

Commit

Permalink
Merge branch 'fix-arch2' of github.com:JabRef/jabref into fix-arch2
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Sep 8, 2024
2 parents 8d659f8 + a3a354a commit e2d405d
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 50 deletions.
28 changes: 0 additions & 28 deletions src/main/java/org/jabref/gui/FallbackExceptionHandler.java

This file was deleted.

8 changes: 7 additions & 1 deletion src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jabref.logic.remote.RemotePreferences;
import org.jabref.logic.remote.server.RemoteListenerServerManager;
import org.jabref.logic.util.BuildInfo;
import org.jabref.logic.util.FallbackExceptionHandler;
import org.jabref.logic.util.HeadlessExecutorService;
import org.jabref.logic.util.TaskExecutor;
import org.jabref.logic.util.WebViewStore;
Expand Down Expand Up @@ -87,7 +88,12 @@ public static void setup(List<UiCommand> uiCommands,
public void start(Stage stage) {
this.mainStage = stage;

FallbackExceptionHandler.installExceptionHandler();
FallbackExceptionHandler.installExceptionHandler((exception, thread) -> {
UiTaskExecutor.runInJavaFXThread(() -> {
DialogService dialogService = Injector.instantiateModelOrService(DialogService.class);
dialogService.showErrorDialogAndWait("Uncaught exception occurred in " + thread, exception);
});
});

initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

public class DuplicateResolverDialog extends BaseDialog<DuplicateResolverResult> {

private final BibDatabaseContext database;
private final StateManager stateManager;

public enum DuplicateResolverType {
Expand Down Expand Up @@ -72,7 +71,6 @@ public DuplicateResolverDialog(BibEntry one,
DialogService dialogService,
PreferencesService preferencesService) {
this.setTitle(Localization.lang("Possible duplicate entries"));
this.database = database;
this.stateManager = stateManager;
this.dialogService = dialogService;
this.preferencesService = preferencesService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private void onSuccess(Path targetDirectory, Path downloadedFile) {
boolean isDuplicate;
boolean isHtml;
try {
isDuplicate = FileNameUniqueness.isDuplicatedFile(targetDirectory, downloadedFile.getFileName(), dialogService);
isDuplicate = FileNameUniqueness.isDuplicatedFile(targetDirectory, downloadedFile.getFileName(), dialogService::notify);
} catch (IOException e) {
LOGGER.error("FileNameUniqueness.isDuplicatedFile failed", e);
return;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/util/UiTaskExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ public static <V> Task<V> getJavaFXTask(BackgroundTask<V> task) {
}

@Override
public V call() throws Exception {
protected V call() throws Exception {
// this requires that background task call is public as it's in another package
return task.call();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class BibFieldsIndexer implements LuceneIndexer {

public BibFieldsIndexer(BibDatabaseContext databaseContext) {
this.databaseContext = databaseContext;
this.libraryName = databaseContext.getDatabasePath().map(path -> path.getFileName().toString()).orElseGet(() -> "unsaved");
this.libraryName = databaseContext.getDatabasePath().map(path -> path.getFileName().toString()).orElse("unsaved");

IndexWriterConfig config = new IndexWriterConfig(SearchFieldConstants.LATEX_AWARE_NGRAM_ANALYZER);

Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/jabref/logic/util/FallbackExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.jabref.logic.util;

import java.util.function.BiConsumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Catch and log any unhandled exceptions.
*/
public class FallbackExceptionHandler implements Thread.UncaughtExceptionHandler {

private static final Logger LOGGER = LoggerFactory.getLogger(FallbackExceptionHandler.class);

private final BiConsumer<Throwable, Thread> onException;

public FallbackExceptionHandler(BiConsumer<Throwable, Thread> onException) {
this.onException = onException;
}

public FallbackExceptionHandler() {
this(null);
}

public static void installExceptionHandler(BiConsumer<Throwable, Thread> onException) {
Thread.setDefaultUncaughtExceptionHandler(new FallbackExceptionHandler(onException));
}

@Override
public void uncaughtException(Thread thread, Throwable exception) {
LOGGER.error("Uncaught exception occurred in {}", thread, exception);
if (this.onException != null) {
this.onException.accept(exception, thread);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.jabref.gui.FallbackExceptionHandler;
import org.jabref.gui.util.UiTaskExecutor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Responsible for managing of all threads (<em>except</em> GUI threads) in JabRef.
* <p>
* GUI background tasks should run in {@link UiTaskExecutor}.
* GUI background tasks should run in {@link org.jabref.gui.util.UiTaskExecutor}.
* <p>
* This is a wrapper around {@link ExecutorService}
* <p>
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/jabref/logic/util/io/FileNameUniqueness.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jabref.gui.DialogService;
import org.jabref.logic.l10n.Localization;

import org.slf4j.Logger;
Expand Down Expand Up @@ -60,18 +60,17 @@ public static String getNonOverWritingFileName(Path targetDirectory, String file
*
* @param directory The directory which saves the files (.pdf, for example)
* @param fileName Suggest name for the newly downloaded file
* @param dialogService To display the error and success message
* @param messageOnDeletion To display the error and success message
* @return true when the content of the newly downloaded file is same as the file with "similar" name,
* false when there is no "similar" file name or the content is different from that of files with "similar" name
* @throws IOException Fail when the file is not exist or something wrong when reading the file
*/
public static boolean isDuplicatedFile(Path directory, Path fileName, DialogService dialogService) throws IOException {
public static boolean isDuplicatedFile(Path directory, Path fileName, Consumer<String> messageOnDeletion ) throws IOException {
Objects.requireNonNull(directory);
Objects.requireNonNull(fileName);
Objects.requireNonNull(dialogService);

String extensionSuffix = FileUtil.getFileExtension(fileName).orElse("");
extensionSuffix = "".equals(extensionSuffix) ? extensionSuffix : "." + extensionSuffix;
extensionSuffix = extensionSuffix.isEmpty() ? extensionSuffix : "." + extensionSuffix;
String newFilename = FileUtil.getBaseName(fileName) + extensionSuffix;

String fileNameWithoutDuplicated = eraseDuplicateMarks(FileUtil.getBaseName(fileName));
Expand All @@ -89,7 +88,7 @@ public static boolean isDuplicatedFile(Path directory, Path fileName, DialogServ
if (com.google.common.io.Files.equal(originalFile.toFile(), duplicateFile.toFile())) {
try {
Files.delete(duplicateFile);
dialogService.notify(Localization.lang("File '%1' is a duplicate of '%0'. Keeping '%0'", originalFileName, fileName));
messageOnDeletion.accept(Localization.lang("File '%1' is a duplicate of '%0'. Keeping '%0'", originalFileName, fileName));
} catch (IOException e) {
LOGGER.error("File '{}' is a duplicate of '{}'. Could not delete '{}'.", fileName, originalFileName, fileName);
}
Expand Down
16 changes: 10 additions & 6 deletions src/test/java/org/jabref/logic/util/io/FileNameUniquenessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import java.nio.file.Path;

import org.jabref.gui.DialogService;
import org.jabref.gui.push.PushToTexShop;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -16,6 +19,8 @@

class FileNameUniquenessTest {

private static final Logger LOGGER = LoggerFactory.getLogger(FileNameUniquenessTest.class);

@TempDir
protected Path tempDir;

Expand Down Expand Up @@ -56,21 +61,20 @@ void isDuplicatedFileWithNoSimilarNames() throws IOException {
Path filePath1 = tempDir.resolve(filename1);
Files.createFile(filePath1);

boolean isDuplicate = FileNameUniqueness.isDuplicatedFile(tempDir, filePath1, dialogService);
boolean isDuplicate = FileNameUniqueness.isDuplicatedFile(tempDir, filePath1, LOGGER::info);
assertFalse(isDuplicate);
}

@Test
void isDuplicatedFileWithOneSimilarNames() throws IOException {
DialogService dialogService = mock(DialogService.class);
String filename1 = "file.txt";
String filename2 = "file (1).txt";
Path filePath1 = tempDir.resolve(filename1);
Path filePath2 = tempDir.resolve(filename2);
Files.createFile(filePath1);
Files.createFile(filePath2);

boolean isDuplicate = FileNameUniqueness.isDuplicatedFile(tempDir, filePath2, dialogService);
boolean isDuplicate = FileNameUniqueness.isDuplicatedFile(tempDir, filePath2, LOGGER::info);
assertTrue(isDuplicate);
}

Expand All @@ -82,21 +86,21 @@ void taseDuplicateMarksReturnsOrignalFileName1() throws IOException {
}

@Test
void taseDuplicateMarksReturnsOrignalFileName2() throws IOException {
void taseDuplicateMarksReturnsOrignalFileName2() {
String fileName1 = "abc (def) gh (1)";
String fileName2 = FileNameUniqueness.eraseDuplicateMarks(fileName1);
assertEquals("abc (def) gh", fileName2);
}

@Test
void taseDuplicateMarksReturnsSameName1() throws IOException {
void taseDuplicateMarksReturnsSameName1() {
String fileName1 = "abc def (g)";
String fileName2 = FileNameUniqueness.eraseDuplicateMarks(fileName1);
assertEquals("abc def (g)", fileName2);
}

@Test
void taseDuplicateMarksReturnsSameName2() throws IOException {
void taseDuplicateMarksReturnsSameName2() {
String fileName1 = "abc def";
String fileName2 = FileNameUniqueness.eraseDuplicateMarks(fileName1);
assertEquals("abc def", fileName2);
Expand Down

0 comments on commit e2d405d

Please sign in to comment.