diff options
Diffstat (limited to 'src/main/java/de/spline/kvm/Launcher.java')
-rw-r--r-- | src/main/java/de/spline/kvm/Launcher.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/main/java/de/spline/kvm/Launcher.java b/src/main/java/de/spline/kvm/Launcher.java new file mode 100644 index 0000000..d27ed95 --- /dev/null +++ b/src/main/java/de/spline/kvm/Launcher.java @@ -0,0 +1,88 @@ +package de.spline.kvm; + +import javax.net.ssl.HttpsURLConnection; +import java.io.Console; +import java.util.Map; + +import de.spline.kvm.events.LifetimeAdapter; +import de.spline.kvm.utils.Kvm; +import de.spline.kvm.utils.SSLUtils; + +public class Launcher +{ + private static final String DEFAULT_USERNAME = "super"; + + private String host; + + public Launcher(String host) + { + this.host = host; + } + + public void run() + { + HttpsURLConnection.setDefaultSSLSocketFactory(SSLUtils.getSocketFactory()); + Kvm kvm = new Kvm("https://" + host); + + Console console = System.console(); + if (console == null) { + System.err.println("Unable to access the console to query login data."); + System.exit(1); + } + + String username = readUsername(console); + String password = readPassword(console, username); + kvm.login(username, password); + + Map<String, String> params = kvm.getAppletParameters(); + StandaloneApplet applet = new StandaloneApplet(host, params); + + applet.addLifetimeListener(new LifetimeAdapter() + { + @Override + public void appletStopped() + { + kvm.logout(); + } + }); + + applet.init(); + applet.start(); + } + + public static void main(String[] args) + { + String host = "kvm.spline.inf.fu-berlin.de"; + if (args.length > 0) { + host = args[0]; + } + + Launcher launcher = new Launcher(host); + launcher.run(); + } + + private String readUsername(Console console) + { + String input = console.readLine("Username for %s [%s]: ", host, DEFAULT_USERNAME); + + if (input == null) { + return null; + } + + if (input.isEmpty()) { + return DEFAULT_USERNAME; + } + + return input; + } + + private String readPassword(Console console, String username) + { + char[] password = console.readPassword("Password for %s@%s: ", username, host); + if (password == null) { + return null; + } + + return String.valueOf(password); + } +}
\ No newline at end of file |