summaryrefslogtreecommitdiffstats
path: root/src/main/java/de/spline/kvm/Launcher.java
blob: d27ed950c2aedd99f6ffb56bbb415e91a36eefb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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);
    }
}