Commit 172ae6da authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Plugins can now define their own caches and be used in a cluster. ENT-274

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@9492 b35dd754-fafc-0310-a699-88a17e54d16e
parent 7b929499
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 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.openfire.container;
import java.util.Map;
/**
* Configuration to use when creating caches. Caches can be used when running stand alone or when
* running in a cluster. When running in a cluster a few extra parameters might be needed. Read
* {@link #getParams()} for more information.
*
* @author Gaston Dombiak
*/
public class CacheInfo {
/**
* Name of the cache
*/
private String cacheName;
/**
* Type of cache to use when running in a cluster. When not running in a cluster this value is not used.
*/
private Type type;
/**
* Map with the configuration of the cache. Openfire expects the following properties to exist:
* <ul>
* <li><b>back-size-high</b> - Maximum size of the cache. Size is in bytes. Zero means that there is no limit.</li>
* <li><b>back-size-low</b> - Size in byte of the cache after a clean up. Use zero to place no limit.</li>
* <li><b>back-expiry</b> - minutes, hours or days before content is expired. 10m, 12h or 2d. Zero means never.</li>
* </ul>
*/
private Map<String, String> params;
/**
* Creates the configuration to use for the specified cache. Caches can be used when running
* as a standalone application or when running in a cluster. When running in a cluster a few
* extra configuration are going to be needed. Read {@link #getParams()} for more information.
*
* @param cacheName name of the cache.
* @param type type of cache to use when running in a cluster. Ignored when running as standalone.
* @param params extra parameters that define cache properties like max size or expiration.
*/
public CacheInfo(String cacheName, Type type, Map<String, String> params) {
this.cacheName = cacheName;
this.type = type;
this.params = params;
}
public String getCacheName() {
return cacheName;
}
public Type getType() {
return type;
}
/**
* Returns a map with the configuration to use for the cache. When running standalone the following
* properties are required.
* <ul>
* <li><b>back-size-high</b> - Maximum size of the cache. Size is in bytes. Zero means that there is no limit.</li>
* <li><b>back-expiry</b> - minutes, hours or days before content is expired. 10m, 12h or 2d. Zero means never.</li>
* </ul>
* When running in a cluster this extra property is required. More properties can be defined depending on the
* clustering solution being used.
* <ul>
* <li><b>back-size-low</b> - Size in byte of the cache after a clean up. Use zero to place no limit.</li>
* </ul>
*
* @return map with the configuration to use for the cache.
*/
public Map<String, String> getParams() {
return params;
}
public static enum Type {
/**
* An optimistic scheme defines a cache which fully replicates all of its data to all cluster nodes that
* are running the service. This cache is good for frequent reads and not frequent writes. However, this
* cache will not scale fine if it has lot of content that will end up consuming all the JVM memory. For
* this case a {@link #distributed} is a better option.
*/
optimistic("optimistic"),
/**
* An distributed-scheme defines caches where the storage for entries is partitioned across cluster nodes.
*/
distributed("near-distributed");
private String name;
Type(String name) {
this.name = name;
}
public static Type valueof(String name) {
if ("optimistic".equals(name)) {
return optimistic;
}
return distributed;
}
public String getName() {
return name;
}
}
}
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 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.openfire.container;
import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.DocumentException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
/**
* A helper class to read cache configuration data for a plugin and register the defined caches with
* the cache factory. Definitions should look something like this:
* <code>
*
* <cache-config>
* <cache-mapping>
* <cache-name>My Cache</cache-name>
* <scheme-name>optimistic</scheme-name>
* <init-params>
* <init-param>
* <param-name>back-size-high</param-name>
* <param-value>131072</param-value>
* </init-param>
* <init-param>
* <param-name>back-expiry</param-name>
* <param-value>6h</param-value>
* </init-param>
* <init-param>
* <param-name>back-size-low</param-name>
* <param-value>117965</param-value>
* </init-param>
* </init-params>
* </cache-mapping>
* </cache-config>
*
* </code>
*/
public class PluginCacheConfigurator {
private InputStream configDataStream;
public void setInputStream(InputStream configDataStream) {
this.configDataStream = configDataStream;
}
public void configure(String pluginName) {
try {
SAXReader saxReader = new SAXReader();
saxReader.setEncoding("UTF-8");
Document cacheXml = saxReader.read(configDataStream);
List<Node> mappings = cacheXml.selectNodes("/cache-config/cache-mapping");
for (Node mapping: mappings) {
registerCache(pluginName, mapping);
}
}
catch (DocumentException e) {
e.printStackTrace();
}
}
private void registerCache(String pluginName, Node configData) {
String cacheName = configData.selectSingleNode("cache-name").getStringValue();
String schemeName = configData.selectSingleNode("scheme-name").getStringValue();
if (cacheName == null || schemeName == null) {
throw new IllegalArgumentException("Both cache-name and scheme-name elements are required. Found cache-name: " + cacheName +
" and scheme-name: " + schemeName);
}
Map<String, String> initParams = readInitParams(configData);
CacheInfo info = new CacheInfo(cacheName, CacheInfo.Type.valueof(schemeName), initParams);
PluginCacheRegistry.registerCache(pluginName, info);
}
private Map<String, String> readInitParams(Node configData) {
Map<String, String> paramMap = new HashMap<String, String>();
List<Node> params = configData.selectNodes("//init-param");
for (Node param : params) {
String paramName = param.selectSingleNode("param-name").getStringValue();
String paramValue = param.selectSingleNode("param-value").getStringValue();
paramMap.put(paramName, paramValue);
}
return paramMap;
}
}
\ No newline at end of file
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2007 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.openfire.container;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.cache.CacheFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A simple registry of cache configuration data for plugins.
*/
public class PluginCacheRegistry {
private static Map<String, CacheInfo> extraCacheMappings = new HashMap<String, CacheInfo>();
private static Map<String, List<CacheInfo>> pluginCaches = new HashMap<String, List<CacheInfo>>();
/**
* Registers cache configuration data for a give cache and plugin.
*
* @param pluginName the name of the plugin which will use the cache.
* @param info the cache configuration data.
*/
public static void registerCache(String pluginName, CacheInfo info) {
extraCacheMappings.put(info.getCacheName(), info);
List<CacheInfo> caches = pluginCaches.get(pluginName);
if (caches == null) {
caches = new ArrayList<CacheInfo>();
pluginCaches.put(pluginName, caches);
}
caches.add(info);
}
/**
* Unregisters all caches for the given plugin.
*
* @param pluginName the name of the plugin whose caches will be unregistered.
*/
public static void unregisterCaches(String pluginName) {
List<CacheInfo> caches = pluginCaches.remove(pluginName);
if (caches != null) {
for (CacheInfo info : caches) {
extraCacheMappings.remove(info.getCacheName());
try {
CacheFactory.destroyCache(info.getCacheName());
}
catch (Exception e) {
Log.warn(e);
}
}
}
}
public static CacheInfo getCacheInfo(String name) {
return extraCacheMappings.get(name);
}
}
......@@ -306,7 +306,7 @@ public class PluginManager {
// See if the parent plugin exists but just hasn't been loaded yet.
// This can only be the case if this plugin name is alphabetically before
// the parent.
if (pluginDir.getName().compareTo(parentPlugin) < 0) {
if (pluginName.compareTo(parentPlugin) < 0) {
// See if the parent exists.
File file = new File(pluginDir.getParentFile(), parentPlugin + ".jar");
if (file.exists()) {
......@@ -322,7 +322,7 @@ public class PluginManager {
return;
}
else {
String msg = "Ignoring plugin " + pluginDir.getName() + ": parent plugin " +
String msg = "Ignoring plugin " + pluginName + ": parent plugin " +
parentPlugin + " not present.";
Log.warn(msg);
System.out.println(msg);
......@@ -331,7 +331,7 @@ public class PluginManager {
}
}
else {
String msg = "Ignoring plugin " + pluginDir.getName() + ": parent plugin " +
String msg = "Ignoring plugin " + pluginName + ": parent plugin " +
parentPlugin + " not present.";
Log.warn(msg);
System.out.println(msg);
......@@ -391,8 +391,7 @@ public class PluginManager {
}
}
plugins.put(pluginDir.getName(), plugin);
plugins.put(pluginName, plugin);
pluginDirs.put(plugin, pluginDir);
// If this is a child plugin, register it as such.
......@@ -403,7 +402,7 @@ public class PluginManager {
childrenPlugins = new ArrayList<String>();
parentPluginMap.put(plugins.get(parentPlugin), childrenPlugins);
}
childrenPlugins.add(pluginDir.getName());
childrenPlugins.add(pluginName);
// Also register child to parent relationship.
childPluginMap.put(plugin, parentPlugin);
}
......@@ -439,6 +438,9 @@ public class PluginManager {
pluginDevelopment.put(plugin, dev);
}
// Configure caches of the plugin
configureCaches(pluginDir, pluginName);
// Init the plugin.
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(pluginLoader);
......@@ -494,7 +496,7 @@ public class PluginManager {
AdminConsole.addModel(pluginName, adminElement);
}
firePluginCreatedEvent(pluginDir.getName(), plugin);
firePluginCreatedEvent(pluginName, plugin);
}
else {
Log.warn("Plugin " + pluginDir + " could not be loaded: no plugin.xml file found");
......@@ -505,6 +507,20 @@ public class PluginManager {
}
}
private void configureCaches(File pluginDir, String pluginName) {
File cacheConfig = new File(pluginDir, "cache-config.xml");
if (cacheConfig.exists()) {
PluginCacheConfigurator configurator = new PluginCacheConfigurator();
try {
configurator.setInputStream(new BufferedInputStream(new FileInputStream(cacheConfig)));
configurator.configure(pluginName);
}
catch (Exception e) {
Log.error(e);
}
}
}
private void firePluginCreatedEvent(String name, Plugin plugin) {
for(PluginListener listener : pluginListeners) {
listener.pluginCreated(name, plugin);
......@@ -593,6 +609,9 @@ public class PluginManager {
}
if (plugin != null && !dir.exists()) {
// Unregister plugin caches
PluginCacheRegistry.unregisterCaches(pluginName);
plugins.remove(pluginName);
pluginDirs.remove(plugin);
classloaders.remove(plugin);
......
......@@ -93,6 +93,18 @@ public class CacheFactory {
return wrapCache(cache, name);
}
/**
* Destroys the cache for the cache name specified.
*
* @param name the name of the cache to destroy.
*/
public static void destroyCache(String name) {
Cache cache = caches.remove(name);
if (cache != null) {
cacheFactoryStrategy.destroyCache(cache);
}
}
public static void lockKey(Object key, long timeout) {
cacheFactoryStrategy.lockKey(key, timeout);
}
......
......@@ -46,6 +46,13 @@ public interface CacheFactoryStrategy {
*/
Cache createCache(String name);
/**
* Destroys the supplied cache.
*
* @param cache the cache to destroy.
*/
void destroyCache(Cache cache);
/**
* Returns true if this node is the maste node of the cluster. When not running
* in cluster mode a value of true should be returned.
......
/**
* $Revision$
* $Date$
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 1999-2007 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
* Copyright (C) 2007 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.util.cache;
import org.jivesoftware.openfire.cluster.ClusterNodeInfo;
import org.jivesoftware.openfire.container.CacheInfo;
import org.jivesoftware.openfire.container.PluginCacheRegistry;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log;
......@@ -161,7 +167,16 @@ public class DefaultLocalCacheStrategy implements CacheFactoryStrategy {
if (propname == null) {
propname = name;
}
return new DefaultCache(name, getMaxSizeFromProperty(propname), getMaxLifetimeFromProperty(propname));
// Check if there is a cache configuration. If not check for system properties to configure cache
CacheInfo cacheInfo = PluginCacheRegistry.getCacheInfo(name);
long maxSizeFromProperty = cacheInfo != null ? getMaxSizeFromProperty(cacheInfo) : getMaxSizeFromProperty(propname);
long lifetimeFromProperty = cacheInfo != null ? getMaxLifetimeFromProperty(cacheInfo) : getMaxLifetimeFromProperty(propname);
// Create cache with located properties
return new DefaultCache(name, maxSizeFromProperty, lifetimeFromProperty);
}
public void destroyCache(Cache cache) {
cache.clear();
}
public boolean isSeniorClusterMember() {
......@@ -253,6 +268,23 @@ public class DefaultLocalCacheStrategy implements CacheFactoryStrategy {
return defaultSize == null ? DEFAULT_MAX_CACHE_SIZE : defaultSize;
}
private static long getMaxSizeFromProperty(CacheInfo cacheInfo) {
String sizeProp = cacheInfo.getParams().get("back-size-high");
if (sizeProp != null) {
if ("0".equals(sizeProp)) {
return -1l;
}
try {
return Integer.parseInt(sizeProp);
}
catch (NumberFormatException nfe) {
Log.warn("Unable to parse back-size-high for cache: " + cacheInfo.getCacheName());
}
}
// Return default
return DEFAULT_MAX_CACHE_SIZE;
}
/**
* 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.
......@@ -275,4 +307,31 @@ public class DefaultLocalCacheStrategy implements CacheFactoryStrategy {
Long defaultLifetime = cacheProps.get(propName);
return defaultLifetime == null ? DEFAULT_MAX_CACHE_LIFETIME : defaultLifetime;
}
private static long getMaxLifetimeFromProperty(CacheInfo cacheInfo) {
String lifetimeProp = cacheInfo.getParams().get("back-size-high");
if (lifetimeProp != null) {
if ("0".equals(lifetimeProp)) {
return -1l;
}
long factor = 1;
if (lifetimeProp.endsWith("m")) {
factor = JiveConstants.MINUTE;
}
else if (lifetimeProp.endsWith("h")) {
factor = JiveConstants.HOUR;
}
else if (lifetimeProp.endsWith("d")) {
factor = JiveConstants.DAY;
}
try {
return Long.parseLong(lifetimeProp.substring(0, lifetimeProp.length()-1)) * factor;
}
catch (NumberFormatException nfe) {
Log.warn("Unable to parse back-size-high for cache: " + cacheInfo.getCacheName());
}
}
// Return default
return DEFAULT_MAX_CACHE_LIFETIME;
}
}
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