This commit is contained in:
Xephi
2015-11-03 15:23:14 +01:00
15 changed files with 292 additions and 368 deletions
@@ -1,18 +1,15 @@
package fr.xephi.authme.datasource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.bukkit.entity.Player;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
public class CacheDataSource implements DataSource {
@@ -229,23 +226,15 @@ public class CacheDataSource implements DataSource {
@Override
public synchronized boolean updateEmail(final PlayerAuth auth) {
if (!cache.containsKey(auth.getNickname())) {
try {
return exec.submit(new Callable<Boolean>() {
public Boolean call() {
return source.updateEmail(auth);
}
}).get();
} catch (Exception e) {
return false;
}
PlayerAuth cachedAuth = cache.get(auth.getNickname());
final String oldEmail = cachedAuth.getEmail();
cachedAuth.setEmail(auth.getEmail());
exec.execute(new Runnable() {
@Override
public void run() {
if (!source.updateEmail(auth)) {
if (cache.containsKey(auth.getNickname())) {
cache.get(auth.getNickname()).setEmail(oldEmail);
}
}
}
});
return true;
}
@Override
@@ -281,25 +270,21 @@ public class CacheDataSource implements DataSource {
}
@Override
public synchronized List<String> getAllAuthsByIp(String ip) {
List<String> result = new ArrayList<>();
for (Map.Entry<String, PlayerAuth> stringPlayerAuthEntry : cache.entrySet()) {
PlayerAuth p = stringPlayerAuthEntry.getValue();
if (p.getIp().equals(ip))
result.add(p.getNickname());
}
return result;
public synchronized List<String> getAllAuthsByIp(final String ip) throws Exception {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return source.getAllAuthsByIp(ip);
}
}).get();
}
@Override
public synchronized List<String> getAllAuthsByEmail(String email) {
List<String> result = new ArrayList<>();
for (Map.Entry<String, PlayerAuth> stringPlayerAuthEntry : cache.entrySet()) {
PlayerAuth p = stringPlayerAuthEntry.getValue();
if (p.getEmail().equals(email))
result.add(p.getNickname());
}
return result;
public synchronized List<String> getAllAuthsByEmail(final String email) throws Exception {
return exec.submit(new Callable<List<String>>() {
public List<String> call() throws Exception {
return source.getAllAuthsByEmail(email);
}
}).get();
}
@Override
@@ -1,5 +1,6 @@
package fr.xephi.authme.datasource;
import java.sql.SQLException;
import java.util.List;
import fr.xephi.authme.cache.auth.PlayerAuth;
@@ -35,9 +36,9 @@ public interface DataSource {
List<String> getAllAuthsByName(PlayerAuth auth);
List<String> getAllAuthsByIp(String ip);
List<String> getAllAuthsByIp(String ip) throws Exception;
List<String> getAllAuthsByEmail(String email);
List<String> getAllAuthsByEmail(String email) throws Exception;
boolean updateEmail(PlayerAuth auth);
@@ -686,6 +686,7 @@ public class MySQL implements DataSource {
pst.executeUpdate();
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
return false;
} finally {
close(pst);
@@ -745,6 +746,7 @@ public class MySQL implements DataSource {
o.close();
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
}
}
}
@@ -802,14 +804,13 @@ public class MySQL implements DataSource {
}
@Override
public synchronized List<String> getAllAuthsByEmail(String email) {
Connection con = null;
public synchronized List<String> getAllAuthsByEmail(String email) throws SQLException {
final Connection con = getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
List<String> countEmail = new ArrayList<>();
try {
if ((con = getConnection()) == null)
return countEmail;
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;");
pst.setString(1, email);
rs = pst.executeQuery();
@@ -817,9 +818,6 @@ public class MySQL implements DataSource {
countEmail.add(rs.getString(columnName));
}
return countEmail;
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
return new ArrayList<>();
} finally {
close(rs);
close(pst);
@@ -472,13 +472,12 @@ public class SQLite_HIKARI implements DataSource {
}
@Override
public List<String> getAllAuthsByIp(String ip) {
Connection con = null;
public List<String> getAllAuthsByIp(String ip) throws SQLException {
final Connection con = getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
List<String> countIp = new ArrayList<>();
try {
con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
pst.setString(1, ip);
rs = pst.executeQuery();
@@ -486,11 +485,6 @@ public class SQLite_HIKARI implements DataSource {
countIp.add(rs.getString(columnName));
}
return countIp;
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
return new ArrayList<>();
} catch (NullPointerException npe) {
return new ArrayList<>();
} finally {
close(rs);
close(pst);
@@ -499,13 +493,12 @@ public class SQLite_HIKARI implements DataSource {
}
@Override
public List<String> getAllAuthsByEmail(String email) {
Connection con = null;
public List<String> getAllAuthsByEmail(String email) throws SQLException {
final Connection con = getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
List<String> countEmail = new ArrayList<>();
try {
con = getConnection();
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;");
pst.setString(1, email);
rs = pst.executeQuery();
@@ -513,11 +506,6 @@ public class SQLite_HIKARI implements DataSource {
countEmail.add(rs.getString(columnName));
}
return countEmail;
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
return new ArrayList<>();
} catch (NullPointerException npe) {
return new ArrayList<>();
} finally {
close(rs);
close(pst);