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/04/27 23:25:24 UTC

svn commit: r769170 - in /geronimo/sandbox/blueprint/blueprint-core/src: main/java/org/apache/geronimo/blueprint/ main/java/org/apache/geronimo/blueprint/context/ test/java/org/apache/geronimo/blueprint/ test/java/org/apache/geronimo/blueprint/pojos/

Author: gawor
Date: Mon Apr 27 21:25:23 2009
New Revision: 769170

URL: http://svn.apache.org/viewvc?rev=769170&view=rev
Log:
improved component destruction - should now be done in the right order and only for components in bundle or singleton scope

Modified:
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/ExtendedComponentDefinitionRegistry.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ScopedRepository.java
    geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/TestBlueprintContext.java
    geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java
    geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/PojoB.java

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/ExtendedComponentDefinitionRegistry.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/ExtendedComponentDefinitionRegistry.java?rev=769170&r1=769169&r2=769170&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/ExtendedComponentDefinitionRegistry.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/ExtendedComponentDefinitionRegistry.java Mon Apr 27 21:25:23 2009
@@ -18,10 +18,7 @@
  */
 package org.apache.geronimo.blueprint;
 
-import java.util.List;
-
 import org.osgi.service.blueprint.namespace.ComponentDefinitionRegistry;
-import org.osgi.service.blueprint.reflect.Target;
 
 /**
  * An extended ComponentDefinitionRegistry that allows converters and processors to be registered.

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java?rev=769170&r1=769169&r2=769170&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java Mon Apr 27 21:25:23 2009
@@ -357,6 +357,8 @@
     }
 
     private void destroyComponents() {
+        ((ScopedRepository)objectGraph.getRepository()).destroy();
+        
         Map<String, Destroyable> destroyables = new HashMap<String, Destroyable>(this.destroyables);
         this.destroyables.clear();
         for (Map.Entry<String, Destroyable> entry : destroyables.entrySet()) {
@@ -449,7 +451,7 @@
         }
         handlers.removeListener(this);
         sender.sendDestroying(this);
-        unregisterAllServices();
+        unregisterAllServices();        
         destroyComponents();
         // TODO: stop all reference / collections
         System.out.println("Module context destroyed: " + this.bundleContext);

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java?rev=769170&r1=769169&r2=769170&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java Mon Apr 27 21:25:23 2009
@@ -32,7 +32,6 @@
 import org.apache.xbean.recipe.Recipe;
 import org.apache.xbean.recipe.RecipeHelper;
 import org.apache.xbean.recipe.ReferenceRecipe;
-import org.apache.geronimo.blueprint.Destroyable;
 import org.apache.geronimo.blueprint.namespace.ComponentDefinitionRegistryImpl;
 import org.apache.geronimo.blueprint.utils.ArgumentsMatch;
 import org.apache.geronimo.blueprint.utils.ArgumentsMatcher;
@@ -268,7 +267,7 @@
      * Returns init method (if any). Throws exception if the init-method was set explicitly on the bean
      * and the method is not found on the instance.
      */
