]> source.dussan.org Git - archiva.git/commitdiff
add support in spring context for Map<role-hint, component> as @plexus.requirement
authorNicolas De Loof <nicolas@apache.org>
Sun, 24 Feb 2008 13:52:54 +0000 (13:52 +0000)
committerNicolas De Loof <nicolas@apache.org>
Sun, 24 Feb 2008 13:52:54 +0000 (13:52 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@630623 13f79535-47bb-0310-9956-ffa450edef68

springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusComponentFactoryBean.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusNamespaceHandler.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusToSpringUtils.java
springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/ComplexPlexusBean.java [new file with mode: 0644]
springy/plexus-spring/src/test/java/org/codehaus/plexus/spring/FieldInjectionTest.java
springy/plexus-spring/src/test/resources/components.xml

index 51a5987e79a0bb298f15976bfe40f4dd2a8a469f..286548ea203bb8a68629e2eef82ace43c4d85080 100644 (file)
@@ -20,6 +20,7 @@ package org.codehaus.plexus.spring;
  */\r
 \r
 import java.lang.reflect.Field;\r
+import java.util.HashMap;\r
 import java.util.Iterator;\r
 import java.util.LinkedList;\r
 import java.util.List;\r
@@ -29,8 +30,6 @@ import org.codehaus.plexus.logging.LogEnabled;
 import org.codehaus.plexus.logging.LoggerManager;\r
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;\r
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;\r
-import org.codehaus.plexus.personality.plexus.lifecycle.phase.Startable;\r
-import org.codehaus.plexus.personality.plexus.lifecycle.phase.StartingException;\r
 import org.springframework.beans.BeansException;\r
 import org.springframework.beans.factory.BeanFactory;\r
 import org.springframework.beans.factory.BeanFactoryAware;\r
@@ -40,17 +39,17 @@ import org.springframework.beans.factory.FactoryBean;
 import org.springframework.beans.factory.InitializingBean;\r
 import org.springframework.beans.factory.ListableBeanFactory;\r
 import org.springframework.beans.factory.config.RuntimeBeanReference;\r
-import org.springframework.context.Lifecycle;\r
 import org.springframework.util.ReflectionUtils;\r
 \r
 /**\r
  * A FactoryBean dedicated to building plexus components. This includes :\r
  * <ul>\r
- *   <li>Support for direct field injection or "requirements"</li>\r
- *   <li>Support for LogEnabled, Initializable and Disposable plexus interfaces</li>\r
+ * <li>Support for direct field injection or "requirements"</li>\r
+ * <li>Support for LogEnabled, Initializable and Disposable plexus interfaces</li>\r
+ * <li>Support for plexus.requirement to get a Map<role-hint, component> for a role\r
  * </ul>\r
- * If not set, the beanFActory will auto-detect the loggerManager to use by searching for the\r
- * adequate bean in the spring context.\r
+ * If not set, the beanFActory will auto-detect the loggerManager to use by searching for the adequate bean in the\r
+ * spring context.\r
  * \r
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>\r
  */\r
@@ -70,7 +69,7 @@ public class PlexusComponentFactoryBean
     private LoggerManager loggerManager;\r
 \r
     private List instances = new LinkedList();\r
-    \r
+\r
     public void afterPropertiesSet()\r
         throws Exception\r
     {\r
@@ -92,7 +91,7 @@ public class PlexusComponentFactoryBean
             }\r
         }\r
     }\r
-    \r
+\r
     public void destroy()\r
         throws Exception\r
     {\r
@@ -103,13 +102,13 @@ public class PlexusComponentFactoryBean
                 Object component = (Object) iterator.next();\r
                 if ( component instanceof Disposable )\r
                 {\r
-                    ((Disposable) component).dispose();\r
-                    \r
+                    ( (Disposable) component ).dispose();\r
+\r
                 }\r
             }\r
-        }        \r
+        }\r
     }\r
