mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
10.19.2018 1537fb95a9977a162dc29e2487c4020e96b43074
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package de.micromata.borgbutler.server;
 
import de.micromata.borgbutler.cache.ButlerCache;
import de.micromata.borgbutler.server.jetty.JettyServer;
import de.micromata.borgbutler.server.user.SingleUserManager;
import de.micromata.borgbutler.server.user.UserManager;
import org.apache.commons.cli.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class Main {
    private static Logger log = LoggerFactory.getLogger(Main.class);
 
    private static final Main main = new Main();
 
    private JettyServer server;
    private boolean shutdownInProgress;
 
    private Main() {
    }
 
    public static void main(String[] args) {
        main._start(args);
    }
 
    public static JettyServer startUp(String... restPackageNames) {
        return main._startUp(restPackageNames);
    }
 
    public static void shutdown() {
        main._shutdown();
    }
 
 
    private void _start(String[] args) {
        // create Options object
        Options options = new Options();
        options.addOption("p", "port", true, "The default port for the web server.");
        options.addOption("q", "quiet", false, "Don't open browser automatically.");
        options.addOption("h", "help", false, "Print this help screen.");
        CommandLineParser parser = new DefaultParser();
        try {
            // parse the command line arguments
            CommandLine line = parser.parse(options, args);
            if (line.hasOption('h')) {
                printHelp(options);
                return;
            }
            if (line.hasOption('p')) {
                // initialise the member variable
                String portString = line.getOptionValue("p");
                try {
                    int port = Integer.parseInt(portString);
                    if (port < 1 || port > 65535) {
                        System.err.println("Port outside range.");
                        return;
                    }
                    ServerConfigurationHandler.getDefaultConfiguration().setPort(port);
                } catch (NumberFormatException ex) {
                    printHelp(options);
                    return;
                }
            }
            RunningMode.setServerType(RunningMode.ServerType.SERVER);
            RunningMode.logMode();
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    main._shutdown();
                }
            });
 
            JettyServer server = startUp();
            if (!line.hasOption('q')) {
 
                try {
                    java.awt.Desktop.getDesktop().browse(java.net.URI.create(server.getUrl()));
                } catch (Exception ex) {
                    log.info("Can't open web browser: " + ex.getMessage());
                }
            }
        } catch (ParseException ex) {
            // oops, something went wrong
            System.err.println("Parsing failed.  Reason: " + ex.getMessage());
            printHelp(options);
        }
    }
 
    private JettyServer _startUp(String... restPackageNames) {
        server = new JettyServer();
        server.start(restPackageNames);
 
        UserManager.setUserManager(new SingleUserManager());
 
        return server;
    }
 
    private void _shutdown() {
        synchronized (this) {
            if (shutdownInProgress == true) {
                // Another thread already called this method. There is nothing further to do.
                return;
            }
            shutdownInProgress = true;
        }
        log.info("Shutting down BorgButler web server...");
        server.stop();
        ButlerCache.getInstance().save();
    }
 
    private static void printHelp(Options options) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("borgbutler-server", options);
    }
}