From 0e6597fb86df7c8809ced0f99ae8a75ceec8fb2b Mon Sep 17 00:00:00 2001 From: zml2008 Date: Sat, 22 Oct 2011 01:13:52 -0700 Subject: [PATCH] Added configurable detection order of permissions resolvers, restored old default order, and updated configurations to the new Configuration --- .../ConfigurationPermissionsResolver.java | 36 +++--- .../bukkit/migration/DinnerPermsResolver.java | 5 +- .../FlatFilePermissionsResolver.java | 2 +- .../migration/NijiPermissionsResolver.java | 2 +- .../migration/PermissionsExResolver.java | 2 +- .../migration/PermissionsResolverManager.java | 109 +++++++++++++----- .../migration/PluginPermissionsResolver.java | 2 +- 7 files changed, 104 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java b/src/main/java/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java index 3919241d7..766210231 100644 --- a/src/main/java/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java @@ -19,12 +19,10 @@ package com.sk89q.bukkit.migration; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.HashMap; -import org.bukkit.util.config.Configuration; +import java.util.*; + +import org.bukkit.configuration.Configuration; +import org.bukkit.configuration.ConfigurationSection; public class ConfigurationPermissionsResolver implements PermissionsResolver { private Configuration config; @@ -36,15 +34,15 @@ public class ConfigurationPermissionsResolver implements PermissionsResolver { this.config = config; } - public static void generateDefaultPerms(Configuration config) { - config.setProperty("permissions.groups.default.permissions", new String[] { + public static ConfigurationSection generateDefaultPerms(ConfigurationSection section) { + section.set("permissions.groups.default.permissions", Arrays.asList( "worldedit.reload", - "worldedit.selection.*", - "worlds.creative.worldedit.region.*"}); - config.setProperty("permissions.groups.admins.permissions", new String[]{"*"}); - config.setProperty("permissions.users.sk89q.permissions", new String[]{"worldedit.*"}); - config.setProperty("permissions.users.sk89q.groups", new String[]{"admins"}); - + "worldedit.selection", + "worlds.creative.worldedit.region")); + section.set("permissions.groups.admins.permissions", Arrays.asList("*")); + section.set("permissions.users.sk89q.permissions", Arrays.asList("worldedit")); + section.set("permissions.users.sk89q.groups", Arrays.asList("admins")); + return section; } public void load() { @@ -54,12 +52,12 @@ public class ConfigurationPermissionsResolver implements PermissionsResolver { Map> userGroupPermissions = new HashMap>(); - List groupKeys = config.getKeys("permissions.groups"); + List groupKeys = config.getList("permissions.groups"); if (groupKeys != null) { for (String key : groupKeys) { List permissions = - config.getStringList("permissions.groups." + key + ".permissions", null); + config.getList("permissions.groups." + key + ".permissions", null); if (permissions.size() > 0) { Set groupPerms = new HashSet(permissions); @@ -72,21 +70,21 @@ public class ConfigurationPermissionsResolver implements PermissionsResolver { } } - List userKeys = config.getKeys("permissions.users"); + List userKeys = config.getList("permissions.users"); if (userKeys != null) { for (String key : userKeys) { Set permsCache = new HashSet(); List permissions = - config.getStringList("permissions.users." + key + ".permissions", null); + config.getList("permissions.users." + key + ".permissions", null); if (permissions.size() > 0) { permsCache.addAll(permissions); } List groups = - config.getStringList("permissions.users." + key + ".groups", null); + config.getList("permissions.users." + key + ".groups", null); groups.add("default"); if (groups.size() > 0) { diff --git a/src/main/java/com/sk89q/bukkit/migration/DinnerPermsResolver.java b/src/main/java/com/sk89q/bukkit/migration/DinnerPermsResolver.java index 8637d02a1..c2dd6ae90 100644 --- a/src/main/java/com/sk89q/bukkit/migration/DinnerPermsResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/DinnerPermsResolver.java @@ -26,7 +26,7 @@ import org.bukkit.permissions.PermissionAttachmentInfo; import java.util.ArrayList; import java.util.List; -import org.bukkit.util.config.Configuration; +import org.bukkit.configuration.Configuration; public class DinnerPermsResolver implements PermissionsResolver { @@ -38,9 +38,6 @@ public class DinnerPermsResolver implements PermissionsResolver { } public static PermissionsResolver factory(Server server, Configuration config) { - if (!config.getBoolean("dinnerperms", true)) { - return null; - } return new DinnerPermsResolver(server); } diff --git a/src/main/java/com/sk89q/bukkit/migration/FlatFilePermissionsResolver.java b/src/main/java/com/sk89q/bukkit/migration/FlatFilePermissionsResolver.java index 76f1c9cf6..ec0ab92df 100644 --- a/src/main/java/com/sk89q/bukkit/migration/FlatFilePermissionsResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/FlatFilePermissionsResolver.java @@ -29,7 +29,7 @@ import java.util.Map; import java.util.Set; import java.util.HashMap; import org.bukkit.Server; -import org.bukkit.util.config.Configuration; +import org.bukkit.configuration.Configuration; public class FlatFilePermissionsResolver implements PermissionsResolver { private Map> userPermissionsCache; diff --git a/src/main/java/com/sk89q/bukkit/migration/NijiPermissionsResolver.java b/src/main/java/com/sk89q/bukkit/migration/NijiPermissionsResolver.java index 5ae15addf..f0163f007 100644 --- a/src/main/java/com/sk89q/bukkit/migration/NijiPermissionsResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/NijiPermissionsResolver.java @@ -26,7 +26,7 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import com.nijikokun.bukkit.Permissions.Permissions; -import org.bukkit.util.config.Configuration; +import org.bukkit.configuration.Configuration; public class NijiPermissionsResolver implements PermissionsResolver { private Server server; diff --git a/src/main/java/com/sk89q/bukkit/migration/PermissionsExResolver.java b/src/main/java/com/sk89q/bukkit/migration/PermissionsExResolver.java index bad0ee2a3..d45b881f4 100644 --- a/src/main/java/com/sk89q/bukkit/migration/PermissionsExResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/PermissionsExResolver.java @@ -25,7 +25,7 @@ import org.bukkit.entity.Player; import ru.tehkode.permissions.PermissionManager; import ru.tehkode.permissions.PermissionUser; -import org.bukkit.util.config.Configuration; +import org.bukkit.configuration.Configuration; public class PermissionsExResolver implements PermissionsResolver { private final PermissionManager manager; diff --git a/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverManager.java b/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverManager.java index 7b7b1f078..d5b99dff5 100644 --- a/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverManager.java +++ b/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverManager.java @@ -22,11 +22,15 @@ package com.sk89q.bukkit.migration; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; -import java.util.List; +import java.util.*; +import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.Server; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.configuration.file.YamlConfigurationOptions; import org.bukkit.plugin.Plugin; -import org.bukkit.util.config.Configuration; +import org.bukkit.configuration.Configuration; public class PermissionsResolverManager implements PermissionsResolver { private static final String CONFIG_HEADER = "#\r\n" + @@ -55,21 +59,22 @@ public class PermissionsResolverManager implements PermissionsResolver { private Server server; private PermissionsResolver permissionResolver; private PermissionsResolverServerListener listener; - private Configuration config; + private FileConfiguration config; private String name; private Logger logger; + private List> enabledResolvers = new ArrayList>(); @SuppressWarnings("unchecked") protected Class[] availableResolvers = new Class[] { - DinnerPermsResolver.class, - NijiPermissionsResolver.class, - PermissionsExResolver.class, PluginPermissionsResolver.class, + PermissionsExResolver.class, + NijiPermissionsResolver.class, + DinnerPermsResolver.class, FlatFilePermissionsResolver.class }; @Deprecated - public PermissionsResolverManager(Configuration config, Server server, String name, Logger logger) { + public PermissionsResolverManager(org.bukkit.util.config.Configuration config, Server server, String name, Logger logger) { this.server = server; this.name = name; this.logger = logger; @@ -88,23 +93,25 @@ public class PermissionsResolverManager implements PermissionsResolver { } public void findResolver() { - for (Class resolverClass : availableResolvers) { + for (Class resolverClass : enabledResolvers) { try { Method factoryMethod = resolverClass.getMethod("factory", Server.class, Configuration.class); this.permissionResolver = (PermissionsResolver) factoryMethod.invoke(null, this.server, this.config); if (this.permissionResolver != null) { - logger.info(name + ": " + this.permissionResolver.getDetectionMessage()); - return; + break; } } catch (Throwable e) { + logger.warning("Error in factory method for " + resolverClass.getSimpleName() + ": " + e); + e.printStackTrace(); continue; } } - - permissionResolver = new ConfigurationPermissionsResolver(config); - logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions."); + if (permissionResolver == null) { + permissionResolver = new ConfigurationPermissionsResolver(config); + } + logger.info(name + ": " + permissionResolver.getDetectionMessage()); } public void setPluginPermissionsResolver(Plugin plugin) { @@ -113,7 +120,7 @@ public class PermissionsResolverManager implements PermissionsResolver { } permissionResolver = new PluginPermissionsResolver((PermissionsProvider) plugin, plugin); - logger.info(name + ": Using plugin '" + plugin.getDescription().getName() + "' for permissions."); + logger.info(name + ": " + permissionResolver.getDetectionMessage()); } public void load() { @@ -136,6 +143,7 @@ public class PermissionsResolverManager implements PermissionsResolver { return permissionResolver.getGroups(player); } + @SuppressWarnings("unchecked") private boolean loadConfig(File file) { boolean isUpdated = false; if (!file.exists()) { @@ -145,29 +153,76 @@ public class PermissionsResolverManager implements PermissionsResolver { e.printStackTrace(); } } - config = new Configuration(file); - config.load(); - List keys = config.getKeys(); - config.setHeader(CONFIG_HEADER); - if (!keys.contains("dinnerperms")) { - config.setProperty("dinnerperms", config.getBoolean("dinner-perms", true)); - isUpdated = true; - } + config = YamlConfiguration.loadConfiguration(file); + ((YamlConfigurationOptions)config.options()).indent(4); + Set keys = config.getKeys(true); + config.options().header(CONFIG_HEADER); + if (!keys.contains("ignore-nijiperms-bridges")) { - config.setProperty("ignore-nijiperms-bridges", true); + config.set("ignore-nijiperms-bridges", true); isUpdated = true; } - if (keys.contains("dinner-perms")) { - config.removeProperty("dinner-perms"); + + if (!keys.contains("resolvers.enabled")) { + List resolvers = new ArrayList(); + for (Class clazz : availableResolvers) { + resolvers.add(clazz.getSimpleName()); + } + enabledResolvers.addAll(Arrays.asList(availableResolvers)); + config.set("resolvers.enabled", resolvers); + isUpdated = true; + } else { + List disabledResolvers = config.getList("resolvers.disabled", new ArrayList()); + List stagedEnabled = config.getList("resolvers.enabled"); + for (Iterator i = stagedEnabled.iterator(); i.hasNext();) { + String nextName = i.next(); + Class next = null; + try { + next = Class.forName(getClass().getPackage().getName() + "." + nextName); + } catch (ClassNotFoundException e) {} + + if (next == null || !PermissionsResolver.class.isAssignableFrom(next)) { + logger.warning("WEPIF: Invalid or unknown class found in enabled resolvers: " + + nextName + ". Moving to disabled resolvers list."); + i.remove(); + disabledResolvers.add(nextName); + isUpdated = true; + continue; + } + enabledResolvers.add(next.asSubclass(PermissionsResolver.class)); + } + + for (Class clazz : availableResolvers) { + if (!stagedEnabled.contains(clazz.getSimpleName()) && + !disabledResolvers.contains(clazz.getSimpleName())) { + disabledResolvers.add(clazz.getName()); + logger.info("New permissions resolver: " + + clazz.getSimpleName() + " detected. " + + "Added to disabled resolvers list."); + isUpdated = true; + } + } + config.set("resolvers.disabled", disabledResolvers); + config.set("resolvers.enabled", stagedEnabled); + } + + if (keys.contains("dinner-perms") || keys.contains("dinnerperms")) { + config.set("dinner-perms", null); + config.set("dinnerperms", null); isUpdated = true; } if (!keys.contains("permissions")) { - ConfigurationPermissionsResolver.generateDefaultPerms(config); + ConfigurationPermissionsResolver.generateDefaultPerms( + config.createSection("permissions")); isUpdated = true; } if (isUpdated) { logger.info("WEPIF: Updated config file"); - config.save(); + try { + config.save(file); + } catch (IOException e) { + logger.log(Level.SEVERE, "WEPIF: Unable to save config file", e); + } } return isUpdated; } diff --git a/src/main/java/com/sk89q/bukkit/migration/PluginPermissionsResolver.java b/src/main/java/com/sk89q/bukkit/migration/PluginPermissionsResolver.java index 10a957a52..39c983299 100644 --- a/src/main/java/com/sk89q/bukkit/migration/PluginPermissionsResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/PluginPermissionsResolver.java @@ -22,7 +22,7 @@ package com.sk89q.bukkit.migration; import org.bukkit.Server; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.RegisteredServiceProvider; -import org.bukkit.util.config.Configuration; +import org.bukkit.configuration.Configuration; public class PluginPermissionsResolver implements PermissionsResolver {