Skip to content

Commit

Permalink
Feature path as parameter (#46)
Browse files Browse the repository at this point in the history
* make paths mutable using parameters

* fix hasArg value for Options with args

* Update README.md

* increase version

* add help
  • Loading branch information
ksarink authored Jul 18, 2022
1 parent 540d9d0 commit 2b8f992
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 60 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ mvn package (.jar lands in target/ directory)
## Setup
* create /opt/applications/SPMLauncher/ManagedSoftware/spm with spm installations
* create /opt/applications/SPMLauncher/ManagedSoftware/toolbox with toolboxes
* add tmp-mount to /usr/local/bin with 777
* add tmp-mount to /usr/local/bin with 755 and root as owner

~~~~
#!/bin/bash
# place this file in /usr/local/bin/tmp-mount
# to let anyone use this command use, add the following to the !!END!! of the sudoers file:
# ALL ALL=NOPASSWD: /usr/local/bin/tmp-mount
# and don't forget to chmod +x this file!
# and don't forget to execute `chown root` and `chmod +x` on this file!
MANAGED_SOFTWARE_DIR=/opt/applications/SPMLauncher/ManagedSoftware
MOUNT_DIR=/tmp/SPMLauncher
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>de.wwu.trap</groupId>
<artifactId>SPMLauncher</artifactId>
<version>2.0.1</version>
<version>2.1.0</version>
<packaging>jar</packaging>

<name>SPMLauncher</name>
Expand Down Expand Up @@ -82,6 +82,11 @@
</build>

<dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.jfxtras</groupId>
<artifactId>jmetro</artifactId>
Expand Down
122 changes: 97 additions & 25 deletions src/main/java/de/wwu/trap/SpmLauncher/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@

import javax.swing.UIManager;

import de.wwu.trap.SpmLauncher.Gui.GuiManager;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import de.wwu.trap.SpmLauncher.Gui.FxGuiController;
import de.wwu.trap.SpmLauncher.Gui.GuiManager;

/**
*
Expand All @@ -22,20 +30,44 @@ public class App {
* constant is changed, be aware that you also have to change the tmp-mount
* script!
*/
public static final String MANAGED_SOFTWARE_DIR = "/opt/applications/SPMLauncher/ManagedSoftware";
public static String getManagedSoftwareDir() {
if (managedSoftwareDir != null)
return App.managedSoftwareDir;
else
return App.managedSoftwareDirDefault;
}

private static String managedSoftwareDir = null;
private static final String managedSoftwareDirDefault = "/opt/applications/SPMLauncher/ManagedSoftware";

/**
* The directory in which the temporary mounts this Launcher creates will be
* placed. If this constant is changed, be aware that you also have to change
* the tmp-mount script!
*/
public static final String MOUNT_DIR = "/tmp/SPMLauncher";
public static String getMountDir() {
if (App.mountDir != null)
return App.mountDir;
else
return App.mountDirDefault;
}

private static String mountDir = null;
private static final String mountDirDefault = "/tmp/SPMLauncher";

/**
* The mount script which can be called with sudo without having to enter a
* password. SEE comment in tmp-mount script.
* password. See comment in tmp-mount script.
*/
public static final String MOUNT_SCRIPT = "/usr/local/bin/tmp-mount";
public static String getMountScript() {
if (App.mountScript != null)
return App.mountScript;
else
return App.mountScriptDefault;
}

private static String mountScript = null;
private static String mountScriptDefault = "/usr/local/bin/tmp-mount";

/**
* The suffix for the name of the file in which the pids of the SPMLauncher and
Expand All @@ -62,39 +94,79 @@ public class App {
* @param args the arguments from the commandline
*/
public static void main(String[] args) {
// Create MOUNT_DIR with chmod 777
File mountDir = new File(App.MOUNT_DIR);

/*
* CMD Argument Parsing
*/
final Options options = new Options();
Option optionHelp = new Option("h", "help", false, "Print help");
options.addOption(optionHelp);

// General options
Option optionNc = new Option("nc", "no-console", false, "no console");
options.addOption(optionNc);
Option optionNofx = new Option("nofx", "disable-fx", false, "Disable the fx GUI and use swing");
options.addOption(optionNofx);

// Path options
Option optionMansofdir = new Option("msd", "managed-software-dir", true,
"Path to the ManagedSoftware directory - place where spm and it's toolboxes are stored. "
+ "Be aware to adjust the tmp-mount script accordingly");
options.addOption(optionMansofdir);
Option optionMountdir = new Option("md", "mount-dir", true,
"Path to the directory where the temporary spm dirs are mounted");
options.addOption(optionMountdir);
Option optionMountscript = new Option("ms", "mount-script", true, "Path to the mount script tmp-mount");
options.addOption(optionMountscript);

// parsing
CommandLineParser parser = new DefaultParser();
CommandLine line = null;
try {
line = parser.parse(options, args);
} catch (ParseException e2) {
e2.printStackTrace();
}

if (line != null && line.hasOption(optionHelp)) {
HelpFormatter fmt = new HelpFormatter();
fmt.printHelp("java -jar <SPMLauncher jar path> [options]\nWhere options include: ", options);
System.exit(0);
}

/*
* Set Options
*/
App.managedSoftwareDir = line.getOptionValue(optionMansofdir);
App.mountDir = line.getOptionValue(optionMountdir);
App.mountScript = line.getOptionValue(optionMountscript);

/*
* Create mount dir with correct permissions for multiuser environments
*/
File mountDir = new File(App.getMountDir());
if (!mountDir.exists()) {
mountDir.mkdirs();
mountDir.setReadable(true, false);
mountDir.setWritable(true, false);
mountDir.setExecutable(true, false);
}

boolean enableFx = true;

for (String arg : args) {
if (arg.equalsIgnoreCase("--no-console") || arg.equalsIgnoreCase("-nc")) {
try {
// Write Sysout to logFile if it isn't started with argument
// --console or -c
File logFile = new File(MOUNT_DIR, LAUNCHER_UUID + ".log");
System.setOut(new PrintStream(logFile));
System.setErr(new PrintStream(logFile));
} catch (FileNotFoundException e1) {

}
}
if (line != null && line.hasOption(optionNc)) {
try {
// Write Sysout to logFile if it isn't started with argument
// --console or -c
File logFile = new File(App.getMountDir(), LAUNCHER_UUID + ".log");
System.setOut(new PrintStream(logFile));
System.setErr(new PrintStream(logFile));
} catch (FileNotFoundException e1) {

if (arg.equalsIgnoreCase("--disable-fx")) {
enableFx = false;
}

}

System.out.println("This PID: " + OSHandler.getPid());

if (!enableFx) {
if (line != null && line.hasOption(optionNofx)) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ protected void updateItem(File item, boolean empty) {
/*
* Changelog
*/
File changelogFile = new File(App.MANAGED_SOFTWARE_DIR, "changelog.md");
File changelogFile = new File(App.getManagedSoftwareDir(), "changelog.md");
if (changelogFile == null || !changelogFile.exists()) {
System.out.println("No or empty changelog! (" + changelogFile.getAbsolutePath() + ")");

Expand Down Expand Up @@ -300,7 +300,7 @@ public void chooseSpmVersion(File spmDir) {
comboxBoxList.clear();
toolboxCount = 0;

File spmToolboxDir = new File(App.MANAGED_SOFTWARE_DIR, "toolbox" + File.separatorChar + spmDir.getName());
File spmToolboxDir = new File(App.getManagedSoftwareDir(), "toolbox" + File.separatorChar + spmDir.getName());
File[] toolboxes = spmToolboxDir.listFiles((dir) -> dir.isDirectory());

if (toolboxes != null) {
Expand Down Expand Up @@ -455,7 +455,7 @@ public void run() {
Thread p1 = new Thread() {
@Override
public void run() {
File tmpSpmDir = new File(App.MOUNT_DIR + "/" + App.LAUNCHER_UUID.toString());
File tmpSpmDir = new File(App.getMountDir() + "/" + App.LAUNCHER_UUID.toString());
File matlabDir = matlabComboBox.getSelectionModel().getSelectedItem();
OSHandler.buildLaunchCmdStartAndWait(matlabDir, tmpSpmDir, activatedToolboxes,
devmodeCheckBox.isSelected());
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/de/wwu/trap/SpmLauncher/Gui/GuiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void getChangelog() {
@Override
public void run() {

File changelogFile = new File(App.MANAGED_SOFTWARE_DIR, "changelog.md");
File changelogFile = new File(App.getManagedSoftwareDir(), "changelog.md");
if (changelogFile == null || !changelogFile.exists()) {
System.out.println("No or empty changelog! (" + changelogFile.getAbsolutePath() + ")");
return;
Expand Down Expand Up @@ -158,7 +159,7 @@ public void run() {
Thread p1 = new Thread() {
@Override
public void run() {
File tmpSpmDir = new File(App.MOUNT_DIR + "/" + App.LAUNCHER_UUID.toString());
File tmpSpmDir = new File(App.getMountDir() + "/" + App.LAUNCHER_UUID.toString());
OSHandler.startSpmAndWait(tmpSpmDir, activatedToolboxes, false);
}
};
Expand All @@ -170,7 +171,7 @@ public void chooseSpmVersion(File spmDir) {
if (spmDir == null)
return;
gui.clearAndSetupToolboxes();
File spmToolboxDir = new File(App.MANAGED_SOFTWARE_DIR, "toolbox" + File.separatorChar + spmDir.getName());
File spmToolboxDir = new File(App.getManagedSoftwareDir(), "toolbox" + File.separatorChar + spmDir.getName());
File[] toolboxes = spmToolboxDir.listFiles((dir) -> dir.isDirectory());

if (toolboxes != null) {
Expand Down
Loading

0 comments on commit 2b8f992

Please sign in to comment.