hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jsedd...@apache.org
Subject svn commit: r1763285 - in /httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl: HttpProxyConfigurationActivator.java OSGiCachingClientBuilderFactory.java OSGiClientBuilderFactory.java
Date Tue, 04 Oct 2016 14:41:23 GMT
Author: jsedding
Date: Tue Oct  4 14:41:23 2016
New Revision: 1763285

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

Modified:
    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/OSGiCachingClientBuilderFactory.java
    httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiClientBuilderFactory.java

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=1763285&r1=1763284&r2=1763285&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
Tue Oct  4 14:41:23 2016
@@ -83,11 +83,7 @@ public final class HttpProxyConfiguratio
 
     private final List<ProxyConfiguration> proxyConfigurations = new CopyOnWriteArrayList<>();
 
-    private final List<CloseableHttpClient> trackedHttpClients;
-
-    public HttpProxyConfigurationActivator() {
-        trackedHttpClients = new WeakList<CloseableHttpClient>();
-    }
+    private final HttpClientTracker httpClientTracker = new HttpClientTracker();
 
     /**
      * {@inheritDoc}
@@ -119,7 +115,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,
-                                                new OSGiClientBuilderFactory(configurator,
trackedHttpClients),
+                                                new OSGiClientBuilderFactory(configurator,
httpClientTracker),
                                                 props);
 
         props.clear();
@@ -127,7 +123,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,
-                                                       new OSGiCachingClientBuilderFactory(configurator,
trackedHttpClients),
+                                                       new OSGiCachingClientBuilderFactory(configurator,
httpClientTracker),
                                                        props);
     }
 
@@ -140,23 +136,16 @@ public final class HttpProxyConfiguratio
         for (final ServiceRegistration<ProxyConfiguration> registeredConfiguration
: registeredConfigurations.values()) {
             safeUnregister(registeredConfiguration);
         }
+        // remove all tracked services
+        registeredConfigurations.clear();
 
         safeUnregister(configurator);
         safeUnregister(clientFactory);
         safeUnregister(cachingClientFactory);
         safeUnregister(trustedHostConfiguration);
 
-        // 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();
     }
 
     /**
@@ -214,11 +203,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<>();
+
+        synchronized void track(final CloseableHttpClient client) {
+            trackedHttpClients.add(client);
+        }
+
+        synchronized void closeAll() {
+            for (final CloseableHttpClient client : trackedHttpClients) {
+                closeQuietly(client);
+            }
+            trackedHttpClients.clear();
+        }
+    }
 }

Modified: httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCachingClientBuilderFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCachingClientBuilderFactory.java?rev=1763285&r1=1763284&r2=1763285&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCachingClientBuilderFactory.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiCachingClientBuilderFactory.java
Tue Oct  4 14:41:23 2016
@@ -26,8 +26,6 @@
  */
 package org.apache.hc.client5.http.osgi.impl;
 
-import java.util.List;
-
 import org.apache.hc.client5.http.impl.cache.CachingHttpClientBuilder;
 import org.apache.hc.client5.http.impl.sync.CloseableHttpClient;
 import org.apache.hc.client5.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/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiClientBuilderFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiClientBuilderFactory.java?rev=1763285&r1=1763284&r2=1763285&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiClientBuilderFactory.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/OSGiClientBuilderFactory.java
Tue Oct  4 14:41:23 2016
@@ -26,8 +26,6 @@
  */
 package org.apache.hc.client5.http.osgi.impl;
 
-import java.util.List;
-
 import org.apache.hc.client5.http.impl.sync.CloseableHttpClient;
 import org.apache.hc.client5.http.impl.sync.HttpClientBuilder;
 import org.apache.hc.client5.http.osgi.services.HttpClientBuilderFactory;
@@ -39,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
@@ -54,7 +52,7 @@ final class OSGiClientBuilderFactory imp
             @Override
             public CloseableHttpClient build() {
                 final CloseableHttpClient client = super.build();
-                trackedHttpClients.add(client);
+                httpClientTracker.track(client);
                 return client;
             }
         });



Mime
View raw message