1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-14 02:24:16 +02:00

Fixed the 'last' nested throw/catch and rework all user and group

reading to be certain to trap all errors.
This commit is contained in:
ElgarL
2012-04-13 13:58:05 +01:00
parent f2d13677bd
commit 6b33504c09
2 changed files with 194 additions and 103 deletions

View File

@@ -142,6 +142,9 @@ public class GlobalGroups {
throw new IllegalArgumentException("Invalid group name for GlobalGroup entry (" + groupCount + ") in file: " + GlobalGroupsFile.getPath(), ex); throw new IllegalArgumentException("Invalid group name for GlobalGroup entry (" + groupCount + ") in file: " + GlobalGroupsFile.getPath(), ex);
} }
/*
* Create a new group with this name.
*/
Group newGroup = new Group(groupName.toLowerCase()); Group newGroup = new Group(groupName.toLowerCase());
Object element; Object element;

View File

@@ -439,9 +439,11 @@ public class WorldDataHolder {
@SuppressWarnings({"rawtypes", "unchecked"}) @SuppressWarnings({"rawtypes", "unchecked"})
protected static void loadGroups(WorldDataHolder ph, File groupsFile) throws FileNotFoundException, IOException { protected static void loadGroups(WorldDataHolder ph, File groupsFile) throws FileNotFoundException, IOException {
//READ GROUPS FILE // READ GROUPS FILE
Yaml yamlGroups = new Yaml(new SafeConstructor()); Yaml yamlGroups = new Yaml(new SafeConstructor());
Map<String, Object> groupsRootDataNode; Map<String, Object> groupsRootDataNode;
if (!groupsFile.exists()) { if (!groupsFile.exists()) {
throw new IllegalArgumentException("The file which should contain groups does not exist!\n" + groupsFile.getPath()); throw new IllegalArgumentException("The file which should contain groups does not exist!\n" + groupsFile.getPath());
} }
@@ -457,18 +459,17 @@ public class WorldDataHolder {
groupsInputStream.close(); groupsInputStream.close();
} }
//PROCESS GROUPS FILE // PROCESS GROUPS FILE
Map<String, List<String>> inheritance = new HashMap<String, List<String>>(); Map<String, List<String>> inheritance = new HashMap<String, List<String>>();
Map<String, Object> allGroupsNode = null;
/* /*
* Fetch all child nodes under the 'groups' entry. * Fetch all groups under the 'groups' entry.
*/ */
Map<String, Object> allGroupsNode = new HashMap<String, Object>();
try { try {
allGroupsNode = (Map<String, Object>) groupsRootDataNode.get("groups"); allGroupsNode = (Map<String, Object>) groupsRootDataNode.get("groups");
} catch (Exception ex) { } catch (Exception ex) {
//ex.printStackTrace();
throw new IllegalArgumentException("Your " + groupsFile.getPath() + " file is invalid. See console for details.", ex); throw new IllegalArgumentException("Your " + groupsFile.getPath() + " file is invalid. See console for details.", ex);
} }
@@ -479,9 +480,10 @@ public class WorldDataHolder {
/* /*
* loop each group entry * loop each group entry
* and read it's data. * and process it's data.
*/ */
while (groupItr.hasNext()) { while (groupItr.hasNext()) {
try { try {
groupCount++; groupCount++;
// Attempt to fetch the next group name. // Attempt to fetch the next group name.
@@ -493,7 +495,7 @@ public class WorldDataHolder {
/* /*
* Fetch this groups child nodes * Fetch this groups child nodes
*/ */
Map<String, Object> thisGroupNode = new HashMap<String, Object>(); Map<String, Object> thisGroupNode = null;
try { try {
thisGroupNode = (Map<String, Object>) allGroupsNode.get(groupKey); thisGroupNode = (Map<String, Object>) allGroupsNode.get(groupKey);
@@ -511,62 +513,80 @@ public class WorldDataHolder {
throw new IllegalArgumentException("I think this Group was declared more than once: " + groupKey + " in file: " + groupsFile.getPath()); throw new IllegalArgumentException("I think this Group was declared more than once: " + groupKey + " in file: " + groupsFile.getPath());
} }
/* // DEFAULT NODE
* If no default node is found set it as false.
*/ Object nodeData = null;
if (thisGroupNode.get("default") == null) { try {
thisGroupNode.put("default", false); nodeData = thisGroupNode.get("default");
} else if ((Boolean.parseBoolean(thisGroupNode.get("default").toString()))) { } catch (Exception ex) {
throw new IllegalArgumentException("Bad format found in 'permissions' for group: " + groupKey + " in file: " + groupsFile.getPath());
}
if (nodeData == null) {
/*
* If no 'default' node is found do nothing.
*/
} else if ((Boolean.parseBoolean(nodeData.toString()))) {
/* /*
* Set this as the default group. * Set this as the default group.
* Warn if some other group has already claimed that position. * Warn if some other group has already claimed that position.
*/ */
if (ph.getDefaultGroup() != null) { if (ph.getDefaultGroup() != null) {
GroupManager.logger.warning("The group " + thisGrp.getName() + " is claiming to be default where" + ph.getDefaultGroup().getName() + " already was."); GroupManager.logger.warning("The group '" + thisGrp.getName() + "' is claiming to be default where '" + ph.getDefaultGroup().getName() + "' already was.");
GroupManager.logger.warning("Overriding first request for file: " + groupsFile.getPath()); GroupManager.logger.warning("Overriding first default request in file: " + groupsFile.getPath());
} }
ph.setDefaultGroup(thisGrp); ph.setDefaultGroup(thisGrp);
} }
//PERMISSIONS NODE // PERMISSIONS NODE
nodeData = null;
try {
nodeData = thisGroupNode.get("permissions");
} catch (Exception ex) {
throw new IllegalArgumentException("Bad format found in 'permissions' for '" + groupKey + "' in file: " + groupsFile.getPath());
}
/* if (nodeData == null) {
* If no permissions node is found, or it's empty /*
* set an empty permission list * If no permissions node is found, or it's empty
*/ * do nothing.
if (thisGroupNode.get("permissions") == null) { */
thisGroupNode.put("permissions", new ArrayList<String>());
} else { } else {
/* /*
* There is a permission list Which seems to hold some data * There is a permission list Which seems to hold some data
*/ */
if (thisGroupNode.get("permissions") instanceof List) { if (nodeData instanceof List) {
/* /*
* Check each entry and add it as a new permission. * Check each entry and add it as a new permission.
*/ */
for (Object o : ((List) thisGroupNode.get("permissions"))) { try {
try { for (Object o : ((List) nodeData)) {
/* try {
* Only add this permission if it's not empty. /*
*/ * Only add this permission if it's not empty.
if (!thisGroupNode.get("permissions").toString().isEmpty()) */
thisGrp.addPermission(o.toString()); if (!o.toString().isEmpty())
thisGrp.addPermission(o.toString());
} catch (NullPointerException ex) {
// Ignore this entry as it's null. It can be safely dropped } catch (NullPointerException ex) {
} catch (Exception ex) { // Ignore this entry as it's null. It can be safely dropped
throw new IllegalArgumentException("Invalid formatting found in permissions section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath(), ex); }
} }
} catch (Exception ex) {
throw new IllegalArgumentException("Invalid formatting found in 'permissions' section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath(), ex);
} }
} else if (thisGroupNode.get("permissions") instanceof String) {
} else if (nodeData instanceof String) {
/* /*
* Only add this permission if it's not empty. * Only add this permission if it's not empty.
*/ */
if (!thisGroupNode.get("permissions").toString().isEmpty()) if (!nodeData.toString().isEmpty())
thisGrp.addPermission((String) thisGroupNode.get("permissions")); thisGrp.addPermission((String) nodeData);
} else { } else {
throw new IllegalArgumentException("Unknown type of permissions node(Should be String or List<String>) for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath()); throw new IllegalArgumentException("Unknown type of 'permissions' node(Should be String or List<String>) for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
} }
/* /*
* Sort all permissions so they are in the correct order for checking. * Sort all permissions so they are in the correct order for checking.
@@ -574,44 +594,70 @@ public class WorldDataHolder {
thisGrp.sortPermissions(); thisGrp.sortPermissions();
} }
//INFO NODE // INFO NODE
try {
if (thisGroupNode.get("info") instanceof Map) { nodeData = null;
Map<String, Object> infoNode = (Map<String, Object>) thisGroupNode.get("info"); try {
if (infoNode != null) { nodeData = thisGroupNode.get("info");
thisGrp.setVariables(infoNode); } catch (Exception ex) {
} throw new IllegalArgumentException("Bad format found in 'info' section for group: " + groupKey + " in file: " + groupsFile.getPath());
} else
throw new IllegalArgumentException("Unknown entry found in Info section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
} catch (Exception ex) {
throw new IllegalArgumentException("Invalid formatting found in info section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath(), ex);
} }
//END INFO NODE if (nodeData == null) {
/*
* No info section was found, so leave all variables as defaults.
*/
GroupManager.logger.warning("The group '" + thisGrp.getName() + "' has no 'info' section!");
GroupManager.logger.warning("Using default values: " + groupsFile.getPath());
} else if (nodeData instanceof Map) {
try {
if (nodeData != null) {
thisGrp.setVariables((Map<String, Object>) nodeData);
}
} catch (Exception ex) {
throw new IllegalArgumentException("Invalid formatting found in 'info' section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath(), ex);
}
} else
throw new IllegalArgumentException("Unknown entry found in 'info' section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
try { // INHERITANCE NODE
if (thisGroupNode.get("inheritance") == null || thisGroupNode.get("inheritance") instanceof List) {
Object inheritNode = thisGroupNode.get("inheritance"); nodeData = null;
if (inheritNode == null) { try {
thisGroupNode.put("inheritance", new ArrayList<String>()); nodeData = thisGroupNode.get("inheritance");
} else if (inheritNode instanceof List) { } catch (Exception ex) {
List<String> groupsInh = (List<String>) inheritNode; throw new IllegalArgumentException("Bad format found in 'inheritance' section for group: " + groupKey + " in file: " + groupsFile.getPath());
for (String grp : groupsInh) { }
if (nodeData == null || nodeData instanceof List) {
if (nodeData == null) {
/*
* If no inheritance node is found, or it's empty
* do nothing.
*/
} else if (nodeData instanceof List) {
try {
for (String grp : (List<String>) nodeData) {
if (inheritance.get(groupKey) == null) { if (inheritance.get(groupKey) == null) {
List<String> thisInherits = new ArrayList<String>(); inheritance.put(groupKey, new ArrayList<String>());
inheritance.put(groupKey, thisInherits);
} }
inheritance.get(groupKey).add(grp); inheritance.get(groupKey).add(grp);
} }
}
}else
throw new IllegalArgumentException("Unknown entry found in inheritance section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
} catch (Exception ex) {
throw new IllegalArgumentException("Invalid formatting found in inheritance section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath(), ex);
}
}
} catch (Exception ex) {
throw new IllegalArgumentException("Invalid formatting found in 'inheritance' section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath(), ex);
}
}
}else
throw new IllegalArgumentException("Unknown entry found in 'inheritance' section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
// END GROUP
}
if (ph.getDefaultGroup() == null) { if (ph.getDefaultGroup() == null) {
throw new IllegalArgumentException("There was no Default Group declared in file: " + groupsFile.getPath()); throw new IllegalArgumentException("There was no Default Group declared in file: " + groupsFile.getPath());
@@ -655,7 +701,7 @@ public class WorldDataHolder {
@SuppressWarnings({"rawtypes", "unchecked"}) @SuppressWarnings({"rawtypes", "unchecked"})
protected static void loadUsers(WorldDataHolder ph, File usersFile) throws FileNotFoundException, IOException { protected static void loadUsers(WorldDataHolder ph, File usersFile) throws FileNotFoundException, IOException {
//READ USERS FILE // READ USERS FILE
Yaml yamlUsers = new Yaml(new SafeConstructor()); Yaml yamlUsers = new Yaml(new SafeConstructor());
Map<String, Object> usersRootDataNode; Map<String, Object> usersRootDataNode;
if (!usersFile.exists()) { if (!usersFile.exists()) {
@@ -674,7 +720,8 @@ public class WorldDataHolder {
} }
// PROCESS USERS FILE // PROCESS USERS FILE
Map<String, Object> allUsersNode = new HashMap<String, Object>();
Map<String, Object> allUsersNode = null;
/* /*
* Fetch all child nodes under the 'users' entry. * Fetch all child nodes under the 'users' entry.
@@ -682,11 +729,11 @@ public class WorldDataHolder {
try { try {
allUsersNode = (Map<String, Object>) usersRootDataNode.get("users"); allUsersNode = (Map<String, Object>) usersRootDataNode.get("users");
} catch (Exception ex) { } catch (Exception ex) {
//ex.printStackTrace();
throw new IllegalArgumentException("Your " + usersFile.getPath() + " file is invalid. See console for details.", ex); throw new IllegalArgumentException("Your " + usersFile.getPath() + " file is invalid. See console for details.", ex);
} }
// Load users if the file is NOT empty // Load users if the file is NOT empty
if (allUsersNode != null) { if (allUsersNode != null) {
Iterator<String> usersItr = allUsersNode.keySet().iterator(); Iterator<String> usersItr = allUsersNode.keySet().iterator();
@@ -706,30 +753,44 @@ public class WorldDataHolder {
try { try {
thisUserNode = (Map<String, Object>) allUsersNode.get(usersKey); thisUserNode = (Map<String, Object>) allUsersNode.get(usersKey);
} catch (Exception ex) { } catch (Exception ex) {
throw new IllegalArgumentException("Bad format found in file: " + usersFile.getPath()); throw new IllegalArgumentException("Bad format found for user: " + usersKey + " in file: " + usersFile.getPath());
} }
User thisUser = ph.createUser(usersKey); User thisUser = ph.createUser(usersKey);
if (thisUser == null) { if (thisUser == null) {
throw new IllegalArgumentException("I think this user was declared more than once: " + usersKey + " in file: " + usersFile.getPath()); throw new IllegalArgumentException("I think this user was declared more than once: " + usersKey + " in file: " + usersFile.getPath());
} }
if (thisUserNode.get("permissions") == null) {
thisUserNode.put("permissions", new ArrayList<String>()); // USER PERMISSIONS NODES
Object nodeData = null;
try {
nodeData = thisUserNode.get("permissions");
} catch (Exception ex) {
throw new IllegalArgumentException("Bad format found in 'permissions' for user: " + usersKey + " in file: " + usersFile.getPath());
}
if (nodeData == null) {
/*
* If no permissions node is found, or it's empty
* do nothing.
*/
} else { } else {
if (thisUserNode.get("permissions") instanceof List) { if (nodeData instanceof List) {
for (Object o : ((List) thisUserNode.get("permissions"))) { for (Object o : ((List) nodeData)) {
/* /*
* Only add this permission if it's not empty * Only add this permission if it's not empty
*/ */
if (!o.toString().isEmpty()) if (!o.toString().isEmpty())
thisUser.addPermission(o.toString()); thisUser.addPermission(o.toString());
} }
} else if (thisUserNode.get("permissions") instanceof String) { } else if (nodeData instanceof String) {
try { try {
/* /*
* Only add this permission if it's not empty * Only add this permission if it's not empty
*/ */
if (!thisUserNode.get("permissions").toString().isEmpty()) if (!nodeData.toString().isEmpty())
thisUser.addPermission(thisUserNode.get("permissions").toString()); thisUser.addPermission(nodeData.toString());
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Ignore this entry as it's null. // Ignore this entry as it's null.
} }
@@ -737,49 +798,76 @@ public class WorldDataHolder {
thisUser.sortPermissions(); thisUser.sortPermissions();
} }
//SUBGROUPS LOADING // SUBGROUPS NODES
if (thisUserNode.get("subgroups") == null) {
thisUserNode.put("subgroups", new ArrayList<String>()); nodeData = null;
try {
nodeData = thisUserNode.get("subgroups");
} catch (Exception ex) {
throw new IllegalArgumentException("Bad format found in 'subgroups' for user: " + usersKey + " in file: " + usersFile.getPath());
} }
if (thisUserNode.get("subgroups") instanceof List) {
for (Object o : ((List) thisUserNode.get("subgroups"))) { if (nodeData == null) {
/*
* If no subgroups node is found, or it's empty
* do nothing.
*/
} else if (nodeData instanceof List) {
for (Object o : ((List) nodeData)) {
Group subGrp = ph.getGroup(o.toString()); Group subGrp = ph.getGroup(o.toString());
if (subGrp != null) { if (subGrp != null) {
thisUser.addSubGroup(subGrp); thisUser.addSubGroup(subGrp);
} else { } else {
GroupManager.logger.warning("Subgroup " + o.toString() + " not found for user " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath()); GroupManager.logger.warning("Subgroup '" + o.toString() + "' not found for user: " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath());
} }
} }
} else if (thisUserNode.get("subgroups") instanceof String) { } else if (nodeData instanceof String) {
Group subGrp = ph.getGroup(thisUserNode.get("subgroups").toString()); Group subGrp = ph.getGroup(nodeData.toString());
if (subGrp != null) { if (subGrp != null) {
thisUser.addSubGroup(subGrp); thisUser.addSubGroup(subGrp);
} else { } else {
GroupManager.logger.warning("Subgroup " + thisUserNode.get("subgroups").toString() + " not found for user " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath()); GroupManager.logger.warning("Subgroup '" + nodeData.toString() + "' not found for user: " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath());
} }
} }
//USER INFO NODE //USER INFO NODE
//INFO NODE nodeData = null;
if (thisUserNode.get("info") instanceof Map) { try {
Map<String, Object> infoNode = (Map<String, Object>) thisUserNode.get("info"); nodeData = thisUserNode.get("info");
if (infoNode != null) { } catch (Exception ex) {
thisUser.setVariables(infoNode); throw new IllegalArgumentException("Bad format found in 'info' section for user: " + usersKey + " in file: " + usersFile.getPath());
} }
} else if (thisUserNode.get("info") != null)
throw new IllegalArgumentException("Unknown entry found in Info section for user: " + thisUser.getName() + " in file: " + usersFile.getPath()); if (nodeData == null) {
/*
* If no info node is found, or it's empty
* do nothing.
*/
} else if (nodeData instanceof Map) {
thisUser.setVariables((Map<String, Object>) nodeData);
} else
throw new IllegalArgumentException("Unknown entry found in 'info' section for user: " + thisUser.getName() + " in file: " + usersFile.getPath());
//END INFO NODE //END INFO NODE
if (thisUserNode.get("group") != null) { // PRIMARY GROUP
Group hisGroup = ph.getGroup(thisUserNode.get("group").toString());
nodeData = null;
try {
nodeData = thisUserNode.get("group");
} catch (Exception ex) {
throw new IllegalArgumentException("Bad format found in 'group' section for user: " + usersKey + " in file: " + usersFile.getPath());
}
if (nodeData != null) {
Group hisGroup = ph.getGroup(nodeData.toString());
if (hisGroup == null) { if (hisGroup == null) {
GroupManager.logger.warning("There is no group " + thisUserNode.get("group").toString() + ", as stated for player " + thisUser.getName() + ": Set to '" + ph.getDefaultGroup().getName() + "' for file: " + usersFile.getPath()); GroupManager.logger.warning("There is no group " + thisUserNode.get("group").toString() + ", as stated for player " + thisUser.getName() + ": Set to '" + ph.getDefaultGroup().getName() + "' for file: " + usersFile.getPath());
hisGroup = ph.getDefaultGroup(); hisGroup = ph.getDefaultGroup();
//throw new IllegalArgumentException("There is no group " + thisUserNode.get("group").toString() + ", as stated for player " + thisUser.getName());
} }
thisUser.setGroup(hisGroup); thisUser.setGroup(hisGroup);
} else { } else {