Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 7F8BF200AF6 for ; Sat, 11 Jun 2016 17:55:21 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 7E2CE160A34; Sat, 11 Jun 2016 15:55:21 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 762CC160A2B for ; Sat, 11 Jun 2016 17:55:20 +0200 (CEST) Received: (qmail 8703 invoked by uid 500); 11 Jun 2016 15:55:19 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 8694 invoked by uid 99); 11 Jun 2016 15:55:19 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 11 Jun 2016 15:55:19 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 48A021A00A3 for ; Sat, 11 Jun 2016 15:55:19 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.374 X-Spam-Level: X-Spam-Status: No, score=0.374 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id EwnmQPQCFNMh for ; Sat, 11 Jun 2016 15:55:16 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id 87F7B5FAC2 for ; Sat, 11 Jun 2016 15:55:16 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id B95BCE021C for ; Sat, 11 Jun 2016 15:55:15 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id B731C3A0051 for ; Sat, 11 Jun 2016 15:55:15 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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/ Date: Sat, 11 Jun 2016 15:55:15 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160611155515.B731C3A0051@svn01-us-west.apache.org> archived-at: Sat, 11 Jun 2016 15:55:21 -0000 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 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 @@ - - org.codehaus.mojo - clirr-maven-plugin - - true - - org.apache.maven.plugins maven-failsafe-plugin 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 registeredConfigurations = new LinkedHashMap<>(); - private final List trackedHttpClients = new LinkedList<>(); + private final List trackedHttpClients; + + public HttpProxyConfigurationActivator() { + trackedHttpClients = new WeakList(); + } /** * {@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 trackedHttpClients; + private final Collection 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 + * . + * + */ +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(); + } + + } +} 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 trackedHttpClients; - - public OSGiHttpClientBuilder( - final BundleContext bundleContext, - final Map registeredConfigurations, - final List 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 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