Move api to config file

This commit is contained in:
samczsun 2017-01-20 18:04:06 -05:00 committed by cnr
parent 3d4e64a208
commit 5b13a87500
4 changed files with 73 additions and 14 deletions

View File

@ -1,21 +1,80 @@
package mineplex.core.common.api;
/**
* TODO: Store this in a file instead of being hardcoded
*
* @author Shaun Bennett
*/
public enum ApiHost
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.configuration.file.YamlConfiguration;
public class ApiHost
{
AMPLIFIERS("10.33.53.12", 7979),
ANTISPAM("10.33.53.12", 8181),
ENDERCHEST("10.33.53.10", 8010)
;
private static final String API_HOST_FILE = "api-config.dat";
private static final Object LOCK = new Object();
private static volatile boolean LOADED = false;
private static final Map<String, ApiHost> API_HOST_MAP = new HashMap<>();
public static ApiHost getAPIHost(String identifier)
{
if (!LOADED)
{
synchronized (LOCK)
{
if (!LOADED)
{
try
{
File configFile = new File(API_HOST_FILE);
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(configFile);
for (String key : configuration.getKeys(false))
{
String ip = configuration.getConfigurationSection(key).getString("ip");
// Use parseInt to catch non-ints instead of a 0
int port = Integer.parseInt(configuration.getConfigurationSection(key).getString("port"));
if (ip == null)
{
throw new NullPointerException();
}
API_HOST_MAP.put(key, new ApiHost(ip, port));
}
}
catch (Throwable t)
{
t.printStackTrace();
}
finally
{
LOADED = true;
}
}
}
}
return API_HOST_MAP.get(identifier);
}
public static ApiHost getAmplifierService()
{
return getAPIHost("AMPLIFIERS");
}
public static ApiHost getAntispamService()
{
return getAPIHost("ANTISPAM");
}
public static ApiHost getEnderchestService()
{
return getAPIHost("ENDERCHEST");
}
private String _host;
private int _port;
ApiHost(String host, int port)
private ApiHost(String host, int port)
{
_host = host;
_port = port;

View File

@ -18,7 +18,7 @@ public class EnderchestWorldLoader
public EnderchestWorldLoader()
{
String url = "http://" + ApiHost.ENDERCHEST.getHost() + ":" + ApiHost.ENDERCHEST.getPort() + "/";
String url = "http://" + ApiHost.getEnderchestService().getHost() + ":" + ApiHost.getEnderchestService().getPort() + "/";
_webCall = new ApiWebCall(url);
}

View File

@ -17,7 +17,7 @@ public class AntiSpamRepository extends ApiEndpoint
{
public AntiSpamRepository()
{
super(ApiHost.ANTISPAM, "/chat");
super(ApiHost.getAntispamService(), "/chat");
}
public AntiSpamApiResponse sendMessage(String source, ChatPayload payload)

View File

@ -24,7 +24,7 @@ public class BoosterRepository extends ApiEndpoint
{
public BoosterRepository()
{
super(ApiHost.AMPLIFIERS, "/booster", new GsonBuilder().setFieldNamingStrategy(new ApiFieldNamingStrategy())
super(ApiHost.getAmplifierService(), "/booster", new GsonBuilder().setFieldNamingStrategy(new ApiFieldNamingStrategy())
// .registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer())
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX").create());
}