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