Author: nicolas
Date: Wed Feb 20 06:36:29 2008
New Revision: 629479

URL: http://svn.apache.org/viewvc?rev=629479&view=rev
Log:
plexus bean factory to convert plexus components descriptor on the fly 
support for instantiation-strategy=per-lookup
basic testcase included

Added:
    
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanFactory.java
   (with props)
Modified:
    
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java
    
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl
    
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReaderTest.java
    
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml

Modified: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java?rev=629479&r1=629478&r2=629479&view=diff
==============================================================================
--- 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java
 (original)
+++ 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java
 Wed Feb 20 06:36:29 2008
@@ -46,7 +46,7 @@
 
     public void registerBeanDefinitions( Document doc, XmlReaderContext 
readerContext )
     {
-        convertPlexusDescriptorToSpringBeans( doc );
+        doc = convertPlexusDescriptorToSpringBeans( doc );
         super.registerBeanDefinitions( doc, readerContext );
     }
 

Added: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanFactory.java
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanFactory.java?rev=629479&view=auto
==============================================================================
--- 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanFactory.java
 (added)
+++ 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanFactory.java
 Wed Feb 20 06:36:29 2008
@@ -0,0 +1,49 @@
+package org.apache.maven.archiva.common.spring;
+
+/*
+ * 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.
+ */
+
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.support.DefaultListableBeanFactory;
+import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
+import org.springframework.core.io.Resource;
+
+/**
+ * @author <a href="mailto:[EMAIL PROTECTED]">Nicolas De Loof</a>
+ * @since 1.1
+ */
+public class PlexusBeanFactory
+    extends DefaultListableBeanFactory
+{
+    private final XmlBeanDefinitionReader reader = new 
XmlBeanDefinitionReader( this );
+
+    public PlexusBeanFactory( Resource resource )
+    {
+        this( resource, null );
+    }
+
+    public PlexusBeanFactory( Resource resource, BeanFactory parentBeanFactory 
)
+    {
+        super( parentBeanFactory );
+        this.reader.setDocumentReaderClass( 
PlexusBeanDefinitionDocumentReader.class );
+        this.reader.setValidationMode( XmlBeanDefinitionReader.VALIDATION_NONE 
);
+        this.reader.loadBeanDefinitions( resource );
+    }
+
+}

Propchange: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl?rev=629479&r1=629478&r2=629479&view=diff
==============================================================================
--- 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl
 (original)
+++ 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl
 Wed Feb 20 06:36:29 2008
@@ -2,7 +2,9 @@
 <xsl:stylesheet
     version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 
-<xsl:output method="xml" indent="yes" />
+<xsl:output method="xml" indent="yes"
+    doctype-public="-//SPRING//DTD BEAN 2.0//EN"
+    doctype-system="http://www.springframework.org/dtd/spring-beans-2.0.dtd"; />
 
 <xsl:template match="/component-set">
 <beans>
@@ -11,18 +13,21 @@
       <xsl:choose>
         <xsl:when test="role-hint">
           <xsl:attribute name="id">
-            <xsl:value-of select="role" />
+            <xsl:value-of select="concat( role, '#', role-hint )" />
           </xsl:attribute>
         </xsl:when>
         <xsl:otherwise>
           <xsl:attribute name="id">
-            <xsl:value-of select="concat( role, '#', role-hint )" />
+            <xsl:value-of select="role" />
           </xsl:attribute>
         </xsl:otherwise>
       </xsl:choose>
       <xsl:attribute name="class">
         <xsl:value-of select="implementation" />
       </xsl:attribute>
+        <xsl:if test="instanciation-strategy/text() = 'per-lookup'">
+          <xsl:attribute name="scope">prototype</xsl:attribute>
+        </xsl:if>
       <xsl:for-each select="requirements/requirement">
         <property>
           <xsl:attribute name="name">

Modified: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReaderTest.java
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReaderTest.java?rev=629479&r1=629478&r2=629479&view=diff
==============================================================================
--- 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReaderTest.java
 (original)
+++ 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReaderTest.java
 Wed Feb 20 06:36:29 2008
@@ -19,30 +19,28 @@
  * under the License.
  */
 
-import java.io.InputStream;
-import java.io.OutputStreamWriter;
 import java.net.URL;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 
+import junit.framework.TestCase;
+
 import org.dom4j.io.DOMReader;
 import org.dom4j.io.OutputFormat;
 import org.dom4j.io.XMLWriter;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.core.io.UrlResource;
 import org.w3c.dom.Document;
 
-import junit.framework.TestCase;
-
 /**
- *
  * @author <a href="mailto:[EMAIL PROTECTED]">Nicolas De Loof</a>
  */
 public class PlexusBeanDefinitionDocumentReaderTest
     extends TestCase
 {
-    PlexusBeanDefinitionDocumentReader reader = new 
PlexusBeanDefinitionDocumentReader();
 
-    public void testConvertPlexusToSpring()
+    public void testXslt()
         throws Exception
     {
         URL plexus = getClass().getResource( "components.xml" );
@@ -50,8 +48,26 @@
         DocumentBuilder builder = factory.newDocumentBuilder();
         Document doc = builder.parse( plexus.openStream() );
 
+        PlexusBeanDefinitionDocumentReader reader = new 
PlexusBeanDefinitionDocumentReader();
         doc = reader.convertPlexusDescriptorToSpringBeans( doc );
 
         new XMLWriter( System.out, OutputFormat.createPrettyPrint() ).write( 
new DOMReader().read( doc ) );
+    }
+
+    /**
+     * Test conversion from a typical Plexus components descriptor to a spring 
beanFactory
+     * @throws Exception
+     */
+    public void testConvertPlexusToSpring()
+        throws Exception
+    {
+        URL plexus = getClass().getResource( "components.xml" );
+        PlexusBeanFactory factory = new PlexusBeanFactory( new UrlResource( 
plexus ) );
+        assertEquals( 2, factory.getBeanDefinitionCount() );
+
+        BeanDefinition bd = factory.getBeanDefinition( 
"org.apache.maven.archiva.configuration.ArchivaConfiguration" );
+        assertEquals( 
"org.apache.maven.archiva.configuration.DefaultArchivaConfiguration", 
bd.getBeanClassName() );
+        assertEquals( "prototype", bd.getScope() );
+        assertEquals( 5, bd.getPropertyValues().size() );
     }
 }

Modified: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml?rev=629479&r1=629478&r2=629479&view=diff
==============================================================================
--- 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml
 (original)
+++ 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml
 Wed Feb 20 06:36:29 2008
@@ -3,6 +3,7 @@
     <component>
       <role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role>
       
<implementation>org.apache.maven.archiva.configuration.DefaultArchivaConfiguration</implementation>
+      <instanciation-strategy>per-lookup</instanciation-strategy>
       <description>&lt;p&gt;
 Implementation of configuration holder that retrieves it from the 
registry.</description>
       <requirements>


Reply via email to