Added better logging and browser support.

We still need to work on the Linux browser a bit.
This commit is contained in:
Markil3
2021-07-25 14:07:07 -07:00
parent 6a95966f9e
commit 77c0b4fa67
517 changed files with 286 additions and 495 deletions

View File

@@ -15,7 +15,11 @@ repositories {
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
implementation 'org.slf4j:slf4j-api:1.7.25'
implementation 'org.slf4j:slf4j-api:1.7.30'
implementation 'org.apache.logging.log4j:log4j-api:2.13.3'
implementation 'org.apache.logging.log4j:log4j-core:2.13.3'
implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.13.3'
implementation 'com.google.code.gson:gson:2.8.7'
implementation 'com.googlecode.soundlibs:jlayer:1.0.1.4'
implementation 'com.mpatric:mp3agic:0.9.1'
@@ -28,6 +32,9 @@ dependencies {
testImplementation 'junit:junit:4.12'
}
sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16
// Define the main class for the application
mainClassName = defaultPackage + '.player.Interface'
//mainClassName = defaultPackage + '.localPlayer.Player'

View File

@@ -4,11 +4,10 @@
package edu.regis.universeplayer.browser;
import com.google.gson.Gson;
import com.google.gson.stream.JsonWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
@@ -19,8 +18,10 @@ import java.io.IOException;
*/
public class Browser
{
private static Logger logger = LoggerFactory.getLogger(Browser.class);
private static Process process;
/**
* Utility method for launching a browser instance
*
@@ -30,116 +31,63 @@ public class Browser
{
String os = System.getProperty("os.name").toLowerCase();
String arch = System.getProperty("os.arch").toLowerCase();
String args = " -profile browser/profile";
if (process != null && process.isAlive())
String args;
int startExit;
File browserDir = new File(System.getProperty("user.dir"), "browser");
if (!browserDir.exists())
{
process.destroy();
browserDir = new File(System.getProperty("user.dir"), "../browser");
}
args = " -profile \"" + browserDir.getAbsolutePath() + "/profile\"";
logger.info("Running on {} {}", os, arch);
// System.getProperties().entrySet().stream().forEach(entry -> logger.info("{}: {}", entry.getKey(), entry.getValue()));
if (os.contains("windows"))
{
setupWindows();
process = Runtime.getRuntime().exec("browser/windows/FirefoxPortable.exe" + args);
}
else if (os.contains("linux"))
{
setupLinux();
if (arch.contains("64"))
{
process = Runtime.getRuntime().exec("./browser/linux64/firefox" + args);
logger.info("Starting Windows x86_64 browser");
process = Runtime.getRuntime().exec(new File(browserDir, "windows64/firefox.exe").getAbsolutePath() + args);
}
else
{
process = Runtime.getRuntime().exec("./browser/linux32/firefox" + args);
logger.info("Starting Windows x86 browser");
process = Runtime.getRuntime().exec(new File(browserDir, "windows32/firefox.exe").getAbsolutePath() + args);
}
}
System.out.println(os);
}
/**
* Performs first-time setup for Windows applications.
*/
private static void setupWindows()
{
try
else if (os.contains("linux"))
{
Runtime.getRuntime()
.exec("REG ADD HKEY_CURRENT_USER\\SOFTWARE\\Mozilla\\NativeMessagingHosts /v universal_music /d \"" + System
.getProperty("user.dir") + "\\bin\\interface.bat\" ");
if (arch.contains("64"))
{
logger.info("Starting Linux x86_64 browser");
process = Runtime.getRuntime().exec(new File(browserDir, "linux64/firefox").getAbsolutePath() + args);
}
else
{
logger.info("Starting Linux 86 browser");
process = Runtime.getRuntime().exec(new File(browserDir, "linux32/firefox").getAbsolutePath() + args);
}
}
catch (IOException e)
if (process == null)
{
System.err.println("Could not set Windows registry key.");
e.printStackTrace();
throw new IOException("Could not find Firefox installation for OS " + os + " " + arch);
}
}
/**
* Performs first-time setup for OSX applications.
*/
private static void setupMac()
{
Gson gson = new Gson();
try (JsonWriter writer = gson.newJsonWriter(new FileWriter(new File(System
.getProperty("user.home"), "Library/Application Support/Mozilla/NativeMessagingHosts/universal_music.json"))))
{
writeManifest(writer, System.getProperty("user.dir") + "/bin/interface");
}
catch (IOException e)
{
System.err.println("Could not set Mac application manifest.");
e.printStackTrace();
}
}
/**
* Performs first-time setup for Linux applications.
*/
private static void setupLinux()
{
Gson gson = new Gson();
try (JsonWriter writer = gson.newJsonWriter(new FileWriter(new File(System
.getProperty("user.home"), ".mozilla/native-messaging-hosts/universal_music.json"))))
{
writeManifest(writer, System.getProperty("user.dir") + "/bin/interface");
}
catch (IOException e)
{
System.err.println("Could not set Linux application manifest.");
e.printStackTrace();
}
}
private static void writeManifest(JsonWriter writer, String path) throws IOException
{
writer.beginObject();
writer.name("name");
writer.value("universal_music");
writer.name("description");
writer.value("Universal Music Player");
writer.name("path");
writer.value(path);
writer.name("type");
writer.value("stdio");
writer.name("allowed_extensions");
writer.beginArray();
writer.value("universal_music@regis.edu");
writer.endArray();
writer.endObject();
}
/**
* Tells the browser process to shut down.
*/
public static void closeBrowser()
{
if (process != null && process.isAlive())
if (process != null)
{
logger.info("Destroying browser processes {}, {}", process, process.descendants().toArray(ProcessHandle[]::new));
process.descendants().forEach(ProcessHandle::destroy);
process.destroy();
}
else
{
logger.info("Process already destroyed.");
}
}
}

View File

@@ -4,6 +4,8 @@
package edu.regis.universeplayer.localPlayer;
import com.intervigil.wave.WaveReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
@@ -20,6 +22,8 @@ import java.util.LinkedList;
*/
public class Player
{
private static final Logger logger = LoggerFactory.getLogger(Player.class);
static
{
System.loadLibrary("player");
@@ -81,7 +85,7 @@ public class Player
}
catch (IOException e)
{
e.printStackTrace();
logger.error("Could not save WAVE file", e);
}
}
@@ -166,7 +170,7 @@ public class Player
}
catch (IOException e)
{
e.printStackTrace();
logger.error("Could not save audio file " + file, e);
}
// Compare JNI vs Java
try (AudioFile stream = getAudioStream(new File(args[0])))
@@ -177,7 +181,7 @@ public class Player
}
catch (IOException e)
{
e.printStackTrace();
logger.error("Could not save audio file " + file, e);
}
}
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
~ Copyright (c) 2021 William Hubbard. All Rights Reserved.
-->
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%c:%L %-5level - %msg%n"/>
</Console>
<File name="File" fileName="interface.log" append="false">
<PatternLayout>
<Pattern>%d{HH:mm:ss.SSS} - %45c:%-4L - %-5level - %msg%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="File"/>
</Root>
<Logger name="edu.regis.universeplayer.browser.Browser" level="info">
<AppenderRef ref="Console"/>
</Logger>
</Loggers>
</Configuration>