*/\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
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
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
private LoggerManager loggerManager;\r
\r
private List instances = new LinkedList();\r
- \r
+\r
public void afterPropertiesSet()\r
throws Exception\r
{\r
}\r
}\r
}\r
- \r
+\r
public void destroy()\r
throws Exception\r
{\r
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
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
* 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
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
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
\r
builder.addPropertyValue( "requirements", dependencies );\r
}\r
- \r
+\r
protected String resolveId( Element element, AbstractBeanDefinition definition, ParserContext parserContext )\r
throws BeanDefinitionStoreException\r
{\r
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
\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
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
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
--- /dev/null
+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
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
<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