]> source.dussan.org Git - archiva.git/commitdiff
plexus lifecycle handling moved to a BeanPostProcessor
authorNicolas De Loof <nicolas@apache.org>
Tue, 26 Feb 2008 16:52:49 +0000 (16:52 +0000)
committerNicolas De Loof <nicolas@apache.org>
Tue, 26 Feb 2008 16:52:49 +0000 (16:52 +0000)
BeanPostProcessor and PlexusContainerAdapter automatically registered in the ApplicationContext
fall back to ConsoleLoggerManager when no LoggerManager bean found
===========================================================
!!! archiva-proxy tests now pass as PlexusInSpringTests !!!
===========================================================

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

16 files changed:
springy/archiva-base/archiva-policies/src/test/resources/org/apache/maven/archiva/policies/CachedFailuresPolicyTest-context.xml
springy/archiva-base/archiva-policies/src/test/resources/org/apache/maven/archiva/policies/ChecksumPolicyTest-context.xml [deleted file]
springy/archiva-base/archiva-policies/src/test/resources/org/apache/maven/archiva/policies/ReleasePolicyTest-context.xml [deleted file]
springy/archiva-base/archiva-policies/src/test/resources/org/apache/maven/archiva/policies/SnapshotsPolicyTest-context.xml [deleted file]
springy/archiva-base/archiva-proxy/src/test/resources/org/apache/maven/archiva/proxy/ChecksumTransferTest.xml
springy/archiva-base/archiva-proxy/src/test/resources/org/apache/maven/archiva/proxy/ManagedDefaultTransferTest.xml
springy/archiva-base/archiva-proxy/src/test/resources/org/apache/maven/archiva/proxy/ManagedLegacyTransferTest.xml
springy/archiva-base/archiva-proxy/src/test/resources/org/apache/maven/archiva/proxy/MetadataTransferTest.xml
springy/archiva-base/archiva-proxy/src/test/resources/org/apache/maven/archiva/proxy/RelocateTransferTest.xml
springy/archiva-base/archiva-proxy/src/test/resources/org/apache/maven/archiva/proxy/SnapshotTransferTest.xml
springy/archiva-base/archiva-proxy/src/test/resources/org/apache/maven/archiva/proxy/spring-context.xml
springy/plexus-spring/pom.xml
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusClassPathXmlApplicationContext.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusComponentFactoryBean.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusContainerAdapter.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusLifecycleBeanPostProcessor.java [new file with mode: 0644]

index 6b9cb384cc3c4799ffae2d5ed216fe6bb3ed9283..840223b1a0298a8f65002f81ab72f32ed8327df1 100644 (file)
@@ -4,11 +4,6 @@
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
-  <bean id="plexusContainer" class="org.codehaus.plexus.spring.PlexusContainerAdapter"/>
-
-  <bean id="loggerManager" class="org.codehaus.plexus.logging.console.ConsoleLoggerManager"
-       init-method="initialize"/>
-
   <bean id="urlFailureCache" class="org.apache.maven.archiva.policies.urlcache.DefaultUrlFailureCache">
     <!-- collaborators and configuration for this bean go here -->
     <constructor-arg ref="cache#url-failures-cache" type="org.codehaus.plexus.cache.Cache"/>
