aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/ymc/acron/config/json/Config.java
blob: e8c5a832a973ef2868b309e7947ccb24bc893609 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package moe.ymc.acron.config.json;

import com.google.gson.annotations.SerializedName;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

class Config implements ConfigJsonObject<moe.ymc.acron.config.Config> {
    private static final Logger LOGGER = LogManager.getLogger();

    @SerializedName("listen")
    private final String listen;

    @SerializedName("port")
    private final Integer port;

    @SerializedName("native_transport")
    private final boolean nativeTransport;

    @SerializedName("clients")
    private final List<Client> clients;

    private Config(String listen,
                   Integer port,
                   boolean nativeTransport,
                   List<Client> clients) {
        this.listen = listen;
        this.port = port;
        this.nativeTransport = nativeTransport;
        this.clients = clients;
    }

    @Override
    public @NotNull moe.ymc.acron.config.Config create(boolean startup) throws ConfigDeserializationException {
        final InetAddress address;
        final int p;
        final boolean nt;
        if (!startup) {
            address = moe.ymc.acron.config.Config.getGlobalConfig().address();
            p = moe.ymc.acron.config.Config.getGlobalConfig().port();
            nt = moe.ymc.acron.config.Config.getGlobalConfig().useNativeTransport();
        } else {
            if (listen == null || listen.trim().equals("")) {
                address = InetAddress.getLoopbackAddress();
            } else {
                try {
                    address = InetAddress.getByName(listen);
                } catch (UnknownHostException e) {
                    throw new ConfigDeserializationException("Cannot parse address: " + e.getMessage(),
                            true);
                }
            }
            if (port == null) {
                p = 25575;
            } else {
                if (port < 0 || port > 65535) {
                    throw new ConfigDeserializationException("The port is out of range.", true);
                }
                p = port;
            }
            nt = nativeTransport;
        }


        Map<String, moe.ymc.acron.auth.Client> map;
        try {
            if (clients != null) {
                map = clients.stream()
                        .collect(Collectors.<Client, String, moe.ymc.acron.auth.Client>
                                toMap(client -> client.create(startup).id(),
                                client -> client.create(startup)));
            } else {
                map = new HashMap<>(0);
            }
        } catch (IllegalStateException e) {
            // Collision.
            LOGGER.error("Duplicate clients with the same ID in the Acron configuration. All clients are ignored. " +
                    "Fix the configuration and reload.", e);
            if (!startup) {
                throw new ConfigDeserializationException("Duplicate clients with the same ID: " + e.getMessage());
            }
            map = new HashMap<>(0);
        } catch (ConfigDeserializationException e) {
            LOGGER.error("Cannot parse the Acron configuration. All clients are ignored. " +
                    "Fix the configuration and reload.", e);
            if (!startup) {
                throw e;
            }
            map = new HashMap<>(0);
        }
        return new moe.ymc.acron.config.Config(address,
                p,
                nt,
                map);
    }
}