ok2c commented on code in PR #473:
URL: 
https://github.com/apache/httpcomponents-core/pull/473#discussion_r1705347120


##########
httpcore5/src/main/java/org/apache/hc/core5/util/ReflectionUtils.java:
##########
@@ -44,12 +46,13 @@ public static void callSetter(final Object object, final 
String setterName, fina
         }
     }
 
-    public static <T> T callGetter(final Object object, final String 
getterName, final Class<T> resultType) {
+    public static <T> T callGetter(final Object object, final String 
getterName, final Object arg, final Class argType, final Class<T> resultType) {

Review Comment:
   @kkewwei Is this method actually being used anywhere besides tests? Do we 
need to change it? 



##########
httpcore5/src/main/java/org/apache/hc/core5/util/ReflectionUtils.java:
##########
@@ -72,4 +75,35 @@ public static int determineJRELevel() {
         return 7;
     }
 
+    /**
+     * @since 5.3
+     */
+    public static <T> SocketOption<T> getExtendedSocketOptionOrNull(final 
String fieldName) {
+        try {
+            final Class<?> extendedSocketOptionsClass = 
Class.forName("jdk.net.ExtendedSocketOptions");
+            final Field field = extendedSocketOptionsClass.getField(fieldName);
+            return (SocketOption)field.get((Object)null);
+        } catch (final Exception ignore) {
+            return null;
+        }
+    }
+
+    /**
+     * object can be ServerSocket or Socket
+     *
+     * @since 5.3
+     */
+    public static <T> void setOption(final T object, final String fieldName, 
final T value) {
+        try {
+            final Class<?> serverSocketClass = object.getClass();
+            final Method setOptionMethod = 
serverSocketClass.getMethod("setOption", SocketOption.class, Object.class);
+            final SocketOption<Integer> tcpKeepIdle = 
getExtendedSocketOptionOrNull(fieldName);
+            if (tcpKeepIdle == null) {

Review Comment:
   @kkewwei The choice of the name looks bad. Can you come up with a better 
name here?



##########
httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java:
##########
@@ -82,9 +82,14 @@
 import org.apache.hc.core5.pool.PoolEntry;
 import org.apache.hc.core5.pool.PoolStats;
 import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.ReflectionUtils;
 import org.apache.hc.core5.util.TimeValue;
 import org.apache.hc.core5.util.Timeout;
 
+import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPCOUNT;

Review Comment:
   @kkewwei This should be avoided. Now classic transport services are directly 
coupled to a async transport class



##########
httpcore5/src/test/java/org/apache/hc/core5/reactor/TestSingleCoreIOReactor.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * ====================================================================

Review Comment:
   @kkewwei I am not sure what this test case actually tests. It does not seem 
to test much of `SingleCoreIOReactor`, just a single reflective call. Drop it.



##########
httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java:
##########
@@ -51,8 +53,14 @@
 import org.apache.hc.core5.util.Args;
 import org.apache.hc.core5.util.Timeout;
 
-class SingleCoreIOReactor extends AbstractSingleCoreIOReactor implements 
ConnectionInitiator {
+import static 
org.apache.hc.core5.util.ReflectionUtils.getExtendedSocketOptionOrNull;
 
+@Internal
+public class SingleCoreIOReactor extends AbstractSingleCoreIOReactor 
implements ConnectionInitiator {
+
+    public static final String TCP_KEEPIDLE = "TCP_KEEPIDLE";

Review Comment:
   @kkewwei This is not the right place. Please move those constants to a 
separate file and mark it internal. The `org.apache.hc.core5.io` might be a 
good place.



##########
httpcore5/src/main/java/org/apache/hc/core5/util/ReflectionUtils.java:
##########
@@ -72,4 +75,35 @@ public static int determineJRELevel() {
         return 7;
     }
 
+    /**
+     * @since 5.3
+     */
+    public static <T> SocketOption<T> getExtendedSocketOptionOrNull(final 
String fieldName) {
+        try {
+            final Class<?> extendedSocketOptionsClass = 
Class.forName("jdk.net.ExtendedSocketOptions");
+            final Field field = extendedSocketOptionsClass.getField(fieldName);
+            return (SocketOption)field.get((Object)null);

Review Comment:
   @kkewwei Please suppress `unchecked` warning



##########
httpcore5/src/test/java/org/apache/hc/core5/util/TestReflectionUtils.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * ====================================================================
+ * 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 org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import javax.net.ServerSocketFactory;
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketOption;
+
+import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPCOUNT;
+import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPIDLE;
+import static org.apache.hc.core5.reactor.SingleCoreIOReactor.TCP_KEEPINTERVAL;
+import static org.apache.hc.core5.util.ReflectionUtils.callGetter;
+import static org.apache.hc.core5.util.ReflectionUtils.determineJRELevel;
+import static org.apache.hc.core5.util.ReflectionUtils.setOption;
+import static 
org.apache.hc.core5.util.ReflectionUtils.getExtendedSocketOptionOrNull;
+
+public class TestReflectionUtils {
+
+    @Test
+    public void testGetExtendedSocketOptionOrNull() {
+        testGetExtendedSocketOption(TCP_KEEPIDLE);
+        testGetExtendedSocketOption(TCP_KEEPINTERVAL);
+        testGetExtendedSocketOption(TCP_KEEPCOUNT);
+    }
+
+    private void testGetExtendedSocketOption(final String option) {
+        final SocketOption socketOption = 
getExtendedSocketOptionOrNull(option);

Review Comment:
   @kkewwei Please fix `SocketOption` generic declaration.



##########
httpcore5/src/main/java/org/apache/hc/core5/util/ReflectionUtils.java:
##########
@@ -72,4 +75,35 @@ public static int determineJRELevel() {
         return 7;
     }
 
+    /**
+     * @since 5.3
+     */
+    public static <T> SocketOption<T> getExtendedSocketOptionOrNull(final 
String fieldName) {
+        try {
+            final Class<?> extendedSocketOptionsClass = 
Class.forName("jdk.net.ExtendedSocketOptions");
+            final Field field = extendedSocketOptionsClass.getField(fieldName);
+            return (SocketOption)field.get((Object)null);
+        } catch (final Exception ignore) {
+            return null;
+        }
+    }
+
+    /**
+     * object can be ServerSocket or Socket
+     *
+     * @since 5.3
+     */
+    public static <T> void setOption(final T object, final String fieldName, 
final T value) {
+        try {
+            final Class<?> serverSocketClass = object.getClass();
+            final Method setOptionMethod = 
serverSocketClass.getMethod("setOption", SocketOption.class, Object.class);
+            final SocketOption<Integer> tcpKeepIdle = 
getExtendedSocketOptionOrNull(fieldName);
+            if (tcpKeepIdle == null) {
+                throw new UnsupportedOperationException(fieldName + " is not 
supported in the current jdk");
+            }
+            setOptionMethod.invoke(object, tcpKeepIdle, value);
+        } catch (final Exception ignore) {

Review Comment:
   @kkewwei `ignore` looks like a bad name here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org
For additional commands, e-mail: dev-h...@hc.apache.org

Reply via email to