You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:19:57 UTC

[sling-org-apache-sling-testing-osgi-mock] 02/05: SLING-5143 osgi-mock: MockBundleContext is not thread-safe when using iterators

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.testing.osgi-mock-1.7.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit 5bdecdf3e1780b928c5463563cea98556fcb584e
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Wed Oct 14 08:09:57 2015 +0000

    SLING-5143 osgi-mock: MockBundleContext is not thread-safe when using iterators
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1708556 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/testing/mock/osgi/MatchAllFilter.java    | 46 ++++++++++++++++++++++
 .../sling/testing/mock/osgi/MockBundleContext.java | 30 ++++++++------
 2 files changed, 65 insertions(+), 11 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java b/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java
new file mode 100644
index 0000000..0a7aaf3
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java
@@ -0,0 +1,46 @@
+/*
+ * 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.sling.testing.mock.osgi;
+
+import java.util.Dictionary;
+
+import org.osgi.framework.Filter;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * OSGi filter that matches all.
+ */
+class MatchAllFilter implements Filter {
+
+    @Override
+    public boolean match(ServiceReference reference) {
+        return true;
+    }
+
+    @Override
+    public boolean match(Dictionary dictionary) {
+        return true;
+    }
+
+    @Override
+    public boolean matchCase(Dictionary dictionary) {
+        return true;
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
index 605fb82..5342e7f 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
@@ -18,21 +18,19 @@
  */
 package org.apache.sling.testing.mock.osgi;
 
-import static java.util.Collections.synchronizedList;
-import static java.util.Collections.synchronizedMap;
-import static java.util.Collections.synchronizedSortedSet;
-
 import java.io.File;
 import java.io.InputStream;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Dictionary;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Queue;
 import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ConcurrentSkipListSet;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.felix.framework.FilterImpl;
@@ -60,9 +58,9 @@ import com.google.common.collect.ImmutableList;
 class MockBundleContext implements BundleContext {
 
     private final MockBundle bundle;
-    private final SortedSet<MockServiceRegistration> registeredServices = synchronizedSortedSet(new TreeSet<MockServiceRegistration>());
-    private final Map<ServiceListener, Filter> serviceListeners = synchronizedMap(new HashMap<ServiceListener, Filter>());
-    private final List<BundleListener> bundleListeners = synchronizedList(new ArrayList<BundleListener>());
+    private final SortedSet<MockServiceRegistration> registeredServices = new ConcurrentSkipListSet<MockServiceRegistration>();
+    private final Map<ServiceListener, Filter> serviceListeners = new ConcurrentHashMap<ServiceListener, Filter>();
+    private final Queue<BundleListener> bundleListeners = new ConcurrentLinkedQueue<BundleListener>();
 
     public MockBundleContext() {
         this.bundle = new MockBundle(this);
@@ -75,7 +73,12 @@ class MockBundleContext implements BundleContext {
 
     @Override
     public Filter createFilter(final String s) throws InvalidSyntaxException {
-        return new FilterImpl(s);
+        if (s == null) {
+            return new MatchAllFilter();
+        }
+        else {
+            return new FilterImpl(s);
+        }
     }
 
     @Override
@@ -221,7 +224,12 @@ class MockBundleContext implements BundleContext {
 
     @Override
     public void addServiceListener(final ServiceListener serviceListener) {
-        serviceListeners.put(serviceListener, null);
+        try {
+            addServiceListener(serviceListener, null);
+        }
+        catch (InvalidSyntaxException ex) {
+            throw new RuntimeException(ex);
+        }
     }
 
     @Override

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.