You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2018/08/04 20:24:37 UTC
[19/19] httpcomponents-core git commit: Refactor common code in a new
Closer utility class.
Refactor common code in a new Closer utility class.
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/dc648a98
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/dc648a98
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/dc648a98
Branch: refs/heads/master
Commit: dc648a98871d6adaff40620ef1187415b9df9018
Parents: 24c351c
Author: Gary Gregory <gg...@apache.org>
Authored: Sat Aug 4 14:24:12 2018 -0600
Committer: Gary Gregory <gg...@apache.org>
Committed: Sat Aug 4 14:24:12 2018 -0600
----------------------------------------------------------------------
.../core5/http/impl/io/BHttpConnectionBase.java | 6 +--
.../core5/http/impl/io/HttpRequestExecutor.java | 10 +---
.../apache/hc/core5/reactor/IOSessionImpl.java | 7 +--
.../hc/core5/reactor/SingleCoreIOReactor.java | 7 +--
.../reactor/SingleCoreListeningIOReactor.java | 6 +--
.../java/org/apache/hc/core5/util/Closer.java | 53 ++++++++++++++++++++
6 files changed, 63 insertions(+), 26 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/dc648a98/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/BHttpConnectionBase.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/BHttpConnectionBase.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/BHttpConnectionBase.java
index 1ce2083..c1979d4 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/BHttpConnectionBase.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/BHttpConnectionBase.java
@@ -61,6 +61,7 @@ import org.apache.hc.core5.http.io.SessionOutputBuffer;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.net.InetAddressUtils;
import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.Closer;
class BHttpConnectionBase implements BHttpConnection {
@@ -229,10 +230,7 @@ class BHttpConnectionBase implements BHttpConnection {
}
} catch (final IOException ignore) {
} finally {
- try {
- socket.close();
- } catch (final IOException ignore) {
- }
+ Closer.closeQuietly(socket);
}
}
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/dc648a98/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpRequestExecutor.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpRequestExecutor.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpRequestExecutor.java
index 86155e3..93ddb47 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpRequestExecutor.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpRequestExecutor.java
@@ -53,6 +53,7 @@ import org.apache.hc.core5.http.protocol.HttpContext;
import org.apache.hc.core5.http.protocol.HttpCoreContext;
import org.apache.hc.core5.http.protocol.HttpProcessor;
import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.Closer;
/**
* {@code HttpRequestExecutor} is a client side HTTP protocol handler based
@@ -198,7 +199,7 @@ public class HttpRequestExecutor {
return response;
} catch (final HttpException | IOException | RuntimeException ex) {
- closeConnection(conn);
+ Closer.closeQuietly(conn);
throw ex;
}
}
@@ -222,13 +223,6 @@ public class HttpRequestExecutor {
return execute(request, conn, null, context);
}
- private static void closeConnection(final HttpClientConnection conn) {
- try {
- conn.close();
- } catch (final IOException ignore) {
- }
- }
-
/**
* Pre-process the given request using the given protocol processor and
* initiates the process of request execution.
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/dc648a98/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java
index c5aff5e..98b3b7a 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java
@@ -27,7 +27,6 @@
package org.apache.hc.core5.reactor;
-import java.io.IOException;
import java.net.SocketAddress;
import java.nio.channels.ByteChannel;
import java.nio.channels.SelectionKey;
@@ -41,6 +40,7 @@ import java.util.concurrent.locks.ReentrantLock;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.Closer;
class IOSessionImpl implements IOSession {
@@ -199,10 +199,7 @@ class IOSessionImpl implements IOSession {
if (this.status.compareAndSet(ACTIVE, CLOSED)) {
this.key.cancel();
this.key.attach(null);
- try {
- this.key.channel().close();
- } catch (final IOException ignore) {
- }
+ Closer.closeQuietly(this.key.channel());
if (this.key.selector().isOpen()) {
this.key.selector().wakeup();
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/dc648a98/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java
index 9761ba1..0b16bed 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java
@@ -48,6 +48,7 @@ import org.apache.hc.core5.function.Decorator;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.net.NamedEndpoint;
import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.Closer;
import org.apache.hc.core5.util.TimeValue;
class SingleCoreIOReactor extends AbstractSingleCoreIOReactor implements ConnectionInitiator {
@@ -293,11 +294,7 @@ class SingleCoreIOReactor extends AbstractSingleCoreIOReactor implements Connect
try {
processConnectionRequest(socketChannel, sessionRequest);
} catch (final IOException | SecurityException ex) {
- try {
- socketChannel.close();
- } catch (final IOException ignore) {
- // Ignore
- }
+ Closer.closeQuietly(socketChannel);
sessionRequest.failed(ex);
}
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/dc648a98/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreListeningIOReactor.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreListeningIOReactor.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreListeningIOReactor.java
index b83cc5c..a30dd2e 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreListeningIOReactor.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreListeningIOReactor.java
@@ -47,6 +47,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.hc.core5.concurrent.BasicFuture;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.function.Callback;
+import org.apache.hc.core5.util.Closer;
class SingleCoreListeningIOReactor extends AbstractSingleCoreIOReactor implements ConnectionAcceptor {
@@ -167,10 +168,7 @@ class SingleCoreListeningIOReactor extends AbstractSingleCoreIOReactor implement
this.endpoints.put(endpoint, Boolean.TRUE);
request.completed(endpoint);
} catch (final IOException ex) {
- try {
- serverChannel.close();
- } catch (final IOException ignore) {
- }
+ Closer.closeQuietly(serverChannel);
request.failed(ex);
}
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/dc648a98/httpcore5/src/main/java/org/apache/hc/core5/util/Closer.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/Closer.java b/httpcore5/src/main/java/org/apache/hc/core5/util/Closer.java
new file mode 100644
index 0000000..6886688
--- /dev/null
+++ b/httpcore5/src/main/java/org/apache/hc/core5/util/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.core5.util;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+/**
+ * Closes resources.
+ */
+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
+ }
+ }
+ }
+}