Skip to content

Commit

Permalink
Merge pull request #18 from Ortus-Solutions/develop
Browse files Browse the repository at this point in the history
fixing lowercase issue for system property
  • Loading branch information
mgmathus authored May 22, 2020
2 parents 533bdba + c7e6aaf commit b82ae67
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 93 deletions.
Binary file modified .nb-gradle/private/cache/runwar-d41d8cd98f00b204e9800998ecf8427e
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.1.0-2faf885816f286b1d7feb581a90fd0ba38488f73-2faf885816f286b1d7feb581a90fd0ba38488f73
4.1.1-8dcf7c5db87f75c08bf077eb42488b559d3bd61f-533bdba0626cc3b904f82a06d72adea104f25d09
2 changes: 1 addition & 1 deletion src/main/java/runwar/LaunchUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class LaunchUtil {
public static final Set<String> replicateProps = new HashSet<String>(Arrays.asList(new String[] { "cfml.cli.home",
"cfml.server.config.dir", "cfml.web.config.dir", "cfml.server.trayicon", "cfml.server.dockicon" }));

private static final String OS_NAME = System.getProperty("os.name").toLowerCase();
private static final String OS_NAME = System.getProperty("os.name");
private static String uname;
private static String linuxRelease;
static {
Expand Down
153 changes: 65 additions & 88 deletions src/main/java/runwar/tray/Tray.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import javax.swing.JTextArea;
import dorkbox.notify.Notify;
import dorkbox.notify.Pos;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -54,7 +53,6 @@
import java.lang.reflect.Method;
import static runwar.util.Reflection.invoke;
import static runwar.util.Reflection.method;
import static runwar.util.Reflection.load;

public class Tray {

Expand Down Expand Up @@ -158,7 +156,6 @@ private void instantiateMenu(String trayConfigJSON, String statusText, String ic
}

public void addMenuItems(JSONArray items, Menu menu, Server server) {
System.setProperty("os.name", System.getProperty("os.name").toLowerCase());
for (Object ob : items) {
JSONObject itemInfo = (JSONObject) ob;
InputStream is = null;
Expand Down Expand Up @@ -193,14 +190,14 @@ public void addMenuItems(JSONArray items, Menu menu, Server server) {
}
}
MenuItem menuItem = null;
if (itemInfo.get("items") != null) {
if (Utils.getIgnoreCase(itemInfo, "items") != null) {
Menu submenu = new Menu(label, is);
submenu.setShortcut(label.charAt(0));
menu.add(submenu);
addMenuItems((JSONArray) itemInfo.get("items"), submenu, server);
} else if (itemInfo.get("separator") != null) {
addMenuItems((JSONArray) Utils.getIgnoreCase(itemInfo, "items"), submenu, server);
} else if (Utils.getIgnoreCase(itemInfo, "separator") != null) {
menu.add(new Separator());
} else if (itemInfo.get("checkbox") != null) {
} else if (Utils.getIgnoreCase(itemInfo, "checkbox") != null) {
Checkbox checkbox = new Checkbox(label, null);
checkbox.setShortcut(label.charAt(0));
checkbox.setEnabled(!isDisabled);
Expand All @@ -217,7 +214,7 @@ public void addMenuItems(JSONArray items, Menu menu, Server server) {
menuItem = new MenuItem("Version: " + Server.getVersion(), is, new GetVersionAction());
menuItem.setShortcut('v');
} else if (action.equalsIgnoreCase("openbrowser")) {
String url = itemInfo.get("url").toString();
String url = Utils.getIgnoreCase(itemInfo, "url").toString();
menuItem = new MenuItem(label, is, new OpenBrowserAction(url));
menuItem.setShortcut('o');
} else if (action.equalsIgnoreCase("openfilesystem")) {
Expand All @@ -240,26 +237,6 @@ public void addMenuItems(JSONArray items, Menu menu, Server server) {
String command = getString(itemInfo, "command", "");
String workingDirectory = getString(itemInfo, "workingDirectory", "");
String shell = getString(itemInfo, "shell", Utils.availableShellPick());
menuItem = new MenuItem(label, is, new RunShellCommandAction(command, workingDirectory, false, shell));
} else if (action.equalsIgnoreCase("runTerminal")) {
String command = getString(itemInfo, "command", "");
String workingDirectory = getString(itemInfo, "workingDirectory", "");
String shell = getString(itemInfo, "shell", Utils.availableShellPick());
String waitResponse = getString(itemInfo, "waitResponse", "true");
RunwarLogger.LOG.info("This action (runTerminal) cannot wait for response, ignoring -> waitResponse:" + waitResponse);

if (Utils.isMac()) {
RunwarLogger.LOG.info("Executing on Mac OS X");
command = "osascript -e 'tell app \"Terminal\" to do script \"" + command + "\"'";
} else if (Utils.isWindows()) {
RunwarLogger.LOG.info("Executing on Windows");
command = "start cmd.exe /k \"" + command + "\"";
} else if (Utils.isUnix()) {
RunwarLogger.LOG.info("Executing on *NIX");
} else {
RunwarLogger.LOG.error("Your OS is not currently supported to perform this action");
}

menuItem = new MenuItem(label, is, new RunShellCommandAction(command, workingDirectory, false, shell));
} else {
RunwarLogger.LOG.error("Unknown menu item action \"" + action + "\" for \"" + label + "\"");
Expand Down Expand Up @@ -299,7 +276,7 @@ public static JSONObject getTrayConfig(String jsonText, String defaultTitle, Has
loadItems = (JSONArray) menuObject;
} else {
config = (JSONObject) JSONValue.parse(jsonText);
loadItems = (JSONArray) config.get("items");
loadItems = (JSONArray) Utils.getIgnoreCase(config, "items");
}
String title = getString(config, "title", defaultTitle);
config.put("title", title);
Expand All @@ -316,20 +293,20 @@ public static JSONObject getTrayConfig(String jsonText, String defaultTitle, Has

for (Object ob : loadItems) {
JSONObject itemInfo = (JSONObject) ob;
if (itemInfo.get("label") == null) {
if (Utils.getIgnoreCase(itemInfo, "label") == null) {
RunwarLogger.LOG.error("No label for menu item: " + itemInfo.toJSONString());
continue;
}
String label = getString(itemInfo, "label", "");
itemInfo.put("label", label);
if (itemInfo.get("action") != null) {
String action = itemInfo.get("action").toString();
String action = Utils.getIgnoreCase(itemInfo, "action").toString();
if (action.toLowerCase().equals("stopserver") && action.toLowerCase().equals("openbrowser")) {
RunwarLogger.LOG.error("Unknown menu item action \"" + action + "\" for \"" + label + "\"");
itemInfo.put("action", null);
}
}
if (itemInfo.get("url") != null) {
if (Utils.getIgnoreCase(itemInfo, "url") != null) {
itemInfo.put("action", getString(itemInfo, "action", "openbrowser"));
itemInfo.put("url", getString(itemInfo, "url", ""));
}
Expand Down Expand Up @@ -530,24 +507,24 @@ private static class RunShellCommandAction implements ActionListener {
private String shell;
private String workingDirectory;
private boolean waitResponse;

String[] shells = new String[]{"/bin/bash", "/usr/bin/bash",
"/bin/pfbash", "/usr/bin/pfbash",
"/bin/csh", "/usr/bin/csh",
"/bin/pfcsh", "/usr/bin/pfcsh",
"/bin/jsh", "/usr/bin/jsh",
"/bin/ksh", "/usr/bin/ksh",
"/bin/pfksh", "/usr/bin/pfksh",
"/bin/ksh93", "/usr/bin/ksh93",
"/bin/pfksh93", "/usr/bin/pfksh93",
"/bin/pfsh", "/usr/bin/pfsh",
"/bin/tcsh", "/usr/bin/tcsh",
"/bin/pftcsh", "/usr/bin/pftcsh",
"/usr/xpg4/bin/sh", "/usr/xp4/bin/pfsh",
"/bin/zsh", "/usr/bin/zsh",
"/bin/pfzsh", "/usr/bin/pfzsh",
"/bin/sh", "/usr/bin/sh",};
"/bin/pfbash", "/usr/bin/pfbash",
"/bin/csh", "/usr/bin/csh",
"/bin/pfcsh", "/usr/bin/pfcsh",
"/bin/jsh", "/usr/bin/jsh",
"/bin/ksh", "/usr/bin/ksh",
"/bin/pfksh", "/usr/bin/pfksh",
"/bin/ksh93", "/usr/bin/ksh93",
"/bin/pfksh93", "/usr/bin/pfksh93",
"/bin/pfsh", "/usr/bin/pfsh",
"/bin/tcsh", "/usr/bin/tcsh",
"/bin/pftcsh", "/usr/bin/pftcsh",
"/usr/xpg4/bin/sh", "/usr/xp4/bin/pfsh",
"/bin/zsh", "/usr/bin/zsh",
"/bin/pfzsh", "/usr/bin/pfzsh",
"/bin/sh", "/usr/bin/sh",};

RunShellCommandAction(String command, String workingDirectory, Boolean waitResponse, String shell) {
this.command = command;
this.workingDirectory = workingDirectory;
Expand Down Expand Up @@ -596,24 +573,24 @@ public void actionPerformed(ActionEvent e) {
Process process = builder.start();

if (waitResponse) {
RunwarLogger.LOG.info("Tray menu sync execution of: " + builder.command() );
RunwarLogger.LOG.info("Tray menu sync execution of: " + builder.command());
Runnable r = new TrayActionSyncRunner(process, command);
new Thread(r).start();

} else {
RunwarLogger.LOG.info("Tray menu async execution of: " + builder.command() );
RunwarLogger.LOG.info("Tray menu async execution of: " + builder.command());
}

// Consistent with server popups
Pos position = OS.isMacOsX() ? Pos.TOP_RIGHT : Pos.BOTTOM_RIGHT;
Notify.create()
.title("Run Command")
.text("Executed " + command )
.text("Executed " + command)
.position(position)
.darkStyle()
.hideAfter(5000)
.showConfirm();

} catch (IOException ex) {
ex.printStackTrace();
}
Expand All @@ -633,31 +610,31 @@ public static class TrayActionSyncRunner implements Runnable {
public void run() {

try {
this.jta = new JTextArea("");
StreamGobbler streamGobbler
= new StreamGobbler(process.getInputStream(), this::printString);
this.service = Executors.newSingleThreadExecutor();
this.jsp = new JScrollPane(jta) {
@Override
public Dimension getPreferredSize() {
return new Dimension(680, 420);
}
};
this.jta = new JTextArea("");
StreamGobbler streamGobbler
= new StreamGobbler(process.getInputStream(), this::printString);
this.service = Executors.newSingleThreadExecutor();
this.jsp = new JScrollPane(jta) {
@Override
public Dimension getPreferredSize() {
return new Dimension(680, 420);
}
};

JFrame frame = new JFrame("Command Output");

long PID = 0;
// PID is only accessable on Java 9+
Method pidMethod = method(process.getClass(), "pid");
if( pidMethod != null ) {
pidMethod.setAccessible(true);
PID = (long)invoke(pidMethod,process);
if (pidMethod != null) {
pidMethod.setAccessible(true);
PID = (long) invoke(pidMethod, process);
}
if( PID > 0 ) {
frame.setTitle( title + " PID: " + PID );

if (PID > 0) {
frame.setTitle(title + " PID: " + PID);
} else {
frame.setTitle( title );
frame.setTitle(title);
}
frame.getContentPane().add(jsp, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
Expand All @@ -667,56 +644,56 @@ public Dimension getPreferredSize() {
stopButton.setLocation(500, 350);
stopButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
stopButton.setEnabled(false);
stopButton.setEnabled(false);
stopCommandAttempt(evt);
}
});
frame.getContentPane().add(stopButton, BorderLayout.SOUTH);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

if( PID > 0 ) {
this.jta.append("PID: " + PID + newline + newline );
if (PID > 0) {
this.jta.append("PID: " + PID + newline + newline);
}
this.jta.append("$> " + title );
this.tsk = service.submit(streamGobbler);
this.jta.append("$> " + title);

this.tsk = service.submit(streamGobbler);

int exitCode = process.waitFor();

// Give the stream gobbler time to finish
service.shutdown();
service.awaitTermination( 5, TimeUnit.SECONDS );
service.awaitTermination(5, TimeUnit.SECONDS);

this.jta.append(newline + newline + "Exit Code:" + exitCode);
stopButton.setEnabled(false);
} catch (InterruptedException ex) {
ex.printStackTrace();
ex.printStackTrace();
RunwarLogger.LOG.error("An Error Occurred:" + ex.getMessage());
}
}

private void printString(String text) {
jta.append(newline + text);
}

public TrayActionSyncRunner(Process process, String title) {
this.process = process;
this.title = title;
}

private void stopCommandAttempt(java.awt.event.ActionEvent evt) {
try {
RunwarLogger.LOG.info("Attempting to stop process: " + this.title );
RunwarLogger.LOG.info("Attempting to stop process: " + this.title);
process.destroy();
process.waitFor(5, TimeUnit.SECONDS);
if (process.isAlive()) {
RunwarLogger.LOG.warn("Attempting to destroy process forcibly: " + this.title );
RunwarLogger.LOG.warn("Attempting to destroy process forcibly: " + this.title);
process.destroyForcibly();
}

if (!process.isAlive()) {
RunwarLogger.LOG.info("Process Stopped: " + this.title );
RunwarLogger.LOG.info("Process Stopped: " + this.title);
} else {
RunwarLogger.LOG.warn("Process cannot be Stopped: " + this.title);
}
Expand All @@ -727,7 +704,7 @@ private void stopCommandAttempt(java.awt.event.ActionEvent evt) {
tsk.cancel(true);
};
} catch (InterruptedException ex) {
RunwarLogger.LOG.error( "An Error Occurred trying to stop command :" + this.title, ex );
RunwarLogger.LOG.error("An Error Occurred trying to stop command :" + this.title, ex);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/runwar/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
public class Utils {

private static String OS = System.getProperty("os.name").toLowerCase();
private static String OS = System.getProperty("os.name");

public static String replaceHost(String openbrowserURL, String oldHost, String newHost) {
String url = openbrowserURL;
Expand All @@ -37,13 +37,13 @@ public static String replaceHost(String openbrowserURL, String oldHost, String n

public static boolean isWindows() {

return (OS.indexOf("win") >= 0);
return (OS.toLowerCase().indexOf("win") >= 0);

}

public static boolean isMac() {

return (OS.indexOf("mac") >= 0);
return (OS.toLowerCase().indexOf("mac") >= 0);

}

Expand Down

0 comments on commit b82ae67

Please sign in to comment.