You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oh...@apache.org on 2006/12/09 20:58:48 UTC
svn commit: r485054 - in /jakarta/commons/proper/configuration/trunk:
src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
xdocs/changes.xml
Author: oheger
Date: Sat Dec 9 11:58:46 2006
New Revision: 485054
URL: http://svn.apache.org/viewvc?view=rev&rev=485054
Log:
Updated some ConfigurationProviders of DefaultConfigurationBuilder to support resolving their classes via reflection; fix for CONFIGURATION-244
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java?view=diff&rev=485054&r1=485053&r2=485054
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java Sat Dec 9 11:58:46 2006
@@ -30,8 +30,6 @@
import org.apache.commons.configuration.beanutils.BeanHelper;
import org.apache.commons.configuration.beanutils.DefaultBeanFactory;
import org.apache.commons.configuration.beanutils.XMLBeanDeclaration;
-import org.apache.commons.configuration.plist.PropertyListConfiguration;
-import org.apache.commons.configuration.plist.XMLPropertyListConfiguration;
import org.apache.commons.configuration.tree.ConfigurationNode;
import org.apache.commons.configuration.tree.DefaultExpressionEngine;
import org.apache.commons.configuration.tree.OverrideCombiner;
@@ -165,8 +163,7 @@
* @author <a
* href="http://jakarta.apache.org/commons/configuration/team-list.html">Commons
* Configuration team</a>
- * @version $Id: DefaultConfigurationBuilder.java 384601 2006-03-09 20:22:58Z
- * oheger $
+ * @version $Id$
*/
public class DefaultConfigurationBuilder extends XMLConfiguration implements
ConfigurationBuilder
@@ -291,8 +288,9 @@
/** Constant for the provider for plist files. */
private static final ConfigurationProvider PLIST_PROVIDER = new FileExtensionConfigurationProvider(
- XMLPropertyListConfiguration.class,
- PropertyListConfiguration.class, EXT_XML);
+ "org.apache.commons.configuration.plist.XMLPropertyListConfiguration",
+ "org.apache.commons.configuration.plist.PropertyListConfiguration",
+ EXT_XML);
/** Constant for the provider for configuration definition files.*/
private static final ConfigurationProvider BUILDER_PROVIDER = new ConfigurationBuilderProvider();
@@ -709,13 +707,16 @@
/** Stores the class of the configuration to be created. */
private Class configurationClass;
+ /** Stores the name of the configuration class to be created.*/
+ private String configurationClassName;
+
/**
* Creates a new uninitialized instance of
* <code>ConfigurationProvider</code>.
*/
public ConfigurationProvider()
{
- this(null);
+ this((Class) null);
}
/**
@@ -730,6 +731,19 @@
}
/**
+ * Creates a new instance of <code>ConfigurationProvider</code> and
+ * sets the name of the class of the configuration created by this
+ * provider.
+ *
+ * @param configClassName the name of the configuration class
+ * @since 1.4
+ */
+ public ConfigurationProvider(String configClassName)
+ {
+ setConfigurationClassName(configClassName);
+ }
+
+ /**
* Returns the class of the configuration returned by this provider.
*
* @return the class of the provided configuration
@@ -750,6 +764,29 @@
}
/**
+ * Returns the name of the configuration class returned by this
+ * provider.
+ *
+ * @return the configuration class name
+ * @since 1.4
+ */
+ public String getConfigurationClassName()
+ {
+ return configurationClassName;
+ }
+
+ /**
+ * Sets the name of the configuration class returned by this provider.
+ *
+ * @param configurationClassName the name of the configuration class
+ * @since 1.4
+ */
+ public void setConfigurationClassName(String configurationClassName)
+ {
+ this.configurationClassName = configurationClassName;
+ }
+
+ /**
* Returns the configuration. This method is called to fetch the
* configuration from the provider. This implementation will call the
* inherited
@@ -764,7 +801,7 @@
public AbstractConfiguration getConfiguration(
ConfigurationDeclaration decl) throws Exception
{
- return (AbstractConfiguration) createBean(getConfigurationClass(),
+ return (AbstractConfiguration) createBean(fetchConfigurationClass(),
decl, null);
}
@@ -788,6 +825,39 @@
{
return null;
}
+
+ /**
+ * Returns the configuration class supported by this provider. If a
+ * class object was set, it is returned. Otherwise the method tries to
+ * resolve the class name.
+ *
+ * @return the class of the configuration to be created
+ * @since 1.4
+ */
+ protected synchronized Class fetchConfigurationClass() throws Exception
+ {
+ if (getConfigurationClass() == null)
+ {
+ setConfigurationClass(loadClass(getConfigurationClassName()));
+ }
+ return getConfigurationClass();
+ }
+
+ /**
+ * Loads the class with the specified name dynamically. If the class's
+ * name is <b>null</b>, <b>null</b> will also be returned.
+ *
+ * @param className the name of the class to be loaded
+ * @return the class object
+ * @throws ClassNotFoundException if class loading fails
+ * @since 1.4
+ */
+ protected Class loadClass(String className)
+ throws ClassNotFoundException
+ {
+ return (className != null) ? Class.forName(className, true,
+ getClass().getClassLoader()) : null;
+ }
}
/**
@@ -1046,6 +1116,18 @@
}
/**
+ * Creates a new instance of <code>FileConfigurationProvider</code>
+ * and sets the configuration class name.
+ *
+ * @param configClassName the name of the configuration to be created
+ * @since 1.4
+ */
+ public FileConfigurationProvider(String configClassName)
+ {
+ super(configClassName);
+ }
+
+ /**
* Creates the configuration. After that <code>load()</code> will be
* called. If this configuration is marked as optional, exceptions will
* be ignored.
@@ -1115,15 +1197,29 @@
static class FileExtensionConfigurationProvider extends
FileConfigurationProvider
{
- /** Stores the class to be created when the file extension matches. */
+ /**
+ * Stores the class to be created when the file extension matches.
+ */
private Class matchingClass;
/**
+ * Stores the name of the class to be created when the file extension
+ * matches.
+ */
+ private String matchingClassName;
+
+ /**
* Stores the class to be created when the file extension does not
* match.
*/
private Class defaultClass;
+ /**
+ * Stores the name of the class to be created when the file extension
+ * does not match.
+ */
+ private String defaultClassName;
+
/** Stores the file extension to be checked against. */
private String fileExtension;
@@ -1146,6 +1242,60 @@
}
/**
+ * Creates a new instance of
+ * <code>FileExtensionConfigurationProvider</code> and initializes it
+ * with the names of the classes to be created.
+ *
+ * @param matchingClassName the name of the class to be created when the
+ * file extension matches
+ * @param defaultClassName the name of the class to be created when the
+ * file extension does not match
+ * @param extension the file extension to be checked agains
+ * @since 1.4
+ */
+ public FileExtensionConfigurationProvider(String matchingClassName,
+ String defaultClassName, String extension)
+ {
+ this.matchingClassName = matchingClassName;
+ this.defaultClassName = defaultClassName;
+ fileExtension = extension;
+ }
+
+ /**
+ * Returns the matching class object, no matter whether it was defined
+ * as a class or as a class name.
+ *
+ * @return the matching class object
+ * @throws Exception if an error occurs
+ * @since 1.4
+ */
+ protected synchronized Class fetchMatchingClass() throws Exception
+ {
+ if (matchingClass == null)
+ {
+ matchingClass = loadClass(matchingClassName);
+ }
+ return matchingClass;
+ }
+
+ /**
+ * Returns the default class object, no matter whether it was defined as
+ * a class or as a class name.
+ *
+ * @return the default class object
+ * @throws Exception if an error occurs
+ * @since 1.4
+ */
+ protected synchronized Class fetchDefaultClass() throws Exception
+ {
+ if (defaultClass == null)
+ {
+ defaultClass = loadClass(defaultClassName);
+ }
+ return defaultClass;
+ }
+
+ /**
* Creates the configuration object. The class is determined by the file
* name's extension.
*
@@ -1162,11 +1312,11 @@
if (fileName != null
&& fileName.toLowerCase().trim().endsWith(fileExtension))
{
- return super.createBeanInstance(matchingClass, data);
+ return super.createBeanInstance(fetchMatchingClass(), data);
}
else
{
- return super.createBeanInstance(defaultClass, data);
+ return super.createBeanInstance(fetchDefaultClass(), data);
}
}
}
Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=485054&r1=485053&r2=485054
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sat Dec 9 11:58:46 2006
@@ -23,6 +23,11 @@
<body>
<release version="1.4-dev" date="in SVN">
+ <action dev="oheger" type="update" issue="CONFIGURATION-244">
+ The number of dependencies needed for DefaultConfigurationBuilder was
+ reduced by letting some of the default configuration providers resolve
+ their classes per reflection.
+ </action>
<action dev="oheger" type="update" issue="CONFIGURATION-240">
File-based configurations with a reloading strategy did not work well
together with CombinedConfiguration because the reloading strategy is
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org