Better handling of errors in user and group yml's.

This commit is contained in:
ElgarL 2012-04-07 19:24:46 +01:00
parent e8dd963545
commit e8a42e3947
2 changed files with 232 additions and 178 deletions

View file

@ -167,4 +167,5 @@ v 2.0:
- Don't throw errors when attempting to remove permission attachments (bukkit will have already removed it). - Don't throw errors when attempting to remove permission attachments (bukkit will have already removed it).
- Remove all permission attachments when performing a manload or restart. - Remove all permission attachments when performing a manload or restart.
- Expand 'manwhois' to also list a users subgroups. - Expand 'manwhois' to also list a users subgroups.
- Fix a concurrent modification error when removing all attachments. - Fix a concurrent modification error when removing all attachments.
- Better handling of errors in user and group yml's.

View file

@ -14,6 +14,7 @@ import java.io.UnsupportedEncodingException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@ -459,98 +460,140 @@ public class WorldDataHolder {
//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>>();
try { try {
/*
* Fetch all child nodes under the 'groups' entry.
*/
Map<String, Object> allGroupsNode = (Map<String, Object>) groupsRootDataNode.get("groups"); Map<String, Object> allGroupsNode = (Map<String, Object>) groupsRootDataNode.get("groups");
try { Iterator<String> groupItr = allGroupsNode.keySet().iterator();
for (String groupKey : allGroupsNode.keySet()) { String groupKey;
Map<String, Object> thisGroupNode = (Map<String, Object>) allGroupsNode.get(groupKey); Integer groupCount = 0;
Group thisGrp = ph.createGroup(groupKey);
if (thisGrp == null) { /*
throw new IllegalArgumentException("I think this Group was declared more than once: " + groupKey + " in file: " + groupsFile.getPath()); * loop each group entry
} * and read it's data.
if (thisGroupNode.get("default") == null) { */
thisGroupNode.put("default", false); while (groupItr.hasNext()) {
} try {
if ((Boolean.parseBoolean(thisGroupNode.get("default").toString()))) { groupCount++;
if (ph.getDefaultGroup() != null) { // Attempt to fetch the next group name.
GroupManager.logger.warning("The group " + thisGrp.getName() + " is claiming to be default where" + ph.getDefaultGroup().getName() + " already was."); groupKey = groupItr.next();
GroupManager.logger.warning("Overriding first request for file: " + groupsFile.getPath()); } catch (Exception e) {
} throw new IllegalArgumentException("Invalid node type for group entry (" + groupCount + ") in file: " + groupsFile.getPath());
ph.setDefaultGroup(thisGrp); }
}
/*
//PERMISSIONS NODE * Fetch this groups child nodes
try { */
if (thisGroupNode.get("permissions") == null) { Map<String, Object> thisGroupNode = (Map<String, Object>) allGroupsNode.get(groupKey);
thisGroupNode.put("permissions", new ArrayList<String>()); /*
* Create a new group with this name
* in the assigned data source.
*/
Group thisGrp = ph.createGroup(groupKey);
if (thisGrp == null) {
throw new IllegalArgumentException("I think this Group was declared more than once: " + groupKey + " in file: " + groupsFile.getPath());
}
/*
* If no default node is found set it as false.
*/
if (thisGroupNode.get("default") == null) {
thisGroupNode.put("default", false);
} else if ((Boolean.parseBoolean(thisGroupNode.get("default").toString()))) {
/*
* Set this as the default group.
* Warn if some other group has already claimed that position.
*/
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("Overriding first request for file: " + groupsFile.getPath());
}
ph.setDefaultGroup(thisGrp);
}
//PERMISSIONS NODE
try {
/*
* If no permissions node is found, or it's empty
* set an empty permission list
*/
if (thisGroupNode.get("permissions") == null) {
thisGroupNode.put("permissions", new ArrayList<String>());
} else {
/*
* There is a permission list Which seems to hold some data
*/
if (thisGroupNode.get("permissions") instanceof List) {
/*
* Check each entry and add it as a new permission.
*/
for (Object o : ((List) thisGroupNode.get("permissions"))) {
try {
/*
* Only add this permission if it's not empty.
*/
if (!thisGroupNode.get("permissions").toString().isEmpty())
thisGrp.addPermission(o.toString());
} catch (NullPointerException e) {
// Ignore this entry as it's null. It can be safely dropped
}
}
} else if (thisGroupNode.get("permissions") instanceof String) {
/*
* Only add this permission if it's not empty.
*/
if (!thisGroupNode.get("permissions").toString().isEmpty())
thisGrp.addPermission((String) thisGroupNode.get("permissions"));
} else { } else {
if (thisGroupNode.get("permissions") instanceof List) { throw new IllegalArgumentException("Unknown type of permissions node(Should be String or List<String>) for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
for (Object o : ((List) thisGroupNode.get("permissions"))) {
try {
/*
* Only add this permission if it's not empty.
*/
if (!thisGroupNode.get("permissions").toString().isEmpty())
thisGrp.addPermission(o.toString());
} catch (NullPointerException e) {
// Ignore this entry as it's null.
//throw new IllegalArgumentException("Invalid permission node in group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
}
}
} else if (thisGroupNode.get("permissions") instanceof String) {
/*
* Only add this permission if it's not empty.
*/
if (!thisGroupNode.get("permissions").toString().isEmpty())
thisGrp.addPermission((String) thisGroupNode.get("permissions"));
} else {
throw new IllegalArgumentException("Unknown type of permissions node(Should be String or List<String>) for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
}
thisGrp.sortPermissions();
} }
} catch (Exception e) { /*
throw new IllegalArgumentException("Invalid formatting found in permissions section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath()); * Sort all permissions so they are in the correct order for checking.
*/
thisGrp.sortPermissions();
} }
} catch (Exception e) {
//INFO NODE throw new IllegalArgumentException("Invalid formatting found in permissions section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
try { }
if (thisGroupNode.get("info") instanceof Map) {
Map<String, Object> infoNode = (Map<String, Object>) thisGroupNode.get("info"); //INFO NODE
if (infoNode != null) { try {
thisGrp.setVariables(infoNode); if (thisGroupNode.get("info") instanceof Map) {
} Map<String, Object> infoNode = (Map<String, Object>) thisGroupNode.get("info");
} else if (infoNode != null) {
throw new IllegalArgumentException("Unknown entry found in Info section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath()); thisGrp.setVariables(infoNode);
} catch (Exception e1) { }
throw new IllegalArgumentException("Invalid formatting found in info section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath()); } else
} throw new IllegalArgumentException("Unknown entry found in Info section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
} catch (Exception e1) {
//END INFO NODE throw new IllegalArgumentException("Invalid formatting found in info section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
}
try {
if (thisGroupNode.get("inheritance") == null || thisGroupNode.get("inheritance") instanceof List) { //END INFO NODE
Object inheritNode = thisGroupNode.get("inheritance");
if (inheritNode == null) { try {
thisGroupNode.put("inheritance", new ArrayList<String>()); if (thisGroupNode.get("inheritance") == null || thisGroupNode.get("inheritance") instanceof List) {
} else if (inheritNode instanceof List) { Object inheritNode = thisGroupNode.get("inheritance");
List<String> groupsInh = (List<String>) inheritNode; if (inheritNode == null) {
for (String grp : groupsInh) { thisGroupNode.put("inheritance", new ArrayList<String>());
if (inheritance.get(groupKey) == null) { } else if (inheritNode instanceof List) {
List<String> thisInherits = new ArrayList<String>(); List<String> groupsInh = (List<String>) inheritNode;
inheritance.put(groupKey, thisInherits); for (String grp : groupsInh) {
} if (inheritance.get(groupKey) == null) {
inheritance.get(groupKey).add(grp); List<String> thisInherits = new ArrayList<String>();
inheritance.put(groupKey, thisInherits);
} }
} 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 e2) { }
throw new IllegalArgumentException("Invalid formatting found in inheritance section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath()); }else
} throw new IllegalArgumentException("Unknown entry found in inheritance section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
} } catch (Exception e2) {
} catch (Exception e) { throw new IllegalArgumentException("Invalid formatting found in inheritance section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
throw new IllegalArgumentException("Invalid node type, or bad indentation in file: " + groupsFile.getPath()); }
} }
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
throw new IllegalArgumentException("Your " + groupsFile.getPath() + " file is invalid. See console for details."); throw new IllegalArgumentException("Your " + groupsFile.getPath() + " file is invalid. See console for details.");
@ -614,97 +657,107 @@ public class WorldDataHolder {
Map<String, Object> allUsersNode = (Map<String, Object>) usersRootDataNode.get("users"); Map<String, Object> allUsersNode = (Map<String, Object>) usersRootDataNode.get("users");
// Load users if the file is NOT empty // Load users if the file is NOT empty
if (allUsersNode != null) if (allUsersNode != null) {
try {
for (String usersKey : allUsersNode.keySet()) { Iterator<String> usersItr = allUsersNode.keySet().iterator();
Map<String, Object> thisUserNode = null; String usersKey;
try { Integer userCount = 0;
thisUserNode = (Map<String, Object>) allUsersNode.get(usersKey);
} catch (Exception ex) { while (usersItr.hasNext()) {
throw new IllegalArgumentException("Bad format found in file: " + usersFile.getPath()); try {
} userCount++;
User thisUser = ph.createUser(usersKey); // Attempt to fetch the next user name.
if (thisUser == null) { usersKey = usersItr.next();
throw new IllegalArgumentException("I think this user was declared more than once: " + usersKey + " in file: " + usersFile.getPath()); } catch (Exception e) {
} throw new IllegalArgumentException("Invalid node type for user entry (" + userCount + ") in file: " + usersFile.getPath());
if (thisUserNode.get("permissions") == null) { }
thisUserNode.put("permissions", new ArrayList<String>());
} else { Map<String, Object> thisUserNode = null;
if (thisUserNode.get("permissions") instanceof List) { try {
for (Object o : ((List) thisUserNode.get("permissions"))) { thisUserNode = (Map<String, Object>) allUsersNode.get(usersKey);
/* } catch (Exception ex) {
* Only add this permission if it's not empty throw new IllegalArgumentException("Bad format found in file: " + usersFile.getPath());
*/ }
if (!o.toString().isEmpty()) User thisUser = ph.createUser(usersKey);
thisUser.addPermission(o.toString()); if (thisUser == null) {
} throw new IllegalArgumentException("I think this user was declared more than once: " + usersKey + " in file: " + usersFile.getPath());
} else if (thisUserNode.get("permissions") instanceof String) { }
try { if (thisUserNode.get("permissions") == null) {
/* thisUserNode.put("permissions", new ArrayList<String>());
* Only add this permission if it's not empty } else {
*/ if (thisUserNode.get("permissions") instanceof List) {
if (!thisUserNode.get("permissions").toString().isEmpty()) for (Object o : ((List) thisUserNode.get("permissions"))) {
thisUser.addPermission(thisUserNode.get("permissions").toString()); /*
} catch (NullPointerException e) { * Only add this permission if it's not empty
// Ignore this entry as it's null. */
//throw new IllegalArgumentException("Invalid permission node for user: " + thisUser.getName() + " in file: " + UserFile.getPath()); if (!o.toString().isEmpty())
} thisUser.addPermission(o.toString());
}
thisUser.sortPermissions();
}
//SUBGROUPS LOADING
if (thisUserNode.get("subgroups") == null) {
thisUserNode.put("subgroups", new ArrayList<String>());
}
if (thisUserNode.get("subgroups") instanceof List) {
for (Object o : ((List) thisUserNode.get("subgroups"))) {
Group subGrp = ph.getGroup(o.toString());
if (subGrp != null) {
thisUser.addSubGroup(subGrp);
} else {
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) {
Group subGrp = ph.getGroup(thisUserNode.get("subgroups").toString());
if (subGrp != null) {
thisUser.addSubGroup(subGrp);
} else {
GroupManager.logger.warning("Subgroup " + thisUserNode.get("subgroups").toString() + " not found for user " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath());
} }
} else if (thisUserNode.get("permissions") instanceof String) {
try {
/*
* Only add this permission if it's not empty
*/
if (!thisUserNode.get("permissions").toString().isEmpty())
thisUser.addPermission(thisUserNode.get("permissions").toString());
} catch (NullPointerException e) {
// Ignore this entry as it's null.
//throw new IllegalArgumentException("Invalid permission node for user: " + thisUser.getName() + " in file: " + UserFile.getPath());
}
} }
thisUser.sortPermissions();
}
//USER INFO NODE
//SUBGROUPS LOADING
//INFO NODE if (thisUserNode.get("subgroups") == null) {
if (thisUserNode.get("info") instanceof Map) { thisUserNode.put("subgroups", new ArrayList<String>());
Map<String, Object> infoNode = (Map<String, Object>) thisUserNode.get("info"); }
if (infoNode != null) { if (thisUserNode.get("subgroups") instanceof List) {
thisUser.setVariables(infoNode); for (Object o : ((List) thisUserNode.get("subgroups"))) {
} Group subGrp = ph.getGroup(o.toString());
} else if (thisUserNode.get("info") != null) if (subGrp != null) {
throw new IllegalArgumentException("Unknown entry found in Info section for user: " + thisUser.getName() + " in file: " + usersFile.getPath()); thisUser.addSubGroup(subGrp);
} else {
//END INFO NODE GroupManager.logger.warning("Subgroup " + o.toString() + " not found for user " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath());
}
}
if (thisUserNode.get("group") != null) { } else if (thisUserNode.get("subgroups") instanceof String) {
Group hisGroup = ph.getGroup(thisUserNode.get("group").toString()); Group subGrp = ph.getGroup(thisUserNode.get("subgroups").toString());
if (hisGroup == null) { if (subGrp != 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()); thisUser.addSubGroup(subGrp);
hisGroup = ph.getDefaultGroup(); } else {
//throw new IllegalArgumentException("There is no group " + thisUserNode.get("group").toString() + ", as stated for player " + thisUser.getName()); GroupManager.logger.warning("Subgroup " + thisUserNode.get("subgroups").toString() + " not found for user " + thisUser.getName() + ". Ignoring entry in file: " + usersFile.getPath());
} }
thisUser.setGroup(hisGroup); }
} else {
thisUser.setGroup(ph.getDefaultGroup());
} //USER INFO NODE
}
} catch (Exception e) { //INFO NODE
throw new IllegalArgumentException("Invalid node type, or bad indentation in file: " + usersFile.getPath()); if (thisUserNode.get("info") instanceof Map) {
} Map<String, Object> infoNode = (Map<String, Object>) thisUserNode.get("info");
if (infoNode != null) {
thisUser.setVariables(infoNode);
}
} else if (thisUserNode.get("info") != null)
throw new IllegalArgumentException("Unknown entry found in Info section for user: " + thisUser.getName() + " in file: " + usersFile.getPath());
//END INFO NODE
if (thisUserNode.get("group") != null) {
Group hisGroup = ph.getGroup(thisUserNode.get("group").toString());
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());
hisGroup = ph.getDefaultGroup();
//throw new IllegalArgumentException("There is no group " + thisUserNode.get("group").toString() + ", as stated for player " + thisUser.getName());
}
thisUser.setGroup(hisGroup);
} else {
thisUser.setGroup(ph.getDefaultGroup());
}
}
}
ph.removeUsersChangedFlag(); ph.removeUsersChangedFlag();
// Update the LastModified time. // Update the LastModified time.