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 { 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 clients; private Config(String listen, Integer port, boolean nativeTransport, List 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 map; try { if (clients != null) { map = clients.stream() .collect(Collectors. 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); } }