DefaultLocalCacheStrategy.java 12.8 KB
Newer Older
1 2 3 4
/**
 * $Revision$
 * $Date$
 *
Gaston Dombiak's avatar
Gaston Dombiak committed
5
 * Copyright (C) 1999-2007 Jive Software. All rights reserved.
6 7 8 9
 * This software is the proprietary information of Jive Software. Use is subject to license terms.
 */
package org.jivesoftware.util.cache;

Gaston Dombiak's avatar
Gaston Dombiak committed
10
import org.jivesoftware.openfire.cluster.ClusterNodeInfo;
11
import org.jivesoftware.util.JiveConstants;
12 13 14 15 16 17
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
18
import java.util.Map;
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

/**
 * CacheFactoryStrategy for use in Openfire. It creates and manages local caches, and it's cluster
 * related method implementations do nothing.
 *
 * @see Cache
 * @see CacheFactory
 */
public class DefaultLocalCacheStrategy implements CacheFactoryStrategy {

    public static final int DEFAULT_MAX_CACHE_SIZE = 1024 * 256;
    public static final long DEFAULT_MAX_CACHE_LIFETIME = 6 * JiveConstants.HOUR;

    /**
     * This map contains property names which were used to store cache configuration data
     * in local xml properties in previous versions.
     */
    private static final Map<String, String> cacheNames = new HashMap<String, String>();
37 38 39 40 41
    /**
     * Default properties to use for local caches. Default properties can be overridden
     * by setting the corresponding system properties.
     */
    private static final Map<String, Long> cacheProps = new HashMap<String, Long>();
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62


    public DefaultLocalCacheStrategy() {
        initCacheNameMap();
    }