diff --git a/springy/archiva-base/archiva-policies/src/test/resources/org/apache/maven/archiva/policies/ChecksumPolicyTest-context.xml b/springy/archiva-base/archiva-policies/src/test/resources/org/apache/maven/archiva/policies/ChecksumPolicyTest-context.xml
deleted file mode 100644 (file)
index 5add86c..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-       xsi:schemaLocation="http://www.springframework.org/schema/beans
-           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
-
-  <bean id="plexusContainer" class="org.codehaus.plexus.spring.PlexusContainerAdapter"/>
-
-  <bean id="loggerManager" class="org.codehaus.plexus.logging.console.ConsoleLoggerManager"
-       init-method="initialize"/>
-
-  <bean id="urlFailureCache" class="org.apache.maven.archiva.policies.urlcache.DefaultUrlFailureCache">
-    <!-- collaborators and configuration for this bean go here -->
-    <constructor-arg ref="cache#url-failures-cache" type="org.codehaus.plexus.cache.Cache"/>
-  </bean>
-</beans>
\ No newline at end of file
diff --git a/springy/archiva-base/archiva-policies/src/test/resources/org/apache/maven/archiva/policies/ReleasePolicyTest-context.xml b/springy/archiva-base/archiva-policies/src/test/resources/org/apache/maven/archiva/policies/ReleasePolicyTest-context.xml
deleted file mode 100644 (file)
index 5add86c..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-       xsi:schemaLocation="http://www.springframework.org/schema/beans
-           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
-
-  <bean id="plexusContainer" class="org.codehaus.plexus.spring.PlexusContainerAdapter"/>
-
-  <bean id="loggerManager" class="org.codehaus.plexus.logging.console.ConsoleLoggerManager"
-       init-method="initialize"/>
-
-  <bean id="urlFailureCache" class="org.apache.maven.archiva.policies.urlcache.DefaultUrlFailureCache">
-    <!-- collaborators and configuration for this bean go here -->
-    <constructor-arg ref="cache#url-failures-cache" type="org.codehaus.plexus.cache.Cache"/>
-  </bean>
-</beans>
\ No newline at end of file
diff --git a/springy/archiva-base/archiva-policies/src/test/resources/org/apache/maven/archiva/policies/SnapshotsPolicyTest-context.xml b/springy/archiva-base/archiva-policies/src/test/resources/org/apache/maven/archiva/policies/SnapshotsPolicyTest-context.xml
deleted file mode 100644 (file)
index 5add86c..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-       xsi:schemaLocation="http://www.springframework.org/schema/beans
-           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
-
-  <bean id="plexusContainer" class="org.codehaus.plexus.spring.PlexusContainerAdapter"/>
-
-  <bean id="loggerManager" class="org.codehaus.plexus.logging.console.ConsoleLoggerManager"
-       init-method="initialize"/>
-
-  <bean id="urlFailureCache" class="org.apache.maven.archiva.policies.urlcache.DefaultUrlFailureCache">
-    <!-- collaborators and configuration for this bean go here -->
-    <constructor-arg ref="cache#url-failures-cache" type="org.codehaus.plexus.cache.Cache"/>
-  </bean>
-</beans>
\ No newline at end of file
index efc12cd527ce0dcce5aadf371deb958f441369ea..19ca390968fb12cee3b1547b8cc741e2dd6dfb5d 100644 (file)
           <role>org.apache.maven.archiva.policies.PostDownloadPolicy</role>
           <field-name>postDownloadPolicies</field-name>
         </requirement>
-        <requirement>
-          <role>org.apache.maven.archiva.common.spring.SpringFactory</role>
-          <role-hint>default</role-hint>
-        </requirement>
         <requirement>
           <role>org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers</role>
           <field-name>consumers</field-name>
         </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.policies.urlcache.UrlFailureCache</role>
+          <field-name>urlFailureCache</field-name>
+        </requirement>
       </requirements>
     </component>
-    
+
     <component>
       <role>org.codehaus.plexus.cache.Cache</role>
       <role-hint>url-failures-cache</role-hint>
         <time-to-live-seconds>1800</time-to-live-seconds>
       </configuration>
     </component>
-        
+
     <component>
       <role>org.codehaus.plexus.logging.LoggerManager</role>
       <implementation>org.codehaus.plexus.logging.slf4j.Slf4jLoggerManager</implementation>
index efc12cd527ce0dcce5aadf371deb958f441369ea..19ca390968fb12cee3b1547b8cc741e2dd6dfb5d 100644 (file)
           <role>org.apache.maven.archiva.policies.PostDownloadPolicy</role>
           <field-name>postDownloadPolicies</field-name>
         </requirement>
-        <requirement>
-          <role>org.apache.maven.archiva.common.spring.SpringFactory</role>
-          <role-hint>default</role-hint>
-        </requirement>
         <requirement>
           <role>org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers</role>
           <field-name>consumers</field-name>
         </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.policies.urlcache.UrlFailureCache</role>
+          <field-name>urlFailureCache</field-name>
+        </requirement>
       </requirements>
     </component>
-    
+
     <component>
       <role>org.codehaus.plexus.cache.Cache</role>
       <role-hint>url-failures-cache</role-hint>
         <time-to-live-seconds>1800</time-to-live-seconds>
       </configuration>
     </component>
-        
+
     <component>
       <role>org.codehaus.plexus.logging.LoggerManager</role>
       <implementation>org.codehaus.plexus.logging.slf4j.Slf4jLoggerManager</implementation>
index efc12cd527ce0dcce5aadf371deb958f441369ea..19ca390968fb12cee3b1547b8cc741e2dd6dfb5d 100644 (file)
           <role>org.apache.maven.archiva.policies.PostDownloadPolicy</role>
           <field-name>postDownloadPolicies</field-name>
         </requirement>
-        <requirement>
-          <role>org.apache.maven.archiva.common.spring.SpringFactory</role>
-          <role-hint>default</role-hint>
-        </requirement>
         <requirement>
           <role>org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers</role>
           <field-name>consumers</field-name>
         </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.policies.urlcache.UrlFailureCache</role>
+          <field-name>urlFailureCache</field-name>
+        </requirement>
       </requirements>
     </component>
-    
+
     <component>
       <role>org.codehaus.plexus.cache.Cache</role>
       <role-hint>url-failures-cache</role-hint>
         <time-to-live-seconds>1800</time-to-live-seconds>
       </configuration>
     </component>
-        
+
     <component>
       <role>org.codehaus.plexus.logging.LoggerManager</role>
       <implementation>org.codehaus.plexus.logging.slf4j.Slf4jLoggerManager</implementation>
index d9611461e94b1ea4beaccc1318da28d4418c23c5..80572a87f6e395277d09b2213963a1d74a05d285 100644 (file)
@@ -42,8 +42,8 @@
         </requirement>
       </requirements>
     </component>
-    
-    
+
+
     <component>
       <role>org.apache.maven.archiva.repository.metadata.MetadataTools</role>
       <implementation>org.apache.maven.archiva.repository.metadata.MetadataTools</implementation>
@@ -64,7 +64,7 @@
         </requirement>
       </requirements>
     </component>
-    
+
     <component>
       <role>org.apache.maven.archiva.proxy.RepositoryProxyConnectors</role>
       <role-hint>default</role-hint>
           <role>org.apache.maven.archiva.policies.PostDownloadPolicy</role>
           <field-name>postDownloadPolicies</field-name>
         </requirement>
-        <requirement>
-          <role>org.apache.maven.archiva.common.spring.SpringFactory</role>
-          <role-hint>default</role-hint>
-        </requirement>
         <requirement>
           <role>org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers</role>
           <field-name>consumers</field-name>
         </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.policies.urlcache.UrlFailureCache</role>
+          <field-name>urlFailureCache</field-name>
+        </requirement>
       </requirements>
     </component>
-    
+
     <component>
       <role>org.codehaus.plexus.cache.Cache</role>
       <role-hint>url-failures-cache</role-hint>
         <time-to-live-seconds>1800</time-to-live-seconds>
       </configuration>
     </component>
-        
+
     <component>
       <role>org.codehaus.plexus.logging.LoggerManager</role>
       <implementation>org.codehaus.plexus.logging.slf4j.Slf4jLoggerManager</implementation>
index efc12cd527ce0dcce5aadf371deb958f441369ea..19ca390968fb12cee3b1547b8cc741e2dd6dfb5d 100644 (file)
           <role>org.apache.maven.archiva.policies.PostDownloadPolicy</role>
           <field-name>postDownloadPolicies</field-name>
         </requirement>
-        <requirement>
-          <role>org.apache.maven.archiva.common.spring.SpringFactory</role>
-          <role-hint>default</role-hint>
-        </requirement>
         <requirement>
           <role>org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers</role>
           <field-name>consumers</field-name>
         </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.policies.urlcache.UrlFailureCache</role>
+          <field-name>urlFailureCache</field-name>
+        </requirement>
       </requirements>
     </component>
-    
+
     <component>
       <role>org.codehaus.plexus.cache.Cache</role>
       <role-hint>url-failures-cache</role-hint>
         <time-to-live-seconds>1800</time-to-live-seconds>
       </configuration>
     </component>
-        
+
     <component>
       <role>org.codehaus.plexus.logging.LoggerManager</role>
       <implementation>org.codehaus.plexus.logging.slf4j.Slf4jLoggerManager</implementation>
index efc12cd527ce0dcce5aadf371deb958f441369ea..19ca390968fb12cee3b1547b8cc741e2dd6dfb5d 100644 (file)
           <role>org.apache.maven.archiva.policies.PostDownloadPolicy</role>
           <field-name>postDownloadPolicies</field-name>
         </requirement>
-        <requirement>
-          <role>org.apache.maven.archiva.common.spring.SpringFactory</role>
-          <role-hint>default</role-hint>
-        </requirement>
         <requirement>
           <role>org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers</role>
           <field-name>consumers</field-name>
         </requirement>
+        <requirement>
+          <role>org.apache.maven.archiva.policies.urlcache.UrlFailureCache</role>
+          <field-name>urlFailureCache</field-name>
+        </requirement>
       </requirements>
     </component>
-    
+
     <component>
       <role>org.codehaus.plexus.cache.Cache</role>
       <role-hint>url-failures-cache</role-hint>
         <time-to-live-seconds>1800</time-to-live-seconds>
       </configuration>
     </component>
-        
+
     <component>
       <role>org.codehaus.plexus.logging.LoggerManager</role>
       <implementation>org.codehaus.plexus.logging.slf4j.Slf4jLoggerManager</implementation>
index aca03f14bba090af8310eae3e55036242a142667..98fcd7fa168a7cbe65436a500bdf70bc99d22233 100644 (file)
@@ -4,10 +4,6 @@
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
-  <bean id="plexusContainer" class="org.codehaus.plexus.spring.PlexusContainerAdapter"/>
-
-  <bean id="loggerManager" class="org.codehaus.plexus.logging.console.ConsoleLoggerManager" init-method="initialize"/>
-
   <bean id="urlFailureCache" class="org.apache.maven.archiva.policies.urlcache.DefaultUrlFailureCache">
     <constructor-arg ref="cache#url-failures-cache" type="org.codehaus.plexus.cache.Cache"/>
   </bean>
index 24fc7b3420ebc264faa2ac52c250e7eef893bcac..876d29980d33efa86aa949413cae52cdd2123b87 100644 (file)
       <artifactId>plexus-component-api</artifactId>\r
       <version>1.0-alpha-22</version>\r
     </dependency>\r
+    <dependency>\r
+      <!-- only required for ConsoleLogger -->\r
+      <groupId>org.codehaus.plexus</groupId>\r
+      <artifactId>plexus-container-default</artifactId>\r
+      <version>1.0-alpha-22</version>\r
+    </dependency>\r
     <dependency>\r
       <!-- only required to debug XSLT result -->\r
       <groupId>dom4j</groupId>\r
index a8d7cb34fdf3927c16fdb104db227e9dc8cd6a36..96acdb8e2538e963ba114976fce8253ff04b0cf6 100644 (file)
@@ -22,6 +22,7 @@ package org.codehaus.plexus.spring;
 import java.io.IOException;\r
 \r
 import org.springframework.beans.BeansException;\r
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;\r
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;\r
 import org.springframework.context.ApplicationContext;\r
 import org.springframework.context.support.ClassPathXmlApplicationContext;\r
@@ -98,4 +99,19 @@ public class PlexusClassPathXmlApplicationContext
         super.loadBeanDefinitions( reader );\r
     }\r
 \r
+    /**\r
+     * {@inheritDoc}\r
+     * @see org.springframework.context.support.AbstractApplicationContext#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)\r
+     */\r
+    protected void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory )\r
+    {\r
+        PlexusContainerAdapter plexus = new PlexusContainerAdapter();\r
+        plexus.setApplicationContext( this );\r
+        beanFactory.registerSingleton( "plexusContainer", plexus );\r
+\r
+        PlexusLifecycleBeanPostProcessor lifecycle = new PlexusLifecycleBeanPostProcessor();\r
+        lifecycle.setBeanFactory( this );\r
+        beanFactory.addBeanPostProcessor( lifecycle );\r
+    }\r
+\r
 }\r
index afbe81cc5b3ff4e2d459bfb53a184844d661c6ff..636c78ad198d5f70f227095d72df995bbb2074c9 100644 (file)
@@ -88,7 +88,7 @@ public class PlexusComponentFactoryBean
     private Class implementation;\r
 \r
     /** The plexus component instantiation strategy */\r
-    private String instantiationStrategy = SINGLETON;\r
+    private String instantiationStrategy;\r
 \r
     /** The plexus component requirements and configurations */\r
     private Map requirements;\r
@@ -96,12 +96,6 @@ public class PlexusComponentFactoryBean
     /** The plexus component created by this FactoryBean */\r
     private List instances = new LinkedList();\r
 \r
-    /** Optional plexus loggerManager */\r
-    private static LoggerManager loggerManager;\r
-\r
-    /** Optional plexus context */\r
-    private static Context context;\r
-\r
     /**\r
      * {@inheritDoc}\r
      *\r
@@ -114,10 +108,11 @@ public class PlexusComponentFactoryBean
         {\r
             for ( Iterator iterator = instances.iterator(); iterator.hasNext(); )\r
             {\r
-                Object isntance = iterator.next();\r
-                if ( isntance instanceof Disposable )\r
+                Object instance = iterator.next();\r
+                if ( instance instanceof Disposable )\r
                 {\r
-                    ( (Disposable) isntance ).dispose();\r
+                    logger.trace( "Dispose plexus bean " + role );\r
+                    ( (Disposable) instance ).dispose();\r
                 }\r
             }\r
         }\r
@@ -125,14 +120,22 @@ public class PlexusComponentFactoryBean
 \r
     /**\r
      * {@inheritDoc}\r
+     *\r
      * @see org.springframework.beans.factory.FactoryBean#getObject()\r
      */\r
     public Object getObject()\r
         throws Exception\r
     {\r
-        if ( isSingleton() && !instances.isEmpty())\r
+        if ( isSingleton() )\r
         {\r
-            return instances.get( 0 );\r
+            synchronized ( instances )\r
+            {\r
+                if ( !instances.isEmpty() )\r
+                {\r
+                    return instances.get( 0 );\r
+                }\r
+                return createInstance();\r
+            }\r
         }\r
         return createInstance();\r
     }\r
@@ -197,22 +200,19 @@ public class PlexusComponentFactoryBean
                 }\r
             }\r
         }\r
