Author: nicolas
Date: Wed Feb 20 05:40:59 2008
New Revision: 629454

URL: http://svn.apache.org/viewvc?rev=629454&view=rev
Log:
Spring BeanDefinitionReader to convert plexus descriptors on the fly

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

Modified: maven/archiva/branches/springy/archiva-base/archiva-common/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/springy/archiva-base/archiva-common/pom.xml?rev=629454&r1=629453&r2=629454&view=diff
==============================================================================
--- maven/archiva/branches/springy/archiva-base/archiva-common/pom.xml 
(original)
+++ maven/archiva/branches/springy/archiva-base/archiva-common/pom.xml Wed Feb 
20 05:40:59 2008
@@ -30,7 +30,7 @@
   <dependencies>
     <!-- TO OTHER DEVELOPERS:
          This module should depend on NO OTHER ARCHIVA MODULES.
-         If you feel tempted to add one, discuss it first in the 
+         If you feel tempted to add one, discuss it first in the
          [EMAIL PROTECTED] mailing-list.
             [EMAIL PROTECTED]
       -->
@@ -55,6 +55,11 @@
       <groupId>org.springframework</groupId>
       <artifactId>spring-beans</artifactId>
       <version>2.5.1</version>
+    </dependency>
+    <dependency>
+      <groupId>dom4j</groupId>
+      <artifactId>dom4j</artifactId>
+      <version>1.6.1</version>
     </dependency>
   </dependencies>
   <build>

Added: 
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=629454&view=auto
==============================================================================
--- 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java
 (added)
+++ 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java
 Wed Feb 20 05:40:59 2008
@@ -0,0 +1,75 @@
+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 java.io.InputStream;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamSource;
+
+import org.springframework.beans.factory.xml.BeanDefinitionDocumentReader;
+import 
org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader;
+import org.springframework.beans.factory.xml.XmlReaderContext;
+import org.w3c.dom.Document;
+
+/**
+ * A Spring [EMAIL PROTECTED] BeanDefinitionDocumentReader} that converts on 
the fly the
+ * Plexus components descriptor to a spring XML context.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Nicolas De Loof</a>
+ * @since 1.1
+ */
+public class PlexusBeanDefinitionDocumentReader
+    extends DefaultBeanDefinitionDocumentReader
+{
+
+    public void registerBeanDefinitions( Document doc, XmlReaderContext 
readerContext )
+    {
+        convertPlexusDescriptorToSpringBeans( doc );
+        super.registerBeanDefinitions( doc, readerContext );
+    }
+
+    protected Document convertPlexusDescriptorToSpringBeans( Document doc )
+    {
+        try
+        {
+            Source xmlSource = new DOMSource( doc );
+            InputStream is = getClass().getResourceAsStream( 
"plexus2spring.xsl" );
+            Source xsltSource = new StreamSource( is );
+
+            DOMResult transResult = new DOMResult();
+
+            TransformerFactory tf = TransformerFactory.newInstance();
+            Transformer t = tf.newTransformer( xsltSource );
+            t.transform( xmlSource, transResult );
+
+            return (Document) transResult.getNode();
+        }
+        catch ( Exception e )
+        {
+            // FIXME log the error;
+            return doc;
+        }
+    }
+}

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

Propchange: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.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/PlexusBeanDefinitionDocumentReader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
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=629454&view=auto
==============================================================================
--- 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl
 (added)
