cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amic...@apache.org
Subject svn commit: r1486353 - in /cxf/dosgi/trunk/dsw/cxf-topology-manager/src: main/java/org/apache/cxf/dosgi/topologymanager/importer/ test/java/org/apache/cxf/dosgi/topologymanager/importer/
Date Sat, 25 May 2013 20:07:18 GMT
Author: amichai
Date: Sat May 25 20:07:18 2013
New Revision: 1486353

URL: http://svn.apache.org/r1486353
Log:
DOSGI-175 Fix TopologyManagerImport's reference counter not counting

Added:
    cxf/dosgi/trunk/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounter.java
    cxf/dosgi/trunk/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounterTest.java
Removed:
    cxf/dosgi/trunk/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/RefManager.java
Modified:
    cxf/dosgi/trunk/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java

Added: cxf/dosgi/trunk/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounter.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounter.java?rev=1486353&view=auto
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounter.java
(added)
+++ cxf/dosgi/trunk/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounter.java
Sat May 25 20:07:18 2013
@@ -0,0 +1,76 @@
+/**
+ * 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.cxf.dosgi.topologymanager.importer;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * Manages a reference count per key.
+ *
+ * @param <K> the key type
+ */
+public class ReferenceCounter<K> {
+
+    private final ConcurrentMap<K, Integer> counts = new ConcurrentHashMap<K, Integer>();
+
+    /**
+     * Increases the reference count for the given key,
+     * or sets it to 1 if the key has no existing count.
+     *
+     * @param key a key
+     * @return the updated reference count
+     */
+    public int add(K key) {
+        while (true) {
+            Integer count = counts.get(key);
+            if (count == null) {
+                if (counts.putIfAbsent(key, 1) == null) {
+                    return 1;
+                }
+            } else if (counts.replace(key, count, count + 1)) {
+                return count + 1;
+            }
+        }
+    }
+
+    /**
+     * Decreases the reference count for the given key,
+     * and removes it if it reaches 0.
+     * If the key has no existing count, -1 is returned.
+     *
+     * @param key a key
+     * @return the updated reference count, or -1 if the key has no existing count
+     */
+    public int remove(K key) {
+        while (true) {
+            Integer count = counts.get(key);
+            if (count == null) {
+                return -1;
+            }
+            if (count == 1) {
+                if (counts.remove(key, 1)) {
+                    return 0;
+                }
+            } else if (counts.replace(key, count, count - 1)) {
+                return count - 1;
+            }
+        }
+    }
+}

Modified: cxf/dosgi/trunk/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java?rev=1486353&r1=1486352&r2=1486353&view=diff
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java
(original)
+++ cxf/dosgi/trunk/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java
Sat May 25 20:07:18 2013
@@ -74,7 +74,7 @@ public class TopologyManagerImport imple
      * counter. If an interest is removed, the related ServiceInterest object is used to
reduce the reference
      * counter until it reaches zero. in this case the interest is removed.
      */
-    private final RefManager importInterests = new RefManager();
+    private final ReferenceCounter<String> importInterestsCounter = new ReferenceCounter<String>();
 
     /**
      * List of Endpoints by matched filter that were reported by the EndpointListener and
can be imported
@@ -118,7 +118,7 @@ public class TopologyManagerImport imple
      * @see org.apache.cxf.dosgi.topologymanager.ServiceInterestListener#addServiceInterest(java.lang.String)
      */
     public void addServiceInterest(String filter) {
-        if (importInterests.addReference(filter) == 1) {
+        if (importInterestsCounter.add(filter) == 1) {
             endpointListenerManager.extendScope(filter);
         }
     }
@@ -127,8 +127,8 @@ public class TopologyManagerImport imple
      * @see org.apache.cxf.dosgi.topologymanager.ServiceInterestListener#removeServiceInterest(java.lang.String)
      */
     public void removeServiceInterest(String filter) {
-        if (importInterests.removeReference(filter) <= 0) {
-            LOG.debug("last reference to import interest is gone -> removing interest
 filter: {}", filter);
+        if (importInterestsCounter.remove(filter) == 0) {
+            LOG.debug("last reference to import interest is gone -> removing interest
filter: {}", filter);
             endpointListenerManager.reduceScope(filter);
             List<ImportRegistration> irs = importedServices.remove(filter);
             if (irs != null) {

Added: cxf/dosgi/trunk/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounterTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounterTest.java?rev=1486353&view=auto
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounterTest.java
(added)
+++ cxf/dosgi/trunk/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounterTest.java
Sat May 25 20:07:18 2013
@@ -0,0 +1,24 @@
+package org.apache.cxf.dosgi.topologymanager.importer;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class ReferenceCounterTest {
+
+    @Test
+    public void testCounter() {
+        ReferenceCounter<String> counter = new ReferenceCounter<String>();
+        assertEquals(-1, counter.remove("a"));
+        assertEquals(-1, counter.remove("a"));
+        assertEquals(1, counter.add("a"));
+        assertEquals(2, counter.add("a"));
+        assertEquals(3, counter.add("a"));
+        assertEquals(2, counter.remove("a"));
+        assertEquals(1, counter.remove("a"));
+        assertEquals(2, counter.add("a"));
+        assertEquals(1, counter.remove("a"));
+        assertEquals(0, counter.remove("a"));
+        assertEquals(-1, counter.remove("a"));
+    }
+}



Mime
View raw message