]> source.dussan.org Git - archiva.git/commitdiff
add support for WebApplicationContext
authorNicolas De Loof <nicolas@apache.org>
Thu, 28 Feb 2008 15:16:09 +0000 (15:16 +0000)
committerNicolas De Loof <nicolas@apache.org>
Thu, 28 Feb 2008 15:16:09 +0000 (15:16 +0000)
fix plexus descriptors that use <plexus> root element (redback)

git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@631995 13f79535-47bb-0310-9956-ffa450edef68

springy/plexus-spring/pom.xml
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusApplicationContextDelegate.java [new file with mode: 0644]
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusBeanDefinitionDocumentReader.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusClassPathXmlApplicationContext.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusContainerAdapter.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusInSpringTestCase.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusToSpringUtils.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusWebApplicationContext.java [new file with mode: 0644]
springy/plexus-spring/src/main/resources/org/codehaus/plexus/spring/PlexusBeanDefinitionDocumentReader.xsl
springy/plexus-spring/src/test/resources/testInjectMapForRole.xml

index 8453dccbfa8131a9104ac925158f8833ece77ab2..cdea479bfedcdb2c3e47846d52a1a1521c44c064 100644 (file)
       <artifactId>spring-context</artifactId>\r
       <version>2.5.1</version>\r
     </dependency>\r
+    <dependency>\r
+      <groupId>org.springframework</groupId>\r
+      <artifactId>spring-web</artifactId>\r
+      <version>2.5.1</version>\r
+      <optional>true</optional>\r
+    </dependency>\r
     <dependency>\r
       <groupId>commons-lang</groupId>\r
       <artifactId>commons-lang</artifactId>\r
diff --git a/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusApplicationContextDelegate.java b/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusApplicationContextDelegate.java
new file mode 100644 (file)
index 0000000..d75e03d
--- /dev/null
@@ -0,0 +1,99 @@
+package org.codehaus.plexus.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.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import org.apache.commons.lang.ClassUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.codehaus.plexus.PlexusConstants;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.ListableBeanFactory;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
+import org.springframework.context.ApplicationContext;
+
+/**
+ * Utility method to convert plexus descriptors to spring bean context.
+ *
+ * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
+ */
+public class PlexusApplicationContextDelegate
+{
+    /** Logger used by this class. */
+    protected final Log logger = LogFactory.getLog(getClass());
+
+    private PlexusLifecycleBeanPostProcessor lifecycleBeanPostProcessor;
+
+    /**
+     * @see org.springframework.context.support.AbstractXmlApplicationContext#loadBeanDefinitions(org.springframework.beans.factory.xml.XmlBeanDefinitionReader)
+     */
+    protected void loadBeanDefinitions( XmlBeanDefinitionReader reader )
+        throws BeansException, IOException
+    {
+        logger.info( "Registering plexus to spring XML translation" );
+        reader.setDocumentReaderClass( PlexusBeanDefinitionDocumentReader.class );
+        reader.setValidationMode( XmlBeanDefinitionReader.VALIDATION_NONE );
+    }
+
+    /**
+     * @see org.springframework.context.support.AbstractApplicationContext#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
+     */
+    protected void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory, ApplicationContext context )
+    {
+        // Register a PlexusContainerAdapter bean to allow context lookups using plexus API
+        PlexusContainerAdapter plexus = new PlexusContainerAdapter();
+        plexus.setApplicationContext( context );
+        beanFactory.registerSingleton( "plexusContainer", plexus );
+
+        // Register a beanPostProcessor to handle plexus interface-based lifecycle management
+        lifecycleBeanPostProcessor = new PlexusLifecycleBeanPostProcessor();
+        lifecycleBeanPostProcessor.setBeanFactory( context );
+        beanFactory.addBeanPostProcessor( lifecycleBeanPostProcessor );
+
+        // Register a PorpertyEditor to support plexus XML <configuration> set as CDATA in
+        // a spring context XML file.
+        beanFactory.addPropertyEditorRegistrar( new PlexusConfigurationPropertyEditor() );
+    }
+
+    /**
+     * @see org.springframework.context.support.AbstractApplicationContext#doClose()
+     */
+    protected void doClose()
+    {
+        try
+        {
+            lifecycleBeanPostProcessor.destroy();
+        }
+        catch ( Throwable ex )
+        {
+            logger.error( "Exception thrown from PlexusLifecycleBeanPostProcessor handling ContextClosedEvent", ex );
+        }
+    }
+}
index cac48637db88b38fad3424824a7de6f053c8adcf..20b612fa2669b0299e4c3d1bb47d00003dee9b3c 100644 (file)
@@ -24,6 +24,7 @@ import java.io.InputStream;
 import javax.xml.transform.Source;\r
 import javax.xml.transform.Transformer;\r
 import javax.xml.transform.TransformerFactory;\r
+import javax.xml.transform.TransformerFactoryConfigurationError;\r
 import javax.xml.transform.dom.DOMResult;\r
 import javax.xml.transform.dom.DOMSource;\r
 import javax.xml.transform.stream.StreamSource;\r
