Commit fa7a4f66 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Refactoring work. JM-600

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3586 b35dd754-fafc-0310-a699-88a17e54d16e
parent 56d1864f
......@@ -9,7 +9,9 @@
package org.jivesoftware.util;
import java.util.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* Centralized management of caches. Caches are essential for performance and scalability.
......@@ -36,10 +38,13 @@ public class CacheManager {
* where CACHE_NAME is the name of the cache.
*
* @param name the name of the cache to initialize.
* @param propertiesName the properties file name prefix where settings for the cache
* are stored. The name is will be prefixed by "cache." before it is
* looked up.
* @param size the size the cache can grow to, in bytes.
*/
public static Cache initializeCache(String name, int size) {
return initializeCache(name, size, DEFAULT_EXPIRATION_TIME);
public static Cache initializeCache(String name, String propertiesName, int size) {
return initializeCache(name, propertiesName, size, DEFAULT_EXPIRATION_TIME);
}
/**
......@@ -55,14 +60,19 @@ public class CacheManager {
* where CACHE_NAME is the name of the cache.
*
* @param name the name of the cache to initialize.
* @param size the size the cache can grow to, in bytes.
* @param propertiesName the properties file name prefix where settings for the cache are
* stored. The name is will be prefixed by "cache." before it is
* looked up.
* @param size the size the cache can grow to, in bytes.
* @param expirationTime the default max lifetime of the cache, in milliseconds.
*/
public static Cache initializeCache(String name, int size, long expirationTime) {
public static Cache initializeCache(String name, String propertiesName, int size,
long expirationTime) {
Cache cache = caches.get(name);
if (cache == null) {
size = JiveGlobals.getIntProperty("cache." + name + ".size", size);
expirationTime = (long)JiveGlobals.getIntProperty("cache." + name + ".expirationTime",
(int)expirationTime);
expirationTime = (long) JiveGlobals.getIntProperty(
"cache." + propertiesName + ".expirationTime", (int) expirationTime);
caches.put(name, new Cache(name, size, expirationTime));
}
return cache;
......@@ -79,4 +89,13 @@ public class CacheManager {
public static Cache getCache(String name) {
return caches.get(name);
}
/**
* Returns the list of caches being managed by this manager.
*
* @return the list of caches being managed by this manager.
*/
public static Collection<Cache> getCaches() {
return caches.values();
}
}
\ No newline at end of file
......@@ -13,6 +13,7 @@ package org.jivesoftware.wildfire;
import org.dom4j.Element;
import org.jivesoftware.util.Cache;
import org.jivesoftware.util.CacheManager;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.wildfire.container.BasicModule;
import org.jivesoftware.wildfire.disco.ServerFeaturesProvider;
......@@ -60,8 +61,7 @@ public class MulticastRouter extends BasicModule implements ServerFeaturesProvid
* Cache for a day discovered information of remote servers. The local server will try
* to discover if remote servers support multicast service.
*/
private Cache cache =
new Cache("MulticastRouter Remote Domains Cache", 128 * 1024, JiveConstants.DAY);
private Cache cache;
/**
* Packets that include recipients that belong to remote servers are not processed by
* the main thread since extra work is required. This variable holds the list of packets
......@@ -85,6 +85,10 @@ public class MulticastRouter extends BasicModule implements ServerFeaturesProvid
public MulticastRouter() {
super("Multicast Packet Router");
String cacheName = "Multicast service";
CacheManager.initializeCache(cacheName, "multicast", 128 * 1024, JiveConstants.DAY);
cache = CacheManager.getCache(cacheName);
}
public void route(Packet packet) {
......
......@@ -82,7 +82,9 @@ public class OfflineMessageStore extends BasicModule implements UserEventListene
public OfflineMessageStore() {
super("Offline Message Store");
dateFormat = FastDateFormat.getInstance("yyyyMMdd'T'HH:mm:ss", TimeZone.getTimeZone("UTC"));
sizeCache = new Cache("Offline Message Size Cache", 1024*100, JiveConstants.HOUR*12);
String cacheName = "Offline Message Size";
CacheManager.initializeCache(cacheName, "offlinemessage", 1024*100, JiveConstants.HOUR*12);
sizeCache = CacheManager.getCache(cacheName);
}
/**
......
......@@ -12,13 +12,13 @@
package org.jivesoftware.wildfire.auth;
import org.jivesoftware.util.*;
import org.jivesoftware.wildfire.user.UserAlreadyExistsException;
import org.jivesoftware.wildfire.user.UserManager;
import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.jivesoftware.wildfire.user.UserAlreadyExistsException;
import javax.mail.Store;
import javax.mail.Session;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import java.util.Properties;
/**
......@@ -80,7 +80,9 @@ public class POP3AuthProvider implements AuthProvider {
int maxSize = JiveGlobals.getXMLProperty("pop3.authCache.size", 512*1024);
long maxLifetime = (long)JiveGlobals.getXMLProperty("pop3.authCache.maxLifetime",
(int)JiveConstants.HOUR);
authCache = new Cache("POP3 Auth Cache", maxSize, maxLifetime);
String cacheName = "POP3 Authentication";
CacheManager.initializeCache(cacheName, "pop3", maxSize, maxLifetime);
authCache = CacheManager.getCache(cacheName);
}
useSSL = Boolean.valueOf(JiveGlobals.getXMLProperty("pop3.ssl"));
......
......@@ -8,7 +8,7 @@
*/
package org.jivesoftware.wildfire.filetransfer;
import org.jivesoftware.util.Cache;
import org.jivesoftware.util.CacheManager;
import org.jivesoftware.util.Log;
import org.xmpp.packet.JID;
......@@ -32,8 +32,7 @@ import java.util.concurrent.Future;
*/
public class ProxyConnectionManager {
private Map<String, ProxyTransfer> connectionMap =
new Cache("File Transfer Cache", -1, 1000 * 60 * 10);
private Map<String, ProxyTransfer> connectionMap;
private final Object connectionLock = new Object();
......@@ -43,6 +42,9 @@ public class ProxyConnectionManager {
private int proxyPort;
public ProxyConnectionManager() {
String cacheName = "File Transfer";
CacheManager.initializeCache(cacheName, "filetransfer", -1, 1000 * 60 * 10);
connectionMap = CacheManager.getCache(cacheName);
}
/*
......
......@@ -45,9 +45,9 @@ public class GroupManager {
private GroupManager() {
// Initialize caches.
CacheManager.initializeCache("group", 128 * 1024);
CacheManager.initializeCache("group member", 32 * 1024);
groupCache = CacheManager.getCache("group");
String cacheName = "Group";
CacheManager.initializeCache(cacheName, "group", 128 * 1024);
groupCache = CacheManager.getCache(cacheName);
// Load a group provider.
String className = JiveGlobals.getXMLProperty("provider.group.className",
"org.jivesoftware.wildfire.group.DefaultGroupProvider");
......@@ -165,6 +165,7 @@ public class GroupManager {
* @return an unmodifiable Collection of all groups.
*/
public Collection<Group> getGroups() {
// TODO: add/use caching
Collection<Group> groups = provider.getGroups();
// Add to cache and ensure correct identity
groups = cacheAndEnsureIdentity(groups);
......
......@@ -11,13 +11,13 @@
package org.jivesoftware.wildfire.handler;
import org.jivesoftware.util.CacheManager;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.*;
import org.jivesoftware.wildfire.container.BasicModule;
import org.jivesoftware.wildfire.roster.Roster;
import org.jivesoftware.wildfire.roster.RosterItem;
import org.jivesoftware.wildfire.roster.RosterManager;
import org.jivesoftware.wildfire.user.UserAlreadyExistsException;
import org.jivesoftware.wildfire.user.UserManager;
import org.jivesoftware.wildfire.user.UserNotFoundException;
......@@ -79,6 +79,7 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
private XMPPServer localServer;
private PacketDeliverer deliverer;
private PresenceManager presenceManager;
private RosterManager rosterManager;
private UserManager userManager;
public PresenceSubscribeHandler() {
......@@ -185,17 +186,10 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
Roster roster = null;
if (localServer.isLocal(address) && userManager.isRegisteredUser(address.getNode())) {
username = address.getNode();
// Check for a cached roster:
roster = (Roster)CacheManager.getCache("username2roster").get(username);
if (roster == null) {
synchronized(address.toString().intern()) {
roster = (Roster)CacheManager.getCache("username2roster").get(username);
if (roster == null) {
// Not in cache so load a new one:
roster = new Roster(username);
CacheManager.getCache("username2roster").put(username, roster);
}
}
try {
roster = rosterManager.getRoster(username);
}
catch (UserNotFoundException e) {
}
}
return roster;
......@@ -454,6 +448,7 @@ public class PresenceSubscribeHandler extends BasicModule implements ChannelHand
routingTable = server.getRoutingTable();
deliverer = server.getPacketDeliverer();
presenceManager = server.getPresenceManager();
rosterManager = server.getRosterManager();
userManager = server.getUserManager();
}
}
\ No newline at end of file
......@@ -11,12 +11,9 @@
package org.jivesoftware.wildfire.ldap;
import org.jivesoftware.util.*;
import org.jivesoftware.wildfire.auth.AuthProvider;
import org.jivesoftware.wildfire.auth.UnauthorizedException;
import org.jivesoftware.util.Cache;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.StringUtils;
/**
* Implementation of auth provider interface for LDAP authentication service plug-in.
......@@ -48,7 +45,9 @@ public class LdapAuthProvider implements AuthProvider {
int maxSize = JiveGlobals.getXMLProperty("ldap.authCache.size", 512*1024);
long maxLifetime = (long)JiveGlobals.getXMLProperty("ldap.authCache.maxLifetime",
(int)JiveConstants.HOUR * 2);
authCache = new Cache("LDAP Auth Cache", maxSize, maxLifetime);
String cacheName = "LDAP Authentication";
CacheManager.initializeCache(cacheName, "ldap", maxSize, maxLifetime);
authCache = CacheManager.getCache(cacheName);
}
}
......
......@@ -28,8 +28,9 @@ public class PrivacyListManager {
private PrivacyListManager() {
// Create the cache of privacy lists
CacheManager.initializeCache("listsCache", 512 * 1024);
listsCache = CacheManager.getCache("listsCache");
String cacheName = "Privacy Lists";
CacheManager.initializeCache(cacheName, "listsCache",512 * 1024);
listsCache = CacheManager.getCache(cacheName);
}
/**
......
......@@ -76,7 +76,7 @@ public class RosterManager extends BasicModule implements GroupEventListener {
*/
public Roster getRoster(String username) throws UserNotFoundException {
if (rosterCache == null) {
rosterCache = CacheManager.getCache("username2roster");
rosterCache = CacheManager.getCache("Roster");
}
if (rosterCache == null) {
throw new UserNotFoundException("Could not load caches");
......@@ -123,7 +123,7 @@ public class RosterManager extends BasicModule implements GroupEventListener {
}
}
// Remove the cached roster from memory
CacheManager.getCache("username2roster").remove(username);
CacheManager.getCache("Roster").remove(username);
// Get the rosters that have a reference to the deleted user
RosterItemProvider rosteItemProvider = RosterItemProvider.getInstance();
......@@ -301,8 +301,7 @@ public class RosterManager extends BasicModule implements GroupEventListener {
// Get the roster to update.
Roster roster = null;
if (server.isLocal(updatedUser)) {
roster = (Roster) CacheManager.getCache("username2roster")
.get(updatedUser.getNode());
roster = (Roster) CacheManager.getCache("Roster").get(updatedUser.getNode());
}
if (roster != null) {
// Update the roster with the new group display name
......@@ -435,8 +434,7 @@ public class RosterManager extends BasicModule implements GroupEventListener {
// Get the roster of the added user.
Roster addedUserRoster = null;
if (server.isLocal(addedUser)) {
addedUserRoster =
(Roster) CacheManager.getCache("username2roster").get(addedUser.getNode());
addedUserRoster = (Roster) CacheManager.getCache("Roster").get(addedUser.getNode());
}
// Iterate on all the affected users and update their rosters
......@@ -452,8 +450,7 @@ public class RosterManager extends BasicModule implements GroupEventListener {
catch (UserNotFoundException e) {
continue;
}
roster = (Roster) CacheManager.getCache("username2roster")
.get(userToUpdate.getNode());
roster = (Roster) CacheManager.getCache("Roster").get(userToUpdate.getNode());
}
// Only update rosters in memory
if (roster != null) {
......@@ -501,8 +498,7 @@ public class RosterManager extends BasicModule implements GroupEventListener {
// Get the roster of the deleted user.
Roster deletedUserRoster = null;
if (server.isLocal(deletedUser)) {
deletedUserRoster =
(Roster) CacheManager.getCache("username2roster").get(deletedUser.getNode());
deletedUserRoster = (Roster) CacheManager.getCache("Roster").get(deletedUser.getNode());
}
// Iterate on all the affected users and update their rosters
......@@ -517,8 +513,7 @@ public class RosterManager extends BasicModule implements GroupEventListener {
catch (UserNotFoundException e) {
continue;
}
roster = (Roster) CacheManager.getCache("username2roster")
.get(userToUpdate.getNode());
roster = (Roster) CacheManager.getCache("Roster").get(userToUpdate.getNode());
}
// Only update rosters in memory
if (roster != null) {
......
......@@ -34,18 +34,24 @@ import java.util.Set;
*/
public class UserManager implements IQResultListener {
/**
* Cache of local users.
*/
private static Cache userCache;
/**
* Cache if a local or remote user exists.
*/
private static Cache registeredUsersCache;
private static UserProvider provider;
private static UserManager instance = new UserManager();
static {
// Initialize caches.
CacheManager.initializeCache("userCache", 512 * 1024);
CacheManager.initializeCache("registeredUsersCache", 512 * 1024);
CacheManager.initializeCache("username2roster", 512 * 1024);
userCache = CacheManager.getCache("userCache");
registeredUsersCache = CacheManager.getCache("registeredUsersCache");
CacheManager.initializeCache("User", "userCache", 512 * 1024);
CacheManager.initializeCache("Users Existence", "registeredUsersCache", 512 * 1024);
CacheManager.initializeCache("Roster", "username2roster", 512 * 1024);
userCache = CacheManager.getCache("User");
registeredUsersCache = CacheManager.getCache("Users Existence");
// Load a user provider.
String className = JiveGlobals.getXMLProperty("provider.user.className",
"org.jivesoftware.wildfire.user.DefaultUserProvider");
......
......@@ -59,8 +59,9 @@ public class VCardManager {
}
private VCardManager() {
CacheManager.initializeCache("vcardCache", 512 * 1024);
vcardCache = CacheManager.getCache("vcardCache");
String cacheName = "VCard";
CacheManager.initializeCache(cacheName, "vcardCache", 512 * 1024);
vcardCache = CacheManager.getCache(cacheName);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment