[DOSGI-229] Simplify TopologyManager
Project: http://git-wip-us.apache.org/repos/asf/aries-rsa/repo Commit: http://git-wip-us.apache.org/repos/asf/aries-rsa/commit/925a0ee6 Tree: http://git-wip-us.apache.org/repos/asf/aries-rsa/tree/925a0ee6 Diff: http://git-wip-us.apache.org/repos/asf/aries-rsa/diff/925a0ee6 Branch: refs/heads/master Commit: 925a0ee6de7ca57560363e872f76729a5883f7ac Parents: 97c2e47 Author: Christian Schneider <[email protected]> Authored: Wed Mar 9 14:13:23 2016 +0100 Committer: Christian Schneider <[email protected]> Committed: Wed Mar 9 14:13:23 2016 +0100 ---------------------------------------------------------------------- .../exporter/EndpointListenerNotifier.java | 17 ++++- .../importer/ListenerHookImpl.java | 4 +- .../importer/ReferenceCounter.java | 76 ++++++++++++++++++++ .../importer/TopologyManagerImport.java | 1 - .../topologymanager/util/ReferenceCounter.java | 76 -------------------- .../cxf/dosgi/topologymanager/util/Utils.java | 32 +-------- .../importer/ReferenceCounterTest.java | 42 +++++++++++ .../util/ReferenceCounterTest.java | 42 ----------- .../dosgi/topologymanager/util/UtilsTest.java | 54 -------------- 9 files changed, 138 insertions(+), 206 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/925a0ee6/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/EndpointListenerNotifier.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/EndpointListenerNotifier.java b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/EndpointListenerNotifier.java index f345744..17e9649 100644 --- a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/EndpointListenerNotifier.java +++ b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/exporter/EndpointListenerNotifier.java @@ -21,6 +21,7 @@ package org.apache.cxf.dosgi.topologymanager.exporter; import java.util.ArrayList; import java.util.Collection; import java.util.Dictionary; +import java.util.Hashtable; import java.util.List; import org.apache.cxf.dosgi.topologymanager.util.SimpleServiceTracker; @@ -129,6 +130,20 @@ public class EndpointListenerNotifier { } } + /** + * Retrieves an endpoint's properties as a Dictionary. + * + * @param endpoint an endpoint description + * @return endpoint properties (will never return null) + */ + public static Dictionary<String, Object> getEndpointProperties(EndpointDescription endpoint) { + if (endpoint == null || endpoint.getProperties() == null) { + return new Hashtable<String, Object>(); + } else { + return new Hashtable<String, Object>(endpoint.getProperties()); + } + } + static List<Filter> getFiltersFromEndpointListenerScope(ServiceReference sref, BundleContext bctx) { List<Filter> filters = new ArrayList<Filter>(); String[] scopes = Utils.getStringPlusProperty(sref.getProperty(EndpointListener.ENDPOINT_LISTENER_SCOPE)); @@ -144,7 +159,7 @@ public class EndpointListenerNotifier { private static List<Filter> getMatchingFilters(List<Filter> filters, EndpointDescription endpoint) { List<Filter> matchingFilters = new ArrayList<Filter>(); - Dictionary<String, Object> dict = Utils.getEndpointProperties(endpoint); + Dictionary<String, Object> dict = EndpointListenerNotifier.getEndpointProperties(endpoint); for (Filter filter : filters) { if (filter.match(dict)) { http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/925a0ee6/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ListenerHookImpl.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ListenerHookImpl.java b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ListenerHookImpl.java index 0ab5e39..8b1b4bb 100644 --- a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ListenerHookImpl.java +++ b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ListenerHookImpl.java @@ -24,6 +24,7 @@ import java.util.Set; import org.apache.cxf.dosgi.topologymanager.util.Utils; import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; import org.osgi.framework.hooks.service.ListenerHook; import org.osgi.service.remoteserviceadmin.RemoteConstants; import org.slf4j.Logger; @@ -114,6 +115,7 @@ public class ListenerHookImpl implements ListenerHook { } static String extendFilter(String filter, BundleContext bctx) { - return "(&" + filter + "(!(" + RemoteConstants.ENDPOINT_FRAMEWORK_UUID + "=" + Utils.getUUID(bctx) + ")))"; + String uuid = bctx.getProperty(Constants.FRAMEWORK_UUID); + return "(&" + filter + "(!(" + RemoteConstants.ENDPOINT_FRAMEWORK_UUID + "=" + uuid + ")))"; } } http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/925a0ee6/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounter.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounter.java b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounter.java new file mode 100644 index 0000000..6ecff31 --- /dev/null +++ b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounter.java @@ -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; + } + } + } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/925a0ee6/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java index 0ab7ab6..faadade 100644 --- a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java +++ b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/importer/TopologyManagerImport.java @@ -30,7 +30,6 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import org.apache.cxf.dosgi.topologymanager.util.ReferenceCounter; import org.apache.cxf.dosgi.topologymanager.util.SimpleServiceTracker; import org.apache.cxf.dosgi.topologymanager.util.SimpleServiceTrackerListener; import org.osgi.framework.BundleContext; http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/925a0ee6/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/ReferenceCounter.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/ReferenceCounter.java b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/ReferenceCounter.java deleted file mode 100644 index 04d2e2c..0000000 --- a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/ReferenceCounter.java +++ /dev/null @@ -1,76 +0,0 @@ -/** - * 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.util; - -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; - } - } - } -} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/925a0ee6/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/Utils.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/Utils.java b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/Utils.java index 7140cd1..021cc55 100644 --- a/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/Utils.java +++ b/dsw/cxf-topology-manager/src/main/java/org/apache/cxf/dosgi/topologymanager/util/Utils.java @@ -19,17 +19,12 @@ package org.apache.cxf.dosgi.topologymanager.util; import java.util.Collection; -import java.util.Dictionary; -import java.util.Hashtable; -import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import org.osgi.framework.ServiceReference; -import org.osgi.service.remoteserviceadmin.EndpointDescription; public final class Utils { @@ -40,20 +35,6 @@ public final class Utils { // prevent instantiation } - /** - * Retrieves an endpoint's properties as a Dictionary. - * - * @param endpoint an endpoint description - * @return endpoint properties (will never return null) - */ - public static Dictionary<String, Object> getEndpointProperties(EndpointDescription endpoint) { - if (endpoint == null || endpoint.getProperties() == null) { - return new Hashtable<String, Object>(); - } else { - return new Hashtable<String, Object>(endpoint.getProperties()); - } - } - public static String getObjectClass(String filter) { if (filter != null) { Matcher matcher = OBJECTCLASS_PATTERN.matcher(filter); @@ -93,18 +74,7 @@ public final class Utils { return new String[0]; } - public static String getUUID(BundleContext bctx) { - synchronized ("org.osgi.framework.uuid") { - String uuid = bctx.getProperty("org.osgi.framework.uuid"); - if (uuid == null) { - uuid = UUID.randomUUID().toString(); - System.setProperty("org.osgi.framework.uuid", uuid); - } - return uuid; - } - } - - public static String getBundleName(ServiceReference sref) { + public static String getBundleName(ServiceReference<?> sref) { Bundle bundle = sref.getBundle(); return bundle == null ? "<unregistered>" : bundle.getSymbolicName(); } http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/925a0ee6/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounterTest.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounterTest.java b/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounterTest.java new file mode 100644 index 0000000..3ab78db --- /dev/null +++ b/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/importer/ReferenceCounterTest.java @@ -0,0 +1,42 @@ +/** + * 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 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")); + } +} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/925a0ee6/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/ReferenceCounterTest.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/ReferenceCounterTest.java b/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/ReferenceCounterTest.java deleted file mode 100644 index 8cdbb9b..0000000 --- a/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/ReferenceCounterTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * 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.util; - -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")); - } -} http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/925a0ee6/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/UtilsTest.java ---------------------------------------------------------------------- diff --git a/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/UtilsTest.java b/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/UtilsTest.java deleted file mode 100644 index 95b5dde..0000000 --- a/dsw/cxf-topology-manager/src/test/java/org/apache/cxf/dosgi/topologymanager/util/UtilsTest.java +++ /dev/null @@ -1,54 +0,0 @@ -/** - * 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.util; - -import org.easymock.classextension.EasyMock; -import org.junit.Test; -import org.osgi.framework.BundleContext; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -public class UtilsTest { - - @Test - public void testGetNewUUID() { - BundleContext bc = EasyMock.createNiceMock(BundleContext.class); - EasyMock.expect(bc.getProperty(EasyMock.eq("org.osgi.framework.uuid"))).andReturn(null).atLeastOnce(); - EasyMock.replay(bc); - String uuid = Utils.getUUID(bc); - assertNotNull(uuid); - - assertEquals(System.getProperty("org.osgi.framework.uuid"), uuid); - - EasyMock.verify(bc); - } - - @Test - public void testGetExistingUUID() { - BundleContext bc = EasyMock.createNiceMock(BundleContext.class); - EasyMock.expect(bc.getProperty(EasyMock.eq("org.osgi.framework.uuid"))).andReturn("MyUUID").atLeastOnce(); - EasyMock.replay(bc); - String uuid = Utils.getUUID(bc); - - assertEquals("MyUUID", uuid); - - EasyMock.verify(bc); - } -}
