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

@ -168,3 +168,4 @@ v 2.0:
- Remove all permission attachments when performing a manload or restart.
- Expand 'manwhois' to also list a users subgroups.
- 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.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
@ -459,18 +460,50 @@ public class WorldDataHolder {
//PROCESS GROUPS FILE
Map<String, List<String>> inheritance = new HashMap<String, List<String>>();
try {
/*
* Fetch all child nodes under the 'groups' entry.
*/
Map<String, Object> allGroupsNode = (Map<String, Object>) groupsRootDataNode.get("groups");
Iterator<String> groupItr = allGroupsNode.keySet().iterator();
String groupKey;
Integer groupCount = 0;
/*
* loop each group entry
* and read it's data.
*/
while (groupItr.hasNext()) {
try {
for (String groupKey : allGroupsNode.keySet()) {
groupCount++;
// Attempt to fetch the next group name.
groupKey = groupItr.next();
} catch (Exception e) {
throw new IllegalArgumentException("Invalid node type for group entry (" + groupCount + ") in file: " + groupsFile.getPath());
}
/*
* Fetch this groups child nodes
*/
Map<String, Object> thisGroupNode = (Map<String, Object>) allGroupsNode.get(groupKey);
/*
* 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);
}
if ((Boolean.parseBoolean(thisGroupNode.get("default").toString()))) {
} 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());
@ -480,10 +513,20 @@ public class WorldDataHolder {
//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 {
/*
@ -492,8 +535,7 @@ public class WorldDataHolder {
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());
// Ignore this entry as it's null. It can be safely dropped
}
}
} else if (thisGroupNode.get("permissions") instanceof String) {
@ -505,6 +547,9 @@ public class WorldDataHolder {
} else {
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.
*/
thisGrp.sortPermissions();
}
} catch (Exception e) {
@ -548,9 +593,7 @@ public class WorldDataHolder {
throw new IllegalArgumentException("Invalid formatting found in inheritance section for group: " + thisGrp.getName() + " in file: " + groupsFile.getPath());
}
}
} catch (Exception e) {
throw new IllegalArgumentException("Invalid node type, or bad indentation in file: " + groupsFile.getPath());
}
} catch (Exception ex) {
ex.printStackTrace();
throw new IllegalArgumentException("Your " + groupsFile.getPath() + " file is invalid. See console for details.");
@ -614,9 +657,21 @@ public class WorldDataHolder {
Map<String, Object> allUsersNode = (Map<String, Object>) usersRootDataNode.get("users");
// Load users if the file is NOT empty
if (allUsersNode != null)
if (allUsersNode != null) {
Iterator<String> usersItr = allUsersNode.keySet().iterator();
String usersKey;
Integer userCount = 0;
while (usersItr.hasNext()) {
try {
for (String usersKey : allUsersNode.keySet()) {
userCount++;
// Attempt to fetch the next user name.
usersKey = usersItr.next();
} catch (Exception e) {
throw new IllegalArgumentException("Invalid node type for user entry (" + userCount + ") in file: " + usersFile.getPath());
}
Map<String, Object> thisUserNode = null;
try {
thisUserNode = (Map<String, Object>) allUsersNode.get(usersKey);
@ -702,8 +757,6 @@ public class WorldDataHolder {
thisUser.setGroup(ph.getDefaultGroup());
}
}
} catch (Exception e) {
throw new IllegalArgumentException("Invalid node type, or bad indentation in file: " + usersFile.getPath());
}
ph.removeUsersChangedFlag();