Skip to content
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
41 changes: 30 additions & 11 deletions brailleblaster-core/src/main/java/org/brailleblaster/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.brailleblaster.logging.initLogback
import org.brailleblaster.logging.preLog
import org.brailleblaster.usage.*
import org.brailleblaster.userHelp.Project
import org.brailleblaster.exceptions.BBNotifyException
import org.brailleblaster.utd.exceptions.NodeException
import org.brailleblaster.util.Notify
import org.brailleblaster.util.NotifyUtils
Expand All @@ -33,6 +34,7 @@ import org.brailleblaster.wordprocessor.WPManager
import org.slf4j.LoggerFactory
import java.io.File
import java.net.URLClassLoader
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.time.Duration
Expand Down Expand Up @@ -69,25 +71,30 @@ object Main {
@Throws(Exception::class)
fun start(args: Array<String>) {
val argsToParse = args.toMutableList()
initBB(argsToParse)
if (System.getProperty("dumpClassPath", "false") == "true") {
dumpClassLoader(ClassLoader.getSystemClassLoader())
//Handle maven-wrapper and presumably other IDE loaders
if (ClassLoader.getSystemClassLoader() !== Thread.currentThread().contextClassLoader) {
dumpClassLoader(Thread.currentThread().contextClassLoader)
}
}
var fileToOpen: Path? = null
if (argsToParse.isNotEmpty()) {
val firstArg = argsToParse[0]
try {
fileToOpen = Paths.get(firstArg)
} catch (e: Exception) {
LoggerFactory.getLogger(Main::class.java).error("Failed to open $firstArg", e)
RECOVERABLE_BOOT_EXCEPTIONS.add(e)
showStartupMessage("Error: The file path '$firstArg' is invalid.")
return
}
if (!Files.exists(fileToOpen)) {
val displayName = fileToOpen.fileName?.toString() ?: fileToOpen.toString()
showStartupMessage("Error: The file '$displayName' was not found.")
return
}
argsToParse.removeAt(0)
}
initBB(argsToParse)
if (System.getProperty("dumpClassPath", "false") == "true") {
dumpClassLoader(ClassLoader.getSystemClassLoader())
//Handle maven-wrapper and presumably other IDE loaders
if (ClassLoader.getSystemClassLoader() !== Thread.currentThread().contextClassLoader) {
dumpClassLoader(Thread.currentThread().contextClassLoader)
}
}
if (argsToParse.isNotEmpty()) {
LoggerFactory.getLogger(Main::class.java)
.error("Unknown extra arguments beyond file: " + args.joinToString(" "))
Expand All @@ -109,14 +116,26 @@ object Main {
usageManager.startPeriodicDataReporting(0, 5, units = TimeUnit.MINUTES)
val bbStartTime = Instant.now()
usageManager.logger.logStart(tool = BB_TOOL, message = runId.toString())
WPManager.createInstance(fileToOpen, usageManager).start()
try {
WPManager.createInstance(fileToOpen, usageManager).start()
} catch (e: BBNotifyException) {
showStartupMessage(e.message)
return@use
}
usageManager.logger.logDurationSeconds(tool = BB_TOOL, duration = Duration.between(bbStartTime, Instant.now()))
usageManager.logger.logEnd(tool = BB_TOOL, message = runId.toString())
usageManager.reportDataAsync().get()
}
}
}

private fun showStartupMessage(message: String?) {
if (message.isNullOrBlank()) {
return
}
println(message)
}

/**
*
* @param argsToParse Arguments that will be removed from list when parsed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.FileSystemException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -324,7 +327,7 @@ public Manager getManager() {
public void open() {
Path file = _initFile;
if (!Files.exists(file)) {
throw new RuntimeException("File " + file.toUri() + " does not exist");
throw new BBNotifyException(fileOpenMessage(file, new NoSuchFileException(file.toString())));
}
// Attempt to open file before all else. RT-7789
openDocument(file);
Expand Down Expand Up @@ -485,6 +488,7 @@ public void openDocument(Path file) {
archiver = ArchiverFactory.INSTANCE.load(file);
} catch (Exception e) {
logger.error("Problem loading file", e);
throw new BBNotifyException(fileOpenMessage(file, e), e);
}

// Recent Files.
Expand Down Expand Up @@ -1961,6 +1965,45 @@ public Archiver2 getArchiver() {
return Objects.requireNonNull(archiver, "Manager not open");
}

private String fileOpenMessage(Path fileName, Throwable cause) {
String displayName = fileName.getFileName() != null ? fileName.getFileName().toString() : fileName.toString();
if (cause instanceof BBNotifyException && cause.getMessage() != null && !cause.getMessage().isBlank()) {
return cause.getMessage();
}
if (cause instanceof NoSuchFileException) {
return "Error: The file '" + displayName + "' was not found.";
}
if (cause instanceof AccessDeniedException) {
return "Error: Access to the specified file was denied.";
}
if (cause instanceof FileSystemException fileSystemException) {
String reason = fileSystemException.getReason() != null ? fileSystemException.getReason().toLowerCase(Locale.getDefault()) : "";
if (reason.contains("denied") || reason.contains("permission") || reason.contains("access")) {
return "Error: Access to the specified file was denied.";
}
if (reason.contains("locked") || reason.contains("use by another process") || reason.contains("in use") || reason.contains("sharing violation")) {
return "Error: The specified file is locked or unavailable.";
}
}
if (cause instanceof XPathException) {
if (fileName.getFileName() != null && fileName.getFileName().toString().toLowerCase(Locale.getDefault()).endsWith(".bbz")) {
return "Error: '" + displayName + "' may be from an earlier release of BrailleBlaster and is not supported in this version. Please use an earlier version of BrailleBlaster to open this file.";
}
return "Error: '" + displayName + "' is not an archive that can be opened by BrailleBlaster.";
}
String message = cause.getMessage() != null ? cause.getMessage().toLowerCase(Locale.getDefault()) : "";
if (message.contains("locked") || message.contains("sharing violation") || message.contains("being used by another process") || message.contains("in use")) {
return "Error: The specified file is locked or unavailable.";
}
if (message.contains("permission") || message.contains("denied") || message.contains("access is denied")) {
return "Error: Access to the specified file was denied.";
}
if (fileName.getParent() != null && !Files.exists(fileName.getParent())) {
return "Error: The path '" + fileName + "' does not exist.";
}
return "Error: Unable to open the specified file.";
}

class Reformatter implements Runnable {
Element newPage;
Element printPageBrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.awt.SplashScreen
import java.io.IOException
import java.nio.file.AccessDeniedException
import java.nio.file.FileSystemException
import java.nio.file.NoSuchFileException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
Expand Down Expand Up @@ -191,7 +194,7 @@ class WPManager private constructor(val usageManager: UsageManager) {
else -> null
}
currentPerspective = BraillePerspective(this)
addDocumentManager(fileToOpen)
addDocumentManager(fileToOpen, startupOpen = true)
val firstManager = controller

//move to here for #6370: Open BB, Close it before it can open, get a fatal exception
Expand Down Expand Up @@ -357,7 +360,7 @@ class WPManager private constructor(val usageManager: UsageManager) {
}
}

fun addDocumentManager(fileName: Path?) {
fun addDocumentManager(fileName: Path?, startupOpen: Boolean = false) {
if (fileName == null || (fileName.exists() && !fileName.isDirectory())) {
val numberOfTab = 10
if (list.size < numberOfTab) {
Expand All @@ -372,12 +375,20 @@ class WPManager private constructor(val usageManager: UsageManager) {
}

val oldManager = currentManager
setupDocumentManager(fileName, oldManager)
setupDocumentManager(fileName, oldManager, startupOpen)
openingDoc = false
} else {
showMessage("The number of tabs allowed open is 10.")
}
} else {
if (startupOpen) {
val cause = if (fileName != null && !Files.exists(fileName)) {
NoSuchFileException(fileName.toString())
} else {
FileSystemException(fileName?.toString(), null, "Cannot open file")
}
throw BBNotifyException(fileOpenMessage(fileName!!, cause))
}
if (fileName in RecentDocs.defaultRecentDocs.recentDocs) {
val removeFromRecentDocs = MessageBox(shell, SWT.ICON_ERROR or SWT.YES or SWT.NO).apply {
text = "File unavailable"
Expand All @@ -393,7 +404,7 @@ class WPManager private constructor(val usageManager: UsageManager) {
}
}

private fun setupDocumentManager(fileName: Path?, oldManager: Manager?) {
private fun setupDocumentManager(fileName: Path?, oldManager: Manager?, startupOpen: Boolean = false) {
val m = newManager(fileName, oldManager)
try {
// try opening file first before everything else / RT-7789
Expand Down Expand Up @@ -425,32 +436,11 @@ class WPManager private constructor(val usageManager: UsageManager) {
folder.setSelection(curSelection.coerceAtMost(folder.itemCount - 1))
folder.notifyListeners(SWT.Selection, Event())

val filenameString = fileName!!.fileName.toString()
val fileExt = filenameString.substring(filenameString.lastIndexOf('.'))

if (e is BBNotifyException) {
//Generally a problem with the Archiver being null - an invalid file type
//Message is already provided
showMessage(e.message)
//throw e;
} else if (e is XPathException) {
//Happens with an invalid ZIP file or outdated / corrupt bbz document
if (fileExt == ".bbz") {
showMessage(
"""$filenameString may be a file from an earlier release of BrailleBlaster and is not supported in this version.
Please use an earlier version of BrailleBlaster to open this file."""
)
} else {
showMessage(
filenameString +
" is not an archive that can be opened by BrailleBlaster."
)
}
} else {
//Some other file open exception
showMessage("$filenameString cannot be opened by BrailleBlaster")
//throw new RuntimeException("Failed to open " + fileName, e);
val message = fileOpenMessage(fileName!!, e)
if (startupOpen) {
throw BBNotifyException(message, e)
}
showMessage(message)
} finally {
val curManager = currentManager
initMenuAndToolbar(curManager)
Expand All @@ -476,6 +466,45 @@ class WPManager private constructor(val usageManager: UsageManager) {
RecentDocs.defaultRecentDocs.addRecentDoc(fileName!!)
}

private fun fileOpenMessage(fileName: Path, cause: Throwable): String {
val displayName = fileName.fileName?.toString() ?: fileName.toString()
if (cause is BBNotifyException && !cause.message.isNullOrBlank()) {
return cause.message!!
}
if (cause is NoSuchFileException) {
return "Error: The file '$displayName' was not found."
}
if (cause is AccessDeniedException) {
return "Error: Access to the specified file was denied."
}
if (cause is FileSystemException) {
val reason = cause.reason?.lowercase(Locale.getDefault()) ?: ""
if (reason.contains("denied") || reason.contains("permission") || reason.contains("access")) {
return "Error: Access to the specified file was denied."
}
if (reason.contains("locked") || reason.contains("use by another process") || reason.contains("in use") || reason.contains("sharing violation")) {
return "Error: The specified file is locked or unavailable."
}
}
if (cause is XPathException) {
if (fileName.toString().endsWith(".bbz", ignoreCase = true)) {
return "Error: '$displayName' may be from an earlier release of BrailleBlaster and is not supported in this version. Please use an earlier version of BrailleBlaster to open this file."
}
return "Error: '$displayName' is not an archive that can be opened by BrailleBlaster."
}
val message = cause.message?.lowercase(Locale.getDefault()) ?: ""
if (message.contains("locked") || message.contains("sharing violation") || message.contains("being used by another process") || message.contains("in use")) {
return "Error: The specified file is locked or unavailable."
}
if (message.contains("permission") || message.contains("denied") || message.contains("access is denied")) {
return "Error: Access to the specified file was denied."
}
if (fileName.parent != null && !Files.exists(fileName.parent)) {
return "Error: The path '$fileName' does not exist."
}
return "Error: Unable to open the specified file."
}

fun setSelection() {
val index = list.size - 1
folder.setSelection(index)
Expand Down
Loading