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.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() {

View File

@ -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();
}

View File

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

View File

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

View File

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

View File

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

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