[jboss-cvs] JBossCache/src/org/jboss/cache/factories ...
Manik Surtani
msurtani at jboss.com
Wed Jul 19 01:10:12 EDT 2006
User: msurtani
Date: 06/07/19 01:10:12
Modified: src/org/jboss/cache/factories DefaultCacheFactory.java
Log:
Implemented and added tests for configuring a tree cache using the factory.
Revision Changes Path
1.3 +80 -0 JBossCache/src/org/jboss/cache/factories/DefaultCacheFactory.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: DefaultCacheFactory.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/factories/DefaultCacheFactory.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- DefaultCacheFactory.java 18 Jul 2006 18:28:39 -0000 1.2
+++ DefaultCacheFactory.java 19 Jul 2006 05:10:12 -0000 1.3
@@ -6,6 +6,8 @@
*/
package org.jboss.cache.factories;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Cache;
import org.jboss.cache.NodeImpl;
import org.jboss.cache.TreeCache;
@@ -13,8 +15,13 @@
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.ConfigurationException;
import org.jboss.cache.config.ConfigurationImpl;
+import org.w3c.dom.Element;
+import java.beans.PropertyEditor;
+import java.beans.PropertyEditorManager;
import java.io.InputStream;
+import java.lang.reflect.Method;
+import java.util.Set;
/**
* Default implementation of the Cache Factory
@@ -23,6 +30,8 @@
*/
public class DefaultCacheFactory implements CacheFactory
{
+ private static Log log = LogFactory.getLog(DefaultCacheFactory.class);
+
public Cache createCache(String configFileName) throws ConfigurationException
{
return createCache(configFileName, true);
@@ -46,6 +55,7 @@
{
TreeCache cache = new TreeCache();
cache.setConfiguration(configuration);
+ configureCache(cache, configuration);
if (start) cache.startService();
return new TreeCacheProxyImpl(cache, (NodeImpl) cache.root);
}
@@ -60,4 +70,74 @@
{
return Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
}
+
+ protected void configureCache(TreeCache cache, Configuration conf)
+ {
+ configureCache(cache, conf, conf.getSimpleAttributeNames(), false);
+ configureCache(cache, conf, conf.getXmlAttributeNames(), true);
+ }
+
+ protected void configureCache(TreeCache cache, Configuration conf, Set<String> attributes, boolean isXmlAttribs)
+ {
+ Class objectClass = cache.getClass();
+
+ // go thru simple string setters first.
+ for (String propName : attributes)
+ {
+ String setter = "set" + propName;
+ Method method = null;
+
+ try
+ {
+ if (isXmlAttribs)
+ {
+ method = objectClass.getMethod(setter, new Class[]{Element.class});
+ method.invoke(cache, new Object[]{conf.getAttributeAsXml(propName)});
+ }
+ else
+ {
+ method = objectClass.getMethod(setter, new Class[]{String.class});
+ method.invoke(cache, new Object[]{conf.getAttribute(propName)});
+ }
+
+ continue;
+ }
+ catch (NoSuchMethodException me)
+ {
+ log.debug("Unable to find String setter for " + setter);
+ }
+ catch (Exception e)
+ {
+ throw new ConfigurationException("Unable to invoke setter " + setter + " on " + objectClass, e);
+ }
+
+ // if we get here, we could not find a String or XML setter.
+ for (Method m : objectClass.getMethods())
+ {
+ if (setter.equals(m.getName()))
+ {
+ Class paramTypes[] = m.getParameterTypes();
+ if (paramTypes.length != 1) throw new ConfigurationException("Setter " + setter + " does not contain the expected number of params. Has " + paramTypes.length + " instead of just 1.");
+
+ Class parameterType = paramTypes[0];
+ PropertyEditor editor= PropertyEditorManager.findEditor(parameterType);
+ if(editor == null) throw new ConfigurationException("Couldn't find a property editor for parameter type " + parameterType);
+
+ editor.setAsText(conf.getAttribute(propName));
+
+ Object parameter = editor.getValue();
+ if (log.isDebugEnabled()) log.debug("Invoking setter method: " + setter + " with parameter \"" + parameter + "\" of type " + parameter.getClass());
+
+ try
+ {
+ m.invoke(cache, new Object[]{parameter});
+ }
+ catch (Exception e)
+ {
+ throw new ConfigurationException("Unable to invoke setter " + setter + " on " + objectClass, e);
+ }
+ }
+ }
+ }
+ }
}
More information about the jboss-cvs-commits
mailing list