WEPIF Improvement: code clean out, hardcoding removal and refactoring. Added Service discovery to PluginPermissionsResolver

This commit is contained in:
t3hk0d3 2011-09-27 07:40:18 +04:00 committed by zml2008
parent 102e5f142e
commit 4f80e78a7e
9 changed files with 188 additions and 128 deletions

View File

@ -142,4 +142,9 @@ public class ConfigurationPermissionsResolver implements PermissionsResolver {
return groups.toArray(new String[groups.size()]); return groups.toArray(new String[groups.size()]);
} }
public String getDetectionMessage() {
return "No known permissions plugin detected. Using configuration file for permissions.";
}
} }

View File

@ -25,6 +25,7 @@ import org.bukkit.permissions.PermissionAttachmentInfo;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.util.config.Configuration;
public class DinnerPermsResolver implements PermissionsResolver { public class DinnerPermsResolver implements PermissionsResolver {
@ -35,6 +36,14 @@ public class DinnerPermsResolver implements PermissionsResolver {
this.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() { public void load() {
// Permissions are already loaded // Permissions are already loaded
} }
@ -84,4 +93,10 @@ public class DinnerPermsResolver implements PermissionsResolver {
} }
return groupNames.toArray(new String[groupNames.size()]); return groupNames.toArray(new String[groupNames.size()]);
} }
public String getDetectionMessage() {
return "Using the Bukkit Permissions API.";
}
} }

View File

