GroupManager.java 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/**
 * $RCSfile$
 * $Revision: 3117 $
 * $Date: 2005-11-25 22:57:29 -0300 (Fri, 25 Nov 2005) $
 *
 * Copyright (C) 2004 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

12
package org.jivesoftware.openfire.group;
13

14 15 16
import org.jivesoftware.util.*;
import org.jivesoftware.util.cache.Cache;
import org.jivesoftware.util.cache.CacheFactory;
17 18 19 20 21 22
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.event.GroupEventDispatcher;
import org.jivesoftware.openfire.event.GroupEventListener;
import org.jivesoftware.openfire.user.User;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException;
23 24
import org.xmpp.packet.JID;

25 26 27
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
28 29 30 31 32 33 34 35 36

/**
 * Manages groups.
 *
 * @see Group
 * @author Matt Tucker
 */
public class GroupManager {

37 38 39
    private static final class GroupManagerContainer {
        private static final GroupManager instance = new GroupManager();
    }
40

41 42 43 44
    private static final String GROUP_COUNT_KEY = "GROUP_COUNT";
    private static final String SHARED_GROUPS_KEY = "SHARED_GROUPS";
    private static final String GROUP_NAMES_KEY = "GROUP_NAMES";

45 46 47 48 49 50
    /**
     * Returns a singleton instance of GroupManager.
     *
     * @return a GroupManager instance.
     */
    public static GroupManager getInstance() {
51
        return GroupManagerContainer.instance;
52 53
    }

54 55 56 57
    Cache<String, Group> groupCache;
    Cache<String, Object> groupMetaCache;
    private GroupProvider provider;

58 59
    private GroupManager() {
        // Initialize caches.
60
        groupCache = CacheFactory.createCache("Group");
61

62 63
        // A cache for meta-data around groups: count, group names, groups associated with
        // a particular user
64
        groupMetaCache = CacheFactory.createCache("Group Metadata Cache");
65

66 67
        // Load a group provider.
        String className = JiveGlobals.getXMLProperty("provider.group.className",
68
                "org.jivesoftware.openfire.group.DefaultGroupProvider");
69 70
        try {
            Class c = ClassUtils.forName(className);
71
            provider = (GroupProvider) c.newInstance();
72 73 74 75 76
        }
        catch (Exception e) {
            Log.error("Error loading group provider: " + className, e);
            provider = new DefaultGroupProvider();
        }
77 78 79

        GroupEventDispatcher.addListener(new GroupEventListener() {
            public void groupCreated(Group group, Map params) {
80
                groupMetaCache.clear();
81 82 83
            }

            public void groupDeleting(Group group, Map params) {
84
                groupMetaCache.clear();
85 86 87
            }

            public void groupModified(Group group, Map params) {
88 89 90 91 92 93 94 95 96 97 98
                String type = (String)params.get("type");
                // If shared group settings changed, expire the cache.
                if (type != null && (type.equals("propertyModified") ||
                        type.equals("propertyDeleted") || type.equals("propertyAdded")))
                {
                    if (params.get("propertyKey") != null &&
                            params.get("propertyKey").equals("sharedRoster.showInRoster"))
                    {
                        groupMetaCache.clear();
                    }
                }
Gaston Dombiak's avatar
Gaston Dombiak committed
99 100 101
                // Set object again in cache. This is done so that other cluster nodes
                // get refreshed with latest version of the object
                groupCache.put(group.getName(), group);
102 103 104
            }

            public void memberAdded(Group group, Map params) {
105
                groupMetaCache.clear();
Gaston Dombiak's avatar
Gaston Dombiak committed
106 107 108
                // Set object again in cache. This is done so that other cluster nodes
                // get refreshed with latest version of the object
                groupCache.put(group.getName(), group);
109 110 111
            }

            public void memberRemoved(Group group, Map params) {
112
                groupMetaCache.clear();
Gaston Dombiak's avatar
Gaston Dombiak committed
113 114 115
                // Set object again in cache. This is done so that other cluster nodes
                // get refreshed with latest version of the object
                groupCache.put(group.getName(), group);
116 117 118
            }

            public void adminAdded(Group group, Map params) {
119
                groupMetaCache.clear();
Gaston Dombiak's avatar
Gaston Dombiak committed
120 121 122
                // Set object again in cache. This is done so that other cluster nodes
                // get refreshed with latest version of the object
                groupCache.put(group.getName(), group);
123 124 125
            }

            public void adminRemoved(Group group, Map params) {
126
                groupMetaCache.clear();
Gaston Dombiak's avatar
Gaston Dombiak committed
127 128 129
                // Set object again in cache. This is done so that other cluster nodes
                // get refreshed with latest version of the object
                groupCache.put(group.getName(), group);
130 131
            }
        });
132 133 134 135 136

        // Pre-load shared groups. This will provide a faster response
        // time to the first client that logs in.
        Runnable task = new Runnable() {
            public void run() {
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
                Collection<Group> groups = getSharedGroups();
                // Load each group into cache.
                for (Group group : groups) {
                    // Load each user in the group into cache.
                    for (JID jid : group.getMembers()) {
                        try {
                            if (XMPPServer.getInstance().isLocal(jid)) {
                                UserManager.getInstance().getUser(jid.getNode());
                            }
                        }
                        catch (UserNotFoundException unfe) {
                            // Ignore.
                        }
                    }
                }
152 153
            }
        };
154
        TaskEngine.getInstance().submit(task);
155 156 157 158 159 160 161 162 163 164 165
    }

