From: Nicolas De Loof Date: Sun, 24 Feb 2008 13:52:54 +0000 (+0000) Subject: add support in spring context for Map as @plexus.requirement X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d6cc446fb284842f800932bd2c7844059105961a;p=archiva.git add support in spring context for Map as @plexus.requirement git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@630623 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusComponentFactoryBean.java b/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusComponentFactoryBean.java index 51a5987e7..286548ea2 100644 --- a/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusComponentFactoryBean.java +++ b/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusComponentFactoryBean.java @@ -20,6 +20,7 @@ package org.codehaus.plexus.spring; */ import java.lang.reflect.Field; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -29,8 +30,6 @@ import org.codehaus.plexus.logging.LogEnabled; import org.codehaus.plexus.logging.LoggerManager; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.Startable; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.StartingException; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; @@ -40,17 +39,17 @@ import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.config.RuntimeBeanReference; -import org.springframework.context.Lifecycle; import org.springframework.util.ReflectionUtils; /** * A FactoryBean dedicated to building plexus components. This includes : *
    - *
  • Support for direct field injection or "requirements"
  • - *
  • Support for LogEnabled, Initializable and Disposable plexus interfaces
  • + *
  • Support for direct field injection or "requirements"
  • + *
  • Support for LogEnabled, Initializable and Disposable plexus interfaces
  • + *
  • Support for plexus.requirement to get a Map for a role *
- * 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 Nicolas De Loof */ @@ -70,7 +69,7 @@ public class PlexusComponentFactoryBean private LoggerManager loggerManager; private List instances = new LinkedList(); - + public void afterPropertiesSet() throws Exception { @@ -92,7 +91,7 @@ public class PlexusComponentFactoryBean } } } - + public void destroy() throws Exception { @@ -103,13 +102,13 @@ public class PlexusComponentFactoryBean Object component = (Object) iterator.next(); if ( component instanceof Disposable ) { - ((Disposable) component).dispose(); - + ( (Disposable) component ).dispose(); + } } - } + } } - + public Object getObject() throws Exception { @@ -128,7 +127,31 @@ public class PlexusComponentFactoryBean Object dependency = requirements.get( field.getName() ); if ( dependency instanceof RuntimeBeanReference ) { - dependency = beanFactory.getBean( ((RuntimeBeanReference) dependency).getBeanName() ); + 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 + '#'; + 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( "default", beanFactory.getBean( beanName ) ); + } + dependency = map; + } + else + { + dependency = beanFactory.getBean( beanName ); + } } if ( dependency != null ) { diff --git a/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusNamespaceHandler.java b/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusNamespaceHandler.java index dfd77ac1f..808b30f39 100644 --- a/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusNamespaceHandler.java +++ b/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusNamespaceHandler.java @@ -19,7 +19,6 @@ package org.codehaus.plexus.spring; * under the License. */ -import java.lang.reflect.Field; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -33,7 +32,6 @@ import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; import org.springframework.beans.factory.xml.ParserContext; -import org.springframework.util.ClassUtils; import org.springframework.util.xml.DomUtils; import org.w3c.dom.Element; @@ -90,14 +88,6 @@ public class PlexusNamespaceHandler String role = child.getAttribute( "role" ); String roleHint = child.getAttribute( "role-hint" ); String ref = PlexusToSpringUtils.buildSpringId( role, roleHint ); - if ( roleHint == null ) - { -// Field f = ClassUtils.forName( implementation ).getField( name ); -// if ( Map.class.isAssignableFrom( f.getType() ) ) -// { -// // TODO add add support for plexus role --> Map -// } - } dependencies.put( name, new RuntimeBeanReference( ref ) ); } @@ -113,7 +103,7 @@ public class PlexusNamespaceHandler builder.addPropertyValue( "requirements", dependencies ); } - + protected String resolveId( Element element, AbstractBeanDefinition definition, ParserContext parserContext ) throws BeanDefinitionStoreException { diff --git a/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusToSpringUtils.java b/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusToSpringUtils.java index a1feb29cd..94fb873ff 100644 --- a/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusToSpringUtils.java +++ b/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusToSpringUtils.java @@ -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 @@ -28,7 +27,7 @@ import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; /** * Utility method to convert plexus descriptors to spring bean context. - * + * * @author Nicolas De Loof * @since 1.1 */ @@ -38,7 +37,7 @@ public class PlexusToSpringUtils public static String toSpringId( String string ) { int i = string.lastIndexOf( '.' ); - if (i >= 0 ) + if ( i >= 0 ) { return Character.toLowerCase( string.charAt( i + 1 ) ) + string.substring( i + 2 ); } @@ -104,5 +103,6 @@ public class PlexusToSpringUtils i = 0; } String id = Character.toLowerCase( role.charAt( i ) ) + role.substring( i + 1 ); - return roleHint.length() == 0 ? id : id + '#' + roleHint; - }} + return ( roleHint.length() == 0 || "default".equals( roleHint ) ) ? id : id + '#' + roleHint; + } +} diff --git a/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/ComplexPlexusBean.java b/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/ComplexPlexusBean.java new file mode 100644 index 000000000..f3e9e7214 --- /dev/null +++ b/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/ComplexPlexusBean.java @@ -0,0 +1,43 @@ +package org.codehaus.plexus.spring; + +import java.util.Map; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * A typical plexus component implementation + * + * @author Nicolas De Loof + */ +public class ComplexPlexusBean +{ + /** + * @plexus.requirement role="org.codehaus.plexus.spring.PlexusBean" + */ + private Map plexusBeans; + + /** + * @see org.codehaus.plexus.spring.PlexusBean#toString() + */ + public String toString() + { + return plexusBeans.size() + " components for role org.codehaus.plexus.spring.PlexusBean"; + } +} diff --git a/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/FieldInjectionTest.java b/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/FieldInjectionTest.java index 92a3ccb19..16c016a5d 100644 --- a/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/FieldInjectionTest.java +++ b/springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/FieldInjectionTest.java @@ -37,5 +37,13 @@ public class FieldInjectionTest assertEquals( PlexusBean.DISPOSED, plexusBean.getState() ); } + + public void testIbjectMapForRole() + throws Exception + { + 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() ); + } } diff --git a/springy/plexus-spring/src/test/resources/components.xml b/springy/plexus-spring/src/test/resources/components.xml index 2176e02cc..32d190cb1 100644 --- a/springy/plexus-spring/src/test/resources/components.xml +++ b/springy/plexus-spring/src/test/resources/components.xml @@ -2,6 +2,7 @@ org.codehaus.plexus.spring.PlexusBean + default org.codehaus.plexus.spring.PlexusBeanImpl @@ -13,5 +14,29 @@ expected + + org.codehaus.plexus.spring.PlexusBean + another + org.codehaus.plexus.spring.PlexusBeanImpl + + + bean + springBean + + + + another + + + + org.codehaus.plexus.spring.ComplexPlexusBean + org.codehaus.plexus.spring.ComplexPlexusBean + + + plexusBeans + org.codehaus.plexus.spring.PlexusBean + + +