-    private Method getInitMethod(Object instance) throws ConstructionException {
+    protected Method getInitMethod(Object instance) throws ConstructionException {
         Method method = null;        
         if (initMethod == null) {
             ComponentDefinitionRegistryImpl registry = blueprintContext.getComponentDefinitionRegistry();
@@ -286,7 +285,7 @@
      * Returns destroy method (if any). Throws exception if the destroy-method was set explicitly on the bean
      * and the method is not found on the instance.
      */
-    private Method getDestroyMethod(Object instance) throws ConstructionException {
+    protected Method getDestroyMethod(Object instance) throws ConstructionException {
         Method method = null;        
         if (destroyMethod == null) {
             ComponentDefinitionRegistryImpl registry = blueprintContext.getComponentDefinitionRegistry();
@@ -335,22 +334,10 @@
             ExecutionContext.getContext().addObject(getName(), obj);
         }
         
-        if (destroyMethod != null && blueprintContext != null) {
-            Destroyable d = new Destroyable() {
-                public void destroy() {
-                    destroyInstance(obj);
-                }
-            };
-            blueprintContext.addDestroyable(getName(), d);
-        }
-        
         return obj;
     }
     
     public void destroyInstance(Object obj) {
-        if (!getType().equals(obj.getClass())) {
-            throw new RuntimeException("");
-        }
         try {
             Method method = getDestroyMethod(obj);
             if (method != null) {

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java?rev=769170&r1=769169&r2=769170&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java Mon Apr 27 21:25:23 2009
@@ -100,10 +100,10 @@
         }
     }
     
-    public Repository createRepository(ExtendedComponentDefinitionRegistry registry) throws Exception {
+    public ScopedRepository createRepository(ExtendedComponentDefinitionRegistry registry) throws Exception {
         this.registry = registry;
 
-        Repository repository = new ScopedRepository();
+        ScopedRepository repository = new ScopedRepository();
         addBuiltinComponents(repository);
         
         // Create component recipes

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ScopedRepository.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ScopedRepository.java?rev=769170&r1=769169&r2=769170&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ScopedRepository.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ScopedRepository.java Mon Apr 27 21:25:23 2009
@@ -17,9 +17,14 @@
  */
 package org.apache.geronimo.blueprint.context;
 
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.SortedMap;
 import java.util.TreeMap;
 
+import org.apache.geronimo.blueprint.Destroyable;
 import org.apache.xbean.recipe.ConstructionException;
 import org.apache.xbean.recipe.Recipe;
 import org.apache.xbean.recipe.Repository;
@@ -33,13 +38,16 @@
 public class ScopedRepository implements Repository {
 
     private SortedMap<String, Object> instances;
+    private List<DestroyCallback> destroyList;
 
     public ScopedRepository() {
         instances = new TreeMap<String, Object>();
+        destroyList = new ArrayList<DestroyCallback>();
     }
     
     public ScopedRepository(ScopedRepository source) {
-        instances = new TreeMap<String, Object>(source.instances);
+        instances = new TreeMap<String, Object>(source.instances);       
+        destroyList = new ArrayList<DestroyCallback>();
     }
     
     public void set(String name, Object instance) {
@@ -57,10 +65,15 @@
     public void add(String name, Object instance) {
         Object existingObj = instances.get(name);
         if (existingObj != null) {
-            if (existingObj instanceof BlueprintObjectRecipe) {                
-                if ( ((BlueprintObjectRecipe) existingObj ).getKeepRecipe()) {
+            if (existingObj instanceof BlueprintObjectRecipe) {    
+                BlueprintObjectRecipe recipe = (BlueprintObjectRecipe) existingObj;
+                if (recipe.getKeepRecipe()) {
                     return;
                 }
+                Method method = recipe.getDestroyMethod(instance);
+                if (method != null) {
+                    destroyList.add(new DestroyCallback(method, instance));
+                }
             } else if (!(existingObj instanceof Recipe)) {
                 throw new ConstructionException("Name " + name + " is already registered to instance " + instance);
             }
@@ -68,4 +81,32 @@
 
         instances.put(name, instance);
     }
+        
+    public void destroy() {
+        for (Destroyable destroyable : destroyList) {
+            destroyable.destroy();
+        }
+        destroyList.clear();
+        instances.clear();
+    }
+    
+    private static class DestroyCallback implements Destroyable {
+
+        private Method method;
+        private Object instance;
+        
+        public DestroyCallback(Method method, Object instance) {
+            this.method = method;
+            this.instance = instance;
+        }
+        
+        public void destroy() {
+            try {
+                method.invoke(instance);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        
+    }
 }

Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/TestBlueprintContext.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/TestBlueprintContext.java?rev=769170&r1=769169&r2=769170&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/TestBlueprintContext.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/TestBlueprintContext.java Mon Apr 27 21:25:23 2009
@@ -19,11 +19,15 @@
 package org.apache.geronimo.blueprint;
 
 import org.apache.geronimo.blueprint.context.BlueprintContextImpl;
+import org.apache.geronimo.blueprint.namespace.ComponentDefinitionRegistryImpl;
 
 public class TestBlueprintContext extends BlueprintContextImpl {
 
-    public TestBlueprintContext() {
+    private ComponentDefinitionRegistryImpl registry;
+    
+    public TestBlueprintContext(ComponentDefinitionRegistryImpl registry) {
         super(new TestBundleContext(), null, null, null, null);
+        this.registry = registry;
     }
     
     @Override
@@ -31,4 +35,9 @@
         return Thread.currentThread().getContextClassLoader();
     }
     
+    @Override
+    public ComponentDefinitionRegistryImpl getComponentDefinitionRegistry() {
+        return registry;
+    }
+    
 }

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=769170&r1=769169&r2=769170&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 Mon Apr 27 21:25:23 2009
@@ -23,6 +23,7 @@
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.geronimo.blueprint.context.Instanciator;
+import org.apache.geronimo.blueprint.context.ScopedRepository;
 import org.apache.geronimo.blueprint.namespace.ComponentDefinitionRegistryImpl;
 import org.apache.geronimo.blueprint.pojos.BeanC;
 import org.apache.geronimo.blueprint.pojos.BeanD;
@@ -37,8 +38,8 @@
 
     public void testWiring() throws Exception {
         ComponentDefinitionRegistryImpl registry = parse("/test-wiring.xml");
-        Instanciator i = new Instanciator(new TestBlueprintContext());
-        Repository repository = i.createRepository(registry);
+        Instanciator i = new Instanciator(new TestBlueprintContext(registry));
+        ScopedRepository repository = i.createRepository(registry);
         ObjectGraph graph = new ObjectGraph(repository);
         
         Object obj1 = graph.create("pojoA");
@@ -113,6 +114,11 @@
         Object obj4 = graph.create("pojoC");
         assertNotNull(obj4);
         assertTrue(obj4 != graph.create("pojoC"));
+        
+        repository.destroy();       
+        
+        // test destroy-method
+        assertEquals(true, pojob.getDestroyCalled());
     }
 
     public void testDependsOn() throws Exception {
@@ -132,7 +138,7 @@
         };
 
         ComponentDefinitionRegistryImpl registry = parse("/test-depends-on.xml");
-        Instanciator i = new Instanciator(new TestBlueprintContext());
+        Instanciator i = new Instanciator(new TestBlueprintContext(registry));
         Repository repository = i.createRepository(registry);
         ObjectGraph graph = new ObjectGraph(repository);
         graph.createAll("c", "d");
@@ -140,7 +146,7 @@
 
     public void testConstructor() throws Exception {
         ComponentDefinitionRegistryImpl registry = parse("/test-constructor.xml");
-        Instanciator i = new Instanciator(new TestBlueprintContext());
+        Instanciator i = new Instanciator(new TestBlueprintContext(registry));
         Repository repository = i.createRepository(registry);
         ObjectGraph graph = new ObjectGraph(repository);
         

Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/PojoB.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/PojoB.java?rev=769170&r1=769169&r2=769170&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/PojoB.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/PojoB.java Mon Apr 27 21:25:23 2009
@@ -23,9 +23,11 @@
 public class PojoB {
 
     private URI uri;
-    private boolean initCalled;
     private int number;
     
+    private boolean initCalled;
+    private boolean destroyCalled;
+    
     public PojoB() {
     }
     
@@ -50,6 +52,14 @@
         return initCalled;
     }
     
+    public void destroy() {
+        destroyCalled = true;
+    }
+    
+    public boolean getDestroyCalled() {
+        return destroyCalled;
+    }
+    
     public int getNumber() {
         return number;
     }