diff --git a/httpclient5-osgi/pom.xml b/httpclient5-osgi/pom.xml index 5980e134b..d881eb6c4 100644 --- a/httpclient5-osgi/pom.xml +++ b/httpclient5-osgi/pom.xml @@ -215,13 +215,6 @@ <_removeheaders>JAVA_1_3_HOME,JAVA_1_4_HOME - - - org.codehaus.mojo - clirr-maven-plugin - - true - org.apache.maven.plugins diff --git a/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java index a28f9def8..4e60b85d7 100644 --- a/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java +++ b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java @@ -31,7 +31,6 @@ import java.io.IOException; import java.util.Dictionary; import java.util.Hashtable; import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -62,7 +61,11 @@ public final class HttpProxyConfigurationActivator implements BundleActivator, M private final Map registeredConfigurations = new LinkedHashMap<>(); - private final List trackedHttpClients = new LinkedList<>(); + private final List trackedHttpClients; + + public HttpProxyConfigurationActivator() { + trackedHttpClients = new WeakList(); + } /** * {@inheritDoc} diff --git a/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java index e5fa83f12..824ab750d 100644 --- a/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java +++ b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java @@ -26,6 +26,7 @@ */ package org.apache.hc.client5.http.osgi.impl; +import java.util.Collection; import java.util.List; import java.util.Map; @@ -39,7 +40,7 @@ import org.osgi.framework.ServiceRegistration; */ final class OSGiHttpClientBuilder extends HttpClientBuilder { - private final List trackedHttpClients; + private final Collection trackedHttpClients; public OSGiHttpClientBuilder( final BundleContext bundleContext, @@ -55,7 +56,9 @@ final class OSGiHttpClientBuilder extends HttpClientBuilder { @Override public CloseableHttpClient build() { final CloseableHttpClient httpClient = super.build(); - trackedHttpClients.add(httpClient); + synchronized (trackedHttpClients) { + trackedHttpClients.add(httpClient); + } return httpClient; } diff --git a/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/WeakList.java b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/WeakList.java new file mode 100644 index 000000000..96b64fab6 --- /dev/null +++ b/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/WeakList.java @@ -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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.hc.client5.http.osgi.impl; + +import java.lang.ref.WeakReference; +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.NoSuchElementException; + +/** + * Implementation of a list backed by WeakReference objects. + * This is not a general purpose list and is only meant to be used by this package. It cannot correctly manage null entries by design. + */ +class WeakList extends AbstractList { + + private final List> innerList; + + public WeakList() { + this.innerList = new ArrayList>(); + } + + @Override + public T get(final int index) { + return innerList.get(index).get(); + } + + @Override + public int size() { + checkReferences(); + return innerList.size(); + } + + @Override + public boolean add(final T t) { + return innerList.add(new WeakReference(t)); + } + + private void checkReferences() { + final ListIterator> references = innerList.listIterator(); + while (references.hasNext()) { + final WeakReference reference = references.next(); + if (reference.get() == null) { + references.remove(); + } + } + } + + @Override + public Iterator iterator() { + return new WeakIterator(innerList.iterator()); + } + + private class WeakIterator implements Iterator { + + private final Iterator> innerIterator; + + private WeakReference next; + + public WeakIterator(final Iterator> innerIterator) { + this.innerIterator = innerIterator; + fetchNext(); + } + + public boolean hasNext() { + return next != null; + } + + public T next() { + if (next != null) { + final T result = next.get(); + fetchNext(); + return result; + } else { + throw new NoSuchElementException(); + } + } + + private void fetchNext() { + while (innerIterator.hasNext()) { + final WeakReference ref = innerIterator.next(); + final T obj = ref.get(); + if (obj != null) { + next = ref; + return; + } + } + next = null; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + } +} diff --git a/httpclient5-osgi/src/test/java/org/apache/hc/client5/http/osgi/impl/WeakListTest.java b/httpclient5-osgi/src/test/java/org/apache/hc/client5/http/osgi/impl/WeakListTest.java new file mode 100644 index 000000000..db19522fa --- /dev/null +++ b/httpclient5-osgi/src/test/java/org/apache/hc/client5/http/osgi/impl/WeakListTest.java @@ -0,0 +1,62 @@ +/* + * ==================================================================== + * 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.hc.client5.http.osgi.impl; + +import org.junit.Test; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertEquals; + +public class WeakListTest { + + @Test + public void testWeakList() { + final WeakList list = new WeakList(); + list.add("hello"); + list.add(null); + + // null objects are seen as GC'd, so we only expect a size of 1 + assertEquals(1, list.size()); + + final Iterator it = list.iterator(); + assertTrue(it.hasNext()); + assertEquals("hello", it.next()); + assertFalse(it.hasNext()); + boolean thrown = false; + try { + it.next(); + } catch (NoSuchElementException e) { + thrown = true; + } + assertTrue(thrown); + } + +} \ No newline at end of file