You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2009/08/20 17:36:52 UTC

svn commit: r806218 - in /geronimo/sandbox/blueprint/blueprint-core/src: main/java/org/apache/geronimo/blueprint/container/ main/java/org/apache/geronimo/blueprint/di/ test/java/org/apache/geronimo/blueprint/

Author: gawor
Date: Thu Aug 20 15:36:52 2009
New Revision: 806218

URL: http://svn.apache.org/viewvc?rev=806218&view=rev
Log:
prototype cycles are no longer allowed and self getComponentInstance() calls are allowed under certain cirumstances

Modified:
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/BlueprintContainerImpl.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/BlueprintRepository.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/di/AbstractRecipe.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/di/ExecutionContext.java
    geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/BlueprintContainerImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/BlueprintContainerImpl.java?rev=806218&r1=806217&r2=806218&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/BlueprintContainerImpl.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/BlueprintContainerImpl.java Thu Aug 20 15:36:52 2009
@@ -608,7 +608,7 @@
         }
         LOGGER.debug("Instantiating components: {}", components);
         try {
-            repository.createAll(components, false);
+            repository.createAll(components);
         } catch (ComponentDefinitionException e) {
             throw e;
         } catch (Throwable t) {
@@ -686,7 +686,7 @@
         }
         try {
             LOGGER.debug("Instantiating component {}", id);
-            return repository.create(id, false);
+            return repository.create(id);
         } catch (NoSuchComponentException e) {
             throw e;
         } catch (ComponentDefinitionException e) {

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/BlueprintRepository.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/BlueprintRepository.java?rev=806218&r1=806217&r2=806218&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/BlueprintRepository.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/BlueprintRepository.java Thu Aug 20 15:36:52 2009
@@ -89,8 +89,6 @@
      */
     private final LinkedList<Recipe> stack = new LinkedList<Recipe>();
     
-    private int createReentered = 0;
-
     public BlueprintRepository(ExtendedBlueprintContainer container) {
         blueprintContainer = container;
     }
@@ -125,29 +123,21 @@
             throw new ComponentDefinitionException("Unable to convert instance " + name, e);
         }
     }
-    
+        
     public Object create(String name) throws ComponentDefinitionException {
-        return create(name, true);
-    }
-    
-    public Object create(String name, boolean allowReentry) throws ComponentDefinitionException {
         ExecutionContext oldContext = ExecutionContext.Holder.setContext(this);
         try {
-            Object instance = createInstance(name, allowReentry);                       
+            Object instance = createInstance(name);                       
             return convert(name, instance);
         } finally {
             ExecutionContext.Holder.setContext(oldContext);
         }
     }
-
-    public Map<String, Object> createAll(Collection<String> names) throws ComponentDefinitionException {
-        return createAll(names, true);        
-    }
     
