]> source.dussan.org Git - archiva.git/commitdiff
Spring BeanDefinitionReader to convert plexus descriptors on the fly
authorNicolas De Loof <nicolas@apache.org>
Wed, 20 Feb 2008 13:40:59 +0000 (13:40 +0000)
committerNicolas De Loof <nicolas@apache.org>
Wed, 20 Feb 2008 13:40:59 +0000 (13:40 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@629454 13f79535-47bb-0310-9956-ffa450edef68

springy/archiva-base/archiva-common/pom.xml
springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java [new file with mode: 0644]
springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl [new file with mode: 0644]
springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReaderTest.java [new file with mode: 0644]
springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml [new file with mode: 0644]

index db977b9a3fff19dfdb701939a738991a2e338567..1e3b0ebaefe375b2dff1f2bd21e2a7683b444590 100644 (file)
@@ -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
          archiva-dev@maven.apache.org mailing-list.
             joakime@apache.org
       -->
       <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>
     <plugins>
diff --git a/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java b/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java
new file mode 100644 (file)
index 0000000..4f0d300
--- /dev/null
@@ -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 {@link BeanDefinitionDocumentReader} that converts on the fly the
+ * Plexus components descriptor to a spring XML context.
+ *
+ * @author <a href="mailto:nicolas@apache.org">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;
+        }
+    }
+}
diff --git a/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl b/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl
new file mode 100644 (file)
index 0000000..7e77161
--- /dev/null
@@ -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
diff --git a/springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReaderTest.java b/springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReaderTest.java
new file mode 100644 (file)
index 0000000..e437b48
--- /dev/null
@@ -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:nicolas@apache.org">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 ) );
+    }
+}
diff --git a/springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml b/springy/archiva-base/archiva-common/src/test/resources/org/apache/maven/archiva/common/spring/components.xml
new file mode 100644 (file)
index 0000000..bc093f6
--- /dev/null
@@ -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>