diff options
author | snowleo <schneeleo@gmail.com> | 2012-09-10 20:49:07 +0200 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2012-09-10 20:49:07 +0200 |
commit | f07c89803f9ae9b2c1780037e458dbb526c11ee3 (patch) | |
tree | cd9d8aacfb94232aeb5d6e04e47d53eb3f4f3d3c /EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder | |
parent | ff18374456ffeaa6d60ec974fb2645c733d3e1ba (diff) | |
download | Essentials-f07c89803f9ae9b2c1780037e458dbb526c11ee3.tar Essentials-f07c89803f9ae9b2c1780037e458dbb526c11ee3.tar.gz Essentials-f07c89803f9ae9b2c1780037e458dbb526c11ee3.tar.lz Essentials-f07c89803f9ae9b2c1780037e458dbb526c11ee3.tar.xz Essentials-f07c89803f9ae9b2c1780037e458dbb526c11ee3.zip |
Thread safety for GM
Diffstat (limited to 'EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder')
4 files changed, 46 insertions, 17 deletions
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/GroupsDataHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/GroupsDataHolder.java index eaaaace74..fdd099c87 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/GroupsDataHolder.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/GroupsDataHolder.java @@ -1,6 +1,7 @@ package org.anjocaido.groupmanager.dataholder;
import java.io.File;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -23,7 +24,7 @@ public class GroupsDataHolder { /**
* The actual groups holder
*/
- private Map<String, Group> groups = new HashMap<String, Group>();
+ private final Map<String, Group> groups = Collections.synchronizedMap(new HashMap<String, Group>());
/**
* Constructor
@@ -36,8 +37,10 @@ public class GroupsDataHolder { this.dataSource = dataSource;
//push this data source to the users, so they pull the correct groups data.
+ synchronized(groups) {
for (Group group : groups.values())
group.setDataSource(this.dataSource);
+ }
}
/**
@@ -57,6 +60,7 @@ public class GroupsDataHolder { }
/**
+ * Note: Iteration over this object has to be synchronized!
* @return the groups
*/
public Map<String, Group> getGroups() {
@@ -67,9 +71,8 @@ public class GroupsDataHolder { /**
* @param groups the groups to set
*/
- public void setGroups(Map<String, Group> groups) {
-
- this.groups = groups;
+ public void resetGroups() {
+ this.groups.clear();
}
/**
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java index 84561b6e5..ef9f605ed 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java @@ -6,6 +6,7 @@ package org.anjocaido.groupmanager.dataholder; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import org.anjocaido.groupmanager.data.User; @@ -19,7 +20,7 @@ public class OverloadedWorldHolder extends WorldDataHolder { /** * */ - protected Map<String, User> overloadedUsers = new HashMap<String, User>(); + protected final Map<String, User> overloadedUsers = Collections.synchronizedMap(new HashMap<String, User>()); /** * @@ -112,9 +113,11 @@ public class OverloadedWorldHolder extends WorldDataHolder { if (groupName.equals(getDefaultGroup())) { return false; } + synchronized(getGroups()) { for (String key : getGroups().keySet()) { if (groupName.equalsIgnoreCase(key)) { getGroups().remove(key); + synchronized(getUsers()) { for (String userKey : getUsers().keySet()) { User user = getUsers().get(userKey); if (user.getGroupName().equalsIgnoreCase(key)) { @@ -122,7 +125,9 @@ public class OverloadedWorldHolder extends WorldDataHolder { } } + } //OVERLOADED CODE + synchronized(overloadedUsers) { for (String userKey : overloadedUsers.keySet()) { User user = overloadedUsers.get(userKey); if (user.getGroupName().equalsIgnoreCase(key)) { @@ -130,11 +135,13 @@ public class OverloadedWorldHolder extends WorldDataHolder { } } + } //END OVERLOAD setGroupsChanged(true); return true; } } + } return false; } @@ -146,6 +153,7 @@ public class OverloadedWorldHolder extends WorldDataHolder { public Collection<User> getUserList() { Collection<User> overloadedList = new ArrayList<User>(); + synchronized(getUsers()) { Collection<User> normalList = getUsers().values(); for (User u : normalList) { if (overloadedUsers.containsKey(u.getName().toLowerCase())) { @@ -154,6 +162,7 @@ public class OverloadedWorldHolder extends WorldDataHolder { overloadedList.add(u); } } + } return overloadedList; } diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/UsersDataHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/UsersDataHolder.java index 665fe227d..8a3c4c102 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/UsersDataHolder.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/UsersDataHolder.java @@ -1,6 +1,7 @@ package org.anjocaido.groupmanager.dataholder;
import java.io.File;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -22,7 +23,7 @@ public class UsersDataHolder { /**
* The actual groups holder
*/
- private Map<String, User> users = new HashMap<String, User>();
+ private final Map<String, User> users = Collections.synchronizedMap(new HashMap<String, User>());
/**
* Constructor
@@ -35,12 +36,14 @@ public class UsersDataHolder { this.dataSource = dataSource;
//push this data source to the users, so they pull the correct groups data.
+ synchronized(users) {
for (User user : users.values())
user.setDataSource(this.dataSource);
-
+ }
}
/**
+ * Note: Iteration over this object has to be synchronized!
* @return the users
*/
public Map<String, User> getUsers() {
@@ -51,9 +54,8 @@ public class UsersDataHolder { /**
* @param users the users to set
*/
- public void setUsers(Map<String, User> users) {
-
- this.users = users;
+ public void resetUsers() {
+ this.users.clear();
}
/**
diff --git a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java index aad59e5aa..4d85eff68 100644 --- a/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java +++ b/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/WorldDataHolder.java @@ -319,8 +319,9 @@ public class WorldDataHolder { * @return a collection of the groups */ public Collection<Group> getGroupList() { - - return getGroups().values(); + synchronized(getGroups()) { + return new ArrayList<Group>(getGroups().values()); + } } /** @@ -328,8 +329,9 @@ public class WorldDataHolder { * @return a collection of the users */ public Collection<User> getUserList() { - - return getUsers().values(); + synchronized(getUsers()) { + return new ArrayList<User>(getUsers().values()); + } } /** @@ -944,6 +946,7 @@ public class WorldDataHolder { Map<String, Object> groupsMap = new HashMap<String, Object>(); root.put("groups", groupsMap); + synchronized(ph.getGroups()) { for (String groupKey : ph.getGroups().keySet()) { Group group = ph.getGroups().get(groupKey); @@ -966,6 +969,7 @@ public class WorldDataHolder { aGroupMap.put("permissions", group.getPermissionList()); } + } if (!root.isEmpty()) { DumperOptions opt = new DumperOptions(); @@ -1031,6 +1035,7 @@ public class WorldDataHolder { Map<String, Object> usersMap = new HashMap<String, Object>(); root.put("users", usersMap); + synchronized(ph.getUsers()) { for (String userKey : ph.getUsers().keySet()) { User user = ph.getUsers().get(userKey); if ((user.getGroup() == null || user.getGroup().equals(ph.getDefaultGroup())) && user.getPermissionList().isEmpty() && user.getVariables().isEmpty() && user.isSubGroupsEmpty()) { @@ -1060,6 +1065,7 @@ public class WorldDataHolder { aUserMap.put("subgroups", user.subGroupListStringCopy()); // END SUBGROUPS NODE - BETA } + } if (!root.isEmpty()) { DumperOptions opt = new DumperOptions(); @@ -1159,11 +1165,13 @@ public class WorldDataHolder { if (users.HaveUsersChanged()) { return true; } + synchronized(users.getUsers()) { for (User u : users.getUsers().values()) { if (u.isChanged()) { return true; } } + } return false; } @@ -1184,11 +1192,13 @@ public class WorldDataHolder { if (groups.HaveGroupsChanged()) { return true; } + synchronized(groups.getGroups()) { for (Group g : groups.getGroups().values()) { if (g.isChanged()) { return true; } } + } return false; } @@ -1198,9 +1208,11 @@ public class WorldDataHolder { public void removeUsersChangedFlag() { setUsersChanged(false); + synchronized(getUsers()) { for (User u : getUsers().values()) { u.flagAsSaved(); } + } } /** @@ -1209,9 +1221,11 @@ public class WorldDataHolder { public void removeGroupsChangedFlag() { setGroupsChanged(false); + synchronized(getGroups()) { for (Group g : getGroups().values()) { g.flagAsSaved(); } + } } /** @@ -1260,18 +1274,18 @@ public class WorldDataHolder { public void resetGroups() { // setDefaultGroup(null); - groups.setGroups(new HashMap<String, Group>()); + groups.resetGroups(); } /** * Resets Users */ public void resetUsers() { - - users.setUsers(new HashMap<String, User>()); + users.resetUsers(); } /** + * Note: Iteration over this object has to be synchronized! * @return the groups */ public Map<String, Group> getGroups() { @@ -1280,6 +1294,7 @@ public class WorldDataHolder { } /** + * Note: Iteration over this object has to be synchronized! * @return the users */ public Map<String, User> getUsers() { |