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 2018/08/04 21:56:09 UTC
httpcomponents-client git commit: Refactor common code in a new
Closer utility class. [Forced Update!]
Repository: httpcomponents-client
Updated Branches:
refs/heads/master 81a39c2f5 -> c3bdc8913 (forced update)
Refactor common code in a new Closer utility class.
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/c3bdc891
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/c3bdc891
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/c3bdc891
Branch: refs/heads/master
Commit: c3bdc8913fea0d4eba7920984d2148c2b133ecf8
Parents: 8d87cf5
Author: Gary Gregory <gg...@apache.org>
Authored: Sat Aug 4 14:16:28 2018 -0600
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Sat Aug 4 23:55:41 2018 +0200
----------------------------------------------------------------------
.../http/impl/cache/ConsumableInputStream.java | 7 ++-
.../impl/HttpProxyConfigurationActivator.java | 15 +-----
.../testing/sync/LocalServerTestBase.java | 10 ++--
.../http/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(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/c3bdc891/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/ConsumableInputStream.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 bce3d36..8f5ce27 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() {
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/c3bdc891/httpclient5-osgi/src/main/java/org/apache/hc/client5/http/osgi/impl/HttpProxyConfigurationActivator.java
----------------------------------------------------------------------
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 c8a8ab7..6ae8af0 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<CloseableHttpClient> 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();
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/c3bdc891/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/LocalServerTestBase.java
----------------------------------------------------------------------
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 5e9da8a..f79bccb 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;
}
};
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/c3bdc891/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/MinimalHttpAsyncClient.java
----------------------------------------------------------------------
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 26b601c..2f57053 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);
}
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/c3bdc891/httpclient5/src/main/java/org/apache/hc/client5/http/socket/PlainConnectionSocketFactory.java
----------------------------------------------------------------------
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 11b474c..aeceaa4 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;
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/c3bdc891/httpclient5/src/main/java/org/apache/hc/client5/http/ssl/SSLConnectionSocketFactory.java
----------------------------------------------------------------------
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 7133966..586d4b2 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;
}
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/c3bdc891/httpclient5/src/main/java/org/apache/hc/client5/http/utils/Closer.java
----------------------------------------------------------------------
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 0000000..3254e3e
--- /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
+ * <http://www.apache.org/>.
+ *
+ */
+
+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
+ }
+ }
+ }
+}