From c3bdc8913fea0d4eba7920984d2148c2b133ecf8 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sat, 4 Aug 2018 14:16:28 -0600 Subject: [PATCH] Refactor common code in a new Closer utility class. --- .../impl/cache/ConsumableInputStream.java | 7 ++- .../impl/HttpProxyConfigurationActivator.java | 15 +----- .../testing/sync/LocalServerTestBase.java | 10 ++-- .../impl/async/MinimalHttpAsyncClient.java | 6 +-- .../socket/PlainConnectionSocketFactory.java | 6 +-- .../http/ssl/SSLConnectionSocketFactory.java | 8 ++- .../apache/hc/client5/http/utils/Closer.java | 53 +++++++++++++++++++ 7 files changed, 68 insertions(+), 37 deletions(-) create mode 100644 httpclient5/src/main/java/org/apache/hc/client5/http/utils/Closer.java diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/ConsumableInputStream.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/ConsumableInputStream.java index bce3d36bc..8f5ce275a 100644 --- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/ConsumableInputStream.java +++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/ConsumableInputStream.java @@ -30,6 +30,8 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import org.apache.hc.client5.http.utils.Closer; + public class ConsumableInputStream extends InputStream { private final ByteArrayInputStream buf; @@ -47,10 +49,7 @@ public class ConsumableInputStream extends InputStream { @Override public void close() { closed = true; - try { - buf.close(); - } catch (final IOException e) { - } + Closer.closeQuietly(buf); } public boolean wasClosed() { 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 c8a8ab7eb..6ae8af048 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 @@ -26,8 +26,6 @@ */ package org.apache.hc.client5.http.osgi.impl; -import java.io.Closeable; -import java.io.IOException; import java.util.Dictionary; import java.util.Hashtable; import java.util.LinkedHashMap; @@ -39,6 +37,7 @@ import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.osgi.services.CachingHttpClientBuilderFactory; import org.apache.hc.client5.http.osgi.services.HttpClientBuilderFactory; import org.apache.hc.client5.http.osgi.services.ProxyConfiguration; +import org.apache.hc.client5.http.utils.Closer; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; @@ -202,16 +201,6 @@ public final class HttpProxyConfigurationActivator implements BundleActivator, M return false; } - private static void closeQuietly(final Closeable closeable) { - if (closeable != null) { - try { - closeable.close(); - } catch (final IOException e) { - // do nothing - } - } - } - static class HttpClientTracker { private final List trackedHttpClients = new WeakList<>(); @@ -222,7 +211,7 @@ public final class HttpProxyConfigurationActivator implements BundleActivator, M synchronized void closeAll() { for (final CloseableHttpClient client : trackedHttpClients) { - closeQuietly(client); + Closer.closeQuietly(client); } trackedHttpClients.clear(); } diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/LocalServerTestBase.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/LocalServerTestBase.java index 5e9da8ad2..f79bccb44 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/LocalServerTestBase.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/LocalServerTestBase.java @@ -33,6 +33,7 @@ import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; +import org.apache.hc.client5.http.utils.Closer; import org.apache.hc.client5.testing.SSLTestContexts; import org.apache.hc.client5.testing.classic.EchoHandler; import org.apache.hc.client5.testing.classic.RandomHandler; @@ -118,13 +119,8 @@ public abstract class LocalServerTestBase { @Override protected void after() { - if (httpclient != null) { - try { - httpclient.close(); - httpclient = null; - } catch (final Exception ignore) { - } - } + Closer.closeQuietly(httpclient); + httpclient = null; } }; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java index 26b601cf4..2f5705332 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java @@ -46,6 +46,7 @@ import org.apache.hc.client5.http.nio.AsyncClientConnectionManager; import org.apache.hc.client5.http.nio.AsyncConnectionEndpoint; import org.apache.hc.client5.http.protocol.HttpClientContext; import org.apache.hc.client5.http.routing.RoutingSupport; +import org.apache.hc.client5.http.utils.Closer; import org.apache.hc.core5.concurrent.BasicFuture; import org.apache.hc.core5.concurrent.Cancellable; import org.apache.hc.core5.concurrent.ComplexCancellable; @@ -424,10 +425,7 @@ public final class MinimalHttpAsyncClient extends AbstractMinimalHttpAsyncClient @Override public void releaseAndDiscard() { if (released.compareAndSet(false, true)) { - try { - connectionEndpoint.close(); - } catch (final IOException ignore) { - } + Closer.closeQuietly(connectionEndpoint); connmgr.release(connectionEndpoint, null, TimeValue.ZERO_MILLISECONDS); } } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/socket/PlainConnectionSocketFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/socket/PlainConnectionSocketFactory.java index 11b474c3e..aeceaa462 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/socket/PlainConnectionSocketFactory.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/socket/PlainConnectionSocketFactory.java @@ -31,6 +31,7 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; +import org.apache.hc.client5.http.utils.Closer; import org.apache.hc.core5.annotation.Contract; import org.apache.hc.core5.annotation.ThreadingBehavior; import org.apache.hc.core5.http.HttpHost; @@ -75,10 +76,7 @@ public class PlainConnectionSocketFactory implements ConnectionSocketFactory { try { sock.connect(remoteAddress, TimeValue.isPositive(connectTimeout) ? connectTimeout.toMillisIntBound() : 0); } catch (final IOException ex) { - try { - sock.close(); - } catch (final IOException ignore) { - } + Closer.closeQuietly(sock); throw ex; } return sock; diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/SSLConnectionSocketFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/SSLConnectionSocketFactory.java index 7133966a7..586d4b270 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/SSLConnectionSocketFactory.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/SSLConnectionSocketFactory.java @@ -49,6 +49,7 @@ import javax.security.auth.x500.X500Principal; import org.apache.hc.client5.http.psl.PublicSuffixMatcherLoader; import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory; +import org.apache.hc.client5.http.utils.Closer; import org.apache.hc.core5.annotation.Contract; import org.apache.hc.core5.annotation.ThreadingBehavior; import org.apache.hc.core5.http.HttpHost; @@ -277,10 +278,7 @@ public class SSLConnectionSocketFactory implements LayeredConnectionSocketFactor } sock.connect(remoteAddress, connectTimeout != null ? connectTimeout.toMillisIntBound() : 0); } catch (final IOException ex) { - try { - sock.close(); - } catch (final IOException ignore) { - } + Closer.closeQuietly(sock); throw ex; } // Setup SSL layering if necessary @@ -412,7 +410,7 @@ public class SSLConnectionSocketFactory implements LayeredConnectionSocketFactor // verifyHostName() didn't blowup - good! } catch (final IOException iox) { // close the socket before re-throwing the exception - try { sslsock.close(); } catch (final Exception x) { /*ignore*/ } + Closer.closeQuietly(sslsock); throw iox; } } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/utils/Closer.java b/httpclient5/src/main/java/org/apache/hc/client5/http/utils/Closer.java new file mode 100644 index 000000000..3254e3ec9 --- /dev/null +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/utils/Closer.java @@ -0,0 +1,53 @@ +/* + * ==================================================================== + * 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.utils; + +import java.io.Closeable; +import java.io.IOException; + +/** + * Closes resources. TODO: Use the HttpCore version when the next beta comes out. + */ +public class Closer { + + /** + * Closes the given closeable quietly even in the event of an exception. + * + * @param closeable + * what to close. + */ + public static void closeQuietly(final Closeable closeable) { + if (closeable != null) { + try { + closeable.close(); + } catch (final IOException e) { + // Quietly ignore + } + } + } +}