#1127 Create DataSource#getEmail

This commit is contained in:
ljacqu
2017-04-18 21:07:31 +02:00
parent cbec5427f2
commit b99cc3bada
13 changed files with 165 additions and 46 deletions
@@ -23,16 +23,20 @@ import java.util.concurrent.TimeUnit;
public class CacheDataSource implements DataSource {
private final DataSource source;
private final PlayerCache playerCache;
private final LoadingCache<String, Optional<PlayerAuth>> cachedAuths;
private final ListeningExecutorService executorService;
/**
* Constructor for CacheDataSource.
*
* @param src DataSource
* @param source the source
* @param playerCache the player cache
*/
public CacheDataSource(DataSource src) {
source = src;
public CacheDataSource(DataSource source, PlayerCache playerCache) {
this.source = source;
this.playerCache = playerCache;
executorService = MoreExecutors.listeningDecorator(
Executors.newCachedThreadPool(new ThreadFactoryBuilder()
.setDaemon(true)
@@ -168,17 +172,17 @@ public class CacheDataSource implements DataSource {
}
@Override
public List<String> getAllAuthsByIp(final String ip) {
public List<String> getAllAuthsByIp(String ip) {
return source.getAllAuthsByIp(ip);
}
@Override
public int countAuthsByEmail(final String email) {
public int countAuthsByEmail(String email) {
return source.countAuthsByEmail(email);
}
@Override
public void purgeRecords(final Collection<String> banned) {
public void purgeRecords(Collection<String> banned) {
source.purgeRecords(banned);
cachedAuths.invalidateAll(banned);
}
@@ -190,7 +194,7 @@ public class CacheDataSource implements DataSource {
@Override
public boolean isLogged(String user) {
return PlayerCache.getInstance().isAuthenticated(user);
return playerCache.isAuthenticated(user);
}
@Override
@@ -223,6 +227,13 @@ public class CacheDataSource implements DataSource {
return result;
}
@Override
public DataSourceResult<String> getEmail(String user) {
return cachedAuths.getUnchecked(user)
.map(auth -> DataSourceResult.of(auth.getEmail()))
.orElse(DataSourceResult.unknownPlayer());
}
@Override
public List<PlayerAuth> getAllAuths() {
return source.getAllAuths();
@@ -230,6 +241,6 @@ public class CacheDataSource implements DataSource {
@Override
public List<PlayerAuth> getLoggedPlayers() {
return new ArrayList<>(PlayerCache.getInstance().getCache().values());
return new ArrayList<>(playerCache.getCache().values());
}
}
@@ -189,6 +189,14 @@ public interface DataSource extends Reloadable {
*/
boolean updateRealName(String user, String realName);
/**
* Returns the email of the user.
*
* @param user the user to retrieve an email for
* @return the email saved for the user, or null if user or email is not present
*/
DataSourceResult<String> getEmail(String user);
/**
* Return all players of the database.
*
@@ -0,0 +1,53 @@
package fr.xephi.authme.datasource;
/**
* Wraps a value and allows to specify whether a value is missing or the player is not registered.
*/
public class DataSourceResult<T> {
/** Instance used when a player does not exist. */
private static final DataSourceResult UNKNOWN_PLAYER = new DataSourceResult<>(null);
private final T value;
private DataSourceResult(T value) {
this.value = value;
}
/**
* Returns a {@link DataSourceResult} for the given value.
*
* @param value the value to wrap
* @param <T> the value's type
* @return DataSourceResult object for the given value
*/
public static <T> DataSourceResult<T> of(T value) {
return new DataSourceResult<>(value);
}
/**
* Returns a {@link DataSourceResult} specifying that the player does not exist.
*
* @param <T> the value type
* @return data source result for unknown player
*/
public static <T> DataSourceResult<T> unknownPlayer() {
return UNKNOWN_PLAYER;
}
/**
* @return whether the player of the associated value exists
*/
public boolean playerExists() {
return this != UNKNOWN_PLAYER;
}
/**
* Returns the value. It is {@code null} if the player is unknown. It is also {@code null}
* if the player exists but does not have the value defined.
*
* @return the value, or null
*/
public T getValue() {
return value;
}
}
@@ -396,6 +396,11 @@ public class FlatFile implements DataSource {
throw new UnsupportedOperationException("Flat file no longer supported");
}
@Override
public DataSourceResult<String> getEmail(String user) {
throw new UnsupportedOperationException("Flat file no longer supported");
}
@Override
public List<PlayerAuth> getAllAuths() {
BufferedReader br = null;
@@ -879,6 +879,22 @@ public class MySQL implements DataSource {
return false;
}
@Override
public DataSourceResult<String> getEmail(String user) {
String sql = "SELECT " + col.EMAIL + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, user);
try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) {
return DataSourceResult.of(rs.getString(1));
}
}
} catch (SQLException ex) {
logSqlException(ex);
}
return DataSourceResult.unknownPlayer();
}
@Override
public List<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<>();
@@ -559,6 +559,22 @@ public class SQLite implements DataSource {
return false;
}
@Override
public DataSourceResult<String> getEmail(String user) {
String sql = "SELECT " + col.EMAIL + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, user);
try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) {
return DataSourceResult.of(rs.getString(1));
}
}
} catch (SQLException ex) {
logSqlException(ex);
}
return DataSourceResult.unknownPlayer();
}
@Override
public List<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<>();