ShellProcessBuilder -> ShellExecutor. Static method to simplify shell

execution added. Added ability to pass in environment variables to new
process. Added better detection for current shell in linux/unix (could
be improved...). Added flag for executing process as a shell command.
Output now defaults to en-US.UTF_8
This commit is contained in:
nathan 2017-07-15 23:55:36 +02:00
parent 1554077d27
commit e588ed19c1
2 changed files with 18 additions and 43 deletions

View File

@ -16,15 +16,13 @@
package dorkbox.util; package dorkbox.util;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.PrintStream;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import dorkbox.util.jna.linux.Gtk; import dorkbox.util.jna.linux.Gtk;
import dorkbox.util.process.ShellProcessBuilder; import dorkbox.util.process.ShellExecutor;
/** /**
* Container for all OS specific tests and methods. These do not exist in OS.java, because of dependency issues (OS.java should not * Container for all OS specific tests and methods. These do not exist in OS.java, because of dependency issues (OS.java should not
@ -191,15 +189,12 @@ class OSUtil {
} }
try { try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8196);
PrintStream outputStream = new PrintStream(byteArrayOutputStream);
// uname // uname
final ShellProcessBuilder shell = new ShellProcessBuilder(outputStream); final ShellExecutor shell = new ShellExecutor();
shell.setExecutable("uname"); shell.setExecutable("uname");
shell.start(); shell.start();
String output = ShellProcessBuilder.getOutput(byteArrayOutputStream); String output = shell.getOutput();
return output.startsWith("FreeBSD"); return output.startsWith("FreeBSD");
} catch (Throwable ignored) { } catch (Throwable ignored) {
} }
@ -378,16 +373,13 @@ class OSUtil {
if (!isSudoOrRoot) { if (!isSudoOrRoot) {
// running as root (also can be "sudo" user). A lot slower that checking a sys env, but this is guaranteed to work // running as root (also can be "sudo" user). A lot slower that checking a sys env, but this is guaranteed to work
try { try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8196);
PrintStream outputStream = new PrintStream(byteArrayOutputStream);
// id -u // id -u
final ShellProcessBuilder shell = new ShellProcessBuilder(outputStream); final ShellExecutor shell = new ShellExecutor();
shell.setExecutable("id"); shell.setExecutable("id");
shell.addArgument("-u"); shell.addArgument("-u");
shell.start(); shell.start();
String output = ShellProcessBuilder.getOutput(byteArrayOutputStream); String output = shell.getOutput();
isSudoOrRoot = "0".equals(output); isSudoOrRoot = "0".equals(output);
} catch (Throwable ignored) { } catch (Throwable ignored) {
} }
@ -489,31 +481,28 @@ class OSUtil {
} }
try { try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8196);
PrintStream outputStream = new PrintStream(byteArrayOutputStream);
// note: some versions of linux can ONLY access "ps a"; FreeBSD and most linux is "ps x" // note: some versions of linux can ONLY access "ps a"; FreeBSD and most linux is "ps x"
// we try "x" first // we try "x" first
// ps x | grep gnome-shell // ps x | grep gnome-shell
ShellProcessBuilder shell = new ShellProcessBuilder(outputStream); ShellExecutor shell = new ShellExecutor();
shell.setExecutable("ps"); shell.setExecutable("ps");
shell.addArgument("x"); shell.addArgument("x");
shell.start(); shell.start();
String output = ShellProcessBuilder.getOutput(byteArrayOutputStream); String output = shell.getOutput();
boolean contains = output.contains("gnome-shell"); boolean contains = output.contains("gnome-shell");
if (!contains && OS.isLinux()) { if (!contains && OS.isLinux()) {
// only try again if we are linux // only try again if we are linux
// ps a | grep gnome-shell // ps a | grep gnome-shell
shell = new ShellProcessBuilder(outputStream); shell = new ShellExecutor();
shell.setExecutable("ps"); shell.setExecutable("ps");
shell.addArgument("a"); shell.addArgument("a");
shell.start(); shell.start();
output = ShellProcessBuilder.getOutput(byteArrayOutputStream); output = shell.getOutput();
contains = output.contains("gnome-shell"); contains = output.contains("gnome-shell");
} }
@ -533,16 +522,13 @@ class OSUtil {
} }
try { try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8196);
PrintStream outputStream = new PrintStream(byteArrayOutputStream);
// gnome-shell --version // gnome-shell --version
final ShellProcessBuilder shellVersion = new ShellProcessBuilder(outputStream); final ShellExecutor shellVersion = new ShellExecutor();
shellVersion.setExecutable("gnome-shell"); shellVersion.setExecutable("gnome-shell");
shellVersion.addArgument("--version"); shellVersion.addArgument("--version");
shellVersion.start(); shellVersion.start();
String versionString = ShellProcessBuilder.getOutput(byteArrayOutputStream); String versionString = shellVersion.getOutput();
if (!versionString.isEmpty()) { if (!versionString.isEmpty()) {
// GNOME Shell 3.14.1 // GNOME Shell 3.14.1
@ -605,17 +591,14 @@ class OSUtil {
} }
try { try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8196);
PrintStream outputStream = new PrintStream(byteArrayOutputStream);
// plasma-desktop -v // plasma-desktop -v
// plasmashell --version // plasmashell --version
final ShellProcessBuilder shellVersion = new ShellProcessBuilder(outputStream); final ShellExecutor shellVersion = new ShellExecutor();
shellVersion.setExecutable("plasmashell"); shellVersion.setExecutable("plasmashell");
shellVersion.addArgument("--version"); shellVersion.addArgument("--version");
shellVersion.start(); shellVersion.start();
String output = ShellProcessBuilder.getOutput(byteArrayOutputStream); String output = shellVersion.getOutput();
if (!output.isEmpty()) { if (!output.isEmpty()) {
// DEFAULT icon size is 16. KDE is bananas on what they did with tray icon scale // DEFAULT icon size is 16. KDE is bananas on what they did with tray icon scale
@ -650,11 +633,8 @@ class OSUtil {
} }
try { try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8196);
PrintStream outputStream = new PrintStream(byteArrayOutputStream);
// xfconf-query -c xfce4-panel -l // xfconf-query -c xfce4-panel -l
final ShellProcessBuilder xfconf_query = new ShellProcessBuilder(outputStream); final ShellExecutor xfconf_query = new ShellExecutor();
xfconf_query.setExecutable("xfconf-query"); xfconf_query.setExecutable("xfconf-query");
xfconf_query.addArgument("-c " + channel); xfconf_query.addArgument("-c " + channel);
if (property != null) { if (property != null) {
@ -666,7 +646,7 @@ class OSUtil {
} }
xfconf_query.start(); xfconf_query.start();
return ShellProcessBuilder.getOutput(byteArrayOutputStream); return xfconf_query.getOutput();
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -21,9 +21,7 @@ import static dorkbox.util.jna.linux.Gtk.Gtk3;
import java.awt.Color; import java.awt.Color;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.PrintStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -42,7 +40,7 @@ import dorkbox.util.Swt;
import dorkbox.util.jna.linux.structs.GtkRequisition; import dorkbox.util.jna.linux.structs.GtkRequisition;
import dorkbox.util.jna.linux.structs.GtkStyle; import dorkbox.util.jna.linux.structs.GtkStyle;
import dorkbox.util.jna.linux.structs.PangoRectangle; import dorkbox.util.jna.linux.structs.PangoRectangle;
import dorkbox.util.process.ShellProcessBuilder; import dorkbox.util.process.ShellExecutor;
/** /**
* Class to contain all of the various methods needed to get information set by a GTK theme. * Class to contain all of the various methods needed to get information set by a GTK theme.
@ -230,18 +228,15 @@ class GtkTheme {
OSUtil.DesktopEnv.Env env = OSUtil.DesktopEnv.get(); OSUtil.DesktopEnv.Env env = OSUtil.DesktopEnv.get();
// sometimes the scaling-factor is set. If we have gsettings, great! otherwise try KDE // sometimes the scaling-factor is set. If we have gsettings, great! otherwise try KDE
try { try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8196);
PrintStream outputStream = new PrintStream(byteArrayOutputStream);
// gsettings get org.gnome.desktop.interface scaling-factor // gsettings get org.gnome.desktop.interface scaling-factor
final ShellProcessBuilder shellVersion = new ShellProcessBuilder(outputStream); final ShellExecutor shellVersion = new ShellExecutor();
shellVersion.setExecutable("gsettings"); shellVersion.setExecutable("gsettings");
shellVersion.addArgument("get"); shellVersion.addArgument("get");
shellVersion.addArgument("org.gnome.desktop.interface"); shellVersion.addArgument("org.gnome.desktop.interface");
shellVersion.addArgument("scaling-factor"); shellVersion.addArgument("scaling-factor");
shellVersion.start(); shellVersion.start();
String output = ShellProcessBuilder.getOutput(byteArrayOutputStream); String output = shellVersion.getOutput();
if (!output.isEmpty()) { if (!output.isEmpty()) {
// DEFAULT icon size is 16. HiDpi changes this scale, so we should use it as well. // DEFAULT icon size is 16. HiDpi changes this scale, so we should use it as well.