You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by js...@apache.org on 2016/10/04 14:41:07 UTC

svn commit: r1763284 - in /httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl: HttpProxyConfigurationActivator.java OSGiCachingClientBuilderFactory.java OSGiClientBuilderFactory.java

Author: jsedding
Date: Tue Oct  4 14:41:07 2016
New Revision: 1763284

URL: http://svn.apache.org/viewvc?rev=1763284&view=rev
Log:
HTTPCLIENT-1782: [OSGi] List of tracked HTTPClients is mutable but not thread-safe

Modified:
    httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java
    httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiCachingClientBuilderFactory.java
    httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiClientBuilderFactory.java

Modified: httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java?rev=1763284&r1=1763283&r2=1763284&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java (original)
+++ httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/HttpProxyConfigurationActivator.java Tue Oct  4 14:41:07 2016
@@ -76,11 +76,7 @@ public final class HttpProxyConfiguratio
 
     private final List<ProxyConfiguration> proxyConfigurations = new CopyOnWriteArrayList<ProxyConfiguration>();
 
-    private final List<CloseableHttpClient> trackedHttpClients;
-
-    public HttpProxyConfigurationActivator() {
-        trackedHttpClients = new WeakList<CloseableHttpClient>();
-    }
+    private final HttpClientTracker httpClientTracker = new HttpClientTracker();
 
     /**
      * {@inheritDoc}
@@ -104,7 +100,7 @@ public final class HttpProxyConfiguratio
         props.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders().get(Constants.BUNDLE_VENDOR));
         props.put(Constants.SERVICE_DESCRIPTION, BUILDER_FACTORY_SERVICE_NAME);
         clientFactory = context.registerService(HttpClientBuilderFactory.class.getName(),
-                                                new OSGiClientBuilderFactory(configurator, trackedHttpClients),
+                                                new OSGiClientBuilderFactory(configurator, httpClientTracker),
                                                 props);
 
         props.clear();
@@ -112,7 +108,7 @@ public final class HttpProxyConfiguratio
         props.put(Constants.SERVICE_VENDOR, context.getBundle().getHeaders().get(Constants.BUNDLE_VENDOR));
         props.put(Constants.SERVICE_DESCRIPTION, CACHEABLE_BUILDER_FACTORY_SERVICE_NAME);
         cachingClientFactory = context.registerService(CachingHttpClientBuilderFactory.class.getName(),
-                                                       new OSGiCachingClientBuilderFactory(configurator, trackedHttpClients),
+                                                       new OSGiCachingClientBuilderFactory(configurator, httpClientTracker),
                                                        props);
     }
 
@@ -125,6 +121,8 @@ public final class HttpProxyConfiguratio
         for (final ServiceRegistration registeredConfiguration : registeredConfigurations.values()) {
             registeredConfiguration.unregister();
         }
+        // remove all tracked services
+        registeredConfigurations.clear();
 
         // unregister service factory
         if (configurator != null) {
@@ -139,17 +137,8 @@ public final class HttpProxyConfiguratio
             cachingClientFactory.unregister();
         }
 
-        // ensure all http clients - generated with the - are terminated
-        for (final CloseableHttpClient client : trackedHttpClients) {
-            if (null != client) {
-                closeQuietly(client);
-            }
-        }
-
-        // remove all tracked services
-        registeredConfigurations.clear();
-        // remove all tracked created clients
-        trackedHttpClients.clear();
+        // ensure all http clients are closed
+        httpClientTracker.closeAll();
     }
 
     /**
@@ -201,11 +190,28 @@ public final class HttpProxyConfiguratio
     }
 
     private static void closeQuietly(final Closeable closeable) {
-        try {
-            closeable.close();
-        } catch (final IOException e) {
-            // do nothing
+        if (closeable != null) {
+            try {
+                closeable.close();
+            } catch (final IOException e) {
+                // do nothing
+            }
         }
     }
 
+    static class HttpClientTracker {
+
+        private final List<CloseableHttpClient> trackedHttpClients = new WeakList<CloseableHttpClient>();
+
+        synchronized void track(final CloseableHttpClient client) {
+            trackedHttpClients.add(client);
+        }
+
+        synchronized void closeAll() {
+            for (final CloseableHttpClient client : trackedHttpClients) {
+                closeQuietly(client);
+            }
+            trackedHttpClients.clear();
+        }
+    }
 }

Modified: httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiCachingClientBuilderFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiCachingClientBuilderFactory.java?rev=1763284&r1=1763283&r2=1763284&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiCachingClientBuilderFactory.java (original)
+++ httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiCachingClientBuilderFactory.java Tue Oct  4 14:41:07 2016
@@ -26,8 +26,6 @@
  */
 package org.apache.http.osgi.impl;
 
-import java.util.List;
-
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.cache.CachingHttpClientBuilder;
 import org.apache.http.osgi.services.CachingHttpClientBuilderFactory;
@@ -39,13 +37,13 @@ final class OSGiCachingClientBuilderFact
 
     private final HttpClientBuilderConfigurator configurator;
 
-    private List<CloseableHttpClient> trackedHttpClients;
+    private final HttpProxyConfigurationActivator.HttpClientTracker httpClientTracker;
 
     OSGiCachingClientBuilderFactory(
             final HttpClientBuilderConfigurator configurator,
-            final List<CloseableHttpClient> trackedHttpClients) {
+            final HttpProxyConfigurationActivator.HttpClientTracker httpClientTracker) {
         this.configurator = configurator;
-        this.trackedHttpClients = trackedHttpClients;
+        this.httpClientTracker = httpClientTracker;
     }
 
     @Override
@@ -54,7 +52,7 @@ final class OSGiCachingClientBuilderFact
             @Override
             public CloseableHttpClient build() {
                 final CloseableHttpClient client = super.build();
-                trackedHttpClients.add(client);
+                httpClientTracker.track(client);
                 return client;
             }
         });

Modified: httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiClientBuilderFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiClientBuilderFactory.java?rev=1763284&r1=1763283&r2=1763284&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiClientBuilderFactory.java (original)
+++ httpcomponents/httpclient/branches/4.5.x/httpclient-osgi/src/main/java/org/apache/http/osgi/impl/OSGiClientBuilderFactory.java Tue Oct  4 14:41:07 2016
@@ -26,11 +26,10 @@
  */
 package org.apache.http.osgi.impl;
 
-import java.util.List;
-
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.osgi.services.HttpClientBuilderFactory;
+
 /**
  * @since 4.3
  */
@@ -38,13 +37,13 @@ final class OSGiClientBuilderFactory imp
 
     private final HttpClientBuilderConfigurator configurator;
 
-    private final List<CloseableHttpClient> trackedHttpClients;
+    private final HttpProxyConfigurationActivator.HttpClientTracker httpClientTracker;
 
     OSGiClientBuilderFactory(
             final HttpClientBuilderConfigurator configurator,
-            final List<CloseableHttpClient> trackedHttpClients) {
+            final HttpProxyConfigurationActivator.HttpClientTracker httpClientTracker) {
         this.configurator = configurator;
-        this.trackedHttpClients = trackedHttpClients;
+        this.httpClientTracker = httpClientTracker;
     }
 
     @Override
@@ -53,7 +52,7 @@ final class OSGiClientBuilderFactory imp
             @Override
             public CloseableHttpClient build() {
                 final CloseableHttpClient client = super.build();
-                trackedHttpClients.add(client);
+                httpClientTracker.track(client);
                 return client;
             }
         });