+++ 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl
 Wed Feb 20 05:40:59 2008
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet
+    version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
+
+<xsl:output method="xml" indent="yes" />
+
+<xsl:template match="/component-set">
+<beans>
+  <xsl:for-each select="components/component">
+    <bean>
+      <xsl:choose>
+        <xsl:when test="role-hint">
+          <xsl:attribute name="id">
+            <xsl:value-of select="role" />
+          </xsl:attribute>
+        </xsl:when>
+        <xsl:otherwise>
+          <xsl:attribute name="id">
+            <xsl:value-of select="concat( role, '#', role-hint )" />
+          </xsl:attribute>
+        </xsl:otherwise>
+      </xsl:choose>
+      <xsl:attribute name="class">
+        <xsl:value-of select="implementation" />
+      </xsl:attribute>
+      <xsl:for-each select="requirements/requirement">
+        <property>
+          <xsl:attribute name="name">
+            <xsl:value-of select="field-name" />
+          </xsl:attribute>
+          <xsl:choose>
+            <xsl:when test="role-hint">
+              <xsl:attribute name="ref">
+                <xsl:value-of select="concat( role, '#', role-hint )" />
+              </xsl:attribute>
+            </xsl:when>
+            <xsl:otherwise>
+              <xsl:attribute name="ref">
+                <xsl:value-of select="role" />
+              </xsl:attribute>
+            </xsl:otherwise>
+          </xsl:choose>
+        </property>
+      </xsl:for-each>
+      <xsl:for-each select="configuration/*">
+        <property>
+          <xsl:attribute name="name">
+            <xsl:value-of select="name(.)" />
+          </xsl:attribute>
+          <xsl:attribute name="value">
+            <xsl:value-of select="." />
+          </xsl:attribute>
+        </property>
+      </xsl:for-each>
+    </bean>
+  </xsl:for-each>
+</beans>
+</xsl:template>
+
+</xsl:stylesheet>
\ No newline at end of file

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

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

Propchange: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: 
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=629454&view=auto
==============================================================================
--- 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReaderTest.java
 (added)
+++ 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReaderTest.java
 Wed Feb 20 05:40:59 2008
@@ -0,0 +1,57 @@
+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 java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.net.URL;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.dom4j.io.DOMReader;
+import org.dom4j.io.OutputFormat;
+import org.dom4j.io.XMLWriter;
+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()
+        throws Exception
+    {
+        URL plexus = getClass().getResource( "components.xml" );
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        DocumentBuilder builder = factory.newDocumentBuilder();
+        Document doc = builder.parse( plexus.openStream() );
+
+        doc = reader.convertPlexusDescriptorToSpringBeans( doc );
+
+        new XMLWriter( System.out, OutputFormat.createPrettyPrint() ).write( 
new DOMReader().read( doc ) );
+    }
+}

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

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

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

Added: 
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=629454&view=auto
==============================================================================
--- 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml
 (added)
+++ 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml
 Wed Feb 20 05:40:59 2008
@@ -0,0 +1,40 @@
+<component-set>
+  <components>
+    <component>
+      <role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role>
+      
<implementation>org.apache.maven.archiva.configuration.DefaultArchivaConfiguration</implementation>
+      <description>&lt;p&gt;
+Implementation of configuration holder that retrieves it from the 
registry.</description>
+      <requirements>
+        <requirement>
+          <role>org.codehaus.plexus.registry.Registry</role>
+          <role-hint>commons-configuration</role-hint>
+          <field-name>registry</field-name>
+        </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.policies.PreDownloadPolicy</role>
+          <field-name>prePolicies</field-name>
+        </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.policies.PostDownloadPolicy</role>
+          <field-name>postPolicies</field-name>
+        </requirement>
+      </requirements>
+      <configuration>
+        
<user-config-filename>${user.home}/.m2/archiva.xml</user-config-filename>
+        
<alt-config-filename>${appserver.base}/conf/archiva.xml</alt-config-filename>
+      </configuration>
+    </component>
+    <component>
+      <role>org.apache.maven.archiva.configuration.FileTypes</role>
+      
<implementation>org.apache.maven.archiva.configuration.FileTypes</implementation>
+      <description>FileTypes</description>
+      <requirements>
+        <requirement>
+          
<role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role>
+          <field-name>archivaConfiguration</field-name>
+        </requirement>
+      </requirements>
+    </component>
+  </components>
+</component-set>

Propchange: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: 
maven/archiva/branches/springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml


Reply via email to