@@ -67,11 +68,21 @@ public class PlexusBeanDefinitionDocumentReader
 \r
     protected Document convertPlexusDescriptorToSpringBeans( Document doc )\r
     {\r
-        if ( !"component-set".equals( doc.getDocumentElement().getNodeName() ) )\r
+        if ( "component-set".equals( doc.getDocumentElement().getNodeName() ) )\r
         {\r
-            return doc;\r
+            return translatePlexusDescriptor( doc );\r
+        }\r
+        if ( "plexus".equals( doc.getDocumentElement().getNodeName() ) )\r
+        {\r
+            return translatePlexusDescriptor( doc );\r
         }\r
 \r
+        return doc;\r
+    }\r
+\r
+    private Document translatePlexusDescriptor( Document doc )\r
+        throws TransformerFactoryConfigurationError\r
+    {\r
         try\r
         {\r
             Source xmlSource = new DOMSource( doc );\r
index 083013582b4162e15701ceac18e8e9a263b0de21..8c43db3fd8878c2ffe1b5a805e655aaa311060ef 100644 (file)
@@ -37,7 +37,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
 public class PlexusClassPathXmlApplicationContext\r
     extends ClassPathXmlApplicationContext\r
 {\r
-    private PlexusLifecycleBeanPostProcessor lifecycleBeanPostProcessor;\r
+    private static PlexusApplicationContextDelegate delegate = new PlexusApplicationContextDelegate();\r
 \r
     public PlexusClassPathXmlApplicationContext( String path, Class clazz )\r
         throws BeansException\r
@@ -45,9 +45,6 @@ public class PlexusClassPathXmlApplicationContext
         super( path, clazz );\r
     }\r
 \r
-\r
-\r
-\r
     public PlexusClassPathXmlApplicationContext( String configLocation )\r
         throws BeansException\r
     {\r
@@ -98,9 +95,7 @@ public class PlexusClassPathXmlApplicationContext
     protected void loadBeanDefinitions( XmlBeanDefinitionReader reader )\r
         throws BeansException, IOException\r
     {\r
-        logger.info( "Registering plexus to spring XML translation" );\r
-        reader.setDocumentReaderClass( PlexusBeanDefinitionDocumentReader.class );\r
-        reader.setValidationMode( XmlBeanDefinitionReader.VALIDATION_NONE );\r
+        delegate.loadBeanDefinitions( reader );\r
         super.loadBeanDefinitions( reader );\r
     }\r
 \r
@@ -111,19 +106,7 @@ public class PlexusClassPathXmlApplicationContext
      */\r
     protected void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory )\r
     {\r
-        // Register a PlexusContainerAdapter bean to allow context lookups using plexus API\r
-        PlexusContainerAdapter plexus = new PlexusContainerAdapter();\r
-        plexus.setApplicationContext( this );\r
-        beanFactory.registerSingleton( "plexusContainer", plexus );\r
-\r
-        // Register a beanPostProcessor to handle plexus interface-based lifecycle management\r
-        lifecycleBeanPostProcessor = new PlexusLifecycleBeanPostProcessor();\r
-        lifecycleBeanPostProcessor.setBeanFactory( this );\r
-        beanFactory.addBeanPostProcessor( lifecycleBeanPostProcessor );\r
-\r
-        // Register a PorpertyEditor to support plexus XML <configuration> set as CDATA in\r
-        // a spring context XML file.\r
-        beanFactory.addPropertyEditorRegistrar( new PlexusConfigurationPropertyEditor() );\r
+        delegate.postProcessBeanFactory( beanFactory, this );\r
     }\r
 \r
     /**\r
@@ -133,14 +116,7 @@ public class PlexusClassPathXmlApplicationContext
      */\r
     protected void doClose()\r
     {\r
-        try\r
-        {\r
-            lifecycleBeanPostProcessor.destroy();\r
-        }\r
-        catch ( Throwable ex )\r
-        {\r
-            logger.error( "Exception thrown from PlexusLifecycleBeanPostProcessor handling ContextClosedEvent", ex );\r
-        }\r
+        delegate.doClose();\r
         super.doClose();\r
     }\r
 \r
index e0fe3d2ec62fb1c78c34ac79e227047d6f8c575b..6b667e7a63d02fa557438c337e78eab5707e9001 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
+import org.codehaus.plexus.PlexusConstants;
 import org.codehaus.plexus.PlexusContainer;
 import org.codehaus.plexus.PlexusContainerException;
 import org.codehaus.plexus.classworlds.realm.ClassRealm;
@@ -48,7 +49,7 @@ import org.springframework.context.ApplicationContextAware;
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
  */
 public class PlexusContainerAdapter
-    implements PlexusContainer, ApplicationContextAware, InitializingBean
+    implements PlexusContainer, ApplicationContextAware
 {
     private Context context = new SimpleContext();
 
@@ -60,16 +61,6 @@ public class PlexusContainerAdapter
         super();
     }
 
-    /**
-     * {@inheritDoc}
-     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
-     */
-    public void afterPropertiesSet()
-        throws Exception
-    {
-        context = new SimpleContext();
-    }
-
     /**
      * {@inheritDoc}
      * @see org.codehaus.plexus.PlexusContainer#addComponentDescriptor(org.codehaus.plexus.component.repository.ComponentDescriptor)
@@ -660,8 +651,6 @@ public class PlexusContainerAdapter
 
     private class SimpleContext implements Context
     {
-        /** the plexus container key in the context */
-        private static final String PLEXUS = "plexus";
 
         /**
          * {@inheritDoc}
@@ -669,7 +658,7 @@ public class PlexusContainerAdapter
          */
         public boolean contains( Object key )
         {
-            return PLEXUS.equals( key );
+            return PlexusConstants.PLEXUS_KEY.equals( key );
         }
 
         /**
@@ -679,7 +668,7 @@ public class PlexusContainerAdapter
         public Object get( Object key )
             throws ContextException
         {
-            return PLEXUS.equals( key ) ? PlexusContainerAdapter.this : null;
+            return PlexusConstants.PLEXUS_KEY.equals( key ) ? PlexusContainerAdapter.this : null;
         }
 
         /**
index ff0dedd18b705c101ccd77d634175e440701b312..40d46f9f0b5d7b05969d9b80b6200a5903814d25 100644 (file)
@@ -25,8 +25,6 @@ import junit.framework.TestCase;
 
 import org.springframework.context.ConfigurableApplicationContext;
 
-;
-
 /**
  * Mimic org.codehaus.plexus.PlexusTestCase as simple replacement for test
  * cases.
index 0c021528e7230d84bd1128255e1a5fc9f085aa59..5450dd933ac018c17e7d8e7d7678417a41a3704e 100644 (file)
@@ -36,7 +36,6 @@ import org.springframework.beans.factory.ListableBeanFactory;
  * Utility method to convert plexus descriptors to spring bean context.\r
  *\r
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>\r
- * @since 1.1\r
  */\r
 public class PlexusToSpringUtils\r
 {\r
diff --git a/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusWebApplicationContext.java b/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusWebApplicationContext.java
new file mode 100644 (file)
index 0000000..87970a0
--- /dev/null
@@ -0,0 +1,75 @@
+package org.codehaus.plexus.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.IOException;
+
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
+import org.springframework.web.context.support.XmlWebApplicationContext;
+
+/**
+ * A custom XmlWebApplicationContext to support plexus
+ * <tr>components.xml</tt> descriptors in Spring, with no changes required to
+ * neither plexus nor spring beans.
+ *
+ * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
+ */
+public class PlexusWebApplicationContext
+    extends XmlWebApplicationContext
+{
+    private static PlexusApplicationContextDelegate delegate = new PlexusApplicationContextDelegate();
+
+    /**
+     * {@inheritDoc}
+     *
+     * @see org.springframework.context.support.AbstractXmlApplicationContext#loadBeanDefinitions(org.springframework.beans.factory.xml.XmlBeanDefinitionReader)
+     */
+    protected void loadBeanDefinitions( XmlBeanDefinitionReader reader )
+        throws BeansException, IOException
+    {
+        delegate.loadBeanDefinitions( reader );
+        super.loadBeanDefinitions( reader );
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * @see org.springframework.context.support.AbstractApplicationContext#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
+     */
+    protected void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory )
+    {
+        delegate.postProcessBeanFactory( beanFactory, this );
+        super.postProcessBeanFactory( beanFactory );
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * @see org.springframework.context.support.AbstractApplicationContext#doClose()
+     */
+    protected void doClose()
+    {
+        delegate.doClose();
+        super.doClose();
+    }
+
+}
index 9b64e187f60c29ab4916f255fa39916361072aad..6bcd650e57f55258d20a6ce6f49809ffd465b0e1 100644 (file)
   to handle IoC containers incompatibilities.\r
  -->\r
 \r
-<xsl:template match="/component-set" >\r
+<xsl:template match="/" >\r
 <spring:beans xmlns:spring="http://www.springframework.org/schema/beans"\r
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
        xmlns="http://plexus.codehaus.org/spring"\r
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd\r
                            http://plexus.codehaus.org/spring http://plexus.codehaus.org/schemas/spring-1.0.xsd"\r
        default-lazy-init="true">\r
-  <xsl:for-each select="components/component">\r
+  <xsl:for-each select="//component">\r
 \r
     <component>\r
       <xsl:attribute name="role">\r
index 819b38c6dbfd3e823d7c699df1b8e3615b80364f..9e6d6e16161e742fdce2496b46b77923c2eaf5dd 100644 (file)
@@ -1,4 +1,7 @@
-<component-set>\r
+<?xml version="1.0" encoding="UTF-8"?>\r
+\r
+<plexus>\r
+\r
   <components>\r
     <component>\r
       <role>org.codehaus.plexus.spring.PlexusBean</role>\r
@@ -16,4 +19,5 @@
       </requirements>\r
     </component>\r
   </components>\r
-</component-set>\r
+\r
+</plexus>\r