MNG-4910 Use BeanConfigurator for configuration sub-elements

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@1037741 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Igor Fedorenko 2010-11-22 14:48:00 +00:00
parent 2c935ddff8
commit 38ced22fda
4 changed files with 55 additions and 0 deletions

View File

@ -52,12 +52,34 @@ public interface BeanConfigurationRequest
/**
* Sets the configuration to unmarshal into the bean. The configuration should be taken from
* {@link org.apache.maven.model.ConfigurationContainer#getConfiguration()} or a similar source.
* Fully equivalent to {@code setConfiguration(configuration, null)}.
*
* @param configuration The configuration to unmarshal, may be {@code null}.
* @return This request for chaining, never {@code null}.
*/
BeanConfigurationRequest setConfiguration( Object configuration );
/**
* Sets the configuration to unmarshal into the bean. The configuration should be taken from
* {@link org.apache.maven.model.ConfigurationContainer#getConfiguration()} or a similar source.
* If {@code element} is not {@code null}, child configuration element with the specified name will
* be unmarshaled.
*
* @param configuration The configuration to unmarshal, may be {@code null}.
* @param element Configuration element name to unmarshal or {@code null} to unmarshal entire configuration.
* @return This request for chaining, never {@code null}.
*/
BeanConfigurationRequest setConfiguration( Object configuration, String element );
/**
* Returns configuration element name or {@code null}.
*
* @see {@link #setConfiguration(Object, String)}
*
* @return Configuration element name or {@code null}
*/
String getConfigurationElement();
/**
* Gets the class loader from which to load any types referenced by the configuration. If unset, the class loader of
* the bean class will be used.

View File

@ -39,6 +39,8 @@ public class DefaultBeanConfigurationRequest
private Object configuration;
private String configurationElement;
private ClassLoader classLoader;
private BeanConfigurationValuePreprocessor valuePreprocessor;
@ -61,9 +63,20 @@ public class DefaultBeanConfigurationRequest
return configuration;
}
public String getConfigurationElement()
{
return configurationElement;
}
public DefaultBeanConfigurationRequest setConfiguration( Object configuration )
{
return setConfiguration( configuration, null );
}
public DefaultBeanConfigurationRequest setConfiguration( Object configuration, String element )
{
this.configuration = configuration;
this.configurationElement = element;
return this;
}

View File

@ -84,6 +84,11 @@ public class DefaultBeanConfigurator
+ configuration.getClass().getName() + ")" );
}
if ( request.getConfigurationElement() != null )
{
plexusConfig = plexusConfig.getChild( request.getConfigurationElement() );
}
ClassLoader classLoader = request.getClassLoader();
if ( classLoader == null )
{

View File

@ -123,6 +123,21 @@ public class DefaultBeanConfiguratorTest
assertEquals( new File( "base/test" ).getAbsoluteFile(), bean.file );
}
public void testChildConfigurationElement()
throws BeanConfigurationException
{
SomeBean bean = new SomeBean();
Xpp3Dom config = toConfig( "<wrapper><file>test</file></wrapper>" );
DefaultBeanConfigurationRequest request = new DefaultBeanConfigurationRequest();
request.setBean( bean ).setConfiguration( config, "wrapper" );
configurator.configureBean( request );
assertEquals( new File( "test" ), bean.file );
}
static class SomeBean
{