    /**
     * Factory method for creating a new Group. A unique name is the only required field.
     *
     * @param name the new and unique name for the group.
     * @return a new Group.
     * @throws GroupAlreadyExistsException if the group name already exists in the system.
     */
    public Group createGroup(String name) throws GroupAlreadyExistsException {
        synchronized (name.intern()) {
166
            Group newGroup;
167 168 169 170 171 172 173 174
            try {
                getGroup(name);
                // The group already exists since now exception, so:
                throw new GroupAlreadyExistsException();
            }
            catch (GroupNotFoundException unfe) {
                // The group doesn't already exist so we can create a new group
                newGroup = provider.createGroup(name);
175
                // Update caches.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
                groupCache.put(name, newGroup);

                // Fire event.
                GroupEventDispatcher.dispatchEvent(newGroup,
                        GroupEventDispatcher.EventType.group_created, Collections.emptyMap());
            }
            return newGroup;
        }
    }

    /**
     * Returns a Group by name.
     *
     * @param name The name of the group to retrieve
     * @return The group corresponding to that name
     * @throws GroupNotFoundException if the group does not exist.
     */
    public Group getGroup(String name) throws GroupNotFoundException {
194
        Group group = groupCache.get(name);
195 196 197
        // If ID wan't found in cache, load it up and put it there.
        if (group == null) {
            synchronized (name.intern()) {
198
                group = groupCache.get(name);
199
                // If group wan't found in cache, load it up and put it there.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
                if (group == null) {
                    group = provider.getGroup(name);
                    groupCache.put(name, group);
                }
            }
        }
        return group;
    }

    /**
     * Deletes a group from the system.
     *
     * @param group the group to delete.
     */
    public void deleteGroup(Group group) {
        // Fire event.
        GroupEventDispatcher.dispatchEvent(group, GroupEventDispatcher.EventType.group_deleting,
                Collections.emptyMap());

        // Delete the group.
        provider.deleteGroup(group.getName());

222
        // Expire cache.
223 224 225 226 227 228 229
        groupCache.remove(group.getName());
    }

    /**
     * Deletes a user from all the groups where he/she belongs. The most probable cause
     * for this request is that the user has been deleted from the system.
     *
230
     * TODO: remove this method and use events instead.
231 232 233 234 235 236 237
     *
     * @param user the deleted user from the system.
     */
    public void deleteUser(User user) {
        JID userJID = XMPPServer.getInstance().createJID(user.getUsername(), null);
        for (Group group : getGroups(userJID)) {
            if (group.getAdmins().contains(userJID)) {
238 239 240 241
                if (group.getAdmins().remove(userJID)) {
                    // Remove the group from cache.
                    groupCache.remove(group.getName());
                }
242 243
            }
            else {
244 245 246 247
                if (group.getMembers().remove(userJID)) {
                    // Remove the group from cache.
                    groupCache.remove(group.getName());
                }
248 249 250 251 252 253 254 255 256 257
            }
        }
    }

    /**
     * Returns the total number of groups in the system.
     *
     * @return the total number of groups.
     */
    public int getGroupCount() {
258 259 260 261 262 263 264 265 266 267 268
        Integer count = (Integer)groupMetaCache.get(GROUP_COUNT_KEY);
        if (count == null) {
            synchronized(GROUP_COUNT_KEY.intern()) {
                count = (Integer)groupMetaCache.get(GROUP_COUNT_KEY);
                if (count == null) {
                    count = provider.getGroupCount();
                    groupMetaCache.put(GROUP_COUNT_KEY, count);
                }
            }
        }
        return count;
269 270 271 272 273 274 275 276
    }