-\r
-        handlePlexusLifecycle( component );\r
-\r
         return component;\r
     }\r
 \r
     private Field findField( String fieldName )\r
     {\r
         Class clazz = implementation;\r
-        while (clazz != Object.class)\r
+        while ( clazz != Object.class )\r
         {\r
             try\r
             {\r
                 return clazz.getDeclaredField( fieldName );\r
             }\r
-            catch (NoSuchFieldException e)\r
+            catch ( NoSuchFieldException e )\r
             {\r
                 clazz = clazz.getSuperclass();\r
             }\r
@@ -222,28 +222,6 @@ public class PlexusComponentFactoryBean
         throw new BeanInitializationException( error );\r
     }\r
 \r
-    private void handlePlexusLifecycle( final Object component )\r
-        throws ContextException, InitializationException\r
-    {\r
-        if ( component instanceof LogEnabled )\r
-        {\r
-            ( (LogEnabled) component ).enableLogging( getLoggerManager().getLoggerForComponent( role.getName() ) );\r
-        }\r
-\r
-        if ( component instanceof Contextualizable )\r
-        {\r
-            // VERRY limited support for Contextualizable\r
-            ( (Contextualizable) component ).contextualize( getContext() );\r
-        }\r
-\r
-        // TODO add support for Startable, Stopable -> LifeCycle ?\r
-\r
-        if ( component instanceof Initializable )\r
-        {\r
-            ( (Initializable) component ).initialize();\r
-        }\r
-    }\r
-\r
     /**\r
      * Resolve the requirement that this field exposes in the component\r
      *\r
@@ -288,19 +266,6 @@ public class PlexusComponentFactoryBean
         return SINGLETON.equals( instantiationStrategy );\r
     }\r
 \r
-    /**\r
-     * @return\r
-     */\r
-    protected Context getContext()\r
-    {\r
-        if ( context == null )\r
-        {\r
-            PlexusContainer container = (PlexusContainer) beanFactory.getBean( "plexusContainer" );\r
-            context = container.getContext();\r
-        }\r
-        return context;\r
-    }\r
-\r
     protected TypeConverter getBeanTypeConverter()\r
     {\r
         if ( beanFactory instanceof ConfigurableBeanFactory )\r
@@ -313,35 +278,6 @@ public class PlexusComponentFactoryBean
         }\r
     }\r
 \r
-    /**\r
-     * Retrieve the loggerManager instance to be used for LogEnabled components\r
-     *\r
-     * @return\r
-     */\r
-    protected LoggerManager getLoggerManager()\r
-    {\r
-        if ( loggerManager == null )\r
-        {\r
-            if ( beanFactory.containsBean( "loggerManager" ) )\r
-            {\r
-                loggerManager = (LoggerManager) beanFactory.getBean( "loggerManager" );\r
-            }\r
-            else\r
-            {\r
-                Map loggers = getListableBeanFactory().getBeansOfType( LoggerManager.class );\r
-                if ( loggers.size() == 1 )\r
-                {\r
-                    loggerManager = (LoggerManager) loggers.values().iterator().next();\r
-                }\r
-            }\r
-        }\r
-        if ( loggerManager == null )\r
-        {\r
-            throw new BeanCreationException( "A LoggerManager instance must be set in the applicationContext" );\r
-        }\r
-        return loggerManager;\r
-    }\r
-\r
     private ListableBeanFactory getListableBeanFactory()\r
     {\r
         if ( beanFactory instanceof ListableBeanFactory )\r
@@ -351,14 +287,6 @@ public class PlexusComponentFactoryBean
         throw new BeanInitializationException( "A ListableBeanFactory is required by the PlexusComponentFactoryBean" );\r
     }\r
 \r
-    /**\r
-     * @param loggerManager the loggerManager to set\r
-     */\r
-    public void setLoggerManager( LoggerManager loggerManager )\r
-    {\r
-        PlexusComponentFactoryBean.loggerManager = loggerManager;\r
-    }\r
-\r
     /**\r
      * @param role the role to set\r
      */\r
@@ -399,11 +327,6 @@ public class PlexusComponentFactoryBean
         this.requirements = requirements;\r
     }\r
 \r