@ -28,28 +28,49 @@ import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.HashMap; import java.util.HashMap;
import org.bukkit.Server;
import org.bukkit.util.config.Configuration;
public class FlatFilePermissionsResolver implements PermissionsResolver { public class FlatFilePermissionsResolver implements PermissionsResolver {
private Map<String,Set<String>> userPermissionsCache; private Map<String,Set<String>> userPermissionsCache;
private Set<String> defaultPermissionsCache; private Set<String> defaultPermissionsCache;
private Map<String,Set<String>> userGroups; private Map<String,Set<String>> 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() { public static boolean filesExists() {
return (new File("perms_groups.txt")).exists() return (new File("perms_groups.txt")).exists() && (new File("perms_users.txt")).exists();
&& (new File("perms_users.txt")).exists();
} }
public Map<String,Set<String>> loadGroupPermissions() { public Map<String,Set<String>> loadGroupPermissions() {
Map<String,Set<String>> userGroupPermissions = new HashMap<String,Set<String>>(); Map<String,Set<String>> userGroupPermissions = new HashMap<String,Set<String>>();
File file = new File("perms_groups.txt");
FileReader input = null; FileReader input = null;
try { try {
input = new FileReader(file); input = new FileReader(this.groupFile);
BufferedReader buff = new BufferedReader(input); BufferedReader buff = new BufferedReader(input);
String line; String line;
@ -99,11 +120,10 @@ public class FlatFilePermissionsResolver implements PermissionsResolver {
defaultPermissionsCache = userGroupPermissions.get("default"); defaultPermissionsCache = userGroupPermissions.get("default");
} }
File file = new File("perms_users.txt");
FileReader input = null; FileReader input = null;
try { try {
input = new FileReader(file); input = new FileReader(this.userFile);
BufferedReader buff = new BufferedReader(input); BufferedReader buff = new BufferedReader(input);
String line; String line;
@ -191,4 +211,9 @@ public class FlatFilePermissionsResolver implements PermissionsResolver {
return groups.toArray(new String[groups.size()]); return groups.toArray(new String[groups.size()]);
} }
public String getDetectionMessage() {
return "perms_groups.txt and perms_users.txt detected! Using flat file permissions.";
}
} }

View File

@ -26,34 +26,37 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import com.nijikokun.bukkit.Permissions.Permissions; import com.nijikokun.bukkit.Permissions.Permissions;
import com.sk89q.bukkit.migration.PermissionsResolverManager.MissingPluginException; import org.bukkit.util.config.Configuration;
public class NijiPermissionsResolver implements PermissionsResolver { public class NijiPermissionsResolver implements PermissionsResolver {
private Server server; private Server server;
private Permissions api; 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 void load() {
} }
public NijiPermissionsResolver(Server server, boolean ignoreBridges) public NijiPermissionsResolver(Server server, Permissions plugin) {
throws PluginAccessException, MissingPluginException {
this.server = server; this.server = server;
PluginManager manager = server.getPluginManager(); this.api = plugin;
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();
}
} }
@SuppressWarnings("static-access") @SuppressWarnings("static-access")
@ -129,6 +132,7 @@ public class NijiPermissionsResolver implements PermissionsResolver {
private static final long serialVersionUID = 7044832912491608706L; private static final long serialVersionUID = 7044832912491608706L;
} }
@Deprecated
public static boolean checkRealNijiPerms(boolean ignoreBridges) { public static boolean checkRealNijiPerms(boolean ignoreBridges) {
if (!ignoreBridges) { if (!ignoreBridges) {
return true; return true;
@ -139,4 +143,14 @@ public class NijiPermissionsResolver implements PermissionsResolver {
} }
return permsCommand.getPlugin().getDescription().getName().equals("Permissions"); 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.";
}
} }

View File

@ -25,18 +25,25 @@ import org.bukkit.entity.Player;
import ru.tehkode.permissions.PermissionManager; import ru.tehkode.permissions.PermissionManager;
import ru.tehkode.permissions.PermissionUser; import ru.tehkode.permissions.PermissionUser;
import com.sk89q.bukkit.migration.PermissionsResolverManager.MissingPluginException; import org.bukkit.util.config.Configuration;
public class PermissionsExResolver implements PermissionsResolver { public class PermissionsExResolver implements PermissionsResolver {
private final PermissionManager manager; private final PermissionManager manager;
private final Server server; private final Server server;
public PermissionsExResolver(Server server) throws MissingPluginException { public static PermissionsResolver factory(Server server, Configuration config){
this.server = server; PermissionManager manager = server.getServicesManager().load(PermissionManager.class);
manager = server.getServicesManager().load(PermissionManager.class);
if (manager == null) { if(manager == null){
throw new MissingPluginException(); return null;
} }
return new PermissionsExResolver(server, manager);
}
public PermissionsExResolver(Server server, PermissionManager manager) {
this.server = server;
this.manager = manager;
} }
public void load() { public void load() {
@ -68,4 +75,7 @@ public class PermissionsExResolver implements PermissionsResolver {
return user.getGroupsNames(); return user.getGroupsNames();
} }
public String getDetectionMessage() {
return "PermissionsEx detected! Using PermissionsEx for permissions.";
}
} }

View File

@ -19,10 +19,8 @@
package com.sk89q.bukkit.migration; package com.sk89q.bukkit.migration;
public interface PermissionsResolver { public interface PermissionsResolver extends PermissionsProvider {
public void load(); public void load();
public boolean hasPermission(String name, String permission);
public boolean hasPermission(String worldName, String name, String permission); public String getDetectionMessage();
public boolean inGroup(String player, String group);
public String[] getGroups(String player);
} }

View File

@ -21,6 +21,7 @@ package com.sk89q.bukkit.migration;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List; import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Server; import org.bukkit.Server;
@ -52,12 +53,19 @@ public class PermissionsResolverManager implements PermissionsResolver {
"\r\n"; "\r\n";
private Server server; private Server server;
private PermissionsResolver perms; private PermissionsResolver permissionResolver;
private PermissionsResolverServerListener listener; private PermissionsResolverServerListener listener;
private Configuration permsConfig; private Configuration config;
private String name; private String name;
private Logger logger; private Logger logger;
protected boolean ignoreNijiPermsBridges;
protected Class<? extends PermissionsResolver>[] availableResolvers = new Class[]{
PluginPermissionsResolver.class,
PermissionsExResolver.class,
NijiPermissionsResolver.class,
DinnerPermsResolver.class,
FlatFilePermissionsResolver.class
};
@Deprecated @Deprecated
public PermissionsResolverManager(Configuration config, Server server, String name, Logger logger) { public PermissionsResolverManager(Configuration config, Server server, String name, Logger logger) {
@ -72,73 +80,30 @@ public class PermissionsResolverManager implements PermissionsResolver {
this.server = plugin.getServer(); this.server = plugin.getServer();
this.name = name; this.name = name;
this.logger = logger; this.logger = logger;
new PermissionsResolverServerListener(this, plugin); this.listener = new PermissionsResolverServerListener(this, plugin);
loadConfig(new File("wepif.yml")); loadConfig(new File("wepif.yml"));
findResolver(); findResolver();
} }
public void findResolver() { public void findResolver() {
if (tryPluginPermissionsResolver()) return; for (Class resolverClass : availableResolvers){
if (tryNijiPermissions()) return; try {
if (tryPermissionsEx()) return; Method factoryMethod = resolverClass.getMethod("factory", Server.class, Configuration.class);
if (tryDinnerPerms()) return;
if (tryFlatFilePermissions()) return;
perms = new ConfigurationPermissionsResolver(permsConfig); this.permissionResolver = (PermissionsResolver)factoryMethod.invoke(null, this.server, this.config);
logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions.");
}
private boolean tryNijiPermissions() { if(this.permissionResolver != null){
try { logger.info(name + ": " + this.permissionResolver.getDetectionMessage());
perms = new NijiPermissionsResolver(server, ignoreNijiPermsBridges); return;
logger.info(name + ": Permissions plugin detected! Using Permissions plugin for permissions."); }
return true; } catch (Throwable e) {
} catch (Throwable e) { continue;
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;
} }
} }
return false; permissionResolver = new ConfigurationPermissionsResolver(config);
} logger.info(name + ": No known permissions plugin detected. Using configuration file for permissions.");
private boolean tryDinnerPerms() {
if (!permsConfig.getBoolean("dinnerperms", true)) {
return false;
}
perms = new DinnerPermsResolver(server);
logger.info(name + ": Using the Bukkit Permissions API.");
return true;
} }
public void setPluginPermissionsResolver(Plugin plugin) { public void setPluginPermissionsResolver(Plugin plugin) {
@ -146,30 +111,28 @@ public class PermissionsResolverManager implements PermissionsResolver {
return; return;
} }
perms = new PluginPermissionsResolver( permissionResolver = new PluginPermissionsResolver((PermissionsProvider) plugin, plugin);
(PermissionsProvider) plugin); logger.info(name + ": Using plugin '" + plugin.getDescription().getName() + "' for permissions.");
logger.info(name + ": Using plugin '"
+ plugin.getDescription().getName() + "' for permissions.");
} }
public void load() { public void load() {
perms.load(); permissionResolver.load();
} }
public boolean hasPermission(String name, String permission) { 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) { 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) { public boolean inGroup(String player, String group) {
return perms.inGroup(player, group); return permissionResolver.inGroup(player, group);
} }
public String[] getGroups(String player) { public String[] getGroups(String player) {
return perms.getGroups(player); return permissionResolver.getGroups(player);
} }
private boolean loadConfig(File file) { private boolean loadConfig(File file) {
@ -181,30 +144,30 @@ public class PermissionsResolverManager implements PermissionsResolver {
e.printStackTrace(); e.printStackTrace();
} }
} }
permsConfig = new Configuration(file); config = new Configuration(file);
permsConfig.load(); config.load();
List<String> keys = permsConfig.getKeys(); List<String> keys = config.getKeys();
permsConfig.setHeader(CONFIG_HEADER); config.setHeader(CONFIG_HEADER);
if (!keys.contains("dinnerperms")) { if (!keys.contains("dinnerperms")) {
permsConfig.setProperty("dinnerperms", permsConfig.getBoolean("dinner-perms", true)); config.setProperty("dinnerperms", config.getBoolean("dinner-perms", true));
isUpdated = true; isUpdated = true;
} }
if (!keys.contains("ignore-nijiperms-bridges")) { if (!keys.contains("ignore-nijiperms-bridges")) {
permsConfig.setProperty("ignore-nijiperms-bridges", true); config.setProperty("ignore-nijiperms-bridges", true);
isUpdated = true; isUpdated = true;
} }
ignoreNijiPermsBridges = permsConfig.getBoolean("ignore-nijiperms-bridges", true);
if (keys.contains("dinner-perms")) { if (keys.contains("dinner-perms")) {
permsConfig.removeProperty("dinner-perms"); config.removeProperty("dinner-perms");
isUpdated = true; isUpdated = true;
} }
if (!keys.contains("permissions")) { if (!keys.contains("permissions")) {
ConfigurationPermissionsResolver.generateDefaultPerms(permsConfig); ConfigurationPermissionsResolver.generateDefaultPerms(config);
isUpdated = true; isUpdated = true;
} }
if (isUpdated) { if (isUpdated) {
logger.info("WEPIF: Updated config file"); logger.info("WEPIF: Updated config file");
permsConfig.save(); config.save();
} }
return isUpdated; return isUpdated;
} }
@ -221,4 +184,8 @@ public class PermissionsResolverManager implements PermissionsResolver {
private static final long serialVersionUID = 7044832912491608706L; private static final long serialVersionUID = 7044832912491608706L;
} }
public String getDetectionMessage() {
return "Using WEPIF for permissions";
}
} }