-    public Map<String, Object> createAll(Collection<String> names, boolean allowReentry) throws ComponentDefinitionException {
+    public Map<String, Object> createAll(Collection<String> names) throws ComponentDefinitionException {
         ExecutionContext oldContext = ExecutionContext.Holder.setContext(this);
         try {
-            Map<String, Object> instances = createInstances(names, allowReentry);
+            Map<String, Object> instances = createInstances(names);
             for (String name : instances.keySet()) {
                 Object obj = instances.get(name);
                 instances.put(name, convert(name, obj));
@@ -195,10 +185,10 @@
         }
     }
 
-    private Object createInstance(String name, boolean allowReentry) {
+    private Object createInstance(String name) {
         Object instance = getInstance(name);
         if (instance == null) {
-            Map <String, Object> instances = createInstances(Arrays.asList(name), allowReentry);
+            Map <String, Object> instances = createInstances(Arrays.asList(name));
             instance = instances.get(name); 
             if (instance == null) {
                 throw new NoSuchComponentException(name);
@@ -207,39 +197,26 @@
         return instance;
     }
 
-    private Map<String, Object> createInstances(Collection<String> names, boolean allowReentry) {
+    private Map<String, Object> createInstances(Collection<String> names) {
         // We need to synchronize recipe creation on the repository
         // so that we don't end up with multiple threads creating the
         // same instance at the same time.
         synchronized (instanceLock) {
-            try {
-                if (!allowReentry) {
-                    createReentered++;
-                }
-                DependencyGraph graph = new DependencyGraph(this);
-                HashMap<String, Object> objects = new LinkedHashMap<String, Object>();
-                for (Map.Entry<String, Recipe> entry : graph.getSortedRecipes(names).entrySet()) {
-                    String name = entry.getKey();
-                    Object object = instances.get(name);
-                    if (object == null) {
-                        Recipe recipe = entry.getValue();
-                        object = recipe.create();
-                    }
-                    objects.put(name, object);
-                }
-                return objects;
-            } finally {
-                if (!allowReentry) {
-                    createReentered--;
+            DependencyGraph graph = new DependencyGraph(this);
+            HashMap<String, Object> objects = new LinkedHashMap<String, Object>();
+            for (Map.Entry<String, Recipe> entry : graph.getSortedRecipes(names).entrySet()) {
+                String name = entry.getKey();
+                Object object = instances.get(name);
+                if (object == null) {
+                    Recipe recipe = entry.getValue();
+                    object = recipe.create();
                 }
+                objects.put(name, object);
             }
+            return objects;
         }
     }
-    
-    public boolean isCreateReentered() {
-        return createReentered >= 2;
-    }
-    
+        
     public void validate() {
         for (Recipe recipe : getAllRecipes()) {
             // Check that references are satisfied

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/di/AbstractRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/di/AbstractRecipe.java?rev=806218&r1=806217&r2=806218&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/di/AbstractRecipe.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/di/AbstractRecipe.java Thu Aug 20 15:36:52 2009
@@ -18,7 +18,6 @@
 package org.apache.geronimo.blueprint.di;
 
 import java.lang.reflect.Type;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
@@ -30,7 +29,6 @@
 
     protected final String name;
     protected boolean prototype = true;
-    private boolean creating = false;
 
     protected AbstractRecipe(String name) {
         if (name == null) throw new NullPointerException("name is null");
@@ -54,11 +52,6 @@
         ExecutionContext context = ExecutionContext.Holder.getContext();
 
         synchronized (context.getInstanceLock()) {
-            if (creating && context.isCreateReentered()) {
-                ArrayList<Recipe> circularity = new ArrayList<Recipe>(1);
-                circularity.add(this);
-                throw new CircularDependencyException("Dynamic cycle detected in recipe", circularity);
-            }
             // if this recipe has already been executed in this container, return the currently registered value
             Object obj = context.getPartialObject(name);
             if (obj != null) {
@@ -66,18 +59,14 @@
             }
 
             // execute the recipe
-            creating = true;
             context.push(this);
             try {
                 obj = internalCreate();
-                if (prototype) {
-                    context.removePartialObject(name);
-                } else {
+                if (!prototype) {
                     context.addFullObject(name, obj);
                 }
                 return obj;
             } finally {
-                creating = false;
                 Recipe popped = context.pop();
                 if (popped != this) {
                     //noinspection ThrowFromFinallyBlock
@@ -91,7 +80,9 @@
     protected abstract Object internalCreate() throws ComponentDefinitionException;
 
     protected void addPartialObject(Object obj) {
-        ExecutionContext.Holder.getContext().addPartialObject(name, obj);
+        if (!prototype) {                 
+            ExecutionContext.Holder.getContext().addPartialObject(name, obj);
+        }
     }
     
     protected Object convert(Object obj, ReifiedType type) throws Exception {

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/di/ExecutionContext.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/di/ExecutionContext.java?rev=806218&r1=806217&r2=806218&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/di/ExecutionContext.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/di/ExecutionContext.java Thu Aug 20 15:36:52 2009
@@ -95,6 +95,5 @@
 
     public abstract Recipe getRecipe(String name);
     
-    public abstract boolean isCreateReentered();
 }
 

Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java?rev=806218&r1=806217&r2=806218&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java Thu Aug 20 15:36:52 2009
@@ -40,6 +40,7 @@
 import org.apache.geronimo.blueprint.pojos.PojoCircular;
 import org.apache.geronimo.blueprint.pojos.PojoGenerics;
 import org.apache.geronimo.blueprint.pojos.PojoListener;
+import org.apache.geronimo.blueprint.pojos.PojoRecursive;
 import org.apache.geronimo.blueprint.container.AggregateConverter;
 import org.apache.geronimo.blueprint.container.BlueprintRepository;
 import org.apache.geronimo.blueprint.container.GenericType;
@@ -358,27 +359,26 @@
     public void testCircularPrototype() throws Exception {
         BlueprintRepository repository = createBlueprintContainer().getRepository();
         
-        PojoCircular driver1 = (PojoCircular) repository.create("circularPrototypeDriver");
-        
-        assertTrue(driver1.getCircular() == driver1.getCircular().getCircular());
-        
-        PojoCircular driver2 = (PojoCircular) repository.create("circularPrototypeDriver");
-        
-        assertTrue(driver1 != driver2);
-        assertTrue(driver1.getCircular() != driver2.getCircular());
-        assertTrue(driver1.getCircular().getCircular() != driver2.getCircular().getCircular());
-        
-        PojoCircular prototype = (PojoCircular) repository.create("circularPrototype");
-        
-        assertTrue(prototype != driver1.getCircular());
-        assertTrue(prototype != driver2.getCircular());
+        try {
+            repository.create("circularPrototypeDriver");
+            fail("Did not throw exception");  
+        } catch (CircularDependencyException e) {
+            // that's what we expect
+        }
+
+        try {
+            repository.create("circularPrototype");
+            fail("Did not throw exception");  
+        } catch (CircularDependencyException e) {
+            // that's what we expect
+        }
     }
     
     public void testRecursive() throws Exception {
         BlueprintRepository repository = createBlueprintContainer().getRepository();
         
         try {
-            repository.create("recursiveConstructor", false);
+            repository.create("recursiveConstructor");
             fail("Did not throw exception");           
         } catch (ComponentDefinitionException e) {
             if (e.getCause() instanceof CircularDependencyException) {                          
@@ -389,29 +389,13 @@
             }
         }
         
-        try {
-            repository.create("recursiveSetter", false);
-            fail("Did not throw exception");           
-        } catch (ComponentDefinitionException e) {
-            if (e.getCause() instanceof CircularDependencyException) {                          
-                // that's what we expect
-            } else {
-                fail("Did not throw expected exception");
-                throw e;
-            }
-        }
+        PojoRecursive pojo;
         
-        try {
-            repository.create("recursiveInitMethod", false);
-            fail("Did not throw exception");
-        } catch (ComponentDefinitionException e) {
-            if (e.getCause() instanceof CircularDependencyException) {                          
-                // that's what we expect
-            } else {
-                fail("Did not throw expected exception");
-                throw e;
-            }
-        }
+        pojo = (PojoRecursive) repository.create("recursiveSetter");
+        assertNotNull(pojo);
+                           
+        pojo = (PojoRecursive) repository.create("recursiveInitMethod");
+        assertNotNull(pojo);
     }
     
     public void testCircularBreaking() throws Exception {