Refactor common code in a new Closer utility class.

This commit is contained in:
Gary Gregory 2018-08-04 14:16:28 -06:00 committed by Oleg Kalnichevski
parent 8d87cf515b
commit c3bdc8913f
7 changed files with 68 additions and 37 deletions

View File

@ -30,6 +30,8 @@
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.apache.hc.client5.http.utils.Closer;
public class ConsumableInputStream extends InputStream { public class ConsumableInputStream extends InputStream {
private final ByteArrayInputStream buf; private final ByteArrayInputStream buf;
@ -47,10 +49,7 @@ public int read() throws IOException {
@Override @Override
public void close() { public void close() {
closed = true; closed = true;
try { Closer.closeQuietly(buf);
buf.close();
} catch (final IOException e) {
}
} }
public boolean wasClosed() { public boolean wasClosed() {

View File

@ -26,8 +26,6 @@
*/ */
package org.apache.hc.client5.http.osgi.impl; package org.apache.hc.client5.http.osgi.impl;
import java.io.Closeable;
import java.io.IOException;
import java.util.Dictionary; import java.util.Dictionary;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -39,6 +37,7 @@
import org.apache.hc.client5.http.osgi.services.CachingHttpClientBuilderFactory; 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.HttpClientBuilderFactory;
import org.apache.hc.client5.http.osgi.services.ProxyConfiguration; 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.BundleActivator;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants; import org.osgi.framework.Constants;
@ -202,16 +201,6 @@ private static <S> boolean safeUnregister(final ServiceRegistration<S> serviceRe
return false; return false;
} }
private static void closeQuietly(final Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (final IOException e) {
// do nothing
}
}
}
static class HttpClientTracker { static class HttpClientTracker {
private final List<CloseableHttpClient> trackedHttpClients = new WeakList<>(); private final List<CloseableHttpClient> trackedHttpClients = new WeakList<>();
@ -222,7 +211,7 @@ synchronized void track(final CloseableHttpClient client) {
synchronized void closeAll() { synchronized void closeAll() {
for (final CloseableHttpClient client : trackedHttpClients) { for (final CloseableHttpClient client : trackedHttpClients) {
closeQuietly(client); Closer.closeQuietly(client);
} }
trackedHttpClients.clear(); trackedHttpClients.clear();
} }

View File

@ -33,6 +33,7 @@
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; 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.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; 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.SSLTestContexts;
import org.apache.hc.client5.testing.classic.EchoHandler; import org.apache.hc.client5.testing.classic.EchoHandler;
import org.apache.hc.client5.testing.classic.RandomHandler; import org.apache.hc.client5.testing.classic.RandomHandler;
@ -118,13 +119,8 @@ protected void before() throws Throwable {
@Override @Override
protected void after() { protected void after() {
if (httpclient != null) { Closer.closeQuietly(httpclient);
try { httpclient = null;
httpclient.close();
httpclient = null;
} catch (final Exception ignore) {
}
}
} }
}; };

View File

@ -46,6 +46,7 @@
import org.apache.hc.client5.http.nio.AsyncConnectionEndpoint; import org.apache.hc.client5.http.nio.AsyncConnectionEndpoint;
import org.apache.hc.client5.http.protocol.HttpClientContext; import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.routing.RoutingSupport; 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.BasicFuture;
import org.apache.hc.core5.concurrent.Cancellable; import org.apache.hc.core5.concurrent.Cancellable;
import org.apache.hc.core5.concurrent.ComplexCancellable; import org.apache.hc.core5.concurrent.ComplexCancellable;
@ -424,10 +425,7 @@ public void releaseAndReuse() {
@Override @Override
public void releaseAndDiscard() { public void releaseAndDiscard() {
if (released.compareAndSet(false, true)) { if (released.compareAndSet(false, true)) {
try { Closer.closeQuietly(connectionEndpoint);
connectionEndpoint.close();
} catch (final IOException ignore) {
}
connmgr.release(connectionEndpoint, null, TimeValue.ZERO_MILLISECONDS); connmgr.release(connectionEndpoint, null, TimeValue.ZERO_MILLISECONDS);
} }
} }

View File

@ -31,6 +31,7 @@
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; 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.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior; import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpHost;
@ -75,10 +76,7 @@ public Socket connectSocket(
try { try {
sock.connect(remoteAddress, TimeValue.isPositive(connectTimeout) ? connectTimeout.toMillisIntBound() : 0); sock.connect(remoteAddress, TimeValue.isPositive(connectTimeout) ? connectTimeout.toMillisIntBound() : 0);
} catch (final IOException ex) { } catch (final IOException ex) {
try { Closer.closeQuietly(sock);
sock.close();
} catch (final IOException ignore) {
}
throw ex; throw ex;
} }
return sock; return sock;

View File

@ -49,6 +49,7 @@
import org.apache.hc.client5.http.psl.PublicSuffixMatcherLoader; import org.apache.hc.client5.http.psl.PublicSuffixMatcherLoader;
import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory; 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.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior; import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpHost;
@ -277,10 +278,7 @@ public Socket connectSocket(
} }
sock.connect(remoteAddress, connectTimeout != null ? connectTimeout.toMillisIntBound() : 0); sock.connect(remoteAddress, connectTimeout != null ? connectTimeout.toMillisIntBound() : 0);
} catch (final IOException ex) { } catch (final IOException ex) {
try { Closer.closeQuietly(sock);
sock.close();
} catch (final IOException ignore) {
}
throw ex; throw ex;
} }
// Setup SSL layering if necessary // Setup SSL layering if necessary
@ -412,7 +410,7 @@ private void verifyHostname(final SSLSocket sslsock, final String hostname) thro
// verifyHostName() didn't blowup - good! // verifyHostName() didn't blowup - good!
} catch (final IOException iox) { } catch (final IOException iox) {
// close the socket before re-throwing the exception // close the socket before re-throwing the exception
try { sslsock.close(); } catch (final Exception x) { /*ignore*/ } Closer.closeQuietly(sslsock);
throw iox; throw iox;
} }
} }

View File

@ -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
}
}
}
}