From 0fbf57cf1a5cfa07b945670792239020b002dde1 Mon Sep 17 00:00:00 2001 From: Xephi59 Date: Wed, 15 Jul 2015 18:05:17 +0200 Subject: [PATCH] Don't Wait ! --- .../xephi/authme/cache/limbo/LimboPlayer.java | 13 +- .../authme/datasource/CacheDataSource.java | 4 +- .../authme/datasource/DatabaseCalls.java | 414 ++++++++++++------ 3 files changed, 291 insertions(+), 140 deletions(-) diff --git a/src/main/java/fr/xephi/authme/cache/limbo/LimboPlayer.java b/src/main/java/fr/xephi/authme/cache/limbo/LimboPlayer.java index 6ce6669ef..702ddb38f 100644 --- a/src/main/java/fr/xephi/authme/cache/limbo/LimboPlayer.java +++ b/src/main/java/fr/xephi/authme/cache/limbo/LimboPlayer.java @@ -1,6 +1,5 @@ package fr.xephi.authme.cache.limbo; -import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.inventory.ItemStack; @@ -84,10 +83,8 @@ public class LimboPlayer { } public void setTimeoutTaskId(BukkitTask i) { - if (this.timeoutTaskId != null) { - if (Bukkit.getScheduler().isCurrentlyRunning(this.timeoutTaskId.getTaskId()) || Bukkit.getScheduler().isQueued(this.timeoutTaskId.getTaskId())) - Bukkit.getScheduler().cancelTask(this.timeoutTaskId.getTaskId()); - } + if (this.timeoutTaskId != null) + this.timeoutTaskId.cancel(); this.timeoutTaskId = i; } @@ -96,10 +93,8 @@ public class LimboPlayer { } public void setMessageTaskId(BukkitTask messageTaskId) { - if (this.messageTaskId != null) { - if (Bukkit.getScheduler().isCurrentlyRunning(this.messageTaskId.getTaskId()) || Bukkit.getScheduler().isQueued(this.messageTaskId.getTaskId())) - Bukkit.getScheduler().cancelTask(this.messageTaskId.getTaskId()); - } + if (this.messageTaskId != null) + this.messageTaskId.cancel(); this.messageTaskId = messageTaskId; } diff --git a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java index 88a39b139..585a60b39 100644 --- a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java @@ -29,9 +29,7 @@ public class CacheDataSource implements DataSource { @Override public synchronized boolean isAuthAvailable(String user) { - if (cache.containsKey(user.toLowerCase())) - return true; - return false; + return cache.containsKey(user.toLowerCase()); } @Override diff --git a/src/main/java/fr/xephi/authme/datasource/DatabaseCalls.java b/src/main/java/fr/xephi/authme/datasource/DatabaseCalls.java index ea82e2569..a783c1f6c 100644 --- a/src/main/java/fr/xephi/authme/datasource/DatabaseCalls.java +++ b/src/main/java/fr/xephi/authme/datasource/DatabaseCalls.java @@ -3,9 +3,9 @@ package fr.xephi.authme.datasource; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.Future; import fr.xephi.authme.cache.auth.PlayerAuth; @@ -20,14 +20,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized boolean isAuthAvailable(final String user) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public Boolean call() throws Exception { - return database.isAuthAvailable(user); - } - }); + Boolean result; try { - return result.get(); + result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.isAuthAvailable(user); + } + }).get(); + } catch (InterruptedException e1) { + return false; + } catch (ExecutionException e1) { + return false; + } finally { + executor.shutdown(); + } + try { + return result.booleanValue(); } catch (Exception e) { return (false); } @@ -36,14 +45,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized PlayerAuth getAuth(final String user) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public PlayerAuth call() throws Exception { - return database.getAuth(user); - } - }); + PlayerAuth result; try { - return result.get(); + result = executor.submit(new Callable() { + + public PlayerAuth call() throws Exception { + return database.getAuth(user); + } + }).get(); + } catch (InterruptedException e1) { + return null; + } catch (ExecutionException e1) { + return null; + } finally { + executor.shutdown(); + } + try { + return result; } catch (Exception e) { return null; } @@ -52,14 +70,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized boolean saveAuth(final PlayerAuth auth) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public Boolean call() throws Exception { - return database.saveAuth(auth); - } - }); + Boolean result; try { - return result.get(); + result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.saveAuth(auth); + } + }).get(); + } catch (InterruptedException e1) { + return false; + } catch (ExecutionException e1) { + return false; + } finally { + executor.shutdown(); + } + try { + return result.booleanValue(); } catch (Exception e) { return (false); } @@ -68,14 +95,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized boolean updateSession(final PlayerAuth auth) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public Boolean call() throws Exception { - return database.updateSession(auth); - } - }); + Boolean result; try { - return result.get(); + result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.updateSession(auth); + } + }).get(); + } catch (InterruptedException e1) { + return false; + } catch (ExecutionException e1) { + return false; + } finally { + executor.shutdown(); + } + try { + return result.booleanValue(); } catch (Exception e) { return (false); } @@ -84,14 +120,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized boolean updatePassword(final PlayerAuth auth) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public Boolean call() throws Exception { - return database.updatePassword(auth); - } - }); + Boolean result; try { - return result.get(); + result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.updatePassword(auth); + } + }).get(); + } catch (InterruptedException e1) { + return false; + } catch (ExecutionException e1) { + return false; + } finally { + executor.shutdown(); + } + try { + return result.booleanValue(); } catch (Exception e) { return (false); } @@ -100,14 +145,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized int purgeDatabase(final long until) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public Integer call() throws Exception { - return database.purgeDatabase(until); - } - }); + Integer result; try { - return result.get(); + result = executor.submit(new Callable() { + + public Integer call() throws Exception { + return database.purgeDatabase(until); + } + }).get(); + } catch (InterruptedException e1) { + return 0; + } catch (ExecutionException e1) { + return 0; + } finally { + executor.shutdown(); + } + try { + return result.intValue(); } catch (Exception e) { return (0); } @@ -116,14 +170,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized List autoPurgeDatabase(final long until) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future> result = executor.submit(new Callable>() { - - public List call() throws Exception { - return database.autoPurgeDatabase(until); - } - }); + List result; try { - return result.get(); + result = executor.submit(new Callable>() { + + public List call() throws Exception { + return database.autoPurgeDatabase(until); + } + }).get(); + } catch (InterruptedException e1) { + return new ArrayList(); + } catch (ExecutionException e1) { + return new ArrayList(); + } finally { + executor.shutdown(); + } + try { + return result; } catch (Exception e) { return (new ArrayList()); } @@ -132,14 +195,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized boolean removeAuth(final String user) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public Boolean call() throws Exception { - return database.removeAuth(user); - } - }); + Boolean result; try { - return result.get(); + result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.removeAuth(user); + } + }).get(); + } catch (InterruptedException e1) { + return false; + } catch (ExecutionException e1) { + return false; + } finally { + executor.shutdown(); + } + try { + return result.booleanValue(); } catch (Exception e) { return (false); } @@ -148,14 +220,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized boolean updateQuitLoc(final PlayerAuth auth) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public Boolean call() throws Exception { - return database.updateQuitLoc(auth); - } - }); + Boolean result; try { - return result.get(); + result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.updateQuitLoc(auth); + } + }).get(); + } catch (InterruptedException e1) { + return false; + } catch (ExecutionException e1) { + return false; + } finally { + executor.shutdown(); + } + try { + return result.booleanValue(); } catch (Exception e) { return (false); } @@ -164,14 +245,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized int getIps(final String ip) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public Integer call() throws Exception { - return database.getIps(ip); - } - }); + Integer result; try { - return result.get(); + result = executor.submit(new Callable() { + + public Integer call() throws Exception { + return database.getIps(ip); + } + }).get(); + } catch (InterruptedException e1) { + return 0; + } catch (ExecutionException e1) { + return 0; + } finally { + executor.shutdown(); + } + try { + return result.intValue(); } catch (Exception e) { return (0); } @@ -180,14 +270,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized List getAllAuthsByName(final PlayerAuth auth) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future> result = executor.submit(new Callable>() { - - public List call() throws Exception { - return database.getAllAuthsByName(auth); - } - }); + List result; try { - return result.get(); + result = executor.submit(new Callable>() { + + public List call() throws Exception { + return database.getAllAuthsByName(auth); + } + }).get(); + } catch (InterruptedException e1) { + return new ArrayList(); + } catch (ExecutionException e1) { + return new ArrayList(); + } finally { + executor.shutdown(); + } + try { + return result; } catch (Exception e) { return (new ArrayList()); } @@ -196,14 +295,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized List getAllAuthsByIp(final String ip) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future> result = executor.submit(new Callable>() { - - public List call() throws Exception { - return database.getAllAuthsByIp(ip); - } - }); + List result; try { - return result.get(); + result = executor.submit(new Callable>() { + + public List call() throws Exception { + return database.getAllAuthsByIp(ip); + } + }).get(); + } catch (InterruptedException e1) { + return new ArrayList(); + } catch (ExecutionException e1) { + return new ArrayList(); + } finally { + executor.shutdown(); + } + try { + return result; } catch (Exception e) { return (new ArrayList()); } @@ -212,14 +320,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized List getAllAuthsByEmail(final String email) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future> result = executor.submit(new Callable>() { - - public List call() throws Exception { - return database.getAllAuthsByEmail(email); - } - }); + List result; try { - return result.get(); + result = executor.submit(new Callable>() { + + public List call() throws Exception { + return database.getAllAuthsByEmail(email); + } + }).get(); + } catch (InterruptedException e1) { + return new ArrayList(); + } catch (ExecutionException e1) { + return new ArrayList(); + } finally { + executor.shutdown(); + } + try { + return result; } catch (Exception e) { return (new ArrayList()); } @@ -228,14 +345,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized boolean updateEmail(final PlayerAuth auth) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public Boolean call() throws Exception { - return database.updateEmail(auth); - } - }); + Boolean result; try { - return result.get(); + result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.updateEmail(auth); + } + }).get(); + } catch (InterruptedException e1) { + return false; + } catch (ExecutionException e1) { + return false; + } finally { + executor.shutdown(); + } + try { + return result.booleanValue(); } catch (Exception e) { return (false); } @@ -244,14 +370,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized boolean updateSalt(final PlayerAuth auth) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public Boolean call() throws Exception { - return database.updateSalt(auth); - } - }); + Boolean result; try { - return result.get(); + result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.updateSalt(auth); + } + }).get(); + } catch (InterruptedException e1) { + return false; + } catch (ExecutionException e1) { + return false; + } finally { + executor.shutdown(); + } + try { + return result.booleanValue(); } catch (Exception e) { return (false); } @@ -286,14 +421,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized boolean isLogged(final String user) { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public Boolean call() throws Exception { - return database.isLogged(user); - } - }); + Boolean result; try { - return result.get(); + result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.isLogged(user); + } + }).get(); + } catch (InterruptedException e1) { + return false; + } catch (ExecutionException e1) { + return false; + } finally { + executor.shutdown(); + } + try { + return result.booleanValue(); } catch (Exception e) { return (false); } @@ -335,14 +479,23 @@ public class DatabaseCalls implements DataSource { @Override public synchronized int getAccountsRegistered() { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future result = executor.submit(new Callable() { - - public Integer call() throws Exception { - return database.getAccountsRegistered(); - } - }); + Integer result; try { - return result.get(); + result = executor.submit(new Callable() { + + public Integer call() throws Exception { + return database.getAccountsRegistered(); + } + }).get(); + } catch (InterruptedException e1) { + return 0; + } catch (ExecutionException e1) { + return 0; + } finally { + executor.shutdown(); + } + try { + return result.intValue(); } catch (Exception e) { return (0); } @@ -363,17 +516,22 @@ public class DatabaseCalls implements DataSource { @Override public synchronized List getAllAuths() { ExecutorService executor = Executors.newSingleThreadExecutor(); - Future> result = executor.submit(new Callable>() { - - public List call() throws Exception { - return database.getAllAuths(); - } - }); + List result; try { - return result.get(); - } catch (Exception e) { + result = executor.submit(new Callable>() { + + public List call() throws Exception { + return database.getAllAuths(); + } + }).get(); + } catch (InterruptedException e1) { return (new ArrayList()); + } catch (ExecutionException e1) { + return (new ArrayList()); + } finally { + executor.shutdown(); } + return result; } }