Author: sseifert Date: Wed Oct 14 08:09:57 2015 New Revision: 1708556 URL: http://svn.apache.org/viewvc?rev=1708556&view=rev Log: SLING-5143 osgi-mock: MockBundleContext is not thread-safe when using iterators
Added: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java (with props) Modified: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java Added: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java?rev=1708556&view=auto ============================================================================== --- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java (added) +++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java Wed Oct 14 08:09:57 2015 @@ -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; + } + +} Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java ------------------------------------------------------------------------------ --- svn:keywords (added) +++ svn:keywords Wed Oct 14 08:09:57 2015 @@ -0,0 +1 @@ +LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java?rev=1708556&r1=1708555&r2=1708556&view=diff ============================================================================== --- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java (original) +++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java Wed Oct 14 08:09:57 2015 @@ -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.Immutab 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 Bundl @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 Bundl @Override public void addServiceListener(final ServiceListener serviceListener) { - serviceListeners.put(serviceListener, null); + try { + addServiceListener(serviceListener, null); + } + catch (InvalidSyntaxException ex) { + throw new RuntimeException(ex); + } } @Override