Config enable check for DataExtension registration

This commit is contained in:
Rsl1122 2019-03-20 13:48:50 +02:00
parent e8da008538
commit 0c95a4cc63
2 changed files with 49 additions and 7 deletions

View File

@ -38,8 +38,11 @@ public class PluginsConfigSection {
} }
public boolean hasSection(PluginData dataSource) { public boolean hasSection(PluginData dataSource) {
return hasSection(dataSource.getSourcePlugin());
}
public boolean hasSection(String pluginName) {
ConfigNode section = getPluginsSection(); ConfigNode section = getPluginsSection();
String pluginName = dataSource.getSourcePlugin();
return section.getNode(pluginName + ".Enabled").isPresent(); return section.getNode(pluginName + ".Enabled").isPresent();
} }
@ -49,8 +52,11 @@ public class PluginsConfigSection {
} }
public void createSection(PluginData dataSource) throws IOException { public void createSection(PluginData dataSource) throws IOException {
createSection(dataSource.getSourcePlugin());
}
public void createSection(String pluginName) throws IOException {
ConfigNode section = getPluginsSection(); ConfigNode section = getPluginsSection();
String pluginName = dataSource.getSourcePlugin();
section.set(pluginName + ".Enabled", true); section.set(pluginName + ".Enabled", true);
section.sort(); section.sort();
@ -58,9 +64,11 @@ public class PluginsConfigSection {
} }
public boolean isEnabled(PluginData dataSource) { public boolean isEnabled(PluginData dataSource) {
ConfigNode section = getPluginsSection(); return isEnabled(dataSource.getSourcePlugin());
}
String pluginName = dataSource.getSourcePlugin(); public boolean isEnabled(String pluginName) {
ConfigNode section = getPluginsSection();
return section.getBoolean(pluginName + ".Enabled"); return section.getBoolean(pluginName + ".Enabled");
} }
} }

View File

@ -16,17 +16,20 @@
*/ */
package com.djrapitops.plan.extension; package com.djrapitops.plan.extension;
import com.djrapitops.plan.data.plugin.PluginsConfigSection;
import com.djrapitops.plan.extension.implementation.DataProviderExtractor; import com.djrapitops.plan.extension.implementation.DataProviderExtractor;
import com.djrapitops.plan.extension.implementation.ExtensionRegister; import com.djrapitops.plan.extension.implementation.ExtensionRegister;
import com.djrapitops.plan.extension.implementation.providers.gathering.ProviderValueGatherer; import com.djrapitops.plan.extension.implementation.providers.gathering.ProviderValueGatherer;
import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plugin.logging.L; import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.console.PluginLogger; import com.djrapitops.plugin.logging.console.PluginLogger;
import com.djrapitops.plugin.logging.error.ErrorHandler; import com.djrapitops.plugin.logging.error.ErrorHandler;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -39,6 +42,7 @@ import java.util.UUID;
@Singleton @Singleton
public class ExtensionServiceImplementation implements ExtensionService { public class ExtensionServiceImplementation implements ExtensionService {
private final PlanConfig config;
private final DBSystem dbSystem; private final DBSystem dbSystem;
private final ServerInfo serverInfo; private final ServerInfo serverInfo;
private final ExtensionRegister extensionRegister; private final ExtensionRegister extensionRegister;
@ -49,12 +53,14 @@ public class ExtensionServiceImplementation implements ExtensionService {
@Inject @Inject
public ExtensionServiceImplementation( public ExtensionServiceImplementation(
PlanConfig config,
DBSystem dbSystem, DBSystem dbSystem,
ServerInfo serverInfo, ServerInfo serverInfo,
ExtensionRegister extensionRegister, ExtensionRegister extensionRegister,
PluginLogger logger, PluginLogger logger,
ErrorHandler errorHandler ErrorHandler errorHandler
) { ) {
this.config = config;
this.dbSystem = dbSystem; this.dbSystem = dbSystem;
this.serverInfo = serverInfo; this.serverInfo = serverInfo;
this.extensionRegister = extensionRegister; this.extensionRegister = extensionRegister;
@ -73,14 +79,38 @@ public class ExtensionServiceImplementation implements ExtensionService {
@Override @Override
public void register(DataExtension extension) { public void register(DataExtension extension) {
DataProviderExtractor extractor = new DataProviderExtractor(extension); DataProviderExtractor extractor = new DataProviderExtractor(extension);
String pluginName = extractor.getPluginName();
if (shouldNotAllowRegistration(pluginName)) return;
for (String warning : extractor.getWarnings()) { for (String warning : extractor.getWarnings()) {
logger.warn("DataExtension API implementation mistake for " + extractor.getPluginName() + ": " + warning); logger.warn("DataExtension API implementation mistake for " + pluginName + ": " + warning);
} }
ProviderValueGatherer gatherer = new ProviderValueGatherer(extension, extractor, dbSystem, serverInfo, logger); ProviderValueGatherer gatherer = new ProviderValueGatherer(extension, extractor, dbSystem, serverInfo, logger);
gatherer.storeExtensionInformation(); gatherer.storeExtensionInformation();
extensionGatherers.put(extractor.getPluginName(), gatherer); extensionGatherers.put(pluginName, gatherer);
logger.debug(pluginName + " extension registered.");
}
private boolean shouldNotAllowRegistration(String pluginName) {
PluginsConfigSection pluginsConfig = config.getPluginsConfigSection();
if (pluginsConfig.hasSection(pluginName)) {
try {
pluginsConfig.createSection(pluginName);
} catch (IOException e) {
errorHandler.log(L.ERROR, this.getClass(), e);
logger.warn("Could not register DataExtension for " + pluginName + " due to " + e.toString());
return true;
}
}
if (!pluginsConfig.isEnabled(pluginName)) {
logger.debug(pluginName + " extension disabled in the config.");
return true;
}
return false; // Should register.
} }
public void updatePlayerValues(UUID playerUUID, String playerName) { public void updatePlayerValues(UUID playerUUID, String playerName) {
@ -88,7 +118,11 @@ public class ExtensionServiceImplementation implements ExtensionService {
try { try {
gatherer.getValue().updateValues(playerUUID, playerName); gatherer.getValue().updateValues(playerUUID, playerName);
} catch (Exception | NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError e) { } catch (Exception | NoClassDefFoundError | NoSuchMethodError | NoSuchFieldError e) {
logger.warn(gatherer.getKey() + " ran into " + e.getClass().getSimpleName() + " when updating value for '" + playerName + "', reason: '" + e.getMessage() + "', stack trace to follow:"); logger.warn(gatherer.getKey() + " ran into (but failed safely) " + e.getClass().getSimpleName() +
" when updating value for '" + playerName +
"', (You can disable integration with setting 'Plugins." + gatherer.getKey() + ".Enabled')" +
" reason: '" + e.getMessage() +
"', stack trace to follow:");
errorHandler.log(L.WARN, gatherer.getValue().getClass(), e); errorHandler.log(L.WARN, gatherer.getValue().getClass(), e);
} }
} }