    private void initCacheNameMap() {
        cacheNames.put("Favicon Hits", "faviconHits");
        cacheNames.put("Favicon Misses", "faviconMisses");
        cacheNames.put("Group", "group");
        cacheNames.put("Group Metadata Cache", "groupMeta");
        cacheNames.put("Javascript Cache", "javascript");
        cacheNames.put("Last Activity Cache", "lastActivity");
        cacheNames.put("Multicast Service", "multicast");
        cacheNames.put("Offline Message Size", "offlinemessage");
        cacheNames.put("Offline Presence Cache", "offlinePresence");
        cacheNames.put("Privacy Lists", "listsCache");
        cacheNames.put("Remote Users Existence", "remoteUsersCache");
        cacheNames.put("Roster", "username2roster");
        cacheNames.put("User", "userCache");
        cacheNames.put("VCard", "vcardCache");
63 64 65 66
        cacheNames.put("File Transfer Cache", "fileTransfer");
        cacheNames.put("File Transfer", "transferProxy");
        cacheNames.put("POP3 Authentication", "pop3");
        cacheNames.put("LDAP Authentication", "ldap");
67 68 69 70
        cacheNames.put("Routing Servers Cache", "routeServer");
        cacheNames.put("Routing Components Cache", "routeComponent");
        cacheNames.put("Routing Users Cache", "routeUser");
        cacheNames.put("Routing AnonymousUsers Cache", "routeAnonymousUser");
71
        cacheNames.put("Routing User Sessions", "routeUserSessions");
72 73 74 75
        cacheNames.put("Components Sessions", "componentsSessions");
        cacheNames.put("Connection Managers Sessions", "connManagerSessions");
        cacheNames.put("Incoming Server Sessions", "incServerSessions");
        cacheNames.put("Sessions by Hostname", "sessionsHostname");
76
        cacheNames.put("Secret Keys Cache", "secretKeys");
77
        cacheNames.put("Validated Domains", "validatedDomains");
78
        cacheNames.put("Directed Presences", "directedPresences");
Gaston Dombiak's avatar
Gaston Dombiak committed
79 80
        cacheNames.put("Disco Server Features", "serverFeatures");
        cacheNames.put("Disco Server Items", "serverItems");
81
        cacheNames.put("Remote Server Configurations", "serversConfigurations");
82
        cacheNames.put("Entity Capabilities", "entityCapabilities");
83
        cacheNames.put("Entity Capabilities Users", "entityCapabilitiesUsers");
84
        cacheNames.put("Entity Capabilities Pending Hashes", "entityCapabilitiesPendingHashes");
85 86

        cacheProps.put("cache.fileTransfer.size", 128 * 1024l);
87
        cacheProps.put("cache.fileTransfer.maxLifetime", 1000 * 60 * 10l);
88
        cacheProps.put("cache.multicast.size", 128 * 1024l);
89
        cacheProps.put("cache.multicast.maxLifetime", JiveConstants.DAY);
90
        cacheProps.put("cache.offlinemessage.size", 100 * 1024l);
91
        cacheProps.put("cache.offlinemessage.maxLifetime", JiveConstants.HOUR * 12);
92
        cacheProps.put("cache.pop3.size", 512 * 1024l);
93
        cacheProps.put("cache.pop3.maxLifetime", JiveConstants.HOUR);
94
        cacheProps.put("cache.transferProxy.size", -1l);
95
        cacheProps.put("cache.transferProxy.maxLifetime", 1000 * 60 * 10l);
96
        cacheProps.put("cache.group.size", 1024 * 1024l);
97
        cacheProps.put("cache.group.maxLifetime", JiveConstants.MINUTE * 15);
98
        cacheProps.put("cache.groupMeta.size", 512 * 1024l);
99
        cacheProps.put("cache.groupMeta.maxLifetime", JiveConstants.MINUTE * 15);
100
        cacheProps.put("cache.javascript.size", 128 * 1024l);
101
        cacheProps.put("cache.javascript.maxLifetime", 3600 * 24 * 10l);
102
        cacheProps.put("cache.ldap.size", 512 * 1024l);
103
        cacheProps.put("cache.ldap.maxLifetime", JiveConstants.HOUR * 2);
104 105 106 107
        cacheProps.put("cache.listsCache.size", 512 * 1024l);
        cacheProps.put("cache.offlinePresence.size", 512 * 1024l);
        cacheProps.put("cache.lastActivity.size", 128 * 1024l);
        cacheProps.put("cache.userCache.size", 512 * 1024l);
108
        cacheProps.put("cache.userCache.maxLifetime", JiveConstants.MINUTE * 30);
109
        cacheProps.put("cache.remoteUsersCache.size", 512 * 1024l);
110
        cacheProps.put("cache.remoteUsersCache.maxLifetime", JiveConstants.MINUTE * 30);
111 112 113
        cacheProps.put("cache.vcardCache.size", 512 * 1024l);
        cacheProps.put("cache.faviconHits.size", 128 * 1024l);
        cacheProps.put("cache.faviconMisses.size", 128 * 1024l);
114
        cacheProps.put("cache.routeServer.size", -1l);
115
        cacheProps.put("cache.routeServer.maxLifetime", -1l);
116
        cacheProps.put("cache.routeComponent.size", -1l);
117
        cacheProps.put("cache.routeComponent.maxLifetime", -1l);
118
        cacheProps.put("cache.routeUser.size", -1l);
119
        cacheProps.put("cache.routeUser.maxLifetime", -1l);
120
        cacheProps.put("cache.routeAnonymousUser.size", -1l);
121
        cacheProps.put("cache.routeAnonymousUser.maxLifetime", -1l);
122
        cacheProps.put("cache.routeUserSessions.size", -1l);
123
        cacheProps.put("cache.routeUserSessions.maxLifetime", -1l);
124
        cacheProps.put("cache.componentsSessions.size", -1l);
125
        cacheProps.put("cache.componentsSessions.maxLifetime", -1l);
126
        cacheProps.put("cache.connManagerSessions.size", -1l);
127
        cacheProps.put("cache.connManagerSessions.maxLifetime", -1l);
128
        cacheProps.put("cache.incServerSessions.size", -1l);
129
        cacheProps.put("cache.incServerSessions.maxLifetime", -1l);
130
        cacheProps.put("cache.sessionsHostname.size", -1l);
131
        cacheProps.put("cache.sessionsHostname.maxLifetime", -1l);
132
        cacheProps.put("cache.secretKeys.size", -1l);
133
        cacheProps.put("cache.secretKeys.maxLifetime", -1l);
134
        cacheProps.put("cache.validatedDomains.size", -1l);
135
        cacheProps.put("cache.validatedDomains.maxLifetime", -1l);
136
        cacheProps.put("cache.directedPresences.size", -1l);
137
        cacheProps.put("cache.directedPresences.maxLifetime", -1l);
Gaston Dombiak's avatar
Gaston Dombiak committed
138
        cacheProps.put("cache.serverFeatures.size", -1l);
139
        cacheProps.put("cache.serverFeatures.maxLifetime", -1l);
Gaston Dombiak's avatar
Gaston Dombiak committed
140
        cacheProps.put("cache.serverItems.size", -1l);
141
        cacheProps.put("cache.serverItems.maxLifetime", -1l);
142 143
        cacheProps.put("cache.serversConfigurations.size", 128 * 1024l);
        cacheProps.put("cache.serversConfigurations.maxLifetime", JiveConstants.MINUTE * 30);
144 145
        cacheProps.put("cache.entityCapabilities.size", -1l);
        cacheProps.put("cache.entityCapabilities.maxLifetime", JiveConstants.DAY * 2);
146 147
        cacheProps.put("cache.entityCapabilitiesUsers.size", -1l);
        cacheProps.put("cache.entityCapabilitiesUsers.maxLifetime", JiveConstants.DAY * 2);
148 149
        cacheProps.put("cache.entityCapabilitiesPendingHashes.size", -1l);
        cacheProps.put("cache.entityCapabilitiesPendingHashes.maxLifetime", JiveConstants.DAY * 2);
150 151 152 153 154 155 156 157 158 159 160 161 162 163
    }

