📄 groupmanager.java
字号:
/**
* $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.
*/
package org.jivesoftware.wildfire.group;
import org.jivesoftware.util.*;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.event.GroupEventDispatcher;
import org.jivesoftware.wildfire.event.GroupEventListener;
import org.jivesoftware.wildfire.user.User;
import org.jivesoftware.wildfire.user.UserManager;
import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.xmpp.packet.JID;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
/**
* Manages groups.
*
* @see Group
* @author Matt Tucker
*/
public class GroupManager {
Cache<String, Group> groupCache;
Cache<String, Object> groupMetaCache;
private GroupProvider provider;
private static GroupManager instance = new GroupManager();
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";
/**
* Returns a singleton instance of GroupManager.
*
* @return a GroupManager instance.
*/
public static GroupManager getInstance() {
return instance;
}
private GroupManager() {
// Initialize caches.
groupCache = CacheManager.initializeCache("Group", "group", 1024 * 1024,
JiveConstants.MINUTE*15);
// A cache for meta-data around groups: count, group names, groups associated with
// a particular user
groupMetaCache = CacheManager.initializeCache("Group Metadata Cache", "groupMeta",
512 * 1024, JiveConstants.MINUTE*15);
// Load a group provider.
String className = JiveGlobals.getXMLProperty("provider.group.className",
"org.jivesoftware.wildfire.group.DefaultGroupProvider");
try {
Class c = ClassUtils.forName(className);
provider = (GroupProvider) c.newInstance();
}
catch (Exception e) {
Log.error("Error loading group provider: " + className, e);
provider = new DefaultGroupProvider();
}
GroupEventDispatcher.addListener(new GroupEventListener() {
public void groupCreated(Group group, Map params) {
groupMetaCache.clear();
}
public void groupDeleting(Group group, Map params) {
groupMetaCache.clear();
}
public void groupModified(Group group, Map params) {
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();
}
}
}
public void memberAdded(Group group, Map params) {
groupMetaCache.clear();
}
public void memberRemoved(Group group, Map params) {
groupMetaCache.clear();
}
public void adminAdded(Group group, Map params) {
groupMetaCache.clear();
}
public void adminRemoved(Group group, Map params) {
groupMetaCache.clear();
}
});
// Pre-load shared groups. This will provide a faster response
// time to the first client that logs in.
// TODO: use a task engine instead of creating a thread directly.
Runnable task = new Runnable() {
public void run() {
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.
}
}
}
}
};
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}
/**
* 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()) {
Group newGroup;
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);
// Update caches.
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 {
Group group = groupCache.get(name);
// If ID wan't found in cache, load it up and put it there.
if (group == null) {
synchronized (name.intern()) {
group = groupCache.get(name);
// If group wan't found in cache, load it up and put it there.
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());
// Expire cache.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -