]> source.dussan.org Git - archiva.git/commitdiff
fix : cache instance in beanFactory when instanciation-strategy = singleton
authorNicolas De Loof <nicolas@apache.org>
Mon, 25 Feb 2008 11:54:45 +0000 (11:54 +0000)
committerNicolas De Loof <nicolas@apache.org>
Mon, 25 Feb 2008 11:54:45 +0000 (11:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@630818 13f79535-47bb-0310-9956-ffa450edef68

springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusComponentFactoryBean.java
springy/plexus-spring/src/main/java/org/codehaus/plexus/spring/PlexusInSpringTestCase.java

index 5aa8188f3ab152517cbb489c66de5cc4f4e43eed..5dc9954f60e46c6acc68c5e8b8cc5b6629218fb1 100644 (file)
@@ -27,6 +27,7 @@ import java.util.LinkedList;
 import java.util.List;\r
 import java.util.Map;\r
 \r
+import org.codehaus.plexus.PlexusConstants;\r
 import org.codehaus.plexus.logging.LogEnabled;\r
 import org.codehaus.plexus.logging.LoggerManager;\r
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Disposable;\r
@@ -34,6 +35,7 @@ import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
 import org.springframework.beans.BeansException;\r
 import org.springframework.beans.SimpleTypeConverter;\r
 import org.springframework.beans.TypeConverter;\r
+import org.springframework.beans.factory.BeanCreationException;\r
 import org.springframework.beans.factory.BeanFactory;\r
 import org.springframework.beans.factory.BeanFactoryAware;\r
 import org.springframework.beans.factory.BeanInitializationException;\r
@@ -58,6 +60,9 @@ import org.springframework.util.ReflectionUtils;
 public class PlexusComponentFactoryBean\r
     implements FactoryBean, BeanFactoryAware, DisposableBean\r
 {\r
+\r
+    private static final char HINT = '#';\r
+\r
     private Class role;\r
 \r
     private Class implementation;\r
@@ -94,6 +99,16 @@ public class PlexusComponentFactoryBean
     public Object getObject()\r
         throws Exception\r
     {\r
+        if ( "poolable".equals( instanciationStrategy ) )\r
+        {\r
+            throw new BeanCreationException( "Plexus poolable instanciation-strategy is not supported" );\r
+        }\r
+\r
+        if ( "singleton".equals( instanciationStrategy ) && !instances.isEmpty() )\r
+        {\r
+            return instances.get( 0 );\r
+        }\r
+\r
         final Object component = implementation.newInstance();\r
         synchronized ( instances )\r
         {\r
@@ -114,7 +129,7 @@ public class PlexusComponentFactoryBean
                         {\r
                             // component ask plexus for a Map of all available components for the role\r
                             Map map = new HashMap();\r
-                            String mask = beanName + '#';\r
+                            String mask = beanName + HINT;\r
                             String[] beans = beanFactory.getBeanDefinitionNames();\r
                             for ( int i = 0; i < beans.length; i++ )\r
                             {\r
@@ -126,14 +141,14 @@ public class PlexusComponentFactoryBean
                             }\r
                             if ( beanFactory.containsBean( beanName ) )\r
                             {\r
-                                map.put( "default", beanFactory.getBean( beanName ) );\r
+                                map.put( PlexusConstants.PLEXUS_DEFAULT_HINT, beanFactory.getBean( beanName ) );\r
                             }\r
                             dependency = map;\r
                         }\r
                         else if ( Collection.class.isAssignableFrom( field.getType() ) )\r
                         {\r
                             List list = new LinkedList();\r
-                            String mask = beanName + '#';\r
+                            String mask = beanName + HINT;\r
                             String[] beans = beanFactory.getBeanDefinitionNames();\r
                             for ( int i = 0; i < beans.length; i++ )\r
                             {\r
@@ -151,7 +166,7 @@ public class PlexusComponentFactoryBean
                         }\r
                         else\r
                         {\r
-                            dependency = beanFactory.getBean( beanName );\r
+                             dependency = beanFactory.getBean( beanName );\r
                         }\r
                     }\r
                     if ( dependency != null )\r
@@ -247,6 +262,10 @@ public class PlexusComponentFactoryBean
      */\r
     public void setInstanciationStrategy( String instanciationStrategy )\r
     {\r
+        if (instanciationStrategy.length() == 0)\r
+        {\r
+            instanciationStrategy = "singleton";\r
+        }\r
         this.instanciationStrategy = instanciationStrategy;\r
     }\r
 \r
index 18d0e2c1e6d6e6a8e2907a5233890be7932ad677..079a567315cadf07b30de9fa58ef904f82135ba1 100644 (file)
@@ -45,7 +45,8 @@ public class PlexusInSpringTestCase
     {
         basedir = getBasedir();
         applicationContext =
-            new PlexusClassPathXmlApplicationContext( new String[] { "classpath*:META-INF/plexus/components.xml",
+            new PlexusClassPathXmlApplicationContext( new String[] {
+                "classpath*:META-INF/plexus/components.xml",
                 "classpath*:META-INF/plexus/components-fragment.xml",
                 "classpath*:" + getPlexusConfigLocation(),
                 "classpath*:" + getSpringConfigLocation()} );
@@ -154,4 +155,9 @@ public class PlexusInSpringTestCase
     {
         return getTestFile( basedir, path ).getAbsolutePath();
     }
+
+    protected ConfigurableApplicationContext getApplicationContext()
+    {
+        return applicationContext;
+    }
 }