From 4f80e78a7e0a2ee3376d583f976f671418547cba Mon Sep 17 00:00:00 2001 From: t3hk0d3 Date: Tue, 27 Sep 2011 07:40:18 +0400 Subject: [PATCH] WEPIF Improvement: code clean out, hardcoding removal and refactoring. Added Service discovery to PluginPermissionsResolver --- .../ConfigurationPermissionsResolver.java | 5 + .../bukkit/migration/DinnerPermsResolver.java | 17 ++- .../FlatFilePermissionsResolver.java | 39 +++++- .../migration/NijiPermissionsResolver.java | 50 ++++--- .../migration/PermissionsExResolver.java | 24 +++- .../bukkit/migration/PermissionsResolver.java | 8 +- .../migration/PermissionsResolverManager.java | 131 +++++++----------- .../PermissionsResolverServerListener.java | 11 +- .../migration/PluginPermissionsResolver.java | 31 ++++- 9 files changed, 188 insertions(+), 128 deletions(-) diff --git a/src/main/java/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java b/src/main/java/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java index e922f71bf..856e9ab72 100644 --- a/src/main/java/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/ConfigurationPermissionsResolver.java @@ -142,4 +142,9 @@ public class ConfigurationPermissionsResolver implements PermissionsResolver { return groups.toArray(new String[groups.size()]); } + + public String getDetectionMessage() { + return "No known permissions plugin detected. Using configuration file for permissions."; + } + } diff --git a/src/main/java/com/sk89q/bukkit/migration/DinnerPermsResolver.java b/src/main/java/com/sk89q/bukkit/migration/DinnerPermsResolver.java index 4eefcd357..16647f9e9 100644 --- a/src/main/java/com/sk89q/bukkit/migration/DinnerPermsResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/DinnerPermsResolver.java @@ -25,6 +25,7 @@ import org.bukkit.permissions.PermissionAttachmentInfo; import java.util.ArrayList; import java.util.List; +import org.bukkit.util.config.Configuration; public class DinnerPermsResolver implements PermissionsResolver { @@ -34,7 +35,15 @@ public class DinnerPermsResolver implements PermissionsResolver { public DinnerPermsResolver(Server server) { this.server = server; } - + + public static PermissionsResolver factory(Server server, Configuration config) { + if(!config.getBoolean("dinnerperms", true)){ + return null; + } + + return new DinnerPermsResolver(server); + } + public void load() { // Permissions are already loaded } @@ -84,4 +93,10 @@ public class DinnerPermsResolver implements PermissionsResolver { } return groupNames.toArray(new String[groupNames.size()]); } + + public String getDetectionMessage() { + return "Using the Bukkit Permissions API."; + } + + } diff --git a/src/main/java/com/sk89q/bukkit/migration/FlatFilePermissionsResolver.java b/src/main/java/com/sk89q/bukkit/migration/FlatFilePermissionsResolver.java index 131cd2cbd..cc7077f7e 100644 --- a/src/main/java/com/sk89q/bukkit/migration/FlatFilePermissionsResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/FlatFilePermissionsResolver.java @@ -28,28 +28,49 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.HashMap; +import org.bukkit.Server; +import org.bukkit.util.config.Configuration; public class FlatFilePermissionsResolver implements PermissionsResolver { private Map> userPermissionsCache; private Set defaultPermissionsCache; private Map> userGroups; - public FlatFilePermissionsResolver() { + protected File groupFile; + protected File userFile; + + public static PermissionsResolver factory(Server server, Configuration config){ + File groups = new File("perms_groups.txt"); + File users = new File("perms_users.txt"); + + if(!groups.exists() || !users.exists()){ + return null; + } + + return new FlatFilePermissionsResolver(groups, users); } + public FlatFilePermissionsResolver(){ + this(new File("perms_groups.txt"), new File("perms_users.txt")); + } + + public FlatFilePermissionsResolver(File groupFile, File userFile) { + this.groupFile = groupFile; + this.userFile = userFile; + } + + @Deprecated public static boolean filesExists() { - return (new File("perms_groups.txt")).exists() - && (new File("perms_users.txt")).exists(); + return (new File("perms_groups.txt")).exists() && (new File("perms_users.txt")).exists(); } public Map> loadGroupPermissions() { Map> userGroupPermissions = new HashMap>(); - File file = new File("perms_groups.txt"); FileReader input = null; try { - input = new FileReader(file); + input = new FileReader(this.groupFile); BufferedReader buff = new BufferedReader(input); String line; @@ -99,11 +120,10 @@ public class FlatFilePermissionsResolver implements PermissionsResolver { defaultPermissionsCache = userGroupPermissions.get("default"); } - File file = new File("perms_users.txt"); FileReader input = null; try { - input = new FileReader(file); + input = new FileReader(this.userFile); BufferedReader buff = new BufferedReader(input); String line; @@ -191,4 +211,9 @@ public class FlatFilePermissionsResolver implements PermissionsResolver { return groups.toArray(new String[groups.size()]); } + + public String getDetectionMessage() { + return "perms_groups.txt and perms_users.txt detected! Using flat file permissions."; + } + } diff --git a/src/main/java/com/sk89q/bukkit/migration/NijiPermissionsResolver.java b/src/main/java/com/sk89q/bukkit/migration/NijiPermissionsResolver.java index 954585b2e..428d082e3 100644 --- a/src/main/java/com/sk89q/bukkit/migration/NijiPermissionsResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/NijiPermissionsResolver.java @@ -26,34 +26,37 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import com.nijikokun.bukkit.Permissions.Permissions; -import com.sk89q.bukkit.migration.PermissionsResolverManager.MissingPluginException; +import org.bukkit.util.config.Configuration; public class NijiPermissionsResolver implements PermissionsResolver { private Server server; private Permissions api; + public static PermissionsResolver factory(Server server, Configuration config) { + PluginManager pluginManager = server.getPluginManager(); + + Plugin plugin = pluginManager.getPlugin("Permissions"); + + // Check if plugin is loaded and have Permissions interface + if(plugin == null || !(plugin instanceof Permissions)){ + return null; + } + + // Check for fake permissions + if(config.getBoolean("ignore-nijiperms-bridges", true) && isFakeNijiPerms(plugin)){ + return null; + } + + return new NijiPermissionsResolver(server, (Permissions)plugin); + } + public void load() { } - public NijiPermissionsResolver(Server server, boolean ignoreBridges) - throws PluginAccessException, MissingPluginException { + public NijiPermissionsResolver(Server server, Permissions plugin) { this.server = server; - PluginManager manager = server.getPluginManager(); - - Plugin plugin = manager.getPlugin("Permissions"); - if (plugin == null) { - throw new MissingPluginException(); - } - if (!checkRealNijiPerms(ignoreBridges)) { - throw new MissingPluginException(); - } - - try { - api = (Permissions)plugin; - } catch (ClassCastException e) { - throw new PluginAccessException(); - } + this.api = plugin; } @SuppressWarnings("static-access") @@ -129,6 +132,7 @@ public class NijiPermissionsResolver implements PermissionsResolver { private static final long serialVersionUID = 7044832912491608706L; } + @Deprecated public static boolean checkRealNijiPerms(boolean ignoreBridges) { if (!ignoreBridges) { return true; @@ -139,4 +143,14 @@ public class NijiPermissionsResolver implements PermissionsResolver { } return permsCommand.getPlugin().getDescription().getName().equals("Permissions"); } + + public static boolean isFakeNijiPerms(Plugin plugin){ + PluginCommand permsCommand = Bukkit.getServer().getPluginCommand("permissions"); + + return !(permsCommand.getPlugin().equals(plugin)); + } + + public String getDetectionMessage() { + return "Permissions plugin detected! Using Permissions plugin for permissions."; + } } diff --git a/src/main/java/com/sk89q/bukkit/migration/PermissionsExResolver.java b/src/main/java/com/sk89q/bukkit/migration/PermissionsExResolver.java index 41c777baa..d7b02a955 100644 --- a/src/main/java/com/sk89q/bukkit/migration/PermissionsExResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/PermissionsExResolver.java @@ -25,18 +25,25 @@ import org.bukkit.entity.Player; import ru.tehkode.permissions.PermissionManager; import ru.tehkode.permissions.PermissionUser; -import com.sk89q.bukkit.migration.PermissionsResolverManager.MissingPluginException; +import org.bukkit.util.config.Configuration; public class PermissionsExResolver implements PermissionsResolver { private final PermissionManager manager; private final Server server; - - public PermissionsExResolver(Server server) throws MissingPluginException { - this.server = server; - manager = server.getServicesManager().load(PermissionManager.class); - if (manager == null) { - throw new MissingPluginException(); + + public static PermissionsResolver factory(Server server, Configuration config){ + PermissionManager manager = server.getServicesManager().load(PermissionManager.class); + + if(manager == null){ + return null; } + + return new PermissionsExResolver(server, manager); + } + + public PermissionsExResolver(Server server, PermissionManager manager) { + this.server = server; + this.manager = manager; } public void load() { @@ -68,4 +75,7 @@ public class PermissionsExResolver implements PermissionsResolver { return user.getGroupsNames(); } + public String getDetectionMessage() { + return "PermissionsEx detected! Using PermissionsEx for permissions."; + } } diff --git a/src/main/java/com/sk89q/bukkit/migration/PermissionsResolver.java b/src/main/java/com/sk89q/bukkit/migration/PermissionsResolver.java index 6544bb01e..7027dc0f4 100644 --- a/src/main/java/com/sk89q/bukkit/migration/PermissionsResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/PermissionsResolver.java @@ -19,10 +19,8 @@ package com.sk89q.bukkit.migration; -public interface PermissionsResolver { +public interface PermissionsResolver extends PermissionsProvider { public void load(); - public boolean hasPermission(String name, String permission); - public boolean hasPermission(String worldName, String name, String permission); - public boolean inGroup(String player, String group); - public String[] getGroups(String player); + + public String getDetectionMessage(); } diff --git a/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverManager.java b/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverManager.java index fdfb2015b..68ebd076a 100644 --- a/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverManager.java +++ b/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverManager.java @@ -21,6 +21,7 @@ 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.logging.Logger; import org.bukkit.Server; @@ -52,12 +53,19 @@ public class PermissionsResolverManager implements PermissionsResolver { "\r\n"; private Server server; - private PermissionsResolver perms; + private PermissionsResolver permissionResolver; private PermissionsResolverServerListener listener; - private Configuration permsConfig; + private Configuration config; private String name; private Logger logger; - protected boolean ignoreNijiPermsBridges; + + protected Class[] availableResolvers = new Class[]{ + PluginPermissionsResolver.class, + PermissionsExResolver.class, + NijiPermissionsResolver.class, + DinnerPermsResolver.class, + FlatFilePermissionsResolver.class + }; @Deprecated public PermissionsResolverManager(Configuration config, Server server, String name, Logger logger) { @@ -72,104 +80,59 @@ public class PermissionsResolverManager implements PermissionsResolver { this.server = plugin.getServer(); this.name = name; this.logger = logger; - new PermissionsResolverServerListener(this, plugin); + this.listener = new PermissionsResolverServerListener(this, plugin); + loadConfig(new File("wepif.yml")); findResolver(); } public void findResolver() { - if (tryPluginPermissionsResolver()) return; - if (tryNijiPermissions()) return; - if (tryPermissionsEx()) return; - if (tryDinnerPerms()) return; - if (tryFlatFilePermissions()) return; - - perms = new ConfigurationPermissionsResolver(permsConfig); - logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions."); - } - - private boolean tryNijiPermissions() { - try { - perms = new NijiPermissionsResolver(server, ignoreNijiPermsBridges); - logger.info(name + ": Permissions plugin detected! Using Permissions plugin for permissions."); - return true; - } catch (Throwable e) { - return false; - } - } - - private boolean tryPermissionsEx() { - try { - perms = new PermissionsExResolver(server); - logger.info(name + ": PermissionsEx detected! Using PermissionsEx for permissions."); - return true; - } catch (Throwable e) { - return false; - } - } - - private boolean tryFlatFilePermissions() { - if (FlatFilePermissionsResolver.filesExists()) { - perms = new FlatFilePermissionsResolver(); - logger.info(name + ": perms_groups.txt and perms_users.txt detected! Using flat file permissions."); - return true; - } - - return false; - } - - private boolean tryPluginPermissionsResolver() { - for (Plugin plugin : server.getPluginManager().getPlugins()) { - if (plugin instanceof PermissionsProvider) { - perms = new PluginPermissionsResolver( - (PermissionsProvider) plugin); - logger.info(name + ": Using plugin '" - + plugin.getDescription().getName() + "' for permissions."); - return true; + for (Class resolverClass : availableResolvers){ + 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; + } + } catch (Throwable e) { + continue; } } - - return false; - } - private boolean tryDinnerPerms() { - if (!permsConfig.getBoolean("dinnerperms", true)) { - return false; - } - perms = new DinnerPermsResolver(server); - logger.info(name + ": Using the Bukkit Permissions API."); - return true; + permissionResolver = new ConfigurationPermissionsResolver(config); + logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions."); } - + public void setPluginPermissionsResolver(Plugin plugin) { if (!(plugin instanceof PermissionsProvider)) { return; } - perms = new PluginPermissionsResolver( - (PermissionsProvider) plugin); - logger.info(name + ": Using plugin '" - + plugin.getDescription().getName() + "' for permissions."); + permissionResolver = new PluginPermissionsResolver((PermissionsProvider) plugin, plugin); + logger.info(name + ": Using plugin '" + plugin.getDescription().getName() + "' for permissions."); } public void load() { - perms.load(); + permissionResolver.load(); } public boolean hasPermission(String name, String permission) { - return perms.hasPermission(name, permission); + return permissionResolver.hasPermission(name, permission); } public boolean hasPermission(String worldName, String name, String permission) { - return perms.hasPermission(worldName, name, permission); + return permissionResolver.hasPermission(worldName, name, permission); } public boolean inGroup(String player, String group) { - return perms.inGroup(player, group); + return permissionResolver.inGroup(player, group); } public String[] getGroups(String player) { - return perms.getGroups(player); + return permissionResolver.getGroups(player); } private boolean loadConfig(File file) { @@ -181,30 +144,30 @@ public class PermissionsResolverManager implements PermissionsResolver { e.printStackTrace(); } } - permsConfig = new Configuration(file); - permsConfig.load(); - List keys = permsConfig.getKeys(); - permsConfig.setHeader(CONFIG_HEADER); + config = new Configuration(file); + config.load(); + List keys = config.getKeys(); + config.setHeader(CONFIG_HEADER); if (!keys.contains("dinnerperms")) { - permsConfig.setProperty("dinnerperms", permsConfig.getBoolean("dinner-perms", true)); + config.setProperty("dinnerperms", config.getBoolean("dinner-perms", true)); isUpdated = true; } if (!keys.contains("ignore-nijiperms-bridges")) { - permsConfig.setProperty("ignore-nijiperms-bridges", true); + config.setProperty("ignore-nijiperms-bridges", true); isUpdated = true; } - ignoreNijiPermsBridges = permsConfig.getBoolean("ignore-nijiperms-bridges", true); + if (keys.contains("dinner-perms")) { - permsConfig.removeProperty("dinner-perms"); + config.removeProperty("dinner-perms"); isUpdated = true; } if (!keys.contains("permissions")) { - ConfigurationPermissionsResolver.generateDefaultPerms(permsConfig); + ConfigurationPermissionsResolver.generateDefaultPerms(config); isUpdated = true; } if (isUpdated) { logger.info("WEPIF: Updated config file"); - permsConfig.save(); + config.save(); } return isUpdated; } @@ -221,4 +184,8 @@ public class PermissionsResolverManager implements PermissionsResolver { private static final long serialVersionUID = 7044832912491608706L; } + public String getDetectionMessage() { + return "Using WEPIF for permissions"; + } + } diff --git a/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java b/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java index 40a532db6..5d88d17e1 100644 --- a/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java +++ b/src/main/java/com/sk89q/bukkit/migration/PermissionsResolverServerListener.java @@ -52,9 +52,7 @@ public class PermissionsResolverServerListener extends ServerListener { String name = plugin.getDescription().getName(); if (plugin instanceof PermissionsProvider) { manager.setPluginPermissionsResolver(plugin); - } else if ((name.equalsIgnoreCase("Permissions") && - NijiPermissionsResolver.checkRealNijiPerms(manager.ignoreNijiPermsBridges)) || - name.equalsIgnoreCase("PermissionsEx")) { + } else if ("Permissions".equals(name) || "PermissionsEx".equals(name)) { manager.findResolver(); manager.load(); } @@ -70,10 +68,7 @@ public class PermissionsResolverServerListener extends ServerListener { Plugin plugin = event.getPlugin(); String name = plugin.getDescription().getName(); - if (plugin instanceof PermissionsProvider - || (name.equalsIgnoreCase("Permissions") && - NijiPermissionsResolver.checkRealNijiPerms(manager.ignoreNijiPermsBridges)) || - name.equalsIgnoreCase("PermissionsEx")) { + if (plugin instanceof PermissionsProvider || "Permissions".equals(name) || "PermissionsEx".equals(name)) { manager.findResolver(); manager.load(); } @@ -86,4 +81,6 @@ public class PermissionsResolverServerListener extends ServerListener { this, Priority.Normal, plugin); manager.setServerListener(this); } + + } diff --git a/src/main/java/com/sk89q/bukkit/migration/PluginPermissionsResolver.java b/src/main/java/com/sk89q/bukkit/migration/PluginPermissionsResolver.java index 602a5ac2f..0bdfcb168 100644 --- a/src/main/java/com/sk89q/bukkit/migration/PluginPermissionsResolver.java +++ b/src/main/java/com/sk89q/bukkit/migration/PluginPermissionsResolver.java @@ -19,12 +19,37 @@ 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; + public class PluginPermissionsResolver implements PermissionsResolver { protected PermissionsProvider resolver; + protected Plugin plugin; - public PluginPermissionsResolver(PermissionsProvider resolver) { + public static PermissionsResolver factory(Server server, Configuration config){ + // Looking for service + RegisteredServiceProvider serviceProvider = server.getServicesManager().getRegistration(PermissionsProvider.class); + + if(serviceProvider != null){ + return new PluginPermissionsResolver(serviceProvider.getProvider(), serviceProvider.getPlugin()); + } + + // Looking for plugin + for (Plugin plugin : server.getPluginManager().getPlugins()) { + if(plugin instanceof PermissionsProvider){ + return new PluginPermissionsResolver((PermissionsProvider)plugin, plugin); + } + } + + return null; + } + + public PluginPermissionsResolver(PermissionsProvider resolver, Plugin permissionsPlugin) { this.resolver = resolver; + this.plugin = permissionsPlugin; } public void load() { @@ -46,4 +71,8 @@ public class PluginPermissionsResolver implements PermissionsResolver { return resolver.getGroups(player); } + public String getDetectionMessage() { + return "Using plugin '" + this.plugin.getDescription().getName() + "' for permissions."; + } + }