You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by ma...@apache.org on 2011/02/17 21:26:37 UTC

svn commit: r1071770 - in /aries/trunk/transaction/transaction-blueprint: ./ src/main/java/org/apache/aries/transaction/ src/main/java/org/apache/aries/transaction/parsing/ src/main/resources/OSGI-INF/blueprint/ src/test/java/org/apache/aries/transaction/

Author: mahrwald
Date: Thu Feb 17 20:26:36 2011
New Revision: 1071770

URL: http://svn.apache.org/viewvc?rev=1071770&view=rev
Log:
ARIES-578: Add some lifecycle to prevent memory leaks

Added:
    aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxBlueprintListener.java
Modified:
    aries/trunk/transaction/transaction-blueprint/pom.xml
    aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxComponentMetaDataHelper.java
    aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxComponentMetaDataHelperImpl.java
    aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java
    aries/trunk/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml
    aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BaseNameSpaceHandlerSetup.java
    aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java
    aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/NameSpaceHandlerTest.java

Modified: aries/trunk/transaction/transaction-blueprint/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-blueprint/pom.xml?rev=1071770&r1=1071769&r2=1071770&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-blueprint/pom.xml (original)
+++ aries/trunk/transaction/transaction-blueprint/pom.xml Thu Feb 17 20:26:36 2011
@@ -70,6 +70,10 @@
 		</dependency>
 		<dependency>
 			<groupId>org.osgi</groupId>
+			<artifactId>org.osgi.core</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.osgi</groupId>
 			<artifactId>org.osgi.compendium</artifactId>
 			<scope>test</scope>
 		</dependency>

Modified: aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxComponentMetaDataHelper.java
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxComponentMetaDataHelper.java?rev=1071770&r1=1071769&r2=1071770&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxComponentMetaDataHelper.java (original)
+++ aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxComponentMetaDataHelper.java Thu Feb 17 20:26:36 2011
@@ -23,8 +23,8 @@ import org.osgi.service.blueprint.reflec
 
 public interface TxComponentMetaDataHelper {
 
-    public void setComponentTransactionData(ComponentMetadata component, String value,
-        String method);
+    public void setComponentTransactionData(ComponentDefinitionRegistry registry, 
+            ComponentMetadata component, String value, String method);
 
     public String getComponentMethodTxAttribute(
             ComponentMetadata component, String methodName);
@@ -32,4 +32,5 @@ public interface TxComponentMetaDataHelp
     public void populateBundleWideTransactionData(ComponentDefinitionRegistry cdr, String value,
         String method, String bean);
 
+    public void unregister(ComponentDefinitionRegistry registry);
 }

Modified: aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxComponentMetaDataHelperImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxComponentMetaDataHelperImpl.java?rev=1071770&r1=1071769&r2=1071770&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxComponentMetaDataHelperImpl.java (original)
+++ aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/TxComponentMetaDataHelperImpl.java Thu Feb 17 20:26:36 2011
@@ -19,11 +19,13 @@
 package org.apache.aries.transaction;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -158,13 +160,29 @@ public class TxComponentMetaDataHelperIm
     // this is configured via top level tx:transaction element for the blueprint managed bundle
     private static final ConcurrentHashMap<ComponentDefinitionRegistry, List<BundleWideTxData>> bundleTransactionMap = new ConcurrentHashMap<ComponentDefinitionRegistry, List<BundleWideTxData>>();
 