    public boolean startCluster() {
        return false;
    }

    public void stopCluster() {
    }

    public Cache createCache(String name) {
        String propname = cacheNames.get(name);
        if (propname == null) {
            propname = name;
        }
164
        return new DefaultCache(name, getMaxSizeFromProperty(propname), getMaxLifetimeFromProperty(propname));
165 166 167 168
    }

    public boolean isSeniorClusterMember() {
        return true;
Gaston Dombiak's avatar
Gaston Dombiak committed
169 170 171 172
    }

    public Collection<ClusterNodeInfo> getClusterNodesInfo() {
        return Collections.emptyList();
Gaston Dombiak's avatar
Gaston Dombiak committed
173 174 175 176
    }

    public int getMaxClusterNodes() {
        return 0;
177 178
    }

179 180 181 182
    public byte[] getSeniorClusterMemberID() {
        return null;
    }

183
    public byte[] getClusterMemberID() {
184
        return new byte[0];
185 186 187 188 189
    }

    public void doClusterTask(final ClusterTask task) {
    }

190
    public boolean doClusterTask(ClusterTask task, byte[] nodeID) {
191
        throw new IllegalStateException("Cluster service is not available");
192 193
    }

194 195 196 197
    public Collection<Object> doSynchronousClusterTask(ClusterTask task, boolean includeLocalMember) {
        return Collections.emptyList();
    }

198
    public Object doSynchronousClusterTask(ClusterTask task, byte[] nodeID) {
199
        throw new IllegalStateException("Cluster service is not available");
200 201
    }

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    public void updateCacheStats(Map<String, Cache> caches) {
    }

    public void lockKey(Object key, long timeout) {
    }

    public void unlockKey(Object key) {
    }

    /**
     * Sets a local property which overrides the maximum cache size as configured in coherence-cache-config.xml for the
     * supplied cache name.
     * @param cacheName the name of the cache to store a value for.
     * @param size the maximum cache size.
     */
    public static void setMaxSizeProperty(String cacheName, int size) {
        cacheName = cacheName.replaceAll(" ", "");
219
        JiveGlobals.setProperty("cache." + cacheName + ".size", Integer.toString(size));
220 221 222 223 224 225 226 227 228 229
    }

    /**
     * Sets a local property which overrides the maximum cache entry lifetime as configured in coherence-cache-config.xml
     * for the supplied cache name.
     * @param cacheName the name of the cache to store a value for.
     * @param lifetime the maximum cache entry lifetime.
     */
    public static void setMaxLifetimeProperty(String cacheName, long lifetime) {
        cacheName = cacheName.replaceAll(" ", "");
230
        JiveGlobals.setProperty(("cache." + cacheName + ".maxLifetime"), Long.toString(lifetime));
231 232 233
    }

    /**
234 235 236
     * If a local property is found for the supplied name which specifies a value for cache size, it is returned.
     * Otherwise, the defaultSize argument is returned.
     * 
237 238 239
     * @param cacheName the name of the cache to look up a corresponding property for.
     * @return either the property value or the default value.
     */
240
    private static long getMaxSizeFromProperty(String cacheName) {
241
        String propName = "cache." + cacheName.replaceAll(" ", "") + ".size";
242
        String sizeProp = JiveGlobals.getProperty(propName);
243 244 245 246 247
        if (sizeProp != null) {
            try {
                return Integer.parseInt(sizeProp);
            }
            catch (NumberFormatException nfe) {
248
                Log.warn("Unable to parse " + propName + " using default value.");
249 250
            }
        }
251 252 253
        // Check if there is a default size value for this cache 
        Long defaultSize = cacheProps.get(propName);
        return defaultSize == null ? DEFAULT_MAX_CACHE_SIZE : defaultSize;
254 255 256
    }

     /**
257 258 259
     * If a local property is found for the supplied name which specifies a value for cache entry lifetime, it
      * is returned. Otherwise, the defaultLifetime argument is returned.
      *
260 261 262
     * @param cacheName the name of the cache to look up a corresponding property for.
     * @return either the property value or the default value.
     */
263
    private static long getMaxLifetimeFromProperty(String cacheName) {
264
        String propName = "cache." + cacheName.replaceAll(" ", "") + ".maxLifetime";
265
        String lifetimeProp = JiveGlobals.getProperty(propName);
266 267 268 269 270
        if (lifetimeProp != null) {
            try {
                return Long.parseLong(lifetimeProp);
            }
            catch (NumberFormatException nfe) {
271
                Log.warn("Unable to parse " + propName + " using default value.");
272 273
            }
        }
274 275 276
         // Check if there is a default lifetime value for this cache
         Long defaultLifetime = cacheProps.get(propName);
         return defaultLifetime == null ? DEFAULT_MAX_CACHE_LIFETIME : defaultLifetime;
277 278
    }
}