-    \r
+\r
     public Object getObject()\r
         throws Exception\r
     {\r
@@ -128,7 +127,31 @@ public class PlexusComponentFactoryBean
                     Object dependency = requirements.get( field.getName() );\r
                     if ( dependency instanceof RuntimeBeanReference )\r
                     {\r
-                        dependency = beanFactory.getBean( ((RuntimeBeanReference) dependency).getBeanName() );                        \r
+                        String beanName = ( (RuntimeBeanReference) dependency ).getBeanName();\r
+                        if ( Map.class.isAssignableFrom( field.getType() ) )\r
+                        {\r
+                            // component ask plexus for a Map of all available components for the role\r
+                            Map map = new HashMap();\r
+                            String mask = beanName + '#';\r
+                            String[] beans = beanFactory.getBeanDefinitionNames();\r
+                            for ( int i = 0; i < beans.length; i++ )\r
+                            {\r
+                                String name = beans[i];\r
+                                if ( name.startsWith( mask ) )\r
+                                {\r
+                                    map.put( name.substring( mask.length() ), beanFactory.getBean( name ) );\r
+                                }\r
+                            }\r
+                            if ( beanFactory.containsBean( beanName ) )\r
+                            {\r
+                                map.put( "default", beanFactory.getBean( beanName ) );\r
+                            }\r
+                            dependency = map;\r
+                        }\r
+                        else\r
+                        {\r
+                            dependency = beanFactory.getBean( beanName );\r
+                        }\r
                     }\r
                     if ( dependency != null )\r
                     {\r
index dfd77ac1f3f4c6cf06f0d6fd7adc6e02a870e11a..808b30f390fe405e4d7dc01b03538bf9e318797a 100644 (file)
@@ -19,7 +19,6 @@ package org.codehaus.plexus.spring;
  * under the License.\r
  */\r
 \r
-import java.lang.reflect.Field;\r
 import java.util.HashMap;\r
 import java.util.Iterator;\r
 import java.util.List;\r
@@ -33,7 +32,6 @@ import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
 import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;\r
 import org.springframework.beans.factory.xml.NamespaceHandlerSupport;\r
 import org.springframework.beans.factory.xml.ParserContext;\r
-import org.springframework.util.ClassUtils;\r
 import org.springframework.util.xml.DomUtils;\r
 import org.w3c.dom.Element;\r
 \r
@@ -90,14 +88,6 @@ public class PlexusNamespaceHandler
                 String role = child.getAttribute( "role" );\r
                 String roleHint = child.getAttribute( "role-hint" );\r
                 String ref = PlexusToSpringUtils.buildSpringId( role, roleHint );\r
-                if ( roleHint == null )\r
-                {\r
-//                    Field f = ClassUtils.forName( implementation ).getField( name );\r
-//                    if ( Map.class.isAssignableFrom( f.getType() ) )\r
-//                    {\r
-//                        // TODO add add support for plexus role --> Map<role-hint, component>\r
-//                    }\r
-                }\r
                 dependencies.put( name, new RuntimeBeanReference( ref ) );\r
 \r
             }\r
@@ -113,7 +103,7 @@ public class PlexusNamespaceHandler
 \r
             builder.addPropertyValue( "requirements", dependencies );\r
         }\r
