You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by ni...@apache.org on 2008/02/25 15:43:32 UTC
svn commit: r630864 - in /maven/archiva/branches/springy/plexus-spring: ./
src/main/java/org/codehaus/plexus/spring/ src/main/resources/
src/test/java/org/codehaus/plexus/spring/
Author: nicolas
Date: Mon Feb 25 06:43:31 2008
New Revision: 630864
URL: http://svn.apache.org/viewvc?rev=630864&view=rev
Log:
multiple fixes
refactoring & code cleanup
throws exception when converted plexus descriptor doesn't specify field-name
"-Dplexus-spring.debug=true" option to dump converted XML (requires dom4j)
Modified:
maven/archiva/branches/springy/plexus-spring/pom.xml
maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusBeanDefinitionDocumentReader.java
maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusComponentFactoryBean.java
maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusInSpringTestCase.java
maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusNamespaceHandler.java
maven/archiva/branches/springy/plexus-spring/src/main/resources/plexus.xsd
maven/archiva/branches/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/FieldInjectionTest.java
Modified: maven/archiva/branches/springy/plexus-spring/pom.xml
URL: http://svn.apache.org/viewvc/maven/archiva/branches/springy/plexus-spring/pom.xml?rev=630864&r1=630863&r2=630864&view=diff
==============================================================================
--- maven/archiva/branches/springy/plexus-spring/pom.xml (original)
+++ maven/archiva/branches/springy/plexus-spring/pom.xml Mon Feb 25 06:43:31 2008
@@ -43,6 +43,12 @@
<version>1.0-alpha-33</version>
</dependency>
<dependency>
+ <groupId>dom4j</groupId>
+ <artifactId>dom4j</artifactId>
+ <version>1.6.1</version>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
Modified: maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusBeanDefinitionDocumentReader.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusBeanDefinitionDocumentReader.java?rev=630864&r1=630863&r2=630864&view=diff
==============================================================================
--- maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusBeanDefinitionDocumentReader.java (original)
+++ maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusBeanDefinitionDocumentReader.java Mon Feb 25 06:43:31 2008
@@ -1,6 +1,5 @@
package org.codehaus.plexus.spring;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -29,6 +28,9 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
+import org.dom4j.io.DOMReader;
+import org.dom4j.io.OutputFormat;
+import org.dom4j.io.XMLWriter;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.xml.BeanDefinitionDocumentReader;
import org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader;
@@ -47,12 +49,25 @@
public void registerBeanDefinitions( Document doc, XmlReaderContext readerContext )
{
doc = convertPlexusDescriptorToSpringBeans( doc );
+ if ( Boolean.getBoolean( "spring-plexus.debug" ) )
+ {
+ try
+ {
+ XMLWriter writer = new XMLWriter( System.out, OutputFormat.createPrettyPrint() );
+ writer.write( new DOMReader().read( doc ) );
+ }
+ catch ( Exception e )
+ {
+ // ignored
+ }
+ }
+
super.registerBeanDefinitions( doc, readerContext );
}
protected Document convertPlexusDescriptorToSpringBeans( Document doc )
{
- if ( ! "component-set".equals( doc.getDocumentElement().getNodeName() ) )
+ if ( !"component-set".equals( doc.getDocumentElement().getNodeName() ) )
{
return doc;
}
@@ -73,7 +88,8 @@
}
catch ( Exception e )
{
- throw new BeanDefinitionStoreException( "Failed to translate plexus component descriptor to Spring XML context", e );
+ throw new BeanDefinitionStoreException(
+ "Failed to translate plexus component descriptor to Spring XML context", e );
}
}
}
Modified: maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusComponentFactoryBean.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusComponentFactoryBean.java?rev=630864&r1=630863&r2=630864&view=diff
==============================================================================
--- maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusComponentFactoryBean.java (original)
+++ maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusComponentFactoryBean.java Mon Feb 25 06:43:31 2008
@@ -20,6 +20,7 @@
*/
import java.lang.reflect.Field;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
@@ -28,8 +29,10 @@
import java.util.Map;
import org.codehaus.plexus.PlexusConstants;
+import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.LoggerManager;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.springframework.beans.BeansException;
@@ -50,17 +53,17 @@
* <ul>
* <li>Support for direct field injection or "requirements"</li>
* <li>Support for LogEnabled, Initializable and Disposable plexus interfaces</li>
- * <li>Support for plexus.requirement to get a Map<role-hint, component> for a role
+ * <li>Support for plexus.requirement to get a Map<role-hint, component> for a
+ * role
* </ul>
- * If not set, the beanFActory will auto-detect the loggerManager to use by searching for the adequate bean in the
- * spring context.
+ * If not set, the beanFActory will auto-detect the loggerManager to use by
+ * searching for the adequate bean in the spring context.
*
* @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
*/
public class PlexusComponentFactoryBean
implements FactoryBean, BeanFactoryAware, DisposableBean
{
-
private static final char HINT = '#';
private Class role;
@@ -73,6 +76,8 @@
private ListableBeanFactory beanFactory;
+ private Context contextWrapper;
+
private LoggerManager loggerManager;
private TypeConverter typeConverter = new SimpleTypeConverter();
@@ -104,6 +109,9 @@
throw new BeanCreationException( "Plexus poolable instanciation-strategy is not supported" );
}
+ // Spring MAY cache the object built by this factory if getSingleton()
+ // returns true,
+ // but can also requires us to ensure unicity.
if ( "singleton".equals( instanciationStrategy ) && !instances.isEmpty() )
{
return instances.get( 0 );
@@ -121,57 +129,9 @@
public void doWith( Field field )
throws IllegalArgumentException, IllegalAccessException
{
- Object dependency = requirements.get( field.getName() );
- if ( dependency instanceof RuntimeBeanReference )
- {
- String beanName = ( (RuntimeBeanReference) dependency ).getBeanName();
- if ( Map.class.isAssignableFrom( field.getType() ) )
- {
- // component ask plexus for a Map of all available components for the role
- Map map = new HashMap();
- String mask = beanName + HINT;
- String[] beans = beanFactory.getBeanDefinitionNames();
- for ( int i = 0; i < beans.length; i++ )
- {
- String name = beans[i];
- if ( name.startsWith( mask ) )
- {
- map.put( name.substring( mask.length() ), beanFactory.getBean( name ) );
- }
- }
- if ( beanFactory.containsBean( beanName ) )
- {
- map.put( PlexusConstants.PLEXUS_DEFAULT_HINT, beanFactory.getBean( beanName ) );
- }
- dependency = map;
- }
- else if ( Collection.class.isAssignableFrom( field.getType() ) )
- {
- List list = new LinkedList();
- String mask = beanName + HINT;
- String[] beans = beanFactory.getBeanDefinitionNames();
- for ( int i = 0; i < beans.length; i++ )
- {
- String name = beans[i];
- if ( name.startsWith( mask ) )
- {
- list.add( beanFactory.getBean( name ) );
- }
- }
- if ( beanFactory.containsBean( beanName ) )
- {
- list.add( beanFactory.getBean( beanName ) );
- }
- dependency = list;
- }
- else
- {
- dependency = beanFactory.getBean( beanName );
- }
- }
+ Object dependency = resolveRequirement( field );
if ( dependency != null )
{
- dependency = typeConverter.convertIfNecessary( dependency, field.getType() );
ReflectionUtils.makeAccessible( field );
ReflectionUtils.setField( field, component, dependency );
}
@@ -179,12 +139,16 @@
}, ReflectionUtils.COPYABLE_FIELDS );
}
-
if ( component instanceof LogEnabled )
{
( (LogEnabled) component ).enableLogging( getLoggerManager().getLoggerForComponent( role.getName() ) );
}
+ if (component instanceof Contextualizable )
+ {
+ ((Contextualizable) component).contextualize( contextWrapper );
+ }
+
if ( component instanceof Initializable )
{
( (Initializable) component ).initialize();
@@ -205,7 +169,11 @@
return "per-lookup".equals( instanciationStrategy );
}
- private LoggerManager getLoggerManager()
+ /**
+ * Retrieve the loggerManager instance to be used for LogEnabled components
+ * @return
+ */
+ protected LoggerManager getLoggerManager()
{
if ( loggerManager == null )
{
@@ -221,7 +189,7 @@
else
{
throw new BeanInitializationException(
- "You must explicitly set a LoggerManager or define a unique one in bean context" );
+ "You must explicitly set a LoggerManager or define a unique one in bean context" );
}
}
return loggerManager;
@@ -262,7 +230,7 @@
*/
public void setInstanciationStrategy( String instanciationStrategy )
{
- if (instanciationStrategy.length() == 0)
+ if ( instanciationStrategy.length() == 0 )
{
instanciationStrategy = "singleton";
}
@@ -280,6 +248,66 @@
protected void setTypeConverter( TypeConverter typeConverter )
{
this.typeConverter = typeConverter;
+ }
+
+ /**
+ * Create a Map of all available implementation of the expected role
+ * @param beanName
+ * @return Map<role-hint, component>
+ */
+ protected Map getRoleMap( String beanName )
+ {
+ Map map = new HashMap();
+ String mask = beanName + HINT;
+ String[] beans = beanFactory.getBeanDefinitionNames();
+ for ( int i = 0; i < beans.length; i++ )
+ {
+ String name = beans[i];
+ if ( name.startsWith( mask ) )
+ {
+ map.put( name.substring( mask.length() ), beanFactory.getBean( name ) );
+ }
+ }
+ if ( beanFactory.containsBean( beanName ) )
+ {
+ map.put( PlexusConstants.PLEXUS_DEFAULT_HINT, beanFactory.getBean( beanName ) );
+ }
+ return map;
+ }
+
+
+ /**
+ * Resolve the requirement that this field exposes in the component
+ * @param field
+ * @return
+ */
+ protected Object resolveRequirement( Field field )
+ {
+ Object dependency = requirements.get( field.getName() );
+ if ( dependency instanceof RuntimeBeanReference )
+ {
+ String beanName = ( (RuntimeBeanReference) dependency ).getBeanName();
+ if ( Map.class.isAssignableFrom( field.getType() ) )
+ {
+ // component ask plexus for a Map of all available
+ // components for the role
+ dependency = getRoleMap( beanName );
+ }
+ else if ( Collection.class.isAssignableFrom( field.getType() ) )
+ {
+ dependency = new ArrayList( getRoleMap( beanName ).values() );
+ }
+ else
+ {
+ dependency = beanFactory.getBean( beanName );
+ }
+ }
+ if (dependency != null)
+ {
+ dependency = typeConverter.convertIfNecessary( dependency, field.getType() );
+ }
+ return dependency;
+
}
}
Modified: maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusInSpringTestCase.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusInSpringTestCase.java?rev=630864&r1=630863&r2=630864&view=diff
==============================================================================
--- maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusInSpringTestCase.java (original)
+++ maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusInSpringTestCase.java Mon Feb 25 06:43:31 2008
@@ -47,7 +47,6 @@
applicationContext =
new PlexusClassPathXmlApplicationContext( new String[] {
"classpath*:META-INF/plexus/components.xml",
- "classpath*:META-INF/plexus/components-fragment.xml",
"classpath*:" + getPlexusConfigLocation(),
"classpath*:" + getSpringConfigLocation()} );
}
Modified: maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusNamespaceHandler.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusNamespaceHandler.java?rev=630864&r1=630863&r2=630864&view=diff
==============================================================================
--- maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusNamespaceHandler.java (original)
+++ maven/archiva/branches/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusNamespaceHandler.java Mon Feb 25 06:43:31 2008
@@ -32,6 +32,7 @@
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.beans.factory.xml.ParserContext;
+import org.springframework.context.ApplicationContextException;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
@@ -48,10 +49,11 @@
public void init()
{
registerBeanDefinitionParser( "component", new PlexusComponentBeanDefinitionParser() );
- registerBeanDefinitionParser( "requirement", new PlexusPropertyBeanDefinitionParser() );
+ registerBeanDefinitionParser( "requirement", new NopBeanDefinitionParser() );
+ registerBeanDefinitionParser( "configuration", new NopBeanDefinitionParser() );
}
- private class PlexusPropertyBeanDefinitionParser
+ private class NopBeanDefinitionParser
extends AbstractBeanDefinitionParser
{
protected AbstractBeanDefinition parseInternal( Element element, ParserContext parserContext )
@@ -86,6 +88,10 @@
{
Element child = (Element) iterator.next();
String name = child.getAttribute( "name" );
+ if (name.length() == 0)
+ {
+ throw new ApplicationContextException( "No field name for plexus requirement on " + implementation );
+ }
String role = child.getAttribute( "role" );
String roleHint = child.getAttribute( "role-hint" );
String ref = PlexusToSpringUtils.buildSpringId( role, roleHint );
Modified: maven/archiva/branches/springy/plexus-spring/src/main/resources/plexus.xsd
URL: http://svn.apache.org/viewvc/maven/archiva/branches/springy/plexus-spring/src/main/resources/plexus.xsd?rev=630864&r1=630863&r2=630864&view=diff
==============================================================================
--- maven/archiva/branches/springy/plexus-spring/src/main/resources/plexus.xsd (original)
+++ maven/archiva/branches/springy/plexus-spring/src/main/resources/plexus.xsd Mon Feb 25 06:43:31 2008
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
-<xsd:schema xmlns="http://plexus.codehaus.org/spring" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
-targetNamespace="http://plexus.codehaus.org/spring"
+<xsd:schema xmlns="http://plexus.codehaus.org/spring" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+targetNamespace="http://plexus.codehaus.org/spring"
elementFormDefault="qualified" attributeFormDefault="unqualified">
-
+
<xsd:element name="component">
<xsd:complexType>
<xsd:sequence>
Modified: maven/archiva/branches/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/FieldInjectionTest.java
URL: http://svn.apache.org/viewvc/maven/archiva/branches/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/FieldInjectionTest.java?rev=630864&r1=630863&r2=630864&view=diff
==============================================================================
--- maven/archiva/branches/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/FieldInjectionTest.java (original)
+++ maven/archiva/branches/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/FieldInjectionTest.java Mon Feb 25 06:43:31 2008
@@ -29,19 +29,19 @@
public void testFieldInjectionInSpringContext()
throws Exception
{
- ConfigurableApplicationContext applicationContext = new PlexusClassPathXmlApplicationContext( new String[] { "components.xml", "applicationContext.xml" } );
+ ConfigurableApplicationContext applicationContext = new PlexusClassPathXmlApplicationContext( new String[] { "components.xml", "applicationContext.xml" } );
PlexusBean plexusBean = (PlexusBean) applicationContext.getBean( "plexusBean" );
assertEquals( PlexusBean.INITIALIZED, plexusBean.getState() );
assertEquals( "field injection failed", "expected SpringBean", plexusBean.toString() );
applicationContext.close();
assertEquals( PlexusBean.DISPOSED, plexusBean.getState() );
-
+
}
-
- public void testIbjectMapForRole()
+
+ public void testInjectMapForRole()
throws Exception
{
- ConfigurableApplicationContext applicationContext = new PlexusClassPathXmlApplicationContext( new String[] { "components.xml", "applicationContext.xml" } );
+ ConfigurableApplicationContext applicationContext = new PlexusClassPathXmlApplicationContext( new String[] { "components.xml", "applicationContext.xml" } );
ComplexPlexusBean plexusBean = (ComplexPlexusBean) applicationContext.getBean( "complexPlexusBean" );
assertEquals( "2 components for role org.codehaus.plexus.spring.PlexusBean", plexusBean.toString() );
}