KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > group > GroupManager


1 /**
2  * $RCSfile: GroupManager.java,v $
3  * $Revision: 1.10 $
4  * $Date: 2005/04/11 21:02:05 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

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 JavaDoc;
20 import java.util.Collections JavaDoc;
21
22 /**
23  * Manages groups.
24  *
25  * @see Group
26  * @author Matt Tucker
27  */

28 public class GroupManager {
29
30     Cache groupCache;
31     private GroupProvider provider;
32
33     private static GroupManager instance = new GroupManager();
34
35     /**
36      * Returns a singleton instance of GroupManager.
37      *
38      * @return a GroupManager instance.
39      */

40     public static GroupManager getInstance() {
41         return instance;
42     }
43
44     private GroupManager() {
45         // Initialize caches.
46
CacheManager.initializeCache("group", 128 * 1024);
47         CacheManager.initializeCache("group member", 32 * 1024);
48         groupCache = CacheManager.getCache("group");
49         // Load a group provider.
50
String JavaDoc className = JiveGlobals.getXMLProperty("provider.group.className",
51                 "org.jivesoftware.messenger.group.DefaultGroupProvider");
52         try {
53             Class JavaDoc c = ClassUtils.forName(className);
54             provider = (GroupProvider)c.newInstance();
55         }
56         catch (Exception JavaDoc e) {
57             Log.error("Error loading group provider: " + className, e);
58             provider = new DefaultGroupProvider();
59         }
60     }
61
62     /**
63      * Factory method for creating a new Group. A unique name is the only required field.
64      *
65      * @param name the new and unique name for the group.
66      * @return a new Group.
67      * @throws GroupAlreadyExistsException if the group name already exists in the system.
68      */

69     public Group createGroup(String JavaDoc name) throws GroupAlreadyExistsException {
70         synchronized (name.intern()) {
71             Group newGroup = null;
72             try {
73                 getGroup(name);
74                 // The group already exists since now exception, so:
75
throw new GroupAlreadyExistsException();
76             }
77             catch (GroupNotFoundException unfe) {
78                 // The group doesn't already exist so we can create a new group
79
newGroup = provider.createGroup(name);
80                 groupCache.put(name, newGroup);
81
82                 // Fire event.
83
GroupEventDispatcher.dispatchEvent(newGroup,
84                         GroupEventDispatcher.EventType.group_created, Collections.emptyMap());
85             }
86             return newGroup;
87         }
88     }
89
90     /**
91      * Returns a Group by name.
92      *
93      * @param name The name of the group to retrieve
94      * @return The group corresponding to that name
95      * @throws GroupNotFoundException if the group does not exist.
96      */

97     public Group getGroup(String JavaDoc name) throws GroupNotFoundException {
98         Group group = (Group)groupCache.get(name);
99         // If ID wan't found in cache, load it up and put it there.
100
if (group == null) {
101             synchronized (name.intern()) {
102                 group = (Group)groupCache.get(name);
103                 // If ID wan't found in cache, load it up and put it there.
104
if (group == null) {
105                     group = provider.getGroup(name);
106                     groupCache.put(name, group);
107                 }
108             }
109         }
110         return group;
111     }
112
113     /**
114      * Deletes a group from the system.
115      *
116      * @param group the group to delete.
117      */

118     public void deleteGroup(Group group) {
119         // Fire event.
120
GroupEventDispatcher.dispatchEvent(group, GroupEventDispatcher.EventType.group_deleting,
121                 Collections.emptyMap());
122
123         // Delete the group.
124
provider.deleteGroup(group.getName());
125
126         // Expire all relevant caches.
127
groupCache.remove(group.getName());
128     }
129
130     /**
131      * Deletes a user from all the groups where he/she belongs. The most probable cause
132      * for this request is that the user has been deleted from the system.
133      *
134      * TODO: remove this method and use events isntead.
135      *
136      * @param user the deleted user from the system.
137      */

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     /**
150      * Returns the total number of groups in the system.
151      *
152      * @return the total number of groups.
153      */

154     public int getGroupCount() {
155         return provider.getGroupCount();
156     }
157
158     /**
159      * Returns an unmodifiable Collection of all groups in the system.
160      *
161      * @return an unmodifiable Collection of all groups.
162      */

163     public Collection JavaDoc<Group> getGroups() {
164         return provider.getGroups();
165     }
166
167     /**
168      * Returns an iterator for all groups according to a filter.
169      * <p/>
170      * This is useful to support
171      * pagination in a GUI where you may only want to display a certain
172      * number of results per page. It is possible that the
173      * number of results returned will be less than that specified by
174      * numResults if numResults is greater than the number of records left in
175      * the system to display.
176      *
177      * @param startIndex start index in results.
178      * @param numResults number of results to return.
179      * @return an Iterator for all groups in the specified range.
180      */

181     public Collection JavaDoc<Group> getGroups(int startIndex, int numResults) {
182         return provider.getGroups(startIndex, numResults);
183     }
184
185     /**
186      * Returns an iterator for all groups that a user is a member of.
187      *
188      * @param user the user to get a list of groups for.
189      * @return all groups that a user belongs to.
190      */

191     public Collection JavaDoc<Group> getGroups(User user) {
192         return provider.getGroups(user);
193     }
194 }
Popular Tags