-        \r
+\r
         protected String resolveId( Element element, AbstractBeanDefinition definition, ParserContext parserContext )\r
             throws BeanDefinitionStoreException\r
         {\r
index a1feb29cd50e8530ca3edef3141a060a4ad8b1e8..94fb873ffd72bb9393f1d8ed07b362cba10eebe8 100644 (file)
@@ -1,6 +1,5 @@
 package org.codehaus.plexus.spring;\r
 \r
-\r
 /*\r
  * Licensed to the Apache Software Foundation (ASF) under one\r
  * or more contributor license agreements.  See the NOTICE file\r
@@ -28,7 +27,7 @@ import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
 \r
 /**\r
  * Utility method to convert plexus descriptors to spring bean context.\r
- *\r
+ * \r
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>\r
  * @since 1.1\r
  */\r
@@ -38,7 +37,7 @@ public class PlexusToSpringUtils
     public static String toSpringId( String string )\r
     {\r
         int i = string.lastIndexOf( '.' );\r
-        if (i >= 0 )\r
+        if ( i >= 0 )\r
         {\r
             return Character.toLowerCase( string.charAt( i + 1 ) ) + string.substring( i + 2 );\r
         }\r
@@ -104,5 +103,6 @@ public class PlexusToSpringUtils
             i = 0;\r
         }\r
         String id = Character.toLowerCase( role.charAt( i ) ) + role.substring( i + 1 );\r
-        return roleHint.length() == 0 ? id : id + '#' + roleHint;\r
-    }}\r
+        return ( roleHint.length() == 0 || "default".equals( roleHint ) ) ? id : id + '#' + roleHint;\r
+    }\r
+}\r
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 (file)
index 0000000..f3e9e72
--- /dev/null
@@ -0,0 +1,43 @@
+package org.codehaus.plexus.spring;\r
+\r
+import java.util.Map;\r
+\r
+/*\r
+ * Licensed to the Apache Software Foundation (ASF) under one\r
+ * or more contributor license agreements.  See the NOTICE file\r
+ * distributed with this work for additional information\r
+ * regarding copyright ownership.  The ASF licenses this file\r
+ * to you under the Apache License, Version 2.0 (the\r
+ * "License"); you may not use this file except in compliance\r
+ * with the License.  You may obtain a copy of the License at\r
+ *\r
+ *  http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing,\r
+ * software distributed under the License is distributed on an\r
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+ * KIND, either express or implied.  See the License for the\r
+ * specific language governing permissions and limitations\r
+ * under the License.\r
+ */\r
+\r
+/**\r
+ * A typical plexus component implementation\r
+ * \r
+ * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>\r
+ */\r
+public class ComplexPlexusBean\r
+{\r
+    /**\r
+     * @plexus.requirement role="org.codehaus.plexus.spring.PlexusBean"\r
+     */\r
+    private Map plexusBeans;\r
+\r
+    /**\r
+     * @see org.codehaus.plexus.spring.PlexusBean#toString()\r
+     */\r
+    public String toString()\r
+    {\r
+        return plexusBeans.size() + " components for role org.codehaus.plexus.spring.PlexusBean";\r
+    }\r
+}\r
index 92a3ccb198ecab280b71c0406f1785ed0cdc1c1f..16c016a5d57c9b1de9e0aedc38519fdde52266db 100644 (file)
@@ -37,5 +37,13 @@ public class FieldInjectionTest
         assertEquals( PlexusBean.DISPOSED, plexusBean.getState() );\r
         \r
     }\r
+    \r
+    public void testIbjectMapForRole()\r
+        throws Exception\r
+    {\r
+        ConfigurableApplicationContext applicationContext = new PlexusClassPathXmlApplicationContext( new String[] { "components.xml", "applicationContext.xml" } );       \r
+        ComplexPlexusBean plexusBean = (ComplexPlexusBean) applicationContext.getBean( "complexPlexusBean" );\r
+        assertEquals( "2 components for role org.codehaus.plexus.spring.PlexusBean", plexusBean.toString() );\r
+    }\r
 \r
 }\r
index 2176e02ccc978225b3e965801cdaf90e701f6372..32d190cb123cb4fca61e46463c18087ad38e7ac1 100644 (file)
@@ -2,6 +2,7 @@
   <components>\r
     <component>\r
       <role>org.codehaus.plexus.spring.PlexusBean</role>\r
+      <role-hint>default</role-hint>\r
       <implementation>org.codehaus.plexus.spring.PlexusBeanImpl</implementation>\r
       <requirements>\r
        <requirement>\r
         <message>expected</message>\r
       </configuration>\r
     </component>\r
+    <component>\r
+      <role>org.codehaus.plexus.spring.PlexusBean</role>\r
+      <role-hint>another</role-hint>\r
+      <implementation>org.codehaus.plexus.spring.PlexusBeanImpl</implementation>\r
+      <requirements>\r
+       <requirement>\r
+               <field-name>bean</field-name>\r
+               <role>springBean</role>\r
+       </requirement>\r
+      </requirements>\r
+      <configuration>\r
+        <message>another</message>\r
+      </configuration>\r
+    </component>\r
+    <component>\r
+      <role>org.codehaus.plexus.spring.ComplexPlexusBean</role>\r
+      <implementation>org.codehaus.plexus.spring.ComplexPlexusBean</implementation>\r
+      <requirements>\r
+       <requirement>\r
+               <field-name>plexusBeans</field-name>\r
+               <role>org.codehaus.plexus.spring.PlexusBean</role>\r
+       </requirement>\r
+      </requirements>\r
+    </component>    \r
   </components>\r
 </component-set>\r