#762 - Move all sessions stuff to new SessionManager class
This commit is contained in:
parent
0c96a3113b
commit
1326606f37
@ -78,7 +78,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import static fr.xephi.authme.settings.properties.EmailSettings.MAIL_ACCOUNT;
|
import static fr.xephi.authme.settings.properties.EmailSettings.MAIL_ACCOUNT;
|
||||||
@ -118,9 +117,8 @@ public class AuthMe extends JavaPlugin {
|
|||||||
private AuthMeServiceInitializer initializer;
|
private AuthMeServiceInitializer initializer;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Private instances (sessions, mail, and ProtocolLib)
|
* Private instances (mail and ProtocolLib)
|
||||||
*/
|
*/
|
||||||
private final ConcurrentHashMap<String, BukkitTask> sessions = new ConcurrentHashMap<>();
|
|
||||||
private SendMailSSL mail;
|
private SendMailSSL mail;
|
||||||
private AuthMeInventoryPacketAdapter inventoryProtector;
|
private AuthMeInventoryPacketAdapter inventoryProtector;
|
||||||
private AuthMeTabCompletePacketAdapter tabComplete;
|
private AuthMeTabCompletePacketAdapter tabComplete;
|
||||||
@ -708,15 +706,6 @@ public class AuthMe extends JavaPlugin {
|
|||||||
return commandHandler.processCommand(sender, commandLabel, args);
|
return commandHandler.processCommand(sender, commandLabel, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get all current player sessions.
|
|
||||||
*
|
|
||||||
* @return A concurrent hashmap containing the sessions.
|
|
||||||
*/
|
|
||||||
public ConcurrentHashMap<String, BukkitTask> getSessions() {
|
|
||||||
return this.sessions;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the mailing instance.
|
* Get the mailing instance.
|
||||||
*
|
*
|
||||||
|
|||||||
75
src/main/java/fr/xephi/authme/cache/SessionManager.java
vendored
Normal file
75
src/main/java/fr/xephi/authme/cache/SessionManager.java
vendored
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package fr.xephi.authme.cache;
|
||||||
|
|
||||||
|
import fr.xephi.authme.initialization.SettingsDependent;
|
||||||
|
import fr.xephi.authme.settings.NewSetting;
|
||||||
|
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||||
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public class SessionManager implements SettingsDependent {
|
||||||
|
|
||||||
|
private final ConcurrentHashMap<String, BukkitTask> sessions = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private boolean enabled;
|
||||||
|
private int sessionTimeout;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
SessionManager(NewSetting settings) {
|
||||||
|
loadSettings(settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a session for a player is currently being cached.
|
||||||
|
*
|
||||||
|
* @param name The name to check.
|
||||||
|
* @return True if a session is found.
|
||||||
|
*/
|
||||||
|
public boolean hasSession(String name) {
|
||||||
|
return sessions.containsKey(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a player session to the cache.
|
||||||
|
*
|
||||||
|
* @param name The name of the player.
|
||||||
|
* @param task The task to run.
|
||||||
|
*/
|
||||||
|
public void addSession(String name, BukkitTask task) {
|
||||||
|
if (!enabled || sessionTimeout == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.sessions.put(name, task);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancels a player's session. After the task is cancelled, it will be removed from
|
||||||
|
* the cache.
|
||||||
|
*
|
||||||
|
* @param name The name of the player who's session to cancel.
|
||||||
|
*/
|
||||||
|
public void cancelSession(String name) {
|
||||||
|
BukkitTask task = sessions.get(name);
|
||||||
|
if (task != null) {
|
||||||
|
task.cancel();
|
||||||
|
removeSession(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a player's session from the cache.
|
||||||
|
*
|
||||||
|
* @param name The name of the player.
|
||||||
|
*/
|
||||||
|
public void removeSession(String name) {
|
||||||
|
this.sessions.remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void loadSettings(NewSetting settings) {
|
||||||
|
this.enabled = settings.getProperty(PluginSettings.SESSIONS_ENABLED);
|
||||||
|
this.sessionTimeout = settings.getProperty(PluginSettings.SESSIONS_TIMEOUT);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,6 +5,7 @@ import com.google.common.io.ByteStreams;
|
|||||||
|
|
||||||
import fr.xephi.authme.AuthMe;
|
import fr.xephi.authme.AuthMe;
|
||||||
import fr.xephi.authme.ConsoleLogger;
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
|
import fr.xephi.authme.cache.SessionManager;
|
||||||
import fr.xephi.authme.cache.auth.PlayerAuth;
|
import fr.xephi.authme.cache.auth.PlayerAuth;
|
||||||
import fr.xephi.authme.cache.auth.PlayerCache;
|
import fr.xephi.authme.cache.auth.PlayerCache;
|
||||||
import fr.xephi.authme.datasource.DataSource;
|
import fr.xephi.authme.datasource.DataSource;
|
||||||
@ -29,6 +30,9 @@ public class BungeeCordMessage implements PluginMessageListener {
|
|||||||
@Inject
|
@Inject
|
||||||
private PlayerCache playerCache;
|
private PlayerCache playerCache;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private SessionManager sessionManager;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private AuthMe plugin;
|
private AuthMe plugin;
|
||||||
|
|
||||||
@ -61,9 +65,8 @@ public class BungeeCordMessage implements PluginMessageListener {
|
|||||||
playerCache.updatePlayer(auth);
|
playerCache.updatePlayer(auth);
|
||||||
dataSource.setLogged(name);
|
dataSource.setLogged(name);
|
||||||
//START 03062016 sgdc3: should fix #731 but we need to recode this mess
|
//START 03062016 sgdc3: should fix #731 but we need to recode this mess
|
||||||
if (plugin.getSessions().containsKey(name)) {
|
if (sessionManager.hasSession(name)) {
|
||||||
plugin.getSessions().get(name).cancel();
|
sessionManager.cancelSession(name);
|
||||||
plugin.getSessions().remove(name);
|
|
||||||
}
|
}
|
||||||
//END
|
//END
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package fr.xephi.authme.process.join;
|
|||||||
|
|
||||||
import fr.xephi.authme.AuthMe;
|
import fr.xephi.authme.AuthMe;
|
||||||
import fr.xephi.authme.ConsoleLogger;
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
|
import fr.xephi.authme.cache.SessionManager;
|
||||||
import fr.xephi.authme.cache.auth.PlayerAuth;
|
import fr.xephi.authme.cache.auth.PlayerAuth;
|
||||||
import fr.xephi.authme.cache.auth.PlayerCache;
|
import fr.xephi.authme.cache.auth.PlayerCache;
|
||||||
import fr.xephi.authme.cache.limbo.LimboCache;
|
import fr.xephi.authme.cache.limbo.LimboCache;
|
||||||
@ -55,6 +56,9 @@ public class AsynchronousJoin implements AsynchronousProcess {
|
|||||||
@Inject
|
@Inject
|
||||||
private LimboCache limboCache;
|
private LimboCache limboCache;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private SessionManager sessionManager;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private PluginHooks pluginHooks;
|
private PluginHooks pluginHooks;
|
||||||
|
|
||||||
@ -135,9 +139,8 @@ public class AsynchronousJoin implements AsynchronousProcess {
|
|||||||
|
|
||||||
// Session logic
|
// Session logic
|
||||||
if (service.getProperty(PluginSettings.SESSIONS_ENABLED) && (playerCache.isAuthenticated(name) || database.isLogged(name))) {
|
if (service.getProperty(PluginSettings.SESSIONS_ENABLED) && (playerCache.isAuthenticated(name) || database.isLogged(name))) {
|
||||||
if (plugin.getSessions().containsKey(name)) {
|
if (sessionManager.hasSession(name)) {
|
||||||
plugin.getSessions().get(name).cancel();
|
sessionManager.cancelSession(name);
|
||||||
plugin.getSessions().remove(name);
|
|
||||||
}
|
}
|
||||||
PlayerAuth auth = database.getAuth(name);
|
PlayerAuth auth = database.getAuth(name);
|
||||||
database.setUnlogged(name);
|
database.setUnlogged(name);
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import com.google.common.io.ByteArrayDataOutput;
|
|||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
import fr.xephi.authme.AuthMe;
|
import fr.xephi.authme.AuthMe;
|
||||||
import fr.xephi.authme.ConsoleLogger;
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
|
import fr.xephi.authme.cache.SessionManager;
|
||||||
import fr.xephi.authme.events.LogoutEvent;
|
import fr.xephi.authme.events.LogoutEvent;
|
||||||
import fr.xephi.authme.output.MessageKey;
|
import fr.xephi.authme.output.MessageKey;
|
||||||
import fr.xephi.authme.process.ProcessService;
|
import fr.xephi.authme.process.ProcessService;
|
||||||
@ -36,6 +37,9 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
|
|||||||
@Inject
|
@Inject
|
||||||
private LimboPlayerTaskManager limboPlayerTaskManager;
|
private LimboPlayerTaskManager limboPlayerTaskManager;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private SessionManager sessionManager;
|
||||||
|
|
||||||
ProcessSynchronousPlayerLogout() { }
|
ProcessSynchronousPlayerLogout() { }
|
||||||
|
|
||||||
|
|
||||||
@ -58,9 +62,8 @@ public class ProcessSynchronousPlayerLogout implements SynchronousProcess {
|
|||||||
|
|
||||||
public void processSyncLogout(Player player) {
|
public void processSyncLogout(Player player) {
|
||||||
final String name = player.getName().toLowerCase();
|
final String name = player.getName().toLowerCase();
|
||||||
if (plugin.getSessions().containsKey(name)) {
|
if (sessionManager.hasSession(name)) {
|
||||||
plugin.getSessions().get(name).cancel();
|
sessionManager.cancelSession(name);
|
||||||
plugin.getSessions().remove(name);
|
|
||||||
}
|
}
|
||||||
if (service.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN)) {
|
if (service.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN)) {
|
||||||
plugin.getInventoryProtector().sendBlankInventoryPacket(player);
|
plugin.getInventoryProtector().sendBlankInventoryPacket(player);
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package fr.xephi.authme.process.quit;
|
package fr.xephi.authme.process.quit;
|
||||||
|
|
||||||
import fr.xephi.authme.AuthMe;
|
import fr.xephi.authme.AuthMe;
|
||||||
|
import fr.xephi.authme.cache.SessionManager;
|
||||||
import fr.xephi.authme.cache.auth.PlayerAuth;
|
import fr.xephi.authme.cache.auth.PlayerAuth;
|
||||||
import fr.xephi.authme.cache.auth.PlayerCache;
|
import fr.xephi.authme.cache.auth.PlayerCache;
|
||||||
import fr.xephi.authme.cache.limbo.LimboCache;
|
import fr.xephi.authme.cache.limbo.LimboCache;
|
||||||
@ -42,6 +43,9 @@ public class AsynchronousQuit implements AsynchronousProcess {
|
|||||||
@Inject
|
@Inject
|
||||||
private SyncProcessManager syncProcessManager;
|
private SyncProcessManager syncProcessManager;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private SessionManager sessionManager;
|
||||||
|
|
||||||
AsynchronousQuit() { }
|
AsynchronousQuit() { }
|
||||||
|
|
||||||
|
|
||||||
@ -82,23 +86,21 @@ public class AsynchronousQuit implements AsynchronousProcess {
|
|||||||
isOp = limbo.isOperator();
|
isOp = limbo.isOperator();
|
||||||
limboCache.deleteLimboPlayer(name);
|
limboCache.deleteLimboPlayer(name);
|
||||||
}
|
}
|
||||||
if (Settings.isSessionsEnabled && !isKick) {
|
if (!isKick) {
|
||||||
if (Settings.getSessionTimeout != 0) {
|
if (plugin.isEnabled()) {
|
||||||
if (plugin.isEnabled()) {
|
BukkitTask task = plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() {
|
||||||
BukkitTask task = plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() {
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
postLogout(name);
|
postLogout(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
}, Settings.getSessionTimeout * TICKS_PER_MINUTE);
|
}, Settings.getSessionTimeout * TICKS_PER_MINUTE);
|
||||||
|
|
||||||
plugin.getSessions().put(name, task);
|
sessionManager.addSession(name, task);
|
||||||
} else {
|
} else {
|
||||||
//plugin is disabled; we cannot schedule more tasks so run it directly here
|
//plugin is disabled; we cannot schedule more tasks so run it directly here
|
||||||
postLogout(name);
|
postLogout(name);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
playerCache.removePlayer(name);
|
playerCache.removePlayer(name);
|
||||||
@ -117,6 +119,6 @@ public class AsynchronousQuit implements AsynchronousProcess {
|
|||||||
private void postLogout(String name) {
|
private void postLogout(String name) {
|
||||||
PlayerCache.getInstance().removePlayer(name);
|
PlayerCache.getInstance().removePlayer(name);
|
||||||
database.setUnlogged(name);
|
database.setUnlogged(name);
|
||||||
plugin.getSessions().remove(name);
|
sessionManager.removeSession(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
98
src/test/java/fr/xephi/authme/cache/SessionManagerTest.java
vendored
Normal file
98
src/test/java/fr/xephi/authme/cache/SessionManagerTest.java
vendored
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
package fr.xephi.authme.cache;
|
||||||
|
|
||||||
|
import fr.xephi.authme.settings.NewSetting;
|
||||||
|
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||||
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link SessionManager}.
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class SessionManagerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHaveSession() {
|
||||||
|
// given
|
||||||
|
NewSetting settings = mockSettings(true, 10);
|
||||||
|
SessionManager manager = new SessionManager(settings);
|
||||||
|
String player = "playah";
|
||||||
|
BukkitTask task = mock(BukkitTask.class);
|
||||||
|
|
||||||
|
// when
|
||||||
|
manager.addSession(player, task);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(manager.hasSession(player), equalTo(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotHaveSession() {
|
||||||
|
// given
|
||||||
|
NewSetting settings = mockSettings(true, 10);
|
||||||
|
SessionManager manager = new SessionManager(settings);
|
||||||
|
String player = "playah";
|
||||||
|
|
||||||
|
// when/then
|
||||||
|
assertThat(manager.hasSession(player), equalTo(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldAddSession() {
|
||||||
|
// given
|
||||||
|
NewSetting settings = mockSettings(true, 10);
|
||||||
|
SessionManager manager = new SessionManager(settings);
|
||||||
|
String player = "playah";
|
||||||
|
BukkitTask task = mock(BukkitTask.class);
|
||||||
|
|
||||||
|
// when
|
||||||
|
manager.addSession(player, task);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(manager.hasSession(player), equalTo(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotAddSessionBecauseDisabled() {
|
||||||
|
// given
|
||||||
|
NewSetting settings = mockSettings(false, 10);
|
||||||
|
SessionManager manager = new SessionManager(settings);
|
||||||
|
String player = "playah";
|
||||||
|
BukkitTask task = mock(BukkitTask.class);
|
||||||
|
|
||||||
|
// when
|
||||||
|
manager.addSession(player, task);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(manager.hasSession(player), equalTo(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldNotAddSessionBecauseTimeoutIsZero() {
|
||||||
|
// given
|
||||||
|
NewSetting settings = mockSettings(true, 0);
|
||||||
|
SessionManager manager = new SessionManager(settings);
|
||||||
|
String player = "playah";
|
||||||
|
BukkitTask task = mock(BukkitTask.class);
|
||||||
|
|
||||||
|
// when
|
||||||
|
manager.addSession(player, task);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(manager.hasSession(player), equalTo(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static NewSetting mockSettings(boolean isEnabled, int sessionTimeout) {
|
||||||
|
NewSetting settings = mock(NewSetting.class);
|
||||||
|
given(settings.getProperty(PluginSettings.SESSIONS_ENABLED)).willReturn(isEnabled);
|
||||||
|
given(settings.getProperty(PluginSettings.SESSIONS_TIMEOUT)).willReturn(sessionTimeout);
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user