arturobernalg commented on code in PR #637: URL: https://github.com/apache/httpcomponents-client/pull/637#discussion_r2126996324
########## httpclient5/src/main/java/org/apache/hc/client5/http/socket/UnixDomainSocketFactory.java: ########## @@ -0,0 +1,179 @@ +/* + * ==================================================================== + * 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.socket; + +import org.apache.hc.core5.annotation.Contract; +import org.apache.hc.core5.annotation.Internal; +import org.apache.hc.core5.annotation.ThreadingBehavior; +import org.apache.hc.core5.util.Args; +import org.apache.hc.core5.util.TimeValue; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.Socket; +import java.net.SocketAddress; +import java.nio.file.Path; + +/** + * A factory for Unix domain sockets. + * <p> + * This implementation supports both the JDK16+ standard library implementation (JEP 380) and the JUnixSocket library. + * It will automatically detect which implementation is available and use it. JUnixSocket is preferred, since it + * supports the {@link java.net.Socket} API used by the classic client. + * </p> + * + * @since 5.6 + */ +@Contract(threading = ThreadingBehavior.STATELESS) +@Internal +public final class UnixDomainSocketFactory { + private static final Logger LOG = LoggerFactory.getLogger(UnixDomainSocketFactory.class); + + private static final String JDK_UNIX_SOCKET_ADDRESS_CLASS = "java.net.UnixDomainSocketAddress"; + private static final String JUNIXSOCKET_SOCKET_CLASS = "org.newsclub.net.unix.AFUNIXSocket"; + private static final String JUNIXSOCKET_ADDRESS_CLASS = "org.newsclub.net.unix.AFUNIXSocketAddress"; + + private enum Implementation { + JDK, + JUNIXSOCKET, + NONE + } + + private static final Implementation IMPLEMENTATION = detectImplementation(); + + private static Implementation detectImplementation() { + try { + Class.forName(JUNIXSOCKET_SOCKET_CLASS); + LOG.debug("Using JUnixSocket Unix Domain Socket implementation"); + return Implementation.JUNIXSOCKET; + } catch (final ClassNotFoundException e) { + try { + Class.forName(JDK_UNIX_SOCKET_ADDRESS_CLASS); + LOG.debug("Using JDK Unix Domain Socket implementation"); + return Implementation.JDK; + } catch (final ClassNotFoundException e2) { + LOG.debug("No Unix Domain Socket implementation found"); + return Implementation.NONE; + } + } + } + + /** + * Checks if Unix Domain Socket support is available. + * + * @return true if Unix Domain Socket support is available, false otherwise + */ + public static boolean isAvailable() { + return IMPLEMENTATION != Implementation.NONE; + } + + /** + * Default instance of {@link UnixDomainSocketFactory}. + */ + private static final UnixDomainSocketFactory INSTANCE = new UnixDomainSocketFactory(); + + /** + * Gets the singleton instance of {@link UnixDomainSocketFactory}. + * + * @return the singleton instance + */ + public static UnixDomainSocketFactory getSocketFactory() { + return INSTANCE; + } + + public SocketAddress createSocketAddress(final Path socketPath) { + if (!isAvailable()) { + throw new UnsupportedOperationException("Unix Domain Socket support is not available"); + } + Args.notNull(socketPath, "Unix domain socket path"); + + try { + if (IMPLEMENTATION == Implementation.JDK) { + // JDK implementation + final Class<?> addressClass = Class.forName(JDK_UNIX_SOCKET_ADDRESS_CLASS); + final Method ofMethod = addressClass.getMethod("of", Path.class); + return (SocketAddress) ofMethod.invoke(null, socketPath); + } else { + // JUnixSocket implementation + final Class<?> addressClass = Class.forName(JUNIXSOCKET_ADDRESS_CLASS); + final Method ofMethod = addressClass.getMethod("of", Path.class); + return (SocketAddress) ofMethod.invoke(null, socketPath); + } + } catch (final ReflectiveOperationException ex) { + throw new RuntimeException("Could not create UDS SocketAddress", ex); Review Comment: @rschmitt Is `RuntimeException` here too generic? Would it be clearer to wrap this in an `IOException` (or an `UncheckedIOException`) instead? -- 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