You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2016/06/11 15:55:15 UTC
svn commit: r1747905 - in /httpcomponents/httpclient/trunk/httpclient5-osgi:
./ src/main/java/org/apache/hc/client5/http/osgi/impl/
src/test/java/org/apache/hc/client5/http/osgi/impl/
Author: olegk
Date: Sat Jun 11 15:55:15 2016
New Revision: 1747905
URL: http://svn.apache.org/viewvc?rev=1747905&view=rev
Log:
HTTPCLIENT-1749: OSGi client builder to use weak references to track HttpClient instances
Contributed by Justin Edelson <justin at justinedelson.com>
Added:
httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/WeakList.java (with props)
httpcomponents/httpclient/trunk/httpclient5-osgi/src/test/java/org/apache/hc/client5/http/osgi/impl/WeakListTest.java
- copied, changed from r1746751, httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java
Modified:
httpcomponents/httpclient/trunk/httpclient5-osgi/pom.xml
httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java
httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java
Modified: httpcomponents/httpclient/trunk/httpclient5-osgi/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-osgi/pom.xml?rev=1747905&r1=1747904&r2=1747905&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5-osgi/pom.xml (original)
+++ httpcomponents/httpclient/trunk/httpclient5-osgi/pom.xml Sat Jun 11 15:55:15 2016
@@ -216,13 +216,6 @@
</instructions>
</configuration>
</plugin>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>clirr-maven-plugin</artifactId>
- <configuration>
- <skip>true</skip>
- </configuration>
- </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
Modified: httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java?rev=1747905&r1=1747904&r2=1747905&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java (original)
+++ httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java Sat Jun 11 15:55:15 2016
@@ -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 HttpProxyConfiguratio
private final Map<String, ServiceRegistration> registeredConfigurations = new LinkedHashMap<>();
- private final List<CloseableHttpClient> trackedHttpClients = new LinkedList<>();
+ private final List<CloseableHttpClient> trackedHttpClients;
+
+ public HttpProxyConfigurationActivator() {
+ trackedHttpClients = new WeakList<CloseableHttpClient>();
+ }
/**
* {@inheritDoc}
Modified: httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java?rev=1747905&r1=1747904&r2=1747905&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java (original)
+++ httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java Sat Jun 11 15:55:15 2016
@@ -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.ServiceRegistr
*/
final class OSGiHttpClientBuilder extends HttpClientBuilder {
- private final List<CloseableHttpClient> trackedHttpClients;
+ private final Collection<CloseableHttpClient> trackedHttpClients;
public OSGiHttpClientBuilder(
final BundleContext bundleContext,
@@ -55,7 +56,9 @@ final class OSGiHttpClientBuilder extend
@Override
public CloseableHttpClient build() {
final CloseableHttpClient httpClient = super.build();
- trackedHttpClients.add(httpClient);
+ synchronized (trackedHttpClients) {
+ trackedHttpClients.add(httpClient);
+ }
return httpClient;
}
Added: httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/WeakList.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/WeakList.java?rev=1747905&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/WeakList.java (added)
+++ httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/WeakList.java Sat Jun 11 15:55:15 2016
@@ -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
+ * <http://www.apache.org/>.
+ *
+ */
+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<T> extends AbstractList<T> {
+
+ private final List<WeakReference<T>> innerList;
+
+ public WeakList() {
+ this.innerList = new ArrayList<WeakReference<T>>();
+ }
+
+ @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>(t));
+ }
+
+ private void checkReferences() {
+ final ListIterator<WeakReference<T>> references = innerList.listIterator();
+ while (references.hasNext()) {
+ final WeakReference<T> reference = references.next();
+ if (reference.get() == null) {
+ references.remove();
+ }
+ }
+ }
+
+ @Override
+ public Iterator<T> iterator() {
+ return new WeakIterator<T>(innerList.iterator());
+ }
+
+ private class WeakIterator<T> implements Iterator<T> {
+
+ private final Iterator<WeakReference<T>> innerIterator;
+
+ private WeakReference<T> next;
+
+ public WeakIterator(final Iterator<WeakReference<T>> 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<T> ref = innerIterator.next();
+ final T obj = ref.get();
+ if (obj != null) {
+ next = ref;
+ return;
+ }
+ }
+ next = null;
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ }
+}
Propchange: httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/WeakList.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/WeakList.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/WeakList.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: httpcomponents/httpclient/trunk/httpclient5-osgi/src/test/java/org/apache/hc/client5/http/osgi/impl/WeakListTest.java (from r1746751, httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-osgi/src/test/java/org/apache/hc/client5/http/osgi/impl/WeakListTest.java?p2=httpcomponents/httpclient/trunk/httpclient5-osgi/src/test/java/org/apache/hc/client5/http/osgi/impl/WeakListTest.java&p1=httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java&r1=1746751&r2=1747905&rev=1747905&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiHttpClientBuilder.java (original)
+++ httpcomponents/httpclient/trunk/httpclient5-osgi/src/test/java/org/apache/hc/client5/http/osgi/impl/WeakListTest.java Sat Jun 11 15:55:15 2016
@@ -26,37 +26,37 @@
*/
package org.apache.hc.client5.http.osgi.impl;
-import java.util.List;
-import java.util.Map;
+import org.junit.Test;
-import org.apache.hc.client5.http.impl.sync.CloseableHttpClient;
-import org.apache.hc.client5.http.impl.sync.HttpClientBuilder;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceRegistration;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
-/**
- * @since 4.3
- */
-final class OSGiHttpClientBuilder extends HttpClientBuilder {
-
- private final List<CloseableHttpClient> trackedHttpClients;
-
- public OSGiHttpClientBuilder(
- final BundleContext bundleContext,
- final Map<String, ServiceRegistration> registeredConfigurations,
- final List<CloseableHttpClient> trackedHttpClients) {
- this.trackedHttpClients = trackedHttpClients;
- setDefaultCredentialsProvider(
- new OSGiCredentialsProvider(bundleContext, registeredConfigurations));
- setRoutePlanner(
- new OSGiHttpRoutePlanner(bundleContext, registeredConfigurations));
- }
-
- @Override
- public CloseableHttpClient build() {
- final CloseableHttpClient httpClient = super.build();
- trackedHttpClients.add(httpClient);
- return httpClient;
+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<Object> list = new WeakList<Object>();
+ 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<Object> 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