2014-08-01 23:56:29 +02:00
|
|
|
package mineplex.serverdata;
|
|
|
|
|
2015-03-14 06:03:22 +01:00
|
|
|
import mineplex.serverdata.servers.ConnectionData;
|
|
|
|
import mineplex.serverdata.servers.ServerManager;
|
2014-09-22 02:42:47 +02:00
|
|
|
import redis.clients.jedis.Jedis;
|
|
|
|
import redis.clients.jedis.JedisPool;
|
|
|
|
import redis.clients.jedis.JedisPoolConfig;
|
|
|
|
|
2014-08-01 23:56:29 +02:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility offers various necessary utility-based methods for use in Mineplex.ServerData.
|
|
|
|
* @author Ty
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class Utility
|
|
|
|
{
|
|
|
|
|
|
|
|
// The Gson instance used to serialize/deserialize objects in JSON form.
|
|
|
|
private static Gson _gson = new GsonBuilder().create();
|
|
|
|
public static Gson getGson() { return _gson; }
|
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
// Public static jedis pool for interacting with central default jedis repo.
|
2015-03-08 05:19:35 +01:00
|
|
|
private static JedisPool _masterPool;
|
|
|
|
private static JedisPool _slavePool;
|
2014-09-22 02:42:47 +02:00
|
|
|
|
2014-08-01 23:56:29 +02:00
|
|
|
/**
|
|
|
|
* @param object - the (non-null) object to serialize
|
|
|
|
* @return the serialized form of {@code object}.
|
|
|
|
*/
|
|
|
|
public static String serialize(Object object)
|
|
|
|
{
|
|
|
|
return _gson.toJson(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param serializedData - the serialized data to be deserialized
|
|
|
|
* @param type - the resulting class type of the object to be deserialized
|
|
|
|
* @return the deserialized form of {@code serializedData} for class {@code type}.
|
|
|
|
*/
|
|
|
|
public static <T> T deserialize(String serializedData, Class<T> type)
|
|
|
|
{
|
|
|
|
return _gson.fromJson(serializedData, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param delimiter - the delimiter character used to separate the concatenated elements
|
|
|
|
* @param elements - the set of string elements to be concatenated and returned.
|
|
|
|
* @return the concatenated string of all {@code elements} separated by the {@code delimiter}.
|
|
|
|
*/
|
|
|
|
public static String concatenate(char delimiter, String... elements)
|
|
|
|
{
|
|
|
|
int length = elements.length;
|
|
|
|
String result = length > 0 ? elements[0] : new String();
|
|
|
|
|
|
|
|
for (int i = 1; i < length; i++)
|
|
|
|
{
|
|
|
|
result += delimiter + elements[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2014-09-22 02:42:47 +02:00
|
|
|
|
2014-10-11 13:40:53 +02:00
|
|
|
/**
|
|
|
|
* @return the current timestamp (in seconds) fetched from the central jedis repository
|
|
|
|
* for synced timestamps.
|
|
|
|
*/
|
|
|
|
public static long currentTimeSeconds()
|
|
|
|
{
|
|
|
|
long currentTime = 0;
|
2015-03-08 05:19:35 +01:00
|
|
|
JedisPool pool = getPool(false);
|
|
|
|
Jedis jedis = pool.getResource();
|
2014-10-11 13:40:53 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
currentTime = Long.parseLong(jedis.time().get(0));
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2015-03-08 05:19:35 +01:00
|
|
|
pool.returnResource(jedis);
|
2014-10-11 13:40:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return currentTime;
|
|
|
|
}
|
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
/**
|
|
|
|
* @return the current timestamp (in milliseconds) fetched from the central jedis repository
|
|
|
|
* for synced timestamps.
|
|
|
|
*/
|
|
|
|
public static long currentTimeMillis()
|
|
|
|
{
|
|
|
|
long currentTime = 0;
|
2015-03-08 05:19:35 +01:00
|
|
|
JedisPool pool = getPool(false);
|
|
|
|
Jedis jedis = pool.getResource();
|
2014-10-11 13:40:53 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
currentTime = Long.parseLong(jedis.time().get(0));
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2015-03-08 05:19:35 +01:00
|
|
|
pool.returnResource(jedis);
|
2014-09-22 02:42:47 +02:00
|
|
|
}
|
|
|
|
|
2014-10-11 11:53:39 +02:00
|
|
|
return currentTime * 1000;
|
2014-09-22 02:42:47 +02:00
|
|
|
}
|
|
|
|
|
2015-03-08 05:19:35 +01:00
|
|
|
/**
|
|
|
|
* @param connData - the connection data specifying the database to be connected to.
|
|
|
|
* @return a newly instantiated {@link JedisPool} connected to the provided {@link ConnectionData} repository.
|
|
|
|
*/
|
|
|
|
public static JedisPool generatePool(ConnectionData connData)
|
|
|
|
{
|
|
|
|
return new JedisPool(new JedisPoolConfig(), connData.getHost(), connData.getPort());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param writeable - whether or not the Jedis connections returned should be writeable to.
|
|
|
|
* @return a globally available {@link JedisPool}
|
|
|
|
*/
|
|
|
|
public static JedisPool getPool(boolean writeable)
|
2014-09-22 02:42:47 +02:00
|
|
|
{
|
2015-03-08 05:19:35 +01:00
|
|
|
if (writeable)
|
2014-09-22 02:42:47 +02:00
|
|
|
{
|
2015-03-08 05:19:35 +01:00
|
|
|
if (_masterPool == null)
|
|
|
|
{
|
2015-03-09 00:17:28 +01:00
|
|
|
_masterPool = generatePool(ServerManager.getMasterConnection());
|
2015-03-08 05:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return _masterPool;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_slavePool == null)
|
|
|
|
{
|
2015-03-09 00:17:28 +01:00
|
|
|
_slavePool = generatePool(ServerManager.getSlaveConnection());
|
2015-03-08 05:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return _slavePool;
|
2014-09-22 02:42:47 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-01 23:56:29 +02:00
|
|
|
}
|