Fixed StackOverflowExceptions on enable caused by dependency cycles

Registered pages on ResponseHandler separately
Registered commands in PlanCommand and PlanBungeeCommand separately

Made WebServer Lazy in InfoSystems and ConnectionSystems
This commit is contained in:
Rsl1122 2018-09-09 14:53:41 +03:00
parent db69d07100
commit 3a7b94a94f
15 changed files with 177 additions and 65 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.djrapitops</groupId>
<artifactId>Plan</artifactId>
<version>4.3.0-SNAPSHOT</version>
<version>4.5.0-SNAPSHOT</version>
<build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>

View File

@ -90,6 +90,7 @@ class BukkitPlanModule {
@Singleton
@Named("mainCommand")
CommandNode provideMainCommand(PlanCommand command) {
command.registerCommands();
return command;
}
}

View File

@ -72,6 +72,7 @@ class BungeePlanModule {
@Singleton
@Named("mainCommand")
CommandNode provideMainCommand(PlanBungeeCommand command) {
command.registerCommands();
return command;
}
}

View File

@ -80,6 +80,7 @@ class SpongePlanModule {
@Singleton
@Named("mainCommand")
CommandNode provideMainCommand(PlanCommand command) {
command.registerCommands();
return command;
}
}

View File

@ -10,6 +10,7 @@ import com.djrapitops.plugin.command.ColorScheme;
import com.djrapitops.plugin.command.CommandNode;
import com.djrapitops.plugin.command.CommandType;
import com.djrapitops.plugin.command.TreeCmdNode;
import dagger.Lazy;
import javax.inject.Inject;
@ -23,26 +24,60 @@ import javax.inject.Inject;
*/
public class PlanBungeeCommand extends TreeCmdNode {
private final NetworkCommand networkCommand;
private final ListServersCommand listServersCommand;
private final ListPlayersCommand listPlayersCommand;
private final RegisterCommand registerCommand;
private final Lazy<WebUserCommand> webUserCommand;
private final ManageConDebugCommand conDebugCommand;
private final ManageRawDataCommand rawDataCommand;
private final BungeeSetupToggleCommand setupToggleCommand;
private final ReloadCommand reloadCommand;
private final DisableCommand disableCommand;
private boolean commandsRegistered;
@Inject
public PlanBungeeCommand(ColorScheme colorScheme, Locale locale,
// Group 1
NetworkCommand networkCommand,
ListServersCommand listServersCommand,
ListPlayersCommand listPlayersCommand,
// Group 2
RegisterCommand registerCommand,
WebUserCommand webUserCommand,
// Group 3
ManageConDebugCommand conDebugCommand,
ManageRawDataCommand rawDataCommand,
BungeeSetupToggleCommand setupToggleCommand,
ReloadCommand reloadCommand,
DisableCommand disableCommand
public PlanBungeeCommand(
ColorScheme colorScheme,
Locale locale,
// Group 1
NetworkCommand networkCommand,
ListServersCommand listServersCommand,
ListPlayersCommand listPlayersCommand,
// Group 2
RegisterCommand registerCommand,
Lazy<WebUserCommand> webUserCommand,
// Group 3
ManageConDebugCommand conDebugCommand,
ManageRawDataCommand rawDataCommand,
BungeeSetupToggleCommand setupToggleCommand,
ReloadCommand reloadCommand,
DisableCommand disableCommand
) {
super("planbungee", Permissions.MANAGE.getPermission(), CommandType.CONSOLE, null);
super.setColorScheme(colorScheme);
commandsRegistered = false;
this.networkCommand = networkCommand;
this.listServersCommand = listServersCommand;
this.listPlayersCommand = listPlayersCommand;
this.registerCommand = registerCommand;
this.webUserCommand = webUserCommand;
this.conDebugCommand = conDebugCommand;
this.rawDataCommand = rawDataCommand;
this.setupToggleCommand = setupToggleCommand;
this.reloadCommand = reloadCommand;
this.disableCommand = disableCommand;
setColorScheme(colorScheme);
setInDepthHelp(locale.getArray(DeepHelpLang.PLAN));
}
public void registerCommands() {
if (commandsRegistered) {
return;
}
CommandNode[] analyticsGroup = {
networkCommand,
@ -51,7 +86,7 @@ public class PlanBungeeCommand extends TreeCmdNode {
};
CommandNode[] webGroup = {
registerCommand,
webUserCommand
webUserCommand.get()
};
CommandNode[] manageGroup = {
conDebugCommand,
@ -61,5 +96,6 @@ public class PlanBungeeCommand extends TreeCmdNode {
disableCommand
};
setNodeGroups(analyticsGroup, webGroup, manageGroup);
commandsRegistered = true;
}
}

View File

@ -9,6 +9,7 @@ import com.djrapitops.plugin.command.ColorScheme;
import com.djrapitops.plugin.command.CommandNode;
import com.djrapitops.plugin.command.CommandType;
import com.djrapitops.plugin.command.TreeCmdNode;
import dagger.Lazy;
import javax.inject.Inject;
@ -22,30 +23,73 @@ import javax.inject.Inject;
*/
public class PlanCommand extends TreeCmdNode {
private final PlanConfig config;
private final InspectCommand inspectCommand;
private final QInspectCommand qInspectCommand;
private final SearchCommand searchCommand;
private final ListPlayersCommand listPlayersCommand;
private final AnalyzeCommand analyzeCommand;
private final NetworkCommand networkCommand;
private final ListServersCommand listServersCommand;
private final Lazy<WebUserCommand> webUserCommand;
private final RegisterCommand registerCommand;
private final InfoCommand infoCommand;
private final ReloadCommand reloadCommand;
private final Lazy<ManageCommand> manageCommand;
private final DevCommand devCommand;
private boolean commandsRegistered;
@Inject
public PlanCommand(ColorScheme colorScheme, Locale locale, PlanConfig config,
// Group 1
InspectCommand inspectCommand,
QInspectCommand qInspectCommand,
SearchCommand searchCommand,
ListPlayersCommand listPlayersCommand,
AnalyzeCommand analyzeCommand,
NetworkCommand networkCommand,
ListServersCommand listServersCommand,
// Group 2
WebUserCommand webUserCommand,
RegisterCommand registerCommand,
// Group 3
InfoCommand infoCommand,
ReloadCommand reloadCommand,
ManageCommand manageCommand,
DevCommand devCommand
public PlanCommand(
ColorScheme colorScheme,
Locale locale,
PlanConfig config,
// Group 1
InspectCommand inspectCommand,
QInspectCommand qInspectCommand,
SearchCommand searchCommand,
ListPlayersCommand listPlayersCommand,
AnalyzeCommand analyzeCommand,
NetworkCommand networkCommand,
ListServersCommand listServersCommand,
// Group 2
Lazy<WebUserCommand> webUserCommand,
RegisterCommand registerCommand,
// Group 3
InfoCommand infoCommand,
ReloadCommand reloadCommand,
Lazy<ManageCommand> manageCommand,
DevCommand devCommand
) {
super("plan", "", CommandType.CONSOLE, null);
super.setDefaultCommand("inspect");
super.setColorScheme(colorScheme);
commandsRegistered = false;
this.config = config;
this.inspectCommand = inspectCommand;
this.qInspectCommand = qInspectCommand;
this.searchCommand = searchCommand;
this.listPlayersCommand = listPlayersCommand;
this.analyzeCommand = analyzeCommand;
this.networkCommand = networkCommand;
this.listServersCommand = listServersCommand;
this.webUserCommand = webUserCommand;
this.registerCommand = registerCommand;
this.infoCommand = infoCommand;
this.reloadCommand = reloadCommand;
this.manageCommand = manageCommand;
this.devCommand = devCommand;
setDefaultCommand("inspect");
setColorScheme(colorScheme);
setInDepthHelp(locale.getArray(DeepHelpLang.PLAN));
}
public void registerCommands() {
if (commandsRegistered) {
return;
}
CommandNode[] analyticsGroup = {
inspectCommand,
@ -57,15 +101,16 @@ public class PlanCommand extends TreeCmdNode {
listServersCommand
};
CommandNode[] webGroup = {
webUserCommand,
webUserCommand.get(),
registerCommand
};
CommandNode[] manageGroup = {
infoCommand,
reloadCommand,
manageCommand,
manageCommand.get(),
config.isTrue(Settings.DEV_MODE) ? devCommand : null
};
setNodeGroups(analyticsGroup, webGroup, manageGroup);
commandsRegistered = true;
}
}

View File

@ -17,6 +17,7 @@ import com.djrapitops.plan.system.webserver.cache.PageId;
import com.djrapitops.plan.system.webserver.cache.ResponseCache;
import com.djrapitops.plan.system.webserver.response.ResponseFactory;
import com.djrapitops.plugin.logging.console.PluginLogger;
import dagger.Lazy;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -38,7 +39,7 @@ public class BungeeInfoSystem extends InfoSystem {
ResponseFactory responseFactory,
ConnectionSystem connectionSystem,
ServerInfo serverInfo,
WebServer webServer,
Lazy<WebServer> webServer,
PluginLogger logger
) {
super(infoRequestFactory, connectionSystem, serverInfo, webServer, logger);

View File

@ -4,7 +4,6 @@
*/
package com.djrapitops.plan.system.info;
import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.api.exceptions.connection.BadRequestException;
import com.djrapitops.plan.api.exceptions.connection.ConnectionFailException;
import com.djrapitops.plan.api.exceptions.connection.NoServersException;
@ -20,6 +19,7 @@ import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.webserver.WebServer;
import com.djrapitops.plugin.api.Check;
import com.djrapitops.plugin.logging.console.PluginLogger;
import dagger.Lazy;
import java.util.UUID;
@ -37,14 +37,14 @@ public abstract class InfoSystem implements SubSystem {
protected final InfoRequestFactory infoRequestFactory;
protected final ConnectionSystem connectionSystem;
protected final ServerInfo serverInfo;
protected final WebServer webServer;
protected final Lazy<WebServer> webServer;
protected final PluginLogger logger;
protected InfoSystem(
InfoRequestFactory infoRequestFactory,
ConnectionSystem connectionSystem,
ServerInfo serverInfo,
WebServer webServer,
Lazy<WebServer> webServer,
PluginLogger logger
) {
this.infoRequestFactory = infoRequestFactory;
@ -125,7 +125,7 @@ public abstract class InfoSystem implements SubSystem {
public abstract void runLocally(InfoRequest infoRequest) throws WebException;
@Override
public void enable() throws EnableException {
public void enable() {
connectionSystem.enable();
}
@ -160,7 +160,7 @@ public abstract class InfoSystem implements SubSystem {
throw new BadRequestException("Method not available on Bungee.");
}
Server bungee = new Server(-1, null, "Bungee", addressToRequestServer, -1);
String addressOfThisServer = webServer.getAccessAddress();
String addressOfThisServer = webServer.get().getAccessAddress();
connectionSystem.setSetupAllowed(true);
connectionSystem.sendInfoRequest(infoRequestFactory.sendDBSettingsRequest(addressOfThisServer), bungee);

View File

@ -14,6 +14,7 @@ import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.webserver.WebServer;
import com.djrapitops.plan.utilities.html.HtmlStructure;
import com.djrapitops.plugin.logging.console.PluginLogger;
import dagger.Lazy;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -34,7 +35,7 @@ public class ServerInfoSystem extends InfoSystem {
ConnectionSystem connectionSystem,
ServerInfo serverInfo,
InfoRequestFactory infoRequestFactory,
WebServer webServer,
Lazy<WebServer> webServer,
PluginLogger logger
) {
super(infoRequestFactory, connectionSystem, serverInfo, webServer, logger);

View File

@ -30,7 +30,7 @@ import java.util.UUID;
public class BungeeConnectionSystem extends ConnectionSystem {
private final Database database;
private final WebServer webServer;
private final Lazy<WebServer> webServer;
private final ErrorHandler errorHandler;
private final WebExceptionLogger webExceptionLogger;
@ -39,7 +39,7 @@ public class BungeeConnectionSystem extends ConnectionSystem {
@Inject
public BungeeConnectionSystem(
Database database,
WebServer webServer,
Lazy<WebServer> webServer,
ConnectionLog connectionLog,
InfoRequests infoRequests,
Lazy<InfoSystem> infoSystem,
@ -102,11 +102,12 @@ public class BungeeConnectionSystem extends ConnectionSystem {
@Override
public String getMainAddress() {
return webServer.getAccessAddress();
return webServer.get().getAccessAddress();
}
@Override
public void enable() {
super.enable();
refreshServerMap();
}

View File

@ -9,6 +9,7 @@ import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.system.SubSystem;
import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.info.request.InfoRequest;
import com.djrapitops.plan.system.info.request.InfoRequests;
import com.djrapitops.plan.system.info.request.WideRequest;
import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.info.server.ServerInfo;
@ -26,7 +27,7 @@ import java.util.*;
public abstract class ConnectionSystem implements SubSystem {
protected final ConnectionLog connectionLog;
protected final Map<String, InfoRequest> dataRequests;
protected final InfoRequests infoRequests;
protected final Lazy<InfoSystem> infoSystem;
protected final ServerInfo serverInfo;
@ -35,7 +36,7 @@ public abstract class ConnectionSystem implements SubSystem {
public ConnectionSystem(
ConnectionLog connectionLog,
Map<String, InfoRequest> dataRequests,
InfoRequests infoRequests,
Lazy<InfoSystem> infoSystem,
ServerInfo serverInfo
) {
@ -44,11 +45,11 @@ public abstract class ConnectionSystem implements SubSystem {
this.serverInfo = serverInfo;
setupAllowed = false;
bukkitServers = new HashMap<>();
this.dataRequests = dataRequests;
this.infoRequests = infoRequests;
}
public InfoRequest getInfoRequest(String name) {
return dataRequests.get(name.toLowerCase());
return infoRequests.get(name.toLowerCase());
}
public void setSetupAllowed(boolean setupAllowed) {
@ -89,10 +90,15 @@ public abstract class ConnectionSystem implements SubSystem {
return new ArrayList<>(bukkitServers.values());
}
@Override
public void enable() {
infoRequests.initializeRequests();
}
@Override
public void disable() {
setupAllowed = false;
bukkitServers.clear();
dataRequests.clear();
infoRequests.clear();
}
}

View File

@ -39,7 +39,7 @@ public class ServerConnectionSystem extends ConnectionSystem {
private final PlanConfig config;
private final Processing processing;
private final Database database;
private final WebServer webServer;
private final Lazy<WebServer> webServer;
private final PluginLogger pluginLogger;
private final WebExceptionLogger webExceptionLogger;
@ -53,7 +53,7 @@ public class ServerConnectionSystem extends ConnectionSystem {
PlanConfig config,
Processing processing,
Database database,
WebServer webServer,
Lazy<WebServer> webServer,
ConnectionLog connectionLog,
InfoRequests infoRequests,
Lazy<InfoSystem> infoSystem,
@ -134,6 +134,7 @@ public class ServerConnectionSystem extends ConnectionSystem {
@Override
public void enable() {
super.enable();
refreshServerMap();
boolean usingBungeeWebServer = isServerAvailable();
@ -143,7 +144,7 @@ public class ServerConnectionSystem extends ConnectionSystem {
pluginLogger.log(L.INFO_COLOR, "§e" + locale.getString(PluginLang.ENABLE_NOTIFY_EMPTY_IP));
}
if (usingBungeeWebServer && usingAlternativeIP) {
String webServerAddress = webServer.getAccessAddress();
String webServerAddress = webServer.get().getAccessAddress();
pluginLogger.info(locale.getString(PluginLang.ENABLE_NOTIFY_ADDRESS_CONFIRMATION, webServerAddress));
}
}

View File

@ -14,8 +14,14 @@ import java.util.HashMap;
@Singleton
public class InfoRequests extends HashMap<String, InfoRequest> {
private final InfoRequestHandlerFactory handlers;
@Inject
public InfoRequests(InfoRequestHandlerFactory handlers) {
this.handlers = handlers;
}
public void initializeRequests() {
putRequest(handlers.cacheAnalysisPageRequest());
putRequest(handlers.cacheInspectPageRequest());
putRequest(handlers.cacheInspectPluginsTabRequest());

View File

@ -18,6 +18,7 @@ import com.djrapitops.plan.system.webserver.response.ResponseFactory;
import com.djrapitops.plan.system.webserver.response.errors.*;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.error.ErrorHandler;
import dagger.Lazy;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -36,13 +37,19 @@ public class ResponseHandler extends TreePageHandler {
private final ResponseFactory responseFactory;
private final DebugPageHandler debugPageHandler;
private final PlayersPageHandler playersPageHandler;
private final PlayerPageHandler playerPageHandler;
private final ServerPageHandler serverPageHandler;
private final InfoRequestPageHandler infoRequestPageHandler;
private final ErrorHandler errorHandler;
private WebServer webServer;
private Lazy<WebServer> webServer;
@Inject
public ResponseHandler(
ResponseFactory responseFactory,
Lazy<WebServer> webServer,
DebugPageHandler debugPageHandler,
PlayersPageHandler playersPageHandler,
@ -52,9 +59,17 @@ public class ResponseHandler extends TreePageHandler {
ErrorHandler errorHandler
) {
this.webServer = webServer;
this.responseFactory = responseFactory;
this.debugPageHandler = debugPageHandler;
this.playersPageHandler = playersPageHandler;
this.playerPageHandler = playerPageHandler;
this.serverPageHandler = serverPageHandler;
this.infoRequestPageHandler = infoRequestPageHandler;
this.errorHandler = errorHandler;
}
public void registerPages() {
registerPage("favicon.ico", responseFactory.redirectResponse("https://puu.sh/tK0KL/6aa2ba141b.ico"), 5);
registerPage("debug", debugPageHandler);
registerPage("players", playersPageHandler);
@ -63,7 +78,7 @@ public class ResponseHandler extends TreePageHandler {
registerPage("network", serverPageHandler);
registerPage("server", serverPageHandler);
if (webServer.isAuthRequired()) {
if (webServer.get().isAuthRequired()) {
registerPage("", new RootPageHandler());
} else {
registerPage("", responseFactory.redirectResponse("/server"), 5);
@ -114,11 +129,11 @@ public class ResponseHandler extends TreePageHandler {
return ResponseCache.loadResponse(PageId.JS.of(targetString), () -> responseFactory.javaScriptResponse(targetString));
}
boolean isNotInfoRequest = target.isEmpty() || !target.get(0).equals("info");
boolean isAuthRequired = webServer.isAuthRequired() && isNotInfoRequest;
boolean isAuthRequired = webServer.get().isAuthRequired() && isNotInfoRequest;
if (isAuthRequired) {
authentication = request.getAuth();
if (!authentication.isPresent()) {
if (webServer.isUsingHTTPS()) {
if (webServer.get().isUsingHTTPS()) {
return DefaultResponses.BASIC_AUTH.get();
} else {
return DefaultResponses.FORBIDDEN.get();
@ -136,8 +151,4 @@ public class ResponseHandler extends TreePageHandler {
return DefaultResponses.FORBIDDEN.get();
}
}
public void setWebServer(WebServer webServer) {
this.webServer = webServer;
}
}

View File

@ -68,7 +68,6 @@ public class WebServer implements SubSystem {
this.config = config;
this.requestHandler = requestHandler;
requestHandler.getResponseHandler().setWebServer(this);
this.logger = logger;
this.errorHandler = errorHandler;
@ -90,6 +89,8 @@ public class WebServer implements SubSystem {
logger.error(locale.getString(PluginLang.WEB_SERVER_FAIL_PORT_BIND, port));
}
}
requestHandler.getResponseHandler().registerPages();
}
/**