View File

@ -52,9 +52,7 @@ public class PermissionsResolverServerListener extends ServerListener {
String name = plugin.getDescription().getName(); String name = plugin.getDescription().getName();
if (plugin instanceof PermissionsProvider) { if (plugin instanceof PermissionsProvider) {
manager.setPluginPermissionsResolver(plugin); manager.setPluginPermissionsResolver(plugin);
} else if ((name.equalsIgnoreCase("Permissions") && } else if ("Permissions".equals(name) || "PermissionsEx".equals(name)) {
NijiPermissionsResolver.checkRealNijiPerms(manager.ignoreNijiPermsBridges)) ||
name.equalsIgnoreCase("PermissionsEx")) {
manager.findResolver(); manager.findResolver();
manager.load(); manager.load();
} }
@ -70,10 +68,7 @@ public class PermissionsResolverServerListener extends ServerListener {
Plugin plugin = event.getPlugin(); Plugin plugin = event.getPlugin();
String name = plugin.getDescription().getName(); String name = plugin.getDescription().getName();
if (plugin instanceof PermissionsProvider if (plugin instanceof PermissionsProvider || "Permissions".equals(name) || "PermissionsEx".equals(name)) {
|| (name.equalsIgnoreCase("Permissions") &&
NijiPermissionsResolver.checkRealNijiPerms(manager.ignoreNijiPermsBridges)) ||
name.equalsIgnoreCase("PermissionsEx")) {
manager.findResolver(); manager.findResolver();
manager.load(); manager.load();
} }
@ -86,4 +81,6 @@ public class PermissionsResolverServerListener extends ServerListener {
this, Priority.Normal, plugin); this, Priority.Normal, plugin);
manager.setServerListener(this); manager.setServerListener(this);
} }
} }

View File

@ -19,12 +19,37 @@
package com.sk89q.bukkit.migration; 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 { public class PluginPermissionsResolver implements PermissionsResolver {
protected PermissionsProvider resolver; protected PermissionsProvider resolver;
protected Plugin plugin;
public PluginPermissionsResolver(PermissionsProvider resolver) { public static PermissionsResolver factory(Server server, Configuration config){
// Looking for service
RegisteredServiceProvider<PermissionsProvider> 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.resolver = resolver;
this.plugin = permissionsPlugin;
} }
public void load() { public void load() {
@ -46,4 +71,8 @@ public class PluginPermissionsResolver implements PermissionsResolver {
return resolver.getGroups(player); return resolver.getGroups(player);
} }
public String getDetectionMessage() {
return "Using plugin '" + this.plugin.getDescription().getName() + "' for permissions.";
}
} }