1 11 12 package org.jivesoftware.messenger.group; 13 14 import org.jivesoftware.util.*; 15 import org.jivesoftware.messenger.user.User; 16 import org.jivesoftware.util.JiveGlobals; 17 import org.jivesoftware.messenger.event.GroupEventDispatcher; 18 19 import java.util.Collection ; 20 import java.util.Collections ; 21 22 28 public class GroupManager { 29 30 Cache groupCache; 31 private GroupProvider provider; 32 33 private static GroupManager instance = new GroupManager(); 34 35 40 public static GroupManager getInstance() { 41 return instance; 42 } 43 44 private GroupManager() { 45 CacheManager.initializeCache("group", 128 * 1024); 47 CacheManager.initializeCache("group member", 32 * 1024); 48 groupCache = CacheManager.getCache("group"); 49 String className = JiveGlobals.getXMLProperty("provider.group.className", 51 "org.jivesoftware.messenger.group.DefaultGroupProvider"); 52 try { 53 Class c = ClassUtils.forName(className); 54 provider = (GroupProvider)c.newInstance(); 55 } 56 catch (Exception e) { 57 Log.error("Error loading group provider: " + className, e); 58 provider = new DefaultGroupProvider(); 59 } 60 } 61 62 69 public Group createGroup(String name) throws GroupAlreadyExistsException { 70 synchronized (name.intern()) { 71 Group newGroup = null; 72 try { 73 getGroup(name); 74 throw new GroupAlreadyExistsException(); 76 } 77 catch (GroupNotFoundException unfe) { 78 newGroup = provider.createGroup(name); 80 groupCache.put(name, newGroup); 81 82 GroupEventDispatcher.dispatchEvent(newGroup, 84 GroupEventDispatcher.EventType.group_created, Collections.emptyMap()); 85 } 86 return newGroup; 87 } 88 } 89 90 97 public Group getGroup(String name) throws GroupNotFoundException { 98 Group group = (Group)groupCache.get(name); 99 if (group == null) { 101 synchronized (name.intern()) { 102 group = (Group)groupCache.get(name); 103 if (group == null) { 105 group = provider.getGroup(name); 106 groupCache.put(name, group); 107 } 108 } 109 } 110 return group; 111 } 112 113 118 public void deleteGroup(Group group) { 119 GroupEventDispatcher.dispatchEvent(group, GroupEventDispatcher.EventType.group_deleting, 121 Collections.emptyMap()); 122 123 provider.deleteGroup(group.getName()); 125 126 groupCache.remove(group.getName()); 128 } 129 130 138 public void deleteUser(User user) { 139 for (Group group : getGroups(user)) { 140 if (group.getAdmins().contains(user.getUsername())) { 141 group.getAdmins().remove(user.getUsername()); 142 } 143 else { 144 group.getMembers().remove(user.getUsername()); 145 } 146 } 147 } 148 149 154 public int getGroupCount() { 155 return provider.getGroupCount(); 156 } 157 158 163 public Collection <Group> getGroups() { 164 return provider.getGroups(); 165 } 166 167 181 public Collection <Group> getGroups(int startIndex, int numResults) { 182 return provider.getGroups(startIndex, numResults); 183 } 184 185 191 public Collection <Group> getGroups(User user) { 192 return provider.getGroups(user); 193 } 194 } | Popular Tags |