PlotSquared/src/main/java/com/plotsquared/bukkit/uuid/SQLUUIDHandler.java

349 lines
14 KiB
Java
Raw Normal View History

2015-07-30 19:24:01 +02:00
package com.plotsquared.bukkit.uuid;
2015-07-24 16:06:58 +02:00
2015-07-30 16:25:16 +02:00
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
2015-07-27 15:10:14 +02:00
import com.google.common.collect.HashBiMap;
2015-07-27 09:26:50 +02:00
import com.intellectualcrafters.json.JSONObject;
2015-07-24 16:06:58 +02:00
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.database.DBFunc;
2015-07-27 09:26:50 +02:00
import com.intellectualcrafters.plot.database.SQLite;
import com.intellectualcrafters.plot.object.RunnableVal;
2015-07-24 16:06:58 +02:00
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.util.TaskManager;
2015-07-27 09:26:50 +02:00
import com.intellectualcrafters.plot.util.UUIDHandler;
import com.intellectualcrafters.plot.util.UUIDHandlerImplementation;
2015-07-24 16:06:58 +02:00
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
2015-09-11 12:09:22 +02:00
public class SQLUUIDHandler extends UUIDHandlerImplementation
{
public SQLUUIDHandler(final UUIDWrapper wrapper)
{
2015-07-27 09:26:50 +02:00
super(wrapper);
2015-07-24 16:06:58 +02:00
_sqLite = new SQLite("./plugins/PlotSquared/usercache.db");
2015-09-11 12:09:22 +02:00
try
{
2015-07-24 16:06:58 +02:00
_sqLite.openConnection();
2015-09-11 12:09:22 +02:00
}
catch (final Exception e)
{
2015-07-24 16:06:58 +02:00
e.printStackTrace();
}
2015-09-11 12:09:22 +02:00
try
{
final PreparedStatement stmt = getConnection()
.prepareStatement("CREATE TABLE IF NOT EXISTS `usercache` (uuid VARCHAR(32) NOT NULL, username VARCHAR(32) NOT NULL, PRIMARY KEY (uuid, username))");
2015-07-24 16:06:58 +02:00
stmt.execute();
stmt.close();
2015-09-11 12:09:22 +02:00
}
catch (final SQLException e)
{
2015-07-24 16:06:58 +02:00
e.printStackTrace();
}
2015-07-27 15:10:14 +02:00
startCaching(null);
2015-07-24 16:06:58 +02:00
}
2015-09-11 12:09:22 +02:00
private class SQLUUIDHandlerException extends RuntimeException
{
SQLUUIDHandlerException(final String s, final Throwable c)
{
2015-07-27 09:26:50 +02:00
super("SQLUUIDHandler caused an exception: " + s, c);
}
2015-07-24 16:06:58 +02:00
}
2015-09-11 12:09:22 +02:00
2015-07-27 09:26:50 +02:00
private final SQLite _sqLite;
2015-09-11 12:09:22 +02:00
private Connection getConnection()
{
synchronized (_sqLite)
{
2015-07-27 09:26:50 +02:00
return _sqLite.getConnection();
}
2015-07-24 16:06:58 +02:00
}
2015-09-11 12:09:22 +02:00
2015-07-24 16:06:58 +02:00
@Override
2015-09-11 12:09:22 +02:00
public boolean startCaching(final Runnable whenDone)
{
if (!super.startCaching(whenDone)) { return false; }
TaskManager.runTaskAsync(new Runnable()
{
2015-07-24 16:06:58 +02:00
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
try
{
2015-07-27 15:10:14 +02:00
final HashBiMap<StringWrapper, UUID> toAdd = HashBiMap.create(new HashMap<StringWrapper, UUID>());
2015-09-11 12:09:22 +02:00
final PreparedStatement statement = getConnection().prepareStatement("SELECT `uuid`, `username` FROM `usercache`");
final ResultSet resultSet = statement.executeQuery();
2015-07-24 16:06:58 +02:00
StringWrapper username;
UUID uuid;
2015-09-11 12:09:22 +02:00
while (resultSet.next())
{
2015-07-24 16:06:58 +02:00
username = new StringWrapper(resultSet.getString("username"));
uuid = UUID.fromString(resultSet.getString("uuid"));
2015-07-27 15:10:14 +02:00
toAdd.put(new StringWrapper(username.value), uuid);
2015-07-24 16:06:58 +02:00
}
statement.close();
2015-07-27 15:10:14 +02:00
add(toAdd);
add(new StringWrapper("*"), DBFunc.everyone);
2015-09-11 12:09:22 +02:00
2015-07-27 15:10:14 +02:00
// This should be called as long as there are some unknown plots
final List<UUID> toFetch = new ArrayList<>();
2015-09-11 12:09:22 +02:00
for (final UUID u : UUIDHandler.getAllUUIDS())
{
if (!uuidExists(u))
{
2015-07-27 15:10:14 +02:00
toFetch.add(u);
2015-07-24 16:06:58 +02:00
}
2015-07-27 15:10:14 +02:00
}
2015-09-11 12:09:22 +02:00
if (toFetch.isEmpty())
{
if (whenDone != null)
{
whenDone.run();
}
2015-07-27 15:10:14 +02:00
return;
}
2015-09-11 12:09:22 +02:00
final FileUUIDHandler fileHandler = new FileUUIDHandler(SQLUUIDHandler.this.uuidWrapper);
fileHandler.startCaching(new Runnable()
{
2015-07-27 15:10:14 +02:00
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
2015-07-27 15:10:14 +02:00
// If the file based UUID handler didn't cache it, then we can't cache offline mode
// Also, trying to cache based on files again, is useless as that's what the file based uuid cacher does
2015-09-11 12:09:22 +02:00
if (Settings.OFFLINE_MODE)
{
if (whenDone != null)
{
whenDone.run();
}
2015-07-24 16:06:58 +02:00
return;
}
2015-09-11 12:09:22 +02:00
if (!Settings.OFFLINE_MODE)
{
2015-07-30 16:25:16 +02:00
PS.debug(C.PREFIX.s() + "&cWill fetch &6" + toFetch.size() + "&c from mojang!");
2015-07-27 15:10:14 +02:00
int i = 0;
2015-09-11 12:09:22 +02:00
final Iterator<UUID> iterator = toFetch.iterator();
while (iterator.hasNext())
{
final StringBuilder url = new StringBuilder("http://api.intellectualsites.com/uuid/?user=");
final List<UUID> currentIteration = new ArrayList<>();
while ((i++ <= 15) && iterator.hasNext())
{
final UUID _uuid = iterator.next();
2015-07-27 15:10:14 +02:00
url.append(_uuid.toString());
2015-09-11 12:09:22 +02:00
if (iterator.hasNext())
{
2015-07-27 15:10:14 +02:00
url.append(",");
}
currentIteration.add(_uuid);
2015-07-24 16:06:58 +02:00
}
2015-07-30 16:25:16 +02:00
PS.debug(C.PREFIX.s() + "&cWill attempt to fetch &6" + currentIteration.size() + "&c uuids from: &6" + url.toString());
2015-09-11 12:09:22 +02:00
try
{
final HttpURLConnection connection = (HttpURLConnection) new URL(url.toString()).openConnection();
2015-07-27 15:10:14 +02:00
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
2015-09-11 12:09:22 +02:00
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
2015-07-27 15:10:14 +02:00
String line;
2015-09-11 12:09:22 +02:00
final StringBuilder rawJSON = new StringBuilder();
while ((line = reader.readLine()) != null)
{
2015-07-27 15:10:14 +02:00
rawJSON.append(line);
}
reader.close();
2015-09-11 12:09:22 +02:00
final JSONObject object = new JSONObject(rawJSON.toString());
for (final UUID _u : currentIteration)
{
final Object o = object.getJSONObject(_u.toString().replace("-", "")).get("username");
if ((o == null) || !(o instanceof String))
{
2015-07-27 15:10:14 +02:00
continue;
}
add(new StringWrapper(o.toString()), _u);
2015-07-24 16:06:58 +02:00
}
2015-09-11 12:09:22 +02:00
}
catch (final Exception e)
{
2015-07-27 15:10:14 +02:00
e.printStackTrace();
2015-07-24 16:06:58 +02:00
}
2015-07-27 15:10:14 +02:00
i = 0;
2015-07-24 16:06:58 +02:00
}
}
2015-09-11 12:09:22 +02:00
if (whenDone != null)
{
whenDone.run();
}
2015-07-24 16:06:58 +02:00
}
2015-07-27 15:10:14 +02:00
});
2015-09-11 12:09:22 +02:00
}
catch (final SQLException e)
{
2015-07-24 16:06:58 +02:00
throw new SQLUUIDHandlerException("Couldn't select :s", e);
}
}
});
2015-07-27 09:26:50 +02:00
return true;
2015-07-24 16:06:58 +02:00
}
2015-09-11 12:09:22 +02:00
2015-07-24 16:06:58 +02:00
@Override
2015-09-11 12:09:22 +02:00
public void fetchUUID(final String name, final RunnableVal<UUID> ifFetch)
{
2015-07-30 16:25:16 +02:00
PS.debug(C.PREFIX.s() + "UUID for '" + name + "' was null. We'll cache this from the mojang servers!");
2015-09-11 12:09:22 +02:00
TaskManager.runTaskAsync(new Runnable()
{
2015-07-24 16:06:58 +02:00
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
final String url = "http://api.intellectualsites.com/uuid/?user=" + name;
try
{
final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
2015-07-27 09:26:50 +02:00
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
2015-09-11 12:09:22 +02:00
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
2015-07-27 09:26:50 +02:00
String line;
2015-09-11 12:09:22 +02:00
final StringBuilder rawJSON = new StringBuilder();
while ((line = reader.readLine()) != null)
{
2015-07-27 09:26:50 +02:00
rawJSON.append(line);
}
reader.close();
2015-09-11 12:09:22 +02:00
final JSONObject object = new JSONObject(rawJSON.toString());
2015-07-27 09:26:50 +02:00
ifFetch.value = UUID.fromString(object.getJSONObject(name).getString("dashed"));
add(new StringWrapper(name), ifFetch.value);
2015-09-11 12:09:22 +02:00
}
catch (final IOException e)
{
2015-07-24 16:06:58 +02:00
e.printStackTrace();
}
2015-07-27 09:26:50 +02:00
TaskManager.runTask(ifFetch);
2015-07-24 16:06:58 +02:00
}
});
}
2015-09-11 12:09:22 +02:00
2015-07-24 16:06:58 +02:00
@Override
2015-09-11 12:09:22 +02:00
public void handleShutdown()
{
2015-07-27 09:26:50 +02:00
super.handleShutdown();
2015-09-11 12:09:22 +02:00
try
{
2015-07-27 09:26:50 +02:00
getConnection().close();
2015-09-11 12:09:22 +02:00
}
catch (final SQLException e)
{
2015-07-27 09:26:50 +02:00
throw new SQLUUIDHandlerException("Couldn't close database connection", e);
2015-07-24 16:06:58 +02:00
}
}
2015-09-11 12:09:22 +02:00
2015-07-24 16:06:58 +02:00
@Override
2015-09-11 12:09:22 +02:00
public boolean add(final StringWrapper name, final UUID uuid)
{
2015-07-27 09:26:50 +02:00
// Ignoring duplicates
2015-09-11 12:09:22 +02:00
if (super.add(name, uuid))
{
TaskManager.runTaskAsync(new Runnable()
{
2015-07-24 16:06:58 +02:00
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
try
{
final PreparedStatement statement = getConnection().prepareStatement("INSERT INTO usercache (`uuid`, `username`) VALUES(?, ?)");
2015-07-27 09:26:50 +02:00
statement.setString(1, uuid.toString());
statement.setString(2, name.toString());
statement.execute();
2015-07-30 16:25:16 +02:00
PS.debug(C.PREFIX.s() + "&cAdded '&6" + uuid + "&c' - '&6" + name + "&c'");
2015-09-11 12:09:22 +02:00
}
catch (final SQLException e)
{
2015-07-24 16:06:58 +02:00
e.printStackTrace();
}
}
});
2015-07-27 15:10:14 +02:00
return true;
2015-07-24 16:06:58 +02:00
}
2015-07-27 15:10:14 +02:00
return false;
2015-07-24 16:06:58 +02:00
}
2015-09-11 12:09:22 +02:00
2015-07-27 09:26:50 +02:00
/**
* This isn't used as any UUID that is unknown is bulk cached (in lots of 16)
* @param uuid
* @return
*/
@Deprecated
2015-09-11 12:09:22 +02:00
public String getName__unused__(final UUID uuid)
{
2015-07-30 16:25:16 +02:00
PS.debug(C.PREFIX.s() + "Name for '" + uuid + "' was null. We'll cache this from the mojang servers!");
2015-09-11 12:09:22 +02:00
TaskManager.runTaskAsync(new Runnable()
{
2015-07-24 16:06:58 +02:00
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
final String url = "http://api.intellectualsites.com/uuid/?user=" + uuid;
try
{
final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
2015-07-24 16:06:58 +02:00
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
2015-09-11 12:09:22 +02:00
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
2015-07-24 16:06:58 +02:00
String line;
2015-09-11 12:09:22 +02:00
final StringBuilder rawJSON = new StringBuilder();
while ((line = reader.readLine()) != null)
{
2015-07-24 16:06:58 +02:00
rawJSON.append(line);
}
reader.close();
2015-09-11 12:09:22 +02:00
final JSONObject object = new JSONObject(rawJSON.toString());
final String username = object.getJSONObject(uuid.toString().replace("-", "")).getString("username");
2015-07-27 09:26:50 +02:00
add(new StringWrapper(username), uuid);
2015-09-11 12:09:22 +02:00
}
catch (final IOException e)
{
2015-07-24 16:06:58 +02:00
e.printStackTrace();
}
}
});
return null;
}
2015-07-27 15:10:14 +02:00
@Override
2015-09-11 12:09:22 +02:00
public void rename(final UUID uuid, final StringWrapper name)
{
2015-07-27 15:10:14 +02:00
super.rename(uuid, name);
2015-09-11 12:09:22 +02:00
TaskManager.runTaskAsync(new Runnable()
{
2015-07-27 15:10:14 +02:00
@Override
2015-09-11 12:09:22 +02:00
public void run()
{
try
{
final PreparedStatement statement = getConnection().prepareStatement("UPDATE usercache SET `username`=? WHERE `uuid`=?");
2015-07-27 15:10:14 +02:00
statement.setString(1, name.value);
statement.setString(2, uuid.toString());
statement.execute();
2015-07-30 16:25:16 +02:00
PS.debug(C.PREFIX.s() + "Name change for '" + uuid + "' to '" + name.value + "'");
2015-09-11 12:09:22 +02:00
}
catch (final SQLException e)
{
2015-07-27 15:10:14 +02:00
e.printStackTrace();
}
}
});
}
2015-07-24 16:06:58 +02:00
}