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 2010/08/04 10:32:15 UTC

svn commit: r982158 - in /incubator/aries/trunk: blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/impl/ jpa/jpa-container-context/src/test/java/o...

Author: mahrwald
Date: Wed Aug  4 08:32:15 2010
New Revision: 982158

URL: http://svn.apache.org/viewvc?rev=982158&view=rev
Log:
ARIES-372: Commit fixes

Added:
    incubator/aries/trunk/jpa/jpa-container-context/src/test/java/org/apache/aries/jpa/container/context/impl/GlobalPersistenceManagerTest.java
Modified:
    incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintEventDispatcher.java
    incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintExtender.java
    incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/NamespaceHandlerRegistry.java
    incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ParserServiceImpl.java
    incubator/aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/impl/GlobalPersistenceManager.java

Modified: incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintEventDispatcher.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintEventDispatcher.java?rev=982158&r1=982157&r2=982158&view=diff
==============================================================================
--- incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintEventDispatcher.java (original)
+++ incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintEventDispatcher.java Wed Aug  4 08:32:15 2010
@@ -37,9 +37,7 @@ import java.util.concurrent.TimeoutExcep
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
 import org.osgi.framework.ServiceReference;
-import org.osgi.framework.SynchronousBundleListener;
 import org.osgi.service.blueprint.container.BlueprintEvent;
 import org.osgi.service.blueprint.container.BlueprintListener;
 import org.osgi.service.blueprint.container.EventConstants;
@@ -58,7 +56,7 @@ import org.apache.aries.blueprint.utils.
  *
  * @version $Rev$, $Date$
  */