-    public void setContext( Context context )\r
-    {\r
-        PlexusComponentFactoryBean.context = context;\r
-    }\r
-\r
     public void setBeanFactory( BeanFactory beanFactory )\r
     {\r
         this.beanFactory = beanFactory;\r
index 7f45c444ddd7e2681103f9d6349eeeb064bcff34..e0fe3d2ec62fb1c78c34ac79e227047d6f8c575b 100644 (file)
@@ -55,6 +55,11 @@ public class PlexusContainerAdapter
     private ApplicationContext applicationContext;
 
 
+    public PlexusContainerAdapter()
+    {
+        super();
+    }
+
     /**
      * {@inheritDoc}
      * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
diff --git a/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusLifecycleBeanPostProcessor.java b/springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusLifecycleBeanPostProcessor.java
new file mode 100644 (file)
index 0000000..a1c4714
--- /dev/null
@@ -0,0 +1,159 @@
+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.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.codehaus.plexus.PlexusContainer;
+import org.codehaus.plexus.context.Context;
+import org.codehaus.plexus.context.ContextException;
+import org.codehaus.plexus.logging.LogEnabled;
+import org.codehaus.plexus.logging.LoggerManager;
+import org.codehaus.plexus.logging.console.ConsoleLoggerManager;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanFactoryAware;
+import org.springframework.beans.factory.BeanInitializationException;
+import org.springframework.beans.factory.FactoryBean;
+import org.springframework.beans.factory.ListableBeanFactory;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+
+/**
+ * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
+ */
+public class PlexusLifecycleBeanPostProcessor
+    implements BeanPostProcessor, BeanFactoryAware
+{
+    /** Logger available to subclasses */
+    protected final Log logger = LogFactory.getLog( getClass() );
+
+    private BeanFactory beanFactory;
+
+    private Context context;
+
+    private LoggerManager loggerManager;
+
+    protected Context getContext()
+    {
+        if ( context == null )
+        {
+            PlexusContainer container = (PlexusContainer) beanFactory.getBean( "plexusContainer" );
+            context = container.getContext();
+        }
+        return context;
+    }
+
+    /**
+     * Retrieve the loggerManager instance to be used for LogEnabled components
+     *
+     * @return
+     */
+    protected LoggerManager getLoggerManager()
+    {
+        if ( loggerManager == null )
+        {
+            if ( beanFactory.containsBean( "loggerManager" ) )
+            {
+                loggerManager = (LoggerManager) beanFactory.getBean( "loggerManager" );
+            }
+            else
+            {
+                logger.warn( "No loggerManager set in context. Falling back to ConsoleLoggerManager" );
+                ConsoleLoggerManager defaultLoggerManager = new ConsoleLoggerManager();
+                defaultLoggerManager.initialize();
+                loggerManager = defaultLoggerManager;
+            }
+        }
+        return loggerManager;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
+     */
+    public Object postProcessBeforeInitialization( Object bean, String beanName )
+        throws BeansException
+    {
+        return bean;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)
+     */
+    public Object postProcessAfterInitialization( Object bean, String beanName )
+        throws BeansException
+    {
+        if ( bean instanceof FactoryBean )
+        {
+            // only apply to beans
+            return bean;
+        }
+
+        if ( bean instanceof LogEnabled )
+        {
+            logger.trace( "Enable Logging on plexus bean " + beanName );
+            ( (LogEnabled) bean ).enableLogging( getLoggerManager().getLoggerForComponent( beanName ) );
+        }
+
+        if ( bean instanceof Contextualizable )
+        {
+            try
+            {
+                logger.trace( "Contextualize plexus bean " + beanName );
+                ( (Contextualizable) bean ).contextualize( getContext() );
+            }
+            catch ( ContextException e )
+            {
+                throw new BeanInitializationException(
+                    "Failed to invoke plexus lifecycle Contextualizable.contextualize on " + beanName, e );
+            }
+        }
+
+        // TODO add support for Startable, Stopable -> LifeCycle ?
+
+        if ( bean instanceof Initializable )
+        {
+            try
+            {
+                logger.trace( "Initialize plexus bean " + beanName );
+                ( (Initializable) bean ).initialize();
+            }
+            catch ( InitializationException e )
+            {
+                throw new BeanInitializationException( "Failed to invoke plexus lifecycle Initializable.initialize on " + beanName, e );
+            }
+        }
+        return bean;
+    }
+
+    public void setBeanFactory( BeanFactory beanFactory )
+    {
+        this.beanFactory = beanFactory;
+    }
+
+
+}