    /**
     * Returns an unmodifiable Collection of all groups in the system.
     *
     * @return an unmodifiable Collection of all groups.
     */
    public Collection<Group> getGroups() {
277 278 279 280 281 282 283 284 285 286
        Collection<String> groupNames = (Collection<String>)groupMetaCache.get(GROUP_NAMES_KEY);
        if (groupNames == null) {
            synchronized(GROUP_NAMES_KEY.intern()) {
                groupNames = (Collection<String>)groupMetaCache.get(GROUP_NAMES_KEY);
                if (groupNames == null) {
                    groupNames = provider.getGroupNames();
                    groupMetaCache.put(GROUP_NAMES_KEY, groupNames);
                }
            }
        }
287
        return new GroupCollection(groupNames);
288 289
    }

290 291 292 293 294 295
    /**
     * Returns an unmodifiable Collection of all shared groups in the system.
     *
     * @return an unmodifiable Collection of all shared groups.
     */
    public Collection<Group> getSharedGroups() {
296 297 298 299 300 301 302 303 304 305
        Collection<String> groupNames = (Collection<String>)groupMetaCache.get(SHARED_GROUPS_KEY);
        if (groupNames == null) {
            synchronized(SHARED_GROUPS_KEY.intern()) {
                groupNames = (Collection<String>)groupMetaCache.get(SHARED_GROUPS_KEY);
                if (groupNames == null) {
                    groupNames = Group.getSharedGroupsNames();
                    groupMetaCache.put(SHARED_GROUPS_KEY, groupNames);
                }
            }
        }
306
        return new GroupCollection(groupNames);
307 308
    }

309
    /**
310 311 312 313 314
     * Returns all groups given a start index and desired number of results. This is
     * useful to support pagination in a GUI where you may only want to display a certain
     * number of results per page. It is possible that the number of results returned will
     * be less than that specified by numResults if numResults is greater than the number
     * of records left in the system to display.
315 316 317 318 319 320
     *
     * @param startIndex start index in results.
     * @param numResults number of results to return.
     * @return an Iterator for all groups in the specified range.
     */
    public Collection<Group> getGroups(int startIndex, int numResults) {
321 322 323 324 325 326 327 328 329 330 331 332
        String key = GROUP_NAMES_KEY + startIndex + "," + numResults;

        Collection<String> groupNames = (Collection<String>)groupMetaCache.get(key);
        if (groupNames == null) {
            synchronized(key.intern()) {
                groupNames = (Collection<String>)groupMetaCache.get(key);
                if (groupNames == null) {
                    groupNames = provider.getGroupNames(startIndex, numResults);
                    groupMetaCache.put(key, groupNames);
                }
            }
        }
333
        return new GroupCollection(groupNames);
334 335
    }

336 337 338 339 340 341 342 343 344 345
    /**
     * Returns an iterator for all groups that the User is a member of.
     *
     * @param user the user.
     * @return all groups the user belongs to.
     */
    public Collection<Group> getGroups(User user) {
        return getGroups(XMPPServer.getInstance().createJID(user.getUsername(), null));
    }

346 347 348 349 350 351 352
    /**
     * Returns an iterator for all groups that the entity with the specified JID is a member of.
     *
     * @param user the JID of the entity to get a list of groups for.
     * @return all groups that an entity belongs to.
     */
    public Collection<Group> getGroups(JID user) {
353 354 355 356 357 358 359 360 361 362 363 364
        String key = user.toBareJID();

        Collection<String> groupNames = (Collection<String>)groupMetaCache.get(key);
        if (groupNames == null) {
            synchronized(key.intern()) {
                groupNames = (Collection<String>)groupMetaCache.get(key);
                if (groupNames == null) {
                    groupNames = provider.getGroupNames(user);
                    groupMetaCache.put(key, groupNames);
                }
            }
        }
365
        return new GroupCollection(groupNames);
366 367
    }

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    /**
     * Returns true if groups are read-only.
     *
     * @return true if groups are read-only.
     */
    public boolean isReadOnly() {
        return provider.isReadOnly();
    }

    /**
     * Returns true if searching for groups is supported.
     *
     * @return true if searching for groups are supported.
     */
    public boolean isSearchSupported() {
        return provider.isSearchSupported();
    }

    /**
     * Returns the groups that match the search. The search is over group names and
     * implicitly uses wildcard matching (although the exact search semantics are left
     * up to each provider implementation). For example, a search for "HR" should match
     * the groups "HR", "HR Department", and "The HR People".<p>
     *
     * Before searching or showing a search UI, use the {@link #isSearchSupported} method
     * to ensure that searching is supported.
     *
     * @param query the search string for group names.
     * @return all groups that match the search.
     */
    public Collection<Group> search(String query) {
399 400
        Collection<String> groupNames = provider.search(query);
        return new GroupCollection(groupNames);
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    }

    /**
     * Returns the groups that match the search given a start index and desired number
     * of results. The search is over group names and implicitly uses wildcard matching
     * (although the exact search semantics are left up to each provider implementation).
     * For example, a search for "HR" should match the groups "HR", "HR Department", and
     * "The HR People".<p>
     *
     * Before searching or showing a search UI, use the {@link #isSearchSupported} method
     * to ensure that searching is supported.
     *
     * @param query the search string for group names.
     * @return all groups that match the search.
     */
    public Collection<Group> search(String query, int startIndex, int numResults) {
417 418
        Collection<String> groupNames = provider.search(query, startIndex, numResults);
        return new GroupCollection(groupNames);
419 420
    }

421 422 423 424 425 426
    /**
     * Returns the configured group provider. Note that this method has special access
     * privileges since only a few certain classes need to access the provider directly.
     *
     * @return the group provider.
     */
427
    public GroupProvider getProvider() {
428 429
        return provider;
    }
430
}