geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r767652 - in /geronimo/sandbox/blueprint: blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/ blueprint-core/src/test/java/org/apache/geronimo/blueprint/ blueprint-itests/src/test/java/org/apache/geronimo/blueprint/itests/ b...
Date Wed, 22 Apr 2009 20:49:14 GMT
Author: gnodet
Date: Wed Apr 22 20:49:13 2009
New Revision: 767652

URL: http://svn.apache.org/viewvc?rev=767652&view=rev
Log:
Make sure singletons are correctly destroyed when the blueprint context is destroyed

Modified:
    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/test/java/org/apache/geronimo/blueprint/WiringTest.java
    geronimo/sandbox/blueprint/blueprint-itests/src/test/java/org/apache/geronimo/blueprint/itests/TestBlueprintContext.java
    geronimo/sandbox/blueprint/blueprint-sample/src/main/java/org/apache/geronimo/blueprint/sample/Foo.java
    geronimo/sandbox/blueprint/blueprint-sample/src/main/resources/OSGI-INF/blueprint/config.xml

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=767652&r1=767651&r2=767652&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
Wed Apr 22 20:49:13 2009
@@ -28,6 +28,7 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.Properties;
+import java.util.HashMap;
 import java.util.concurrent.ExecutorService;
 
 import org.apache.geronimo.blueprint.BlueprintConstants;
@@ -35,6 +36,7 @@
 import org.apache.geronimo.blueprint.HeaderParser.PathElement;
 import org.apache.geronimo.blueprint.ModuleContextEventSender;
 import org.apache.geronimo.blueprint.NamespaceHandlerRegistry;
+import org.apache.geronimo.blueprint.Destroyable;
 import org.apache.geronimo.blueprint.convert.ConversionServiceImpl;
 import org.apache.geronimo.blueprint.namespace.ComponentDefinitionRegistryImpl;
 import org.apache.geronimo.blueprint.namespace.NamespaceHandlerRegistryImpl;
@@ -54,6 +56,8 @@
 import org.osgi.service.blueprint.reflect.BeanMetadata;
 import org.osgi.service.blueprint.reflect.ServiceMetadata;
 import org.osgi.service.blueprint.reflect.ServiceReferenceMetadata;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * TODO: javadoc
@@ -63,6 +67,8 @@
  */
 public class BlueprintContextImpl implements BlueprintContext, NamespaceHandlerRegistry.Listener,
Runnable {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(BlueprintContextImpl.class);
+
     private enum State {
         Unknown,
         WaitForNamespaceHandlers,
@@ -84,6 +90,7 @@
     private ObjectGraph objectGraph;
     private ServiceRegistration registration;
     private boolean waitForNamespaceHandlersEventSent;
+    private Map<String, Destroyable> destroyables = new HashMap<String, Destroyable>();
 
     public BlueprintContextImpl(BundleContext bundleContext, ModuleContextEventSender sender,
NamespaceHandlerRegistry handlers, ExecutorService executors, List<URL> urls) {
         this.bundleContext = bundleContext;
@@ -95,6 +102,10 @@
         this.executors = executors;
     }
 
+    public void addDestroyable(String name, Destroyable destroyable) {
+        destroyables.put(name, destroyable);
+    }
+
     public ModuleContextEventSender getSender() {
         return sender;
     }
@@ -180,8 +191,9 @@
             }
         } catch (Exception e) {
             state = State.Failed;
+            // TODO: clean up
+            LOGGER.error("Unable to start blueprint context", e);
             sender.sendFailure(this, e);
-            e.printStackTrace(); // TODO: log failure
         }
     }
 
@@ -217,6 +229,18 @@
         Map instances = objectGraph.createAll(components);
         System.out.println("Component instances: " + instances);
     }
+
+    private void destroyComponents() {
+        Map<String, Destroyable> destroyables = new HashMap<String, Destroyable>(this.destroyables);
+        this.destroyables.clear();
+        for (Map.Entry<String, Destroyable> entry : destroyables.entrySet()) {
+            try {
+                entry.getValue().destroy();
+            } catch (Exception e) {
+                LOGGER.info("Error destroying bean " + entry.getKey(), e);
+            }
+        }
+    }
     
     private void registerAllServices() {
         for (ServiceMetadata service : getExportedServicesMetadata()) {
@@ -281,7 +305,7 @@
         return objectGraph;
     }
     
-    protected ComponentDefinitionRegistry getComponentDefinitionRegistry() {
+    protected ComponentDefinitionRegistryImpl getComponentDefinitionRegistry() {
         return componentDefinitionRegistry;
     }
     
@@ -300,8 +324,9 @@
         handlers.removeListener(this);
         sender.sendDestroying(this);
         unregisterAllServices();
+        destroyComponents();
+        // TODO: stop all reference / collections
         System.out.println("Module context destroyed: " + this.bundleContext);
-        // TODO: destroy all instances
         sender.sendDestroyed(this);
     }
 
@@ -313,7 +338,9 @@
 
     public synchronized void namespaceHandlerUnregistered(URI uri) {
         if (namespaces != null && namespaces.contains(uri)) {
-            // TODO: destroy all instances
+            unregisterAllServices();
+            destroyComponents();
+            // TODO: stop all reference / collections
             waitForNamespaceHandlersEventSent = false;
             state = State.WaitForNamespaceHandlers;
             executors.submit(this);

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=767652&r1=767651&r2=767652&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
Wed Apr 22 20:49:13 2009
@@ -24,6 +24,7 @@
 
 import org.apache.xbean.recipe.ConstructionException;
 import org.apache.xbean.recipe.ObjectRecipe;
+import org.apache.geronimo.blueprint.Destroyable;
 
 /**
  *
@@ -31,13 +32,15 @@
  * @version $Rev$, $Date$
  */
 public class BlueprintObjectRecipe extends ObjectRecipe {
-    
+
+    private final BlueprintContextImpl blueprintContext;
     private boolean keepRecipe = false;
     private Method initMethod;
     private Method destroyMethod;
     
-    public BlueprintObjectRecipe(Class typeName) {
+    public BlueprintObjectRecipe(BlueprintContextImpl blueprintContext, Class typeName) {
         super(typeName);
+        this.blueprintContext = blueprintContext;
     }
     
     public void setKeepRecipe(boolean keepRecipe) {
@@ -66,7 +69,7 @@
         
     @Override
     protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException
{
-        Object obj = super.internalCreate(expectedType, lazyRefAllowed);
+        final Object obj = super.internalCreate(expectedType, lazyRefAllowed);
         if (initMethod != null) {
             try {
                 initMethod.invoke(obj, new Object[] {});
@@ -77,6 +80,14 @@
                 e.printStackTrace();
             }
         }
+        if (destroyMethod != null && blueprintContext != null) {
+            Destroyable d = new Destroyable() {
+                public void destroy() throws Exception {
+                    destroyInstance(obj);
+                }
+            };
+            blueprintContext.addDestroyable(getName(), d);
+        }
         return obj;
     }
     

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=767652&r1=767651&r2=767652&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
Wed Apr 22 20:49:13 2009
@@ -84,23 +84,23 @@
         primitiveClasses.put("boolean", boolean.class);
     }
     
-    private BlueprintContextImpl moduleContext;
-    
-    public Instanciator(BlueprintContextImpl moduleContext) {
-        this.moduleContext = moduleContext;
+    private BlueprintContextImpl blueprintContext;
+
+    public Instanciator(BlueprintContextImpl blueprintContext) {
+        this.blueprintContext = blueprintContext;
     }
     
     private void addBuiltinComponents(Repository repository) {
-        if (moduleContext != null) {
-            repository.add("moduleContext", moduleContext);
-            repository.add("bundleContext", moduleContext.getBundleContext());          
        
-            repository.add("bundle", moduleContext.getBundleContext().getBundle());
-            repository.add("conversionService", moduleContext.getConversionService());
+        if (blueprintContext != null) {
+            repository.add("moduleContext", blueprintContext);
+            repository.add("bundleContext", blueprintContext.getBundleContext());
+            repository.add("bundle", blueprintContext.getBundleContext().getBundle());
+            repository.add("conversionService", blueprintContext.getConversionService());
         }
     }
     
     public Repository createRepository() throws Exception {
-        ComponentDefinitionRegistryImpl registry = (ComponentDefinitionRegistryImpl)getComponentDefinitionRegistry();
+        ComponentDefinitionRegistryImpl registry = getComponentDefinitionRegistry();
         Repository repository = new ScopedRepository();
         addBuiltinComponents(repository);
         
@@ -153,8 +153,8 @@
             comparatorRecipe = (Recipe) getValue(metadata.getComparator(), Comparator.class);
         }
         CollectionBasedServiceReferenceRecipe recipe = new CollectionBasedServiceReferenceRecipe(
-                                                                   moduleContext,
-                                                                   moduleContext.getSender(),
+                blueprintContext,
+                                                                   blueprintContext.getSender(),
                                                                    metadata,
                                                                    listenersRecipe,
                                                                    comparatorRecipe);
@@ -170,8 +170,8 @@
                 listenersRecipe.add(createRecipe(listener));
             }
         }
-        UnaryServiceReferenceRecipe recipe = new UnaryServiceReferenceRecipe(moduleContext,
-                                                                   moduleContext.getSender(),
+        UnaryServiceReferenceRecipe recipe = new UnaryServiceReferenceRecipe(blueprintContext,
+                                                                   blueprintContext.getSender(),
                                                                    metadata,
                                                                    listenersRecipe);
         recipe.setName(metadata.getId());
@@ -182,11 +182,11 @@
         ObjectRecipe recipe = new ObjectRecipe(ServiceRegistrationProxy.class);
         recipe.allow(Option.PRIVATE_PROPERTIES);
         recipe.setName(serviceExport.getId());
-        recipe.setProperty("moduleContext", moduleContext);
+        recipe.setProperty("moduleContext", blueprintContext);
         BeanMetadata exportedComponent = getLocalServiceComponent(serviceExport.getServiceComponent());
         if (exportedComponent != null && BeanMetadata.SCOPE_BUNDLE.equals(exportedComponent.getScope()))
{
             BlueprintObjectRecipe exportedComponentRecipe = createComponentRecipe(exportedComponent);
-            recipe.setProperty("service", new BundleScopeServiceFactory(moduleContext, exportedComponentRecipe));
+            recipe.setProperty("service", new BundleScopeServiceFactory(blueprintContext,
exportedComponentRecipe));
         } else {
             recipe.setProperty("service", getValue(serviceExport.getServiceComponent(), null));
         }
@@ -208,7 +208,7 @@
     }
 
     private BlueprintObjectRecipe createComponentRecipe(BeanMetadata local) throws Exception
{
-        BlueprintObjectRecipe recipe = new BlueprintObjectRecipe(loadClass(local.getClassName()));
+        BlueprintObjectRecipe recipe = new BlueprintObjectRecipe(blueprintContext, loadClass(local.getClassName()));
         recipe.allow(Option.PRIVATE_PROPERTIES);
         recipe.setName(local.getId());
         for (BeanProperty property : local.getProperties()) {
@@ -218,7 +218,7 @@
         if (BeanMetadata.SCOPE_PROTOTYPE.equals(local.getScope())) {
             recipe.setKeepRecipe(true);
         }
-        ComponentDefinitionRegistryImpl registry = (ComponentDefinitionRegistryImpl)getComponentDefinitionRegistry();
+        ComponentDefinitionRegistryImpl registry = getComponentDefinitionRegistry();
         // check for init-method and set it on Recipe
         String initMethod = local.getInitMethodName();
         if (initMethod == null) {
@@ -342,12 +342,12 @@
         }
     }
     
-    protected ComponentDefinitionRegistry getComponentDefinitionRegistry() {
-        return moduleContext.getComponentDefinitionRegistry();
+    protected ComponentDefinitionRegistryImpl getComponentDefinitionRegistry() {
+        return blueprintContext.getComponentDefinitionRegistry();
     }
     
     protected ConversionService getConversionService() {
-        return moduleContext.getConversionService();
+        return blueprintContext.getConversionService();
     }
     
     private Class loadClass(String typeName) throws ClassNotFoundException {
@@ -357,11 +357,11 @@
 
         Class clazz = primitiveClasses.get(typeName);
         if (clazz == null) {
-            if (moduleContext == null) {
+            if (blueprintContext == null) {
                 ClassLoader loader = Thread.currentThread().getContextClassLoader();
                 clazz = loader.loadClass(typeName);
             } else {
-                clazz = moduleContext.getBundleContext().getBundle().loadClass(typeName);
+                clazz = blueprintContext.getBundleContext().getBundle().loadClass(typeName);
             }
         }
         return clazz;

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=767652&r1=767651&r2=767652&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
Wed Apr 22 20:49:13 2009
@@ -125,7 +125,7 @@
         }
         
         @Override
-        public ComponentDefinitionRegistry getComponentDefinitionRegistry() {
+        public ComponentDefinitionRegistryImpl getComponentDefinitionRegistry() {
             return registry;
         }
         

Modified: geronimo/sandbox/blueprint/blueprint-itests/src/test/java/org/apache/geronimo/blueprint/itests/TestBlueprintContext.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-itests/src/test/java/org/apache/geronimo/blueprint/itests/TestBlueprintContext.java?rev=767652&r1=767651&r2=767652&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-itests/src/test/java/org/apache/geronimo/blueprint/itests/TestBlueprintContext.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-itests/src/test/java/org/apache/geronimo/blueprint/itests/TestBlueprintContext.java
Wed Apr 22 20:49:13 2009
@@ -43,6 +43,7 @@
         Resource res = locateBundle(getBundle("org.apache.geronimo", "blueprint-sample"));
         Bundle bundle = installBundle(res);
         assertNotNull(bundle);
+
         bundle.start();
 
         BlueprintContext blueprintContext = getOsgiService(BlueprintContext.class, 5000);
@@ -68,6 +69,9 @@
         assertEquals(Currency.getInstance("PLN"), foo.getCurrency());
         assertEquals(new SimpleDateFormat("yyyy.MM.dd").parse("2009.04.17"), foo.getDate());
 
+        assertTrue(foo.isInitialized());
+        assertFalse(foo.isDestroyed());
+
         obj = getOsgiService(Foo.class, 5000);
         assertNotNull(obj);
         assertSame(foo, obj);
@@ -79,6 +83,9 @@
         } catch (Exception e) {
             // Expected, as the module context should have been unregistered
         }
+
+        assertTrue(foo.isInitialized());
+        assertTrue(foo.isDestroyed());
     }
 
     public void testUnaryReference() throws Exception {

Modified: geronimo/sandbox/blueprint/blueprint-sample/src/main/java/org/apache/geronimo/blueprint/sample/Foo.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-sample/src/main/java/org/apache/geronimo/blueprint/sample/Foo.java?rev=767652&r1=767651&r2=767652&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-sample/src/main/java/org/apache/geronimo/blueprint/sample/Foo.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-sample/src/main/java/org/apache/geronimo/blueprint/sample/Foo.java
Wed Apr 22 20:49:13 2009
@@ -28,6 +28,9 @@
     private Currency currency;
     private Date date;
 
+    public boolean initialized;
+    public boolean destroyed;
+
     public int getA() {
         return a;
     }
@@ -52,5 +55,22 @@
         return a + " " + b + " " + bar + " " + currency + " " + date;
     }
 
+    public void init() {
+        System.out.println("======== Initializing Foo =========");
+        initialized = true;
+    }
+
+    public void destroy() {
+        System.out.println("======== Destroying Foo =========");
+        destroyed = true;
+    }
+
+    public boolean isInitialized() {
+        return initialized;
+    }
+
+    public boolean isDestroyed() {
+        return destroyed;
+    }
 }
 

Modified: geronimo/sandbox/blueprint/blueprint-sample/src/main/resources/OSGI-INF/blueprint/config.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-sample/src/main/resources/OSGI-INF/blueprint/config.xml?rev=767652&r1=767651&r2=767652&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-sample/src/main/resources/OSGI-INF/blueprint/config.xml
(original)
+++ geronimo/sandbox/blueprint/blueprint-sample/src/main/resources/OSGI-INF/blueprint/config.xml
Wed Apr 22 20:49:13 2009
@@ -8,7 +8,7 @@
             <ref component="converter2"/>
     </type-converters>
 
-    <bean id="foo" class="org.apache.geronimo.blueprint.sample.Foo">
+    <bean id="foo" class="org.apache.geronimo.blueprint.sample.Foo" init-method="init"
destroy-method="destroy">
         <property name="a" value="5" />
         <property name="b" value="10" />
         <property name="bar" ref="bar" />



Mime
View raw message