-class BlueprintEventDispatcher implements BlueprintListener, SynchronousBundleListener {
+class BlueprintEventDispatcher implements BlueprintListener {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(BlueprintEventDispatcher.class);
 
@@ -76,8 +74,6 @@ class BlueprintEventDispatcher implement
 
         this.sharedExecutor = sharedExecutor;
 
-        bundleContext.addBundleListener(this);
-
         EventAdminListener listener = null;
         try {
             getClass().getClassLoader().loadClass("org.osgi.service.event.EventAdmin");
@@ -220,10 +216,8 @@ class BlueprintEventDispatcher implement
         }
     }
 
-    public void bundleChanged(BundleEvent event) {
-        if (BundleEvent.STOPPING == event.getType()) {
-            states.remove(event.getBundle());
-        }
+    public void removeBlueprintBundle(Bundle bundle) {
+        states.remove(bundle);
     }
 
     private static class EventAdminListener implements BlueprintListener {

Modified: incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintExtender.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintExtender.java?rev=982158&r1=982157&r2=982158&view=diff
==============================================================================
--- incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintExtender.java (original)
+++ incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BlueprintExtender.java Wed Aug  4 08:32:15 2010
@@ -203,6 +203,7 @@ public class BlueprintExtender implement
             LOGGER.debug("Destroying BlueprintContainer for bundle {}", bundle.getSymbolicName());
             blueprintContainer.destroy();
         }
+        eventDispatcher.removeBlueprintBundle(bundle);
     }
     
     private void checkBundle(Bundle bundle) {

Modified: incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/NamespaceHandlerRegistry.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/NamespaceHandlerRegistry.java?rev=982158&r1=982157&r2=982158&view=diff
==============================================================================
--- incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/NamespaceHandlerRegistry.java (original)
+++ incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/NamespaceHandlerRegistry.java Wed Aug  4 08:32:15 2010
@@ -26,7 +26,6 @@ import javax.xml.validation.Schema;
 
 import org.apache.aries.blueprint.NamespaceHandler;
 import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
 import org.xml.sax.SAXException;
 
 /**
@@ -37,7 +36,7 @@ import org.xml.sax.SAXException;
 public interface NamespaceHandlerRegistry {
 
     /**
-     * Retrieve the <code>NamespaceHandler</code> for the specified URI
+     * Retrieve the <code>NamespaceHandler</code> for the specified URI. Make sure
      *
      * @param uri the namespace identifying the namespace handler
      * @param bundle the blueprint bundle to be checked for class space consistency

Modified: incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ParserServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ParserServiceImpl.java?rev=982158&r1=982157&r2=982158&view=diff
==============================================================================
--- incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ParserServiceImpl.java (original)
+++ incubator/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ParserServiceImpl.java Wed Aug  4 08:32:15 2010
@@ -73,12 +73,18 @@ public class ParserServiceImpl implement
   private ComponentDefinitionRegistry validateAndPopulate (Parser parser, Bundle clientBundle, boolean validate) 
   throws IOException, SAXException { 
     Set<URI> nsuris = parser.getNamespaces();
+    ComponentDefinitionRegistry cdr;
     NamespaceHandlerSet nshandlers = _namespaceHandlerRegistry.getNamespaceHandlers(nsuris, clientBundle);
-    if (validate) { 
-      parser.validate( nshandlers.getSchema());
+    try {
+        if (validate) { 
+          parser.validate( nshandlers.getSchema());
+        }
+        cdr = new ComponentDefinitionRegistryImpl();
+        parser.populate(nshandlers, cdr);
+    } finally {
+        nshandlers.destroy();
     }
-    ComponentDefinitionRegistry cdr = new ComponentDefinitionRegistryImpl();
-    parser.populate(nshandlers, cdr);
+    
     return cdr;   
   }
 

Modified: incubator/aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/impl/GlobalPersistenceManager.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/impl/GlobalPersistenceManager.java?rev=982158&r1=982157&r2=982158&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/impl/GlobalPersistenceManager.java (original)
+++ incubator/aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/impl/GlobalPersistenceManager.java Wed Aug  4 08:32:15 2010
@@ -18,7 +18,6 @@
  */
 package org.apache.aries.jpa.container.context.impl;
 
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
@@ -117,7 +116,7 @@ public class GlobalPersistenceManager im
     
     //We only care about bundles stopping
     if (event.getType() == BundleEvent.STOPPING) {
-      Set<String> contextsToBeRemoved = Collections.emptySet();
+      Set<String> contextsToBeRemoved = new HashSet<String>();
       Bundle frameworkBundle = bundle.getBundleContext().getBundle(0);
       PersistenceContextManager manager = null;
       boolean removeManager = false;
@@ -149,6 +148,12 @@ public class GlobalPersistenceManager im
           removeManager = true;
           manager = managers.remove(bundle);
           bundle.getBundleContext().removeBundleListener(this);
+          
+          for (Bundle b : bundle.getBundleContext().getBundles()) {
+              if (persistenceContexts.containsKey(b)) {
+                contextsToBeRemoved.addAll(persistenceContexts.remove(b));
+              }
+            }
         }
       }
       

Added: incubator/aries/trunk/jpa/jpa-container-context/src/test/java/org/apache/aries/jpa/container/context/impl/GlobalPersistenceManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-container-context/src/test/java/org/apache/aries/jpa/container/context/impl/GlobalPersistenceManagerTest.java?rev=982158&view=auto
==============================================================================
--- incubator/aries/trunk/jpa/jpa-container-context/src/test/java/org/apache/aries/jpa/container/context/impl/GlobalPersistenceManagerTest.java (added)
+++ incubator/aries/trunk/jpa/jpa-container-context/src/test/java/org/apache/aries/jpa/container/context/impl/GlobalPersistenceManagerTest.java Wed Aug  4 08:32:15 2010
@@ -0,0 +1,122 @@
+/*
+ * 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.jpa.container.context.impl;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.aries.jpa.container.context.transaction.impl.JTAPersistenceContextRegistry;
+import org.apache.aries.mocks.BundleMock;
+import org.apache.aries.unittest.mocks.MethodCall;
+import org.apache.aries.unittest.mocks.Skeleton;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+
+import static org.junit.Assert.*;
+
+public class GlobalPersistenceManagerTest {
+    private GlobalPersistenceManager sut;
+    private Bundle client;
+    private Bundle otherClient;
+    private Bundle framework;
+    
+    @Before
+    public void setup()
+    {
+        framework = Skeleton.newMock(new BundleMock("framework", new Hashtable<Object, Object>()), Bundle.class);
+
+        BundleContext ctx = Skeleton.newMock(BundleContext.class);
+        Skeleton.getSkeleton(ctx).setReturnValue(new MethodCall(BundleContext.class, "getBundle", long.class), framework);
+        
+        client = Skeleton.newMock(Bundle.class);
+        Skeleton.getSkeleton(client).setReturnValue(new MethodCall(Bundle.class, "getBundleContext"), ctx);
+
+        otherClient = Skeleton.newMock(Bundle.class);
+        Skeleton.getSkeleton(otherClient).setReturnValue(new MethodCall(Bundle.class, "getBundleContext"), ctx);        
+        
+        sut = new GlobalPersistenceManager();
+        sut.setRegistry(new JTAPersistenceContextRegistry());
+        
+        Skeleton.getSkeleton(framework.getBundleContext()).setReturnValue(
+                new MethodCall(BundleContext.class, "getBundles"), new Bundle[] {framework, client, otherClient});
+    }
+    
+    @Test
+    public void testRegister() throws Exception {
+        sut.registerContext("name", client, new HashMap<String, Object>());
+        sut.registerContext("otherName", otherClient, new HashMap<String, Object>());
+        
+        assertEquals(1, getManagers().size());
+        assertEquals(new HashSet<String>(Arrays.asList("name")), getContexts().get(client));
+        assertEquals(new HashSet<String>(Arrays.asList("otherName")), getContexts().get(otherClient));
+        
+        sut.registerContext("name2", client, new HashMap<String, Object>());
+        assertEquals(new HashSet<String>(Arrays.asList("name", "name2")), getContexts().get(client));
+    }
+    
+    @Test
+    public void testStopFramework() throws Exception {
+        sut.registerContext("name", client, new HashMap<String, Object>());
+        sut.registerContext("otherName", otherClient, new HashMap<String, Object>());
+
+        sut.bundleChanged(new BundleEvent(BundleEvent.STOPPING, framework));
+        
+        assertTrue(getManagers().isEmpty());
+        assertTrue(getContexts().isEmpty());
+    }
+    
+    @Test
+    public void testIndividual() throws Exception {
+        sut.registerContext("name", client, new HashMap<String, Object>());
+        sut.registerContext("otherName", otherClient, new HashMap<String, Object>());
+        
+        sut.bundleChanged(new BundleEvent(BundleEvent.STOPPING, client));
+        assertEquals(1, getManagers().size());
+        assertNull(getContexts().get(client));
+        assertNotNull(getContexts().get(otherClient));
+        
+        sut.bundleChanged(new BundleEvent(BundleEvent.STOPPING, otherClient));
+        assertEquals(1, getManagers().size());
+        assertNull(getContexts().get(client));
+        assertNull(getContexts().get(otherClient));
+        
+        sut.bundleChanged(new BundleEvent(BundleEvent.STOPPING, framework));
+        assertTrue(getManagers().isEmpty());
+    }
+    
+    private Map<Bundle,PersistenceContextManager> getManagers() throws Exception {
+        Field f = GlobalPersistenceManager.class.getDeclaredField("managers");
+        f.setAccessible(true);
+        return (Map<Bundle,PersistenceContextManager>) f.get(sut);
+    }
+    
+    private Map<Bundle, Set<String>> getContexts() throws Exception {
+        Field f = GlobalPersistenceManager.class.getDeclaredField("persistenceContexts");
+        f.setAccessible(true);
+        return (Map<Bundle,Set<String>>) f.get(sut);        
+    }
+}