[jboss-cvs] JBossCache/src/org/jboss/cache/config ...
Brian Stansberry
brian.stansberry at jboss.com
Mon Oct 23 01:46:39 EDT 2006
User: bstansberry
Date: 06/10/23 01:46:39
Modified: src/org/jboss/cache/config Configuration.java
CacheLoaderConfig.java
Added: src/org/jboss/cache/config EvictionConfig.java
ConfigurationComponent.java
BuddyReplicationConfig.java
Log:
[JBCACHE-809] Pojo-style configuration for use by Microcontainer
Revision Changes Path
1.22 +126 -47 JBossCache/src/org/jboss/cache/config/Configuration.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Configuration.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/config/Configuration.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -b -r1.21 -r1.22
--- Configuration.java 25 Sep 2006 05:50:03 -0000 1.21
+++ Configuration.java 23 Oct 2006 05:46:39 -0000 1.22
@@ -6,8 +6,6 @@
*/
package org.jboss.cache.config;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.jboss.cache.TreeCache;
import org.jboss.cache.Version;
import org.jboss.cache.lock.IsolationLevel;
@@ -16,17 +14,16 @@
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
-import java.io.Serializable;
-
/**
- * Implementation of the Configuration interface.
+ * Encapsulates the configuration of a Cache.
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
*/
-public class Configuration implements Serializable, Cloneable
+public class Configuration
+ extends ConfigurationComponent
+ implements Cloneable
{
- private Log log = LogFactory.getLog(Configuration.class);
- private TreeCache cache; // back-reference to test whether the cache is running.
+ private static final long serialVersionUID = 5553791890144997466L;
/**
* Public enum that encapsulates cache mode
@@ -74,14 +71,17 @@
private IsolationLevel isolationLevel = IsolationLevel.REPEATABLE_READ;
@Dynamic
private Element evictionPolicyConfig = null;
+ private EvictionConfig evictionConfig = null;
private boolean useRegionBasedMarshalling = false;
private String transactionManagerLookupClass = null;
private Element cacheLoaderConfiguration = null;
+ private CacheLoaderConfig cacheLoaderConfig = null;
@Dynamic
private boolean syncCommitPhase = false;
@Dynamic
private boolean syncRollbackPhase = false;
- private Element buddyReplicationConfig;
+ private Element buddyReplicationElement;
+ private BuddyReplicationConfig buddyReplicationConfig;
private boolean nodeLockingOptimistic = false;
private NodeLockingScheme nodeLockingScheme = NodeLockingScheme.PESSIMISTIC;
private String muxServiceName = null;
@@ -98,7 +98,7 @@
*/
public Configuration(TreeCache cache)
{
- this.cache = cache;
+ setTreeCache(cache);
}
/**
@@ -184,6 +184,10 @@
{
testImmutability("evictionPolicyClass");
this.evictionPolicyClass = evictionPolicyClass;
+ if (evictionPolicyConfig != null && evictionConfig == null)
+ {
+ createEvictionConfig();
+ }
}
public void setCacheMode(CacheMode cacheModeInt)
@@ -204,6 +208,16 @@
}
}
+ public String getCacheModeString()
+ {
+ return cacheMode == null ? null : cacheMode.toString();
+ }
+
+ public void setCacheModeString(String cacheMode)
+ {
+ setCacheMode(cacheMode);
+ }
+
public void setInactiveOnStartup(boolean inactiveOnStartup)
{
testImmutability("inactiveOnStartup");
@@ -214,6 +228,33 @@
{
testImmutability("evictionPolicyConfig");
this.evictionPolicyConfig = evictionPolicyConfig;
+ if (evictionPolicyClass != null && evictionConfig == null)
+ {
+ createEvictionConfig();
+ }
+ }
+
+ public EvictionConfig getEvictionConfig()
+ {
+ if (evictionConfig == null)
+ {
+ createEvictionConfig();
+ }
+ return evictionConfig;
+ }
+
+ public void setEvictionConfig(EvictionConfig config)
+ {
+ testImmutability("evictionConfig");
+ this.evictionConfig = config;
+ }
+
+ private void createEvictionConfig()
+ {
+ if (evictionPolicyConfig != null || evictionPolicyClass != null)
+ {
+ evictionConfig = new EvictionConfig(evictionPolicyConfig, evictionPolicyClass);
+ }
}
public void setUseRegionBasedMarshalling(boolean useRegionBasedMarshalling)
@@ -231,9 +272,28 @@
public void setCacheLoaderConfiguration(Element cacheLoaderConfiguration)
{
testImmutability("cacheLoaderConfiguration");
+ if (cacheLoaderConfiguration == null)
+ {
+ setCacheLoaderConfig(null);
+ }
+ else
+ {
+ setCacheLoaderConfig(new CacheLoaderConfig(cacheLoaderConfiguration));
+ }
+ // cache the element *after* the config, as the call to cache
+ // the config nulls the element field
this.cacheLoaderConfiguration = cacheLoaderConfiguration;
}
+ public void setCacheLoaderConfig(CacheLoaderConfig config)
+ {
+ testImmutability("cacheLoaderConfig");
+ replaceChildConfig(this.cacheLoaderConfig, config);
+ this.cacheLoaderConfig = config;
+ // null out the element
+ this.cacheLoaderConfiguration = null;
+ }
+
public void setSyncCommitPhase(boolean syncCommitPhase)
{
testImmutability("syncCommitPhase");
@@ -246,10 +306,29 @@
this.syncRollbackPhase = syncRollbackPhase;
}
- public void setBuddyReplicationConfig(Element buddyReplicationConfig)
+ public void setBuddyReplicationConfig(Element buddyReplicationElement)
+ {
+ testImmutability("buddyReplicationConfig");
+ if (buddyReplicationElement == null)
+ {
+ setBuddyReplicationConfiguration(null);
+ }
+ else
+ {
+ setBuddyReplicationConfiguration(new BuddyReplicationConfig(buddyReplicationElement));
+ }
+ // cache the element *after* the config, as the call to cache
+ // the config nulls the element field
+ this.buddyReplicationElement = buddyReplicationElement;
+ }
+
+ public void setBuddyReplicationConfiguration(BuddyReplicationConfig config)
{
testImmutability("buddyReplicationConfig");
- this.buddyReplicationConfig = buddyReplicationConfig;
+ replaceChildConfig(this.buddyReplicationConfig, config);
+ this.buddyReplicationConfig = config;
+ // null out the element as it may be out of sync
+ buddyReplicationElement = null;
}
public void setNodeLockingScheme(NodeLockingScheme nodeLockingScheme)
@@ -302,6 +381,16 @@
this.nodeLockingOptimistic = (this.nodeLockingScheme == NodeLockingScheme.OPTIMISTIC);
}
+ public String getNodeLockingSchemeString()
+ {
+ return nodeLockingScheme == null ? null : nodeLockingScheme.toString();
+ }
+
+ public void setNodeLockingSchemeString(String nodeLockingScheme)
+ {
+ setNodeLockingScheme(nodeLockingScheme);
+ }
+
public void setIsolationLevel(String isolationLevel)
{
testImmutability("isolationLevel");
@@ -314,6 +403,16 @@
}
}
+ public String getIsolationLevelString()
+ {
+ return isolationLevel == null ? null : isolationLevel.toString();
+ }
+
+ public void setIsolationLevelString(String isolationLevel)
+ {
+ setIsolationLevel(isolationLevel);
+ }
+
public void setMultiplexerStack(String stackName)
{
testImmutability("muxStackName");
@@ -442,6 +541,11 @@
return cacheLoaderConfiguration;
}
+ public CacheLoaderConfig getCacheLoaderConfig()
+ {
+ return cacheLoaderConfig;
+ }
+
public boolean isSyncCommitPhase()
{
return syncCommitPhase;
@@ -454,6 +558,11 @@
public Element getBuddyReplicationConfig()
{
+ return buddyReplicationElement;
+ }
+
+ public BuddyReplicationConfig getBuddyReplicationConfiguration()
+ {
return buddyReplicationConfig;
}
@@ -542,36 +651,6 @@
return buffer.toString();
}
- /**
- * Checks field modifications via setters
- *
- * @param fieldName
- */
- private void testImmutability(String fieldName)
- {
- try
- {
- if (cache != null && cache.started && !getClass().getDeclaredField(fieldName).isAnnotationPresent(Dynamic.class))
- {
- throw new ConfigurationException("Attempted to modify a non-Dynamic configuration element [" + fieldName + "] after the cache has started!");
- }
- }
- catch (NoSuchFieldException e)
- {
- log.warn("Field " + fieldName + " not found!!");
- }
- }
-
- /**
- * Sets a back-reference to the cache associated with this configuration
- *
- * @param cache
- */
- public void setTreeCache(TreeCache cache)
- {
- this.cache = cache;
- }
-
// ------------------------------------------------------------------------------------------------------------
// OVERRIDDEN METHODS
// ------------------------------------------------------------------------------------------------------------
@@ -598,11 +677,11 @@
if (useMbean != that.useMbean) return false;
if (useRegionBasedMarshalling != that.useRegionBasedMarshalling) return false;
if (useReplQueue != that.useReplQueue) return false;
- if (buddyReplicationConfig != null ? !buddyReplicationConfig.equals(that.buddyReplicationConfig) : that.buddyReplicationConfig != null)
+ if (buddyReplicationConfig != null ? !buddyReplicationConfig.equals(that.buddyReplicationConfig) : that.buddyReplicationElement != null)
{
return false;
}
- if (cacheLoaderConfiguration != null ? !cacheLoaderConfiguration.equals(that.cacheLoaderConfiguration) : that.cacheLoaderConfiguration != null)
+ if (cacheLoaderConfig != null ? !cacheLoaderConfig.equals(that.cacheLoaderConfig) : that.cacheLoaderConfiguration != null)
{
return false;
}
@@ -634,8 +713,8 @@
public int hashCode()
{
- int result;
- result = (clusterName != null ? clusterName.hashCode() : 0);
+ int result = 17;
+ result = 29 * result + (clusterName != null ? clusterName.hashCode() : 0);
result = 29 * result + (clusterConfig != null ? clusterConfig.hashCode() : 0);
result = 29 * result + (useReplQueue ? 1 : 0);
result = 29 * result + replQueueMaxElements;
@@ -653,7 +732,7 @@
result = 29 * result + (evictionPolicyConfig != null ? evictionPolicyConfig.hashCode() : 0);
result = 29 * result + (useRegionBasedMarshalling ? 1 : 0);
result = 29 * result + (transactionManagerLookupClass != null ? transactionManagerLookupClass.hashCode() : 0);
- result = 29 * result + (cacheLoaderConfiguration != null ? cacheLoaderConfiguration.hashCode() : 0);
+ result = 29 * result + (cacheLoaderConfig != null ? cacheLoaderConfig.hashCode() : 0);
result = 29 * result + (syncCommitPhase ? 1 : 0);
result = 29 * result + (syncRollbackPhase ? 1 : 0);
result = 29 * result + (buddyReplicationConfig != null ? buddyReplicationConfig.hashCode() : 0);
1.16 +184 -28 JBossCache/src/org/jboss/cache/config/CacheLoaderConfig.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CacheLoaderConfig.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/config/CacheLoaderConfig.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -b -r1.15 -r1.16
--- CacheLoaderConfig.java 29 Sep 2006 18:27:21 -0000 1.15
+++ CacheLoaderConfig.java 23 Oct 2006 05:46:39 -0000 1.16
@@ -6,28 +6,71 @@
*/
package org.jboss.cache.config;
-import org.jboss.cache.xml.XmlHelper;
-
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
-import java.io.IOException;
-import java.io.ByteArrayInputStream;
+
+import org.jboss.cache.xml.XmlHelper;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
/**
* Holds the configuration of the cache loader chain. ALL cache loaders should be defined using this class, adding
* individual cache loaders to the chain by calling {@see CacheLoaderConfig#addIndividualCacheLoaderConfig}
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
+ * @author Brian Stansberry
*/
-public class CacheLoaderConfig
+public class CacheLoaderConfig extends ConfigurationComponent
{
+ private static final long serialVersionUID = 2210349340378984424L;
+
private boolean passivation;
private String preload;
- private List cacheLoaderConfigs = new ArrayList();
+ private List<IndividualCacheLoaderConfig> cacheLoaderConfigs = new ArrayList<IndividualCacheLoaderConfig>();
private boolean shared;
+ public CacheLoaderConfig()
+ {
+ }
+
+ public CacheLoaderConfig(Element cacheLoaderConfig)
+ {
+ setPassivation(XmlHelper.readBooleanContents(cacheLoaderConfig, "passivation"));
+ setPreload(XmlHelper.readStringContents(cacheLoaderConfig, "preload"));
+ setShared(XmlHelper.readBooleanContents(cacheLoaderConfig, "shared"));
+ NodeList cacheLoaderNodes = cacheLoaderConfig.getElementsByTagName("cacheloader");
+ for (int i = 0; i < cacheLoaderNodes.getLength(); i++)
+ {
+ Node node = cacheLoaderNodes.item(i);
+ if (node.getNodeType() == Node.ELEMENT_NODE)
+ {
+ Element element = (Element) node;
+ CacheLoaderConfig.IndividualCacheLoaderConfig clc = new CacheLoaderConfig.IndividualCacheLoaderConfig();
+ clc.setAsync(XmlHelper.readBooleanContents(element, "async", false));
+ clc.setIgnoreModifications(XmlHelper.readBooleanContents(element, "ignoreModifications", false));
+ clc.setFetchPersistentState(XmlHelper.readBooleanContents(element, "fetchPersistentState", false));
+ clc.setPurgeOnStartup(XmlHelper.readBooleanContents(element, "purgeOnStartup", false));
+ clc.setClassName(XmlHelper.readStringContents(element, "class"));
+ try
+ {
+ clc.setProperties(XmlHelper.readPropertiesContents(element, "properties"));
+ }
+ catch (IOException e)
+ {
+ throw new ConfigurationException("Problem loader cache loader properties", e);
+ }
+ clc.setSingletonStore(XmlHelper.readBooleanContents(element, "singletonStore", false));
+ clc.setPushStateWhenCoordinator(XmlHelper.readBooleanAttribute(element, "singletonStore", "pushStateWhenCoordinator", false));
+ addIndividualCacheLoaderConfig(clc);
+ }
+ }
+ }
+
public String getPreload()
{
return preload;
@@ -35,11 +78,13 @@
public void setPreload(String preload)
{
+ testImmutability("preload");
this.preload = preload;
}
public void setPassivation(boolean passivation)
{
+ testImmutability("passivation");
this.passivation = passivation;
}
@@ -50,7 +95,10 @@
public void addIndividualCacheLoaderConfig(IndividualCacheLoaderConfig clc)
{
+ testImmutability("cacheLoaderConfigs");
cacheLoaderConfigs.add(clc);
+ // Ensure this config gets our back ref to the cache
+ addChildConfig(clc);
}
public List getIndividualCacheLoaderConfigs()
@@ -58,6 +106,14 @@
return cacheLoaderConfigs;
}
+ public void setIndividualCacheLoaderConfigs(List<IndividualCacheLoaderConfig> configs)
+ {
+ testImmutability("cacheLoaderConfigs");
+ // Ensure these configs get our back ref to the cache
+ replaceChildConfigs(this.cacheLoaderConfigs, configs);
+ this.cacheLoaderConfigs = configs == null ? new ArrayList<IndividualCacheLoaderConfig>() : configs;
+ }
+
public IndividualCacheLoaderConfig getFirstCacheLoaderConfig()
{
if (cacheLoaderConfigs.size() == 0) return null;
@@ -76,6 +132,7 @@
public void setShared(boolean shared)
{
+ testImmutability("shared");
this.shared = shared;
}
@@ -84,14 +141,43 @@
return shared;
}
+ public boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+
+ if (obj instanceof CacheLoaderConfig)
+ {
+ CacheLoaderConfig other = (CacheLoaderConfig) obj;
+ return (this.passivation == other.passivation)
+ && (this.shared == other.shared)
+ && safeEquals(this.preload, other.preload)
+ && safeEquals(this.cacheLoaderConfigs, other.cacheLoaderConfigs);
+ }
+ return false;
+ }
+
+ public int hashCode()
+ {
+ int result = 19;
+ result = 51 * result + (passivation ? 0 : 1);
+ result = 51 * result + (shared ? 0 : 1);
+ result = 51 * result + (preload == null ? 0 : preload.hashCode());
+ result = 51 * result + (cacheLoaderConfigs == null ? 0 : cacheLoaderConfigs.hashCode());
+ return result;
+ }
+
+
/**
* Configuration object that holds the confguration of an individual cache loader.
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
*/
- public static class IndividualCacheLoaderConfig
+ public static class IndividualCacheLoaderConfig extends ConfigurationComponent
{
+ private static final long serialVersionUID = -2282396799100828593L;
+
private String className;
private boolean async;
private boolean ignoreModifications;
@@ -99,13 +185,33 @@
private boolean singletonStore;
private boolean pushStateWhenCoordinator;
+ private boolean purgeOnStartup;
+
+ private Properties properties;
+
+ public IndividualCacheLoaderConfig() {}
+
+ protected void populateFromBaseConfig(IndividualCacheLoaderConfig base)
+ {
+ if (base != null)
+ {
+ setAsync(base.isAsync());
+ setIgnoreModifications(base.isIgnoreModifications());
+ setFetchPersistentState(base.isFetchPersistentState());
+ setSingletonStore(base.isSingletonStore());
+ setPushStateWhenCoordinator(base.isPushStateWhenCoordinator());
+ setPurgeOnStartup(base.isPurgeOnStartup());
+ Properties props = base.getProperties();
+ if (props != null)
+ setProperties(props);
+ }
+ }
+
public boolean isPurgeOnStartup()
{
return purgeOnStartup;
}
- private boolean purgeOnStartup;
-
public boolean isFetchPersistentState()
{
return fetchPersistentState;
@@ -113,13 +219,13 @@
public void setFetchPersistentState(boolean fetchPersistentState)
{
+ testImmutability("fetchPersistentState");
this.fetchPersistentState = fetchPersistentState;
}
- private Properties properties;
-
public void setClassName(String className)
{
+ testImmutability("className");
this.className = className;
}
@@ -130,6 +236,7 @@
public void setAsync(boolean async)
{
+ testImmutability("async");
this.async = async;
}
@@ -140,6 +247,7 @@
public void setIgnoreModifications(boolean ignoreModifications)
{
+ testImmutability("ignoreModifications");
this.ignoreModifications = ignoreModifications;
}
@@ -151,6 +259,8 @@
public void setProperties(String properties) throws IOException
{
if (properties == null) return;
+
+ testImmutability("properties");
// JBCACHE-531: escape all backslash characters
// replace any "\" that is not preceded by a backslash with "\\"
properties = XmlHelper.escapeBackslashes(properties);
@@ -162,6 +272,7 @@
public void setProperties(Properties properties)
{
+ testImmutability("properties");
this.properties = properties;
}
@@ -170,22 +281,9 @@
return properties;
}
-
- public String toString()
- {
- return new StringBuffer().append("IndividualCacheLoaderConfig{").append("className='").append(className).append('\'')
- .append(", async=").append(async)
- .append(", ignoreModifications=").append(ignoreModifications)
- .append(", fetchPersistentState=").append(fetchPersistentState)
- .append(", properties=").append(properties).append('}')
- .append(", purgeOnStartup=").append(purgeOnStartup)
- .append(", singletonStore=").append(singletonStore)
- .append(", singletonStore.pushStateWhenCoordinator=").append(pushStateWhenCoordinator)
- .toString();
- }
-
public void setPurgeOnStartup(boolean purgeOnStartup)
{
+ testImmutability("purgeOnStartup");
this.purgeOnStartup = purgeOnStartup;
}
@@ -196,6 +294,7 @@
public void setSingletonStore(boolean singletonStore)
{
+ testImmutability("singletonStore");
this.singletonStore = singletonStore;
}
@@ -206,6 +305,7 @@
public void setPushStateWhenCoordinator(boolean pushStateWhenCoordinator)
{
+ testImmutability("pushStateWhenCoordinator");
if (singletonStore)
{
/* pushStateWhenCoordinator only makes sense if the cache loader
@@ -213,5 +313,61 @@
this.pushStateWhenCoordinator = pushStateWhenCoordinator;
}
}
+
+ public boolean equals(Object obj)
+ {
+ return equalsExcludingProperties(obj)
+ && safeEquals(this.properties, ((IndividualCacheLoaderConfig)obj).properties);
+ }
+
+ protected boolean equalsExcludingProperties(Object obj)
+ {
+ if (this == obj)
+ return true;
+
+ if (obj instanceof IndividualCacheLoaderConfig)
+ {
+ IndividualCacheLoaderConfig other = (IndividualCacheLoaderConfig) obj;
+ return safeEquals(this.className, other.className)
+ && (this.async == other.async)
+ && (this.ignoreModifications == other.ignoreModifications)
+ && (this.fetchPersistentState == other.fetchPersistentState)
+ && (this.singletonStore == other.singletonStore)
+ && (this.pushStateWhenCoordinator == other.pushStateWhenCoordinator);
+ }
+ return false;
+
+ }
+
+ public int hashCode()
+ {
+ return 31 * hashCodeExcludingProperties() + (properties == null ? 0 : properties.hashCode());
+ }
+
+ protected int hashCodeExcludingProperties()
+ {
+ int result = 17;
+ result = 31 * result + (className == null ? 0 : className.hashCode());
+ result = 31 * result + (async ? 0 : 1);
+ result = 31 * result + (ignoreModifications ? 0 : 1);
+ result = 31 * result + (fetchPersistentState ? 0 : 1);
+ result = 31 * result + (singletonStore ? 0 : 1);
+ result = 31 * result + (pushStateWhenCoordinator ? 0 : 1);
+ result = 31 * result + (purgeOnStartup ? 0 : 1);
+ return result;
+ }
+
+ public String toString()
+ {
+ return new StringBuffer().append("IndividualCacheLoaderConfig{").append("className='").append(className).append('\'')
+ .append(", async=").append(async)
+ .append(", ignoreModifications=").append(ignoreModifications)
+ .append(", fetchPersistentState=").append(fetchPersistentState)
+ .append(", properties=").append(properties).append('}')
+ .append(", purgeOnStartup=").append(purgeOnStartup)
+ .append(", singletonStore=").append(singletonStore)
+ .append(", singletonStore.pushStateWhenCoordinator=").append(pushStateWhenCoordinator)
+ .toString();
+ }
}
}
1.1 date: 2006/10/23 05:46:39; author: bstansberry; state: Exp;JBossCache/src/org/jboss/cache/config/EvictionConfig.java
Index: EvictionConfig.java
===================================================================
package org.jboss.cache.config;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.eviction.EvictionConfiguration;
import org.jboss.cache.eviction.EvictionPolicy;
import org.jboss.cache.xml.XmlHelper;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class EvictionConfig extends ConfigurationComponent
{
/** The serialVersionUID */
private static final long serialVersionUID = -7979639000026975201L;
private String defaultEvictionPolicyClass;
private int wakeupIntervalSeconds = EvictionConfiguration.WAKEUP_DEFAULT;;
private List evictionRegionConfigs;
public EvictionConfig() {}
public EvictionConfig(String defaultEvictionClass)
{
setDefaultEvictionPolicyClass(defaultEvictionClass);
}
public EvictionConfig(Element element, String defaultEvictionClass)
{
setDefaultEvictionPolicyClass(defaultEvictionClass);
if (element != null)
{
String temp = XmlHelper.getTagContents(element,
EvictionConfiguration.WAKEUP_INTERVAL_SECONDS, EvictionConfiguration.ATTR, EvictionConfiguration.NAME);
if (temp != null)
{
wakeupIntervalSeconds = Integer.parseInt(temp);
}
if (wakeupIntervalSeconds <= 0)
wakeupIntervalSeconds = EvictionConfiguration.WAKEUP_DEFAULT;
NodeList list = element.getElementsByTagName(EvictionConfiguration.REGION);
List regionConfigs = new ArrayList(list.getLength());
for (int i = 0; i < list.getLength(); i++)
{
org.w3c.dom.Node node = list.item(i);
if (node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE)
continue;
try
{
regionConfigs.add(new EvictionRegionConfig((Element) node, defaultEvictionClass));
}
catch (MissingPolicyException ignored)
{
LogFactory.getLog(getClass()).warn(ignored.getLocalizedMessage());
}
}
setEvictionRegionConfigs(regionConfigs);
}
}
public boolean isValidConfig()
{
return defaultEvictionPolicyClass != null
|| (evictionRegionConfigs != null && evictionRegionConfigs.size() > 0);
}
public String getDefaultEvictionPolicyClass()
{
return defaultEvictionPolicyClass;
}
public void setDefaultEvictionPolicyClass(String defaultEvictionPolicyClass)
{
testImmutability("defaultEvictionPolicyClass");
this.defaultEvictionPolicyClass = defaultEvictionPolicyClass;
}
public List getEvictionRegionConfigs()
{
return evictionRegionConfigs;
}
public void setEvictionRegionConfigs(List evictionRegionConfigs)
{
testImmutability("evictionRegionConfigs");
// Make sure region configs built by MC have a policy class
if (evictionRegionConfigs != null && this.defaultEvictionPolicyClass != null)
{
for (Iterator it = evictionRegionConfigs.iterator(); it.hasNext();)
{
EvictionRegionConfig cfg = (EvictionRegionConfig) it.next();
if (cfg.getEvictionPolicyClass() == null
|| cfg.getEvictionPolicyClass().length() == 0)
{
cfg.setEvictionPolicyClass(this.defaultEvictionPolicyClass);
}
}
}
replaceChildConfigs(this.evictionRegionConfigs, evictionRegionConfigs);
this.evictionRegionConfigs = evictionRegionConfigs;
}
public int getWakeupIntervalSeconds()
{
return wakeupIntervalSeconds;
}
public void setWakeupIntervalSeconds(int wakeupIntervalSeconds)
{
testImmutability("wakeupIntervalSeconds");
this.wakeupIntervalSeconds = wakeupIntervalSeconds;
}
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj instanceof EvictionConfig)
{
EvictionConfig other = (EvictionConfig) obj;
return (this.wakeupIntervalSeconds == other.wakeupIntervalSeconds)
&& safeEquals(this.defaultEvictionPolicyClass, other.defaultEvictionPolicyClass)
&& safeEquals(this.evictionRegionConfigs, other.evictionRegionConfigs);
}
return false;
}
public int hashCode()
{
int result = 17;
result = 37 * result + wakeupIntervalSeconds;
result = 37 * result + (defaultEvictionPolicyClass == null ? 0 : defaultEvictionPolicyClass.hashCode());
result = 37 * result + (evictionRegionConfigs == null ? 0 : evictionRegionConfigs.hashCode());
return result;
}
public static class EvictionRegionConfig extends ConfigurationComponent
{
/** The serialVersionUID */
private static final long serialVersionUID = 8384302335667342493L;
private Fqn regionFqn;
private String evictionPolicyClass;
private EvictionConfiguration evictionConfiguration;
public EvictionRegionConfig() {}
public EvictionRegionConfig(Element element, String defaultEvictionClass)
{
setEvictionPolicyClass(defaultEvictionClass);
setRegionName(element.getAttribute(EvictionConfiguration.NAME));
String evictionClass = element.getAttribute(EvictionConfiguration.REGION_POLICY_CLASS);
if (evictionClass == null || evictionClass.length() == 0)
{
evictionClass = defaultEvictionClass;
// if it's still null... what do we configure?
if (evictionClass == null || evictionClass.length() == 0)
{
throw new MissingPolicyException(
"There is no Eviction Policy Class specified on the region or for the entire cache!");
}
}
EvictionPolicy policy = null;
try
{
policy = (EvictionPolicy) loadClass(evictionClass).newInstance();
}
catch (RuntimeException e)
{
throw e;
}
catch (Exception e)
{
throw new RuntimeException("Eviction class is not properly loaded in classloader", e);
}
setEvictionPolicyClass(evictionClass);
EvictionConfiguration evc = null;
try
{
evc = (EvictionConfiguration) policy.getEvictionConfigurationClass().newInstance();
}
catch (RuntimeException e)
{
throw e;
}
catch (Exception e)
{
throw new RuntimeException("Failed to instantiate EvictionConfiguration of class " +
policy.getEvictionConfigurationClass(), e);
}
evc.parseXMLConfig(element);
setEvictionConfiguration(evc);
}
public EvictionConfiguration getEvictionConfiguration()
{
return evictionConfiguration;
}
public void setEvictionConfiguration(EvictionConfiguration config)
{
testImmutability("evictionConfiguration");
if (this.evictionConfiguration instanceof ConfigurationComponent)
{
removeChildConfig((ConfigurationComponent) this.evictionConfiguration);
}
if (config instanceof ConfigurationComponent)
{
addChildConfig((ConfigurationComponent) this.evictionConfiguration);
}
this.evictionConfiguration = config;
}
public String getEvictionPolicyClass()
{
return evictionPolicyClass;
}
public void setEvictionPolicyClass(String evictionPolicyClass)
{
testImmutability("evictionPolicyClass");
this.evictionPolicyClass = evictionPolicyClass;
}
public Fqn getRegionFqn()
{
return regionFqn;
}
public void setRegionFqn(Fqn regionFqn)
{
testImmutability("regionFqn");
this.regionFqn = regionFqn;
}
public String getRegionName()
{
return regionFqn == null ? null : regionFqn.toString();
}
public void setRegionName(String name)
{
setRegionFqn(name == null ? null : Fqn.fromString(name));
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj instanceof EvictionRegionConfig)
{
EvictionRegionConfig other = (EvictionRegionConfig) obj;
return safeEquals(this.regionFqn, other.regionFqn)
&& safeEquals(this.evictionPolicyClass, other.evictionPolicyClass)
&& safeEquals(this.evictionConfiguration, other.evictionConfiguration);
}
return false;
}
@Override
public int hashCode()
{
int result = 17;
result = 31 * result + (regionFqn == null ? 0 : regionFqn.hashCode());
result = 31 * result + (evictionPolicyClass == null ? 0 : evictionPolicyClass.hashCode());
result = 31 * result + (evictionConfiguration == null ? 0 : evictionConfiguration.hashCode());
return result;
}
}
private static class MissingPolicyException extends RuntimeException
{
/** The serialVersionUID */
private static final long serialVersionUID = -1054460763043849473L;
public MissingPolicyException(String message)
{
super(message);
}
}
private static Class loadClass(String classname) throws ClassNotFoundException
{
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null)
cl = ClassLoader.getSystemClassLoader();
return cl.loadClass(classname);
}
}
1.1 date: 2006/10/23 05:46:39; author: bstansberry; state: Exp;JBossCache/src/org/jboss/cache/config/ConfigurationComponent.java
Index: ConfigurationComponent.java
===================================================================
package org.jboss.cache.config;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.TreeCache;
/**
* Base superclass of Cache configuration classes that expose some properties
* that can be changed after the cache is started.
*
* @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
* @version $Revision: 1.1 $
*
* @see #testImmutability(String)
*/
public class ConfigurationComponent implements Serializable
{
private static final long serialVersionUID = 4879873994727821938L;
protected Log log = LogFactory.getLog(getClass());
private TreeCache cache; // back-reference to test whether the cache is running.
private Set<ConfigurationComponent> children =
Collections.synchronizedSet(new HashSet<ConfigurationComponent>());
protected ConfigurationComponent() {}
public void passCacheToChildConfig(ConfigurationComponent child)
{
if (child != null)
{
child.setTreeCache(cache);
}
}
protected void addChildConfig(ConfigurationComponent child)
{
if (child != null && children.add(child))
child.setTreeCache(cache);
}
protected void addChildConfigs(Collection<? extends ConfigurationComponent> toAdd)
{
if (toAdd != null)
{
for (ConfigurationComponent child : toAdd)
addChildConfig(child);
}
}
protected void removeChildConfig(ConfigurationComponent child)
{
children.remove(child);
}
protected void removeChildConfigs(Collection<? extends ConfigurationComponent> toRemove)
{
if (toRemove != null)
{
for (ConfigurationComponent child : toRemove)
removeChildConfig(child);
}
}
protected void replaceChildConfig(ConfigurationComponent oldConfig, ConfigurationComponent newConfig)
{
removeChildConfig(oldConfig);
addChildConfig(newConfig);
}
protected void replaceChildConfigs(Collection<? extends ConfigurationComponent> oldConfigs,
Collection<? extends ConfigurationComponent> newConfigs)
{
synchronized (children)
{
removeChildConfigs(oldConfigs);
addChildConfigs(newConfigs);
}
}
/**
* Checks field modifications via setters
*
* @param fieldName
*/
protected void testImmutability(String fieldName)
{
try
{
if (cache != null && cache.started && !getClass().getDeclaredField(fieldName).isAnnotationPresent(Dynamic.class))
{
throw new ConfigurationException("Attempted to modify a non-Dynamic configuration element [" + fieldName + "] after the cache has started!");
}
}
catch (NoSuchFieldException e)
{
log.warn("Field " + fieldName + " not found!!");
}
}
protected TreeCache getTreeCache()
{
return cache;
}
/**
* Sets a back-reference to the cache associated with this configuration
*
* @param cache
*/
public void setTreeCache(TreeCache cache)
{
this.cache = cache;
synchronized (children)
{
for (Iterator it = children.iterator(); it.hasNext();)
{
((ConfigurationComponent) it.next()).setTreeCache(cache);
}
}
}
/**
* Null-safe equality test.
*
* FIXME this must be written elsewhere.
*/
protected static boolean safeEquals(Object a, Object b)
{
return (a == b) || (a != null && a.equals(b));
}
}
1.1 date: 2006/10/23 05:46:39; author: bstansberry; state: Exp;JBossCache/src/org/jboss/cache/config/BuddyReplicationConfig.java
Index: BuddyReplicationConfig.java
===================================================================
/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.cache.config;
import java.io.IOException;
import java.util.Properties;
import org.jboss.cache.buddyreplication.NextMemberBuddyLocator;
import org.jboss.cache.xml.XmlHelper;
import org.w3c.dom.Element;
public class BuddyReplicationConfig extends ConfigurationComponent
{
private static final long serialVersionUID = -4826380823985089339L;
/**
* Test whether buddy replication is enabled.
*/
private boolean enabled;
/**
* Name of the buddy pool for current instance. May be null if buddy pooling is not used.
*/
private String buddyPoolName;
private boolean autoDataGravitation = true;
private boolean dataGravitationRemoveOnFind = true;
private boolean dataGravitationSearchBackupTrees = true;
@Dynamic
private int buddyCommunicationTimeout = 10000;
private BuddyLocatorConfig buddyLocatorConfig;
public BuddyReplicationConfig()
{
}
public BuddyReplicationConfig(Element element)
{
enabled = XmlHelper.readBooleanContents(element, "buddyReplicationEnabled");
dataGravitationRemoveOnFind = XmlHelper.readBooleanContents(element, "dataGravitationRemoveOnFind", true);
dataGravitationSearchBackupTrees = XmlHelper.readBooleanContents(element, "dataGravitationSearchBackupTrees", true);
autoDataGravitation = enabled && XmlHelper.readBooleanContents(element, "autoDataGravitation", false);
String strBuddyCommunicationTimeout = XmlHelper.readStringContents(element, "buddyCommunicationTimeout");
try
{
buddyCommunicationTimeout = Integer.parseInt(strBuddyCommunicationTimeout);
}
catch (Exception e)
{
}
finally
{
if (log.isDebugEnabled())
log.debug("Using buddy communication timeout of " + buddyCommunicationTimeout + " millis");
}
buddyPoolName = XmlHelper.readStringContents(element, "buddyPoolName");
if ("".equals(buddyPoolName))
buddyPoolName = null;
// now read the buddy locator details
String buddyLocatorClass = XmlHelper.readStringContents(element, "buddyLocatorClass");
if (buddyLocatorClass == null || buddyLocatorClass.length() == 0)
buddyLocatorClass = NextMemberBuddyLocator.class.getName();
Properties props = null;
try
{
props = XmlHelper.readPropertiesContents(element, "buddyLocatorProperties");
}
catch (IOException e)
{
log.warn("Caught exception reading buddyLocatorProperties", e);
log.error("Unable to read buddyLocatorProperties specified! Using defaults for [" + buddyLocatorClass + "]");
}
BuddyLocatorConfig blc = new BuddyLocatorConfig();
blc.setBuddyLocatorClass(buddyLocatorClass);
blc.setBuddyLocatorProperties(props);
setBuddyLocatorConfig(blc);
}
public boolean isAutoDataGravitation()
{
return autoDataGravitation;
}
public void setAutoDataGravitation(boolean autoDataGravitation)
{
testImmutability("autoDataGravitation");
this.autoDataGravitation = autoDataGravitation;
}
public int getBuddyCommunicationTimeout()
{
return buddyCommunicationTimeout;
}
public void setBuddyCommunicationTimeout(int buddyCommunicationTimeout)
{
testImmutability("buddyCommunicationTimeout");
this.buddyCommunicationTimeout = buddyCommunicationTimeout;
}
public String getBuddyPoolName()
{
return buddyPoolName;
}
public void setBuddyPoolName(String buddyPoolName)
{
testImmutability("buddyPoolName");
this.buddyPoolName = buddyPoolName;
}
public boolean isDataGravitationRemoveOnFind()
{
return dataGravitationRemoveOnFind;
}
public void setDataGravitationRemoveOnFind(boolean dataGravitationRemoveOnFind)
{
testImmutability("dataGravitationRemoveOnFind");
this.dataGravitationRemoveOnFind = dataGravitationRemoveOnFind;
}
public boolean isDataGravitationSearchBackupTrees()
{
return dataGravitationSearchBackupTrees;
}
public void setDataGravitationSearchBackupTrees(boolean dataGravitationSearchBackupTrees)
{
testImmutability("dataGravitationSearchBackupTrees");
this.dataGravitationSearchBackupTrees = dataGravitationSearchBackupTrees;
}
public boolean isEnabled()
{
return enabled;
}
public void setEnabled(boolean enabled)
{
testImmutability("enabled");
this.enabled = enabled;
}
public BuddyLocatorConfig getBuddyLocatorConfig()
{
return buddyLocatorConfig;
}
public void setBuddyLocatorConfig(BuddyLocatorConfig buddyLocatorConfig)
{
testImmutability("buddyLocatorConfig");
replaceChildConfig(this.buddyLocatorConfig, buddyLocatorConfig);
this.buddyLocatorConfig = buddyLocatorConfig;
}
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj instanceof BuddyReplicationConfig)
{
BuddyReplicationConfig other = (BuddyReplicationConfig) obj;
return (this.autoDataGravitation == other.autoDataGravitation)
&& (this.dataGravitationRemoveOnFind == other.dataGravitationRemoveOnFind)
&& (this.dataGravitationSearchBackupTrees == other.dataGravitationSearchBackupTrees)
&& (this.enabled == other.enabled)
&& (this.buddyCommunicationTimeout == other.buddyCommunicationTimeout)
&& safeEquals(this.buddyPoolName, other.buddyPoolName)
&& safeEquals(this.buddyLocatorConfig, other.buddyLocatorConfig);
}
return false;
}
public int hashCode()
{
int result = 11;
result = 29 * result + (autoDataGravitation ? 0 : 1);
result = 29 * result + (dataGravitationRemoveOnFind ? 0 : 1);
result = 29 * result + (dataGravitationSearchBackupTrees ? 0 : 1);
result = 29 * result + (enabled ? 0 : 1);
result = 29 * result + buddyCommunicationTimeout;
result = 29 * result + (buddyPoolName == null ? 0 : buddyPoolName.hashCode());
result = 29 * result + (buddyLocatorConfig == null ? 0 : buddyLocatorConfig.hashCode());
return result;
}
public static class BuddyLocatorConfig extends ConfigurationComponent
{
private static final long serialVersionUID = -8003634097931826091L;
private String buddyLocatorClass = NextMemberBuddyLocator.class.getName();;
private Properties buddyLocatorProperties;
public BuddyLocatorConfig()
{
}
public String getBuddyLocatorClass()
{
return buddyLocatorClass;
}
public void setBuddyLocatorClass(String buddyLocatorClass)
{
testImmutability("buddyLocatorClass");
this.buddyLocatorClass = buddyLocatorClass;
if (buddyLocatorClass == null)
buddyLocatorClass = NextMemberBuddyLocator.class.getName();
}
public Properties getBuddyLocatorProperties()
{
return buddyLocatorProperties;
}
public void setBuddyLocatorProperties(Properties buddyLocatorProperties)
{
testImmutability("buddyLocatorProperties");
this.buddyLocatorProperties = buddyLocatorProperties;
}
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj instanceof BuddyLocatorConfig)
{
BuddyLocatorConfig other = (BuddyLocatorConfig) obj;
return (safeEquals(this.buddyLocatorClass, other.buddyLocatorClass)
&& safeEquals(this.buddyLocatorProperties, other.buddyLocatorProperties));
}
return false;
}
public int hashCode()
{
int result = 19;
result = 41 * result + (buddyLocatorClass == null ? 0 : buddyLocatorClass.hashCode());
result = 41 * result + (buddyLocatorProperties == null ? 0 : buddyLocatorProperties.hashCode());
return result;
}
}
}
More information about the jboss-cvs-commits
mailing list