-    public synchronized void setComponentTransactionData(ComponentMetadata component, String value, String method)
+    // this gives us a reverse lookup when we need to clean up
+    private static final ConcurrentMap<ComponentDefinitionRegistry, Collection<ComponentMetadata>> dataForCDR = 
+        new ConcurrentHashMap<ComponentDefinitionRegistry, Collection<ComponentMetadata>>();
+    
+    public void unregister(ComponentDefinitionRegistry registry) {
+        Collection<ComponentMetadata> components = dataForCDR.remove(registry);
+        bundleTransactionMap.remove(registry);
+        
+        if (components != null) {
+            for (ComponentMetadata meta : components) data.remove(meta);
+        }
+    }
+    
+    public synchronized void setComponentTransactionData(ComponentDefinitionRegistry registry, ComponentMetadata component, String value, String method)
     {
       TranData td = data.get(component);
           
       if (td == null) {
           td = new TranData();
           data.put(component, td);
+
+          dataForCDR.putIfAbsent(registry, new HashSet<ComponentMetadata>());
+          dataForCDR.get(registry).add(component);
       }
       
       if (method == null || method.isEmpty()) {
@@ -208,14 +226,13 @@ public class TxComponentMetaDataHelperIm
              * 4. top level tx w/ no other attribute
              */
             //result = calculateBundleWideTransaction(component, methodName);
-            String compId = component.getId();
-            ComponentDefinitionRegistry cdr = getComponentDefinitionRegistry(compId);
+            ComponentDefinitionRegistry cdr = getComponentDefinitionRegistry(component);
             if (cdr == null) {
                 // no bundle wide transaction configuration avail
             	result = null;
             } else {
                 List<BundleWideTxData> bundleData = bundleTransactionMap.get(cdr);
-                result = BundleWideTxDataUtil.getAttribute(compId, methodName, bundleData);
+                result = BundleWideTxDataUtil.getAttribute(component.getId(), methodName, bundleData);
             }
         }
 
@@ -247,15 +264,13 @@ public class TxComponentMetaDataHelperIm
         
     }
     
-    private ComponentDefinitionRegistry getComponentDefinitionRegistry(String compId) {
+    private ComponentDefinitionRegistry getComponentDefinitionRegistry(ComponentMetadata metadata) {
         Enumeration<ComponentDefinitionRegistry> keys = bundleTransactionMap.keys();
         while (keys.hasMoreElements()) {
             ComponentDefinitionRegistry cdr = keys.nextElement();
-            Set<String> names = cdr.getComponentDefinitionNames();
-            for (String name : names) {
-                if (name.equals(compId)) {
-                    return cdr;
-                }
+            if (cdr.containsComponentDefinition(metadata.getId()) 
+                    && metadata.equals(cdr.getComponentDefinition(metadata.getId()))) {
+                return cdr;
             }
         }
         

Added: aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxBlueprintListener.java
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxBlueprintListener.java?rev=1071770&view=auto
==============================================================================
--- aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxBlueprintListener.java (added)
+++ aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxBlueprintListener.java Thu Feb 17 20:26:36 2011
@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+package org.apache.aries.transaction.parsing;
+
+import org.apache.aries.transaction.TxComponentMetaDataHelper;
+import org.osgi.service.blueprint.container.BlueprintEvent;
+import org.osgi.service.blueprint.container.BlueprintListener;
+
+/**
+ * A blueprint listener that tracks DESTROYED events to clean up any stored metadata for that blueprint.
+ * (So that we don't leak memory).
+ */
+public class TxBlueprintListener implements BlueprintListener {
+    
+    private final TxElementHandler handler;
+    private final TxComponentMetaDataHelper helper;
+    
+    public TxBlueprintListener(TxElementHandler handler, TxComponentMetaDataHelper helper) {
+        this.handler = handler;
+        this.helper = helper;
+    }
+    
+    public void blueprintEvent(BlueprintEvent event) {
+        if (event.getType() == BlueprintEvent.DESTROYED) {
+            handler.unregister(event.getBundle());
+        }
+    }
+
+}

Modified: aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java?rev=1071770&r1=1071769&r2=1071770&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java (original)
+++ aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/TxElementHandler.java Thu Feb 17 20:26:36 2011
@@ -21,9 +21,12 @@ package org.apache.aries.transaction.par
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
-import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -31,8 +34,10 @@ import org.apache.aries.blueprint.Compon
 import org.apache.aries.blueprint.Interceptor;
 import org.apache.aries.blueprint.NamespaceHandler;
 import org.apache.aries.blueprint.ParserContext;
+import org.apache.aries.blueprint.PassThroughMetadata;
 import org.apache.aries.transaction.Constants;
 import org.apache.aries.transaction.TxComponentMetaDataHelper;
+import org.osgi.framework.Bundle;
 import org.osgi.service.blueprint.container.BlueprintContainer;
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
 import org.osgi.service.blueprint.reflect.Metadata;
@@ -45,13 +50,12 @@ public class TxElementHandler implements
     public static final String DEFAULT_INTERCEPTOR_ID = "txinterceptor";
     public static final String INTERCEPTOR_BLUEPRINT_ID = "interceptor.blueprint.id";
 
-    private static final Logger LOGGER =
-        LoggerFactory.getLogger(TxElementHandler.class);
+    private static final Logger LOGGER = LoggerFactory.getLogger(TxElementHandler.class);
 
     private TxComponentMetaDataHelper metaDataHelper;
     private Interceptor interceptor = null;
 
-    private Set<ComponentDefinitionRegistry> registered = new HashSet<ComponentDefinitionRegistry>();
+    private ConcurrentMap<ComponentDefinitionRegistry,Bundle> registered = new ConcurrentHashMap<ComponentDefinitionRegistry, Bundle>();
 
     private void parseElement(Element elt, ComponentMetadata cm, ParserContext pc)
     {
@@ -65,8 +69,7 @@ public class TxElementHandler implements
             ComponentDefinitionRegistry cdr = pc.getComponentDefinitionRegistry();
             
             if (cm == null) {
-                // if the enclosing component is null, then we assume this is the top element 
-                
+                // if the enclosing component is null, then we assume this is the top element                 
                 
                 String bean = elt.getAttribute(Constants.BEAN);
                 registerComponentsWithInterceptor(cdr, bean);
@@ -79,7 +82,7 @@ public class TxElementHandler implements
                     LOGGER.debug("parser setting comp trans data for " + elt.getAttribute(Constants.VALUE) + "  "
                             + elt.getAttribute(Constants.METHOD));
     
-                metaDataHelper.setComponentTransactionData(cm, elt.getAttribute(Constants.VALUE), elt
+                metaDataHelper.setComponentTransactionData(cdr, cm, elt.getAttribute(Constants.VALUE), elt
                         .getAttribute(Constants.METHOD));
             }
         }
@@ -106,9 +109,9 @@ public class TxElementHandler implements
     public URL getSchemaLocation(String arg0)
     {
     	if (arg0.equals(Constants.TRANSACTION10URI)) {
-    		return this.getClass().getResource(Constants.TX10_SCHEMA);
+    	    return this.getClass().getResource(Constants.TX10_SCHEMA);
     	} else {
-            return this.getClass().getResource(Constants.TX11_SCHEMA);
+    	    return this.getClass().getResource(Constants.TX11_SCHEMA);
     	}
     }
 
@@ -149,50 +152,51 @@ public class TxElementHandler implements
         return null;
     }
     
-    private boolean isRegistered(ComponentDefinitionRegistry cdr) {
-        for (ComponentDefinitionRegistry compdr : registered) {
-            if (compdr == cdr) {
-                return true;
+    public boolean isRegistered(ComponentDefinitionRegistry cdr) {
+        return registered.containsKey(cdr);
+    }
+    
+    public void unregister(Bundle blueprintBundle) {
+        Iterator<Map.Entry<ComponentDefinitionRegistry, Bundle>> it = registered.entrySet().iterator();
+        while (it.hasNext()) {
+            Map.Entry<ComponentDefinitionRegistry, Bundle> e = it.next();
+            if (blueprintBundle.equals(e.getValue())) {
+                metaDataHelper.unregister(e.getKey());
+                it.remove();
             }
         }
-        
-        return false;
     }
     
     private void registerComponentsWithInterceptor(ComponentDefinitionRegistry cdr, String bean) {
-        // if it is already registered all components in the component definition registry, do nothing
-        if (isRegistered(cdr)) {
-            return;
+        ComponentMetadata meta = cdr.getComponentDefinition("blueprintBundle");
+        Bundle blueprintBundle = null;
+        if (meta instanceof PassThroughMetadata) {
+            blueprintBundle = (Bundle) ((PassThroughMetadata) meta).getObject();
         }
         
-        Set<String> ids = cdr.getComponentDefinitionNames();
-        
-        if (bean == null || bean.isEmpty()) {
-            // in this case, let's attempt to register all components
-            // if the component has already been registered with this interceptor,
-            // the registration will be ignored.
-            for (String id : ids) {
-                ComponentMetadata componentMetadata = cdr.getComponentDefinition(id);
-                cdr.registerInterceptorWithComponent(componentMetadata, interceptor);
-            }
-            synchronized (registered) {
-                registered.add(cdr);
-            }
-        } else {
-            // register the beans specified
-            Pattern p = Pattern.compile(bean);
-            for (String id : ids) {
-                Matcher m = p.matcher(id);
-                if (m.matches()) {
+        // if it is already registered all components in the component definition registry, do nothing
+        if (registered.putIfAbsent(cdr, blueprintBundle) == null) {
+            Set<String> ids = cdr.getComponentDefinitionNames();
+            
+            if (bean == null || bean.isEmpty()) {
+                // in this case, let's attempt to register all components
+                // if the component has already been registered with this interceptor,
+                // the registration will be ignored.
+                for (String id : ids) {
                     ComponentMetadata componentMetadata = cdr.getComponentDefinition(id);
                     cdr.registerInterceptorWithComponent(componentMetadata, interceptor);
                 }
+            } else {
+                // register the beans specified
+                Pattern p = Pattern.compile(bean);
+                for (String id : ids) {
+                    Matcher m = p.matcher(id);
+                    if (m.matches()) {
+                        ComponentMetadata componentMetadata = cdr.getComponentDefinition(id);
+                        cdr.registerInterceptorWithComponent(componentMetadata, interceptor);
+                    }
+                }
             }
-        }
+        }        
     }
-    
-    // check if the beans pattern includes the particular component/bean id
-    /*private boolean includes(String beans, String id) {
-        return Pattern.matches(beans, id);
-    }*/
 }

Modified: aries/trunk/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml?rev=1071770&r1=1071769&r2=1071770&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml (original)
+++ aries/trunk/transaction/transaction-blueprint/src/main/resources/OSGI-INF/blueprint/transaction.xml Thu Feb 17 20:26:36 2011
@@ -22,7 +22,7 @@
             xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0" 
             default-activation="lazy">
             
-  <service interface="org.apache.aries.blueprint.NamespaceHandler">
+  <service interface="org.apache.aries.blueprint.NamespaceHandler" ref="nsHandler">
         <service-properties>
             <entry key="osgi.service.blueprint.namespace">
             	<list>
@@ -31,11 +31,12 @@
 			    </list>
 			</entry>
         </service-properties>
-        <bean class="org.apache.aries.transaction.parsing.TxElementHandler">
-            <property ref="txenhancer" name="txMetaDataHelper"/>
-            <property name="blueprintContainer" ref="blueprintContainer" />
-        </bean>
   </service>              
+
+  <bean id="nsHandler" class="org.apache.aries.transaction.parsing.TxElementHandler">
+    <property ref="txenhancer" name="txMetaDataHelper"/>
+    <property name="blueprintContainer" ref="blueprintContainer" />
+  </bean>
   
   <bean id="txenhancer" class="org.apache.aries.transaction.TxComponentMetaDataHelperImpl"/>
   
@@ -45,5 +46,12 @@
   </bean>
   
   <reference id="tm" interface="javax.transaction.TransactionManager"/>
-
+  
+  <service interface="org.osgi.service.blueprint.container.BlueprintListener">
+  	<bean class="org.apache.aries.transaction.parsing.TxElementHandler">
+  	  <argument ref="nsHandler" />
+  	  <argument ref="txenhancer" />
+  	</bean>
+  </service>
+  
 </blueprint>
\ No newline at end of file

Modified: aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BaseNameSpaceHandlerSetup.java
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BaseNameSpaceHandlerSetup.java?rev=1071770&r1=1071769&r2=1071770&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BaseNameSpaceHandlerSetup.java (original)
+++ aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BaseNameSpaceHandlerSetup.java Thu Feb 17 20:26:36 2011
@@ -18,13 +18,23 @@
  */
 package org.apache.aries.transaction;
 
+import java.net.URI;
+import java.net.URL;
+import java.util.Arrays;
 import java.util.Properties;
+import java.util.Set;
 
 import javax.transaction.TransactionManager;
 
+import org.apache.aries.blueprint.ComponentDefinitionRegistry;
 import org.apache.aries.blueprint.NamespaceHandler;
 import org.apache.aries.blueprint.container.NamespaceHandlerRegistry;
+import org.apache.aries.blueprint.container.Parser;
+import org.apache.aries.blueprint.container.NamespaceHandlerRegistry.NamespaceHandlerSet;
+import org.apache.aries.blueprint.namespace.ComponentDefinitionRegistryImpl;
 import org.apache.aries.blueprint.namespace.NamespaceHandlerRegistryImpl;
+import org.apache.aries.blueprint.reflect.PassThroughMetadataImpl;
+import org.apache.aries.mocks.BundleContextMock;
 import org.apache.aries.mocks.BundleMock;
 import org.apache.aries.transaction.parsing.TxElementHandler;
 import org.apache.aries.unittest.mocks.MethodCall;
@@ -39,6 +49,7 @@ public class BaseNameSpaceHandlerSetup {
     protected Bundle b;
     protected NamespaceHandlerRegistry nhri;
     protected TxComponentMetaDataHelperImpl txenhancer;
+    protected TxElementHandler namespaceHandler;
     
     @Before
     public void setUp() {
@@ -54,7 +65,7 @@ public class BaseNameSpaceHandlerSetup {
         txinterceptor.setTransactionManager(tm);
         txinterceptor.setTxMetaDataHelper(txenhancer);
         
-        TxElementHandler namespaceHandler = new TxElementHandler();
+        namespaceHandler = new TxElementHandler();
         
         BlueprintContainer container = Skeleton.newMock(BlueprintContainer.class);
         Skeleton.getSkeleton(container).setReturnValue(
@@ -68,10 +79,29 @@ public class BaseNameSpaceHandlerSetup {
         ctx.registerService(NamespaceHandler.class.getName(), namespaceHandler, props);
     }
       
-      @After
-      public void tearDown() throws Exception{
-        b = null;
-          nhri = null;
-          txenhancer = null;
-      }
+    @After
+    public void tearDown() throws Exception{
+      b = null;
+      nhri = null;
+      txenhancer = null;
+      
+      BundleContextMock.clear();
+    }
+    
+    protected ComponentDefinitionRegistry parseCDR(String name) throws Exception {
+        Parser p = new Parser();
+        
+        URL bpxml = this.getClass().getResource(name);
+        p.parse(Arrays.asList(bpxml));
+        
+        Set<URI> nsuris = p.getNamespaces();
+        NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
+        p.validate(nshandlers.getSchema());
+        
+        ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
+        cdr.registerComponentDefinition(new PassThroughMetadataImpl("blueprintBundle", b));
+        p.populate(nshandlers, cdr);
+        
+        return cdr;
+    }
 }

Modified: aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java?rev=1071770&r1=1071769&r2=1071770&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java (original)
+++ aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/BundleWideNameSpaceHandlerTest.java Thu Feb 17 20:26:36 2011
@@ -21,16 +21,7 @@ package org.apache.aries.transaction;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
-import java.net.URI;
-import java.net.URL;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
-
 import org.apache.aries.blueprint.ComponentDefinitionRegistry;
-import org.apache.aries.blueprint.container.Parser;
-import org.apache.aries.blueprint.container.NamespaceHandlerRegistry.NamespaceHandlerSet;
-import org.apache.aries.blueprint.namespace.ComponentDefinitionRegistryImpl;
 import org.junit.Test;
 import org.osgi.service.blueprint.reflect.BeanMetadata;
 
@@ -39,20 +30,7 @@ public class BundleWideNameSpaceHandlerT
     @Test
     public void testMultipleElements() throws Exception
     {
-
-      Parser p = new Parser();
-      
-      URL bpxml = this.getClass().getResource("bundlewide-aries.xml");
-      List<URL> bpxmlList = new LinkedList<URL>();
-      bpxmlList.add(bpxml); 
-      
-      p.parse(bpxmlList);
-      Set<URI> nsuris = p.getNamespaces();
-      NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
-      p.validate(nshandlers.getSchema());
-      
-      ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
-      p.populate(nshandlers, cdr);
+      ComponentDefinitionRegistry cdr = parseCDR("bundlewide-aries.xml");
             
       BeanMetadata compTop = (BeanMetadata) cdr.getComponentDefinition("top1");
       BeanMetadata compDown = (BeanMetadata) cdr.getComponentDefinition("down1");
@@ -68,19 +46,7 @@ public class BundleWideNameSpaceHandlerT
     @Test
     public void testMultipleElements2() throws Exception
     {
-      Parser p = new Parser();
-      
-      URL bpxml = this.getClass().getResource("bundlewide-aries2.xml");
-      List<URL> bpxmlList = new LinkedList<URL>();
-      bpxmlList.add(bpxml); 
-      
-      p.parse(bpxmlList);
-      Set<URI> nsuris = p.getNamespaces();
-      NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
-      p.validate(nshandlers.getSchema());
-      
-      ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
-      p.populate(nshandlers, cdr);
+      ComponentDefinitionRegistry cdr = parseCDR("bundlewide-aries2.xml");
       
       BeanMetadata compTop = (BeanMetadata) cdr.getComponentDefinition("top2");
       BeanMetadata compDown = (BeanMetadata) cdr.getComponentDefinition("down2");
@@ -104,19 +70,7 @@ public class BundleWideNameSpaceHandlerT
     @Test
     public void testMultipleElements3() throws Exception
     {
-      Parser p = new Parser();
-      
-      URL bpxml = this.getClass().getResource("bundlewide-aries3.xml");
-      List<URL> bpxmlList = new LinkedList<URL>();
-      bpxmlList.add(bpxml); 
-      
-      p.parse(bpxmlList);
-      Set<URI> nsuris = p.getNamespaces();
-      NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
-      p.validate(nshandlers.getSchema());
-      
-      ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
-      p.populate(nshandlers, cdr);
+      ComponentDefinitionRegistry cdr = parseCDR("bundlewide-aries3.xml");
       
       BeanMetadata compTop = (BeanMetadata) cdr.getComponentDefinition("top3");
       BeanMetadata compDown = (BeanMetadata) cdr.getComponentDefinition("down3");

Modified: aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/NameSpaceHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/NameSpaceHandlerTest.java?rev=1071770&r1=1071769&r2=1071770&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/NameSpaceHandlerTest.java (original)
+++ aries/trunk/transaction/transaction-blueprint/src/test/java/org/apache/aries/transaction/NameSpaceHandlerTest.java Thu Feb 17 20:26:36 2011
@@ -18,19 +18,14 @@
  */
 package org.apache.aries.transaction;
 
-import static org.junit.Assert.assertEquals;
-
-import java.net.URI;
-import java.net.URL;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
+import static org.junit.Assert.*;
 
 import org.apache.aries.blueprint.ComponentDefinitionRegistry;
-import org.apache.aries.blueprint.container.Parser;
-import org.apache.aries.blueprint.container.NamespaceHandlerRegistry.NamespaceHandlerSet;
-import org.apache.aries.blueprint.namespace.ComponentDefinitionRegistryImpl;
+import org.apache.aries.transaction.parsing.TxBlueprintListener;
+import org.apache.aries.unittest.mocks.Skeleton;
 import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.service.blueprint.container.BlueprintEvent;
 import org.osgi.service.blueprint.reflect.BeanMetadata;
 
 public class NameSpaceHandlerTest extends BaseNameSpaceHandlerSetup {
@@ -38,19 +33,7 @@ public class NameSpaceHandlerTest extend
     @Test
     public void testMultipleElements_100() throws Exception
     {
-      Parser p = new Parser();
-      
-      URL bpxml = this.getClass().getResource("aries.xml");
-      List<URL> bpxmlList = new LinkedList<URL>();
-      bpxmlList.add(bpxml); 
-      
-      p.parse(bpxmlList);
-      Set<URI> nsuris = p.getNamespaces();
-      NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
-      p.validate(nshandlers.getSchema());
-      
-      ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
-      p.populate(nshandlers, cdr);
+      ComponentDefinitionRegistry cdr = parseCDR("aries.xml");
       
       BeanMetadata comp = (BeanMetadata) cdr.getComponentDefinition("top");
       
@@ -65,20 +48,8 @@ public class NameSpaceHandlerTest extend
     @Test
     public void testMultipleElements_110() throws Exception
     {
-      Parser p = new Parser();
-      
-      URL bpxml = this.getClass().getResource("aries4.xml");
-      List<URL> bpxmlList = new LinkedList<URL>();
-      bpxmlList.add(bpxml); 
-      
-      p.parse(bpxmlList);
-      Set<URI> nsuris = p.getNamespaces();
-      NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
-      p.validate(nshandlers.getSchema());
-      
-      ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
-      p.populate(nshandlers, cdr);
-      
+      ComponentDefinitionRegistry cdr = parseCDR("aries4.xml");
+        
       BeanMetadata comp = (BeanMetadata) cdr.getComponentDefinition("top");
       
       BeanMetadata anon = (BeanMetadata) (comp.getProperties().get(0)).getValue();
@@ -92,19 +63,7 @@ public class NameSpaceHandlerTest extend
     @Test
     public void testOptionalMethodAttribute_100() throws Exception
     {
-      Parser p = new Parser();
-      
-      URL bpxml = this.getClass().getResource("aries2.xml");
-      List<URL> bpxmlList = new LinkedList<URL>();
-      bpxmlList.add(bpxml); 
-      
-      p.parse(bpxmlList);
-      Set<URI> nsuris = p.getNamespaces();
-      NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
-      p.validate(nshandlers.getSchema());
-      
-      ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
-      p.populate(nshandlers, cdr);
+      ComponentDefinitionRegistry cdr = parseCDR("aries2.xml");
       
       BeanMetadata comp = (BeanMetadata) cdr.getComponentDefinition("top");
       
@@ -119,19 +78,7 @@ public class NameSpaceHandlerTest extend
     @Test
     public void testOptionalMethodAttribute_110() throws Exception
     {
-      Parser p = new Parser();
-      
-      URL bpxml = this.getClass().getResource("aries5.xml");
-      List<URL> bpxmlList = new LinkedList<URL>();
-      bpxmlList.add(bpxml); 
-      
-      p.parse(bpxmlList);
-      Set<URI> nsuris = p.getNamespaces();
-      NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
-      p.validate(nshandlers.getSchema());
-      
-      ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
-      p.populate(nshandlers, cdr);
+      ComponentDefinitionRegistry cdr = parseCDR("aries5.xml");
       
       BeanMetadata comp = (BeanMetadata) cdr.getComponentDefinition("top");
       
@@ -146,19 +93,7 @@ public class NameSpaceHandlerTest extend
     @Test
     public void testOptionalValueAttribute_100() throws Exception
     {
-      Parser p = new Parser();
-      
-      URL bpxml = this.getClass().getResource("aries3.xml");
-      List<URL> bpxmlList = new LinkedList<URL>();
-      bpxmlList.add(bpxml); 
-      
-      p.parse(bpxmlList);
-      Set<URI> nsuris = p.getNamespaces();
-      NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
-      p.validate(nshandlers.getSchema());
-      
-      ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
-      p.populate(nshandlers, cdr);
+      ComponentDefinitionRegistry cdr = parseCDR("aries3.xml");
       
       BeanMetadata comp = (BeanMetadata) cdr.getComponentDefinition("top");
       
@@ -173,19 +108,7 @@ public class NameSpaceHandlerTest extend
     @Test
     public void testOptionalValueAttribute_110() throws Exception
     {
-      Parser p = new Parser();
-      
-      URL bpxml = this.getClass().getResource("aries6.xml");
-      List<URL> bpxmlList = new LinkedList<URL>();
-      bpxmlList.add(bpxml); 
-      
-      p.parse(bpxmlList);
-      Set<URI> nsuris = p.getNamespaces();
-      NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
-      p.validate(nshandlers.getSchema());
-      
-      ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
-      p.populate(nshandlers, cdr);
+      ComponentDefinitionRegistry cdr = parseCDR("aries6.xml");
       
       BeanMetadata comp = (BeanMetadata) cdr.getComponentDefinition("top");
       
@@ -200,19 +123,7 @@ public class NameSpaceHandlerTest extend
     @Test
     public void testBundleWideAndBeanLevelTx() throws Exception
     {
-      Parser p = new Parser();
-      
-      URL bpxml = this.getClass().getResource("mixed-aries.xml");
-      List<URL> bpxmlList = new LinkedList<URL>();
-      bpxmlList.add(bpxml); 
-      
-      p.parse(bpxmlList);
-      Set<URI> nsuris = p.getNamespaces();
-      NamespaceHandlerSet nshandlers = nhri.getNamespaceHandlers(nsuris, b);
-      p.validate(nshandlers.getSchema());
-      
-      ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
-      p.populate(nshandlers, cdr);
+      ComponentDefinitionRegistry cdr = parseCDR("mixed-aries.xml");
       
       BeanMetadata compRequiresNew = (BeanMetadata) cdr.getComponentDefinition("requiresNew");
       BeanMetadata compNoTx = (BeanMetadata) cdr.getComponentDefinition("noTx");
@@ -224,7 +135,38 @@ public class NameSpaceHandlerTest extend
       assertEquals("Required", txenhancer.getComponentMethodTxAttribute(compSomeTx, "doSomething"));
       assertEquals("Mandatory", txenhancer.getComponentMethodTxAttribute(compSomeTx, "getRows"));
       assertEquals("Required", txenhancer.getComponentMethodTxAttribute(compAnotherBean, "doSomething"));
-      assertEquals("Supports", txenhancer.getComponentMethodTxAttribute(compAnotherBean, "getWhatever"));
+      assertEquals("Supports", txenhancer.getComponentMethodTxAttribute(compAnotherBean, "getWhatever"));       
+    }
+    
+    @Test
+    public void testLifecycle() throws Exception
+    {
+        ComponentDefinitionRegistry cdr = parseCDR("mixed-aries.xml");
+        
+        BeanMetadata compRequiresNew = (BeanMetadata) cdr.getComponentDefinition("requiresNew");
+        BeanMetadata compNoTx = (BeanMetadata) cdr.getComponentDefinition("noTx");
+        BeanMetadata compSomeTx = (BeanMetadata) cdr.getComponentDefinition("someTx");
+        BeanMetadata compAnotherBean = (BeanMetadata) cdr.getComponentDefinition("anotherBean");
+
+        assertEquals("RequiresNew", txenhancer.getComponentMethodTxAttribute(compRequiresNew, "doSomething"));
+        assertEquals("Never", txenhancer.getComponentMethodTxAttribute(compNoTx, "doSomething"));
+        assertEquals("Required", txenhancer.getComponentMethodTxAttribute(compSomeTx, "doSomething"));
+        assertEquals("Mandatory", txenhancer.getComponentMethodTxAttribute(compSomeTx, "getRows"));
+        assertEquals("Required", txenhancer.getComponentMethodTxAttribute(compAnotherBean, "doSomething"));
+        assertEquals("Supports", txenhancer.getComponentMethodTxAttribute(compAnotherBean, "getWhatever"));   
+        
+        // cleanup
+        
+        assertTrue(namespaceHandler.isRegistered(cdr));
+        
+        new TxBlueprintListener(namespaceHandler, txenhancer).blueprintEvent(
+                new BlueprintEvent(BlueprintEvent.DESTROYED, b, Skeleton.newMock(Bundle.class)));
         
+        assertFalse(namespaceHandler.isRegistered(cdr));
+        assertNull(txenhancer.getComponentMethodTxAttribute(compRequiresNew, "doSomething"));
+        assertNull(txenhancer.getComponentMethodTxAttribute(compSomeTx, "doSomething"));
+        assertNull(txenhancer.getComponentMethodTxAttribute(compSomeTx, "getRows"));
+        assertNull(txenhancer.getComponentMethodTxAttribute(compAnotherBean, "doSomething"));
+        assertNull(txenhancer.getComponentMethodTxAttribute(compAnotherBean, "getWhatever"));   
     }
 }