Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions net/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
<name>SmallRye Common: Net</name>

<dependencies>
<dependency>
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-annotation</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-constraint</artifactId>
</dependency>
<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>nativeimage</artifactId>
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-os</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
Expand All @@ -29,6 +34,10 @@
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.ffm</groupId>
<artifactId>smallrye-ffm</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand All @@ -38,6 +47,10 @@

<build>
<plugins>
<plugin>
<groupId>io.smallrye.ffm</groupId>
<artifactId>smallrye-ffm-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
Expand All @@ -49,6 +62,35 @@
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<configuration>
<argLine>--enable-native-access=io.smallrye.ffm,io.smallrye.common.net</argLine>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/module-info.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>net.revelc.code</groupId>
<artifactId>impsort-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/module-info.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
53 changes: 50 additions & 3 deletions net/src/main/java/io/smallrye/common/net/HostName.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package io.smallrye.common.net;

import static io.smallrye.ffm.AsType.size_t;
import static io.smallrye.ffm.AsType.stdc_int;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;

import io.smallrye.common.constraint.Assert;
import io.smallrye.common.os.OS;
import io.smallrye.ffm.As;
import io.smallrye.ffm.Critical;
import io.smallrye.ffm.In;
import io.smallrye.ffm.Lib;
import io.smallrye.ffm.Link;
import io.smallrye.ffm.Out;

/**
* Methods for getting the system host name. The host name is detected from the environment, but may be overridden by
* use of the {@code jboss.host.name} and/or {@code jboss.qualified.host.name} system properties.
*/
@SuppressWarnings("removal")
public final class HostName {

private static final Object lock = new Object();
Expand Down Expand Up @@ -99,6 +109,33 @@ private static String[] resolveHosts() {
if (qualifiedHostName == null) {
// if host name is specified, don't pick a qualified host name that isn't related to it
qualifiedHostName = providedHostName;
if (qualifiedHostName == null && Runtime.version().feature() >= 22) {
try {
switch (OS.current()) {
case MAC, LINUX, AIX, Z -> {
byte[] bytes = new byte[512];
int res = gethostname(bytes, bytes.length);
if (res == 0) {
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] == 0) {
qualifiedHostName = new String(bytes, 0, i, StandardCharsets.UTF_8);
break;
}
}
}
}
case WINDOWS -> {
char[] chars = new char[512];
int[] lenBuf = new int[1];
lenBuf[0] = chars.length;
if (GetComputerNameW(chars, lenBuf)) {
qualifiedHostName = new String(chars, 0, lenBuf[0]);
}
}
}
} catch (IllegalCallerException ignored) {
}
}
if (qualifiedHostName == null) {
// POSIX-like OSes including Mac should have this set
qualifiedHostName = System.getenv("HOSTNAME");
Expand All @@ -110,8 +147,7 @@ private static String[] resolveHosts() {
if (qualifiedHostName == null) {
try {
qualifiedHostName = HostName.getLocalHost().getHostName();
} catch (UnknownHostException e) {
qualifiedHostName = null;
} catch (UnknownHostException ignored) {
}
}
if (qualifiedHostName != null
Expand Down Expand Up @@ -141,4 +177,15 @@ private static String[] resolveHosts() {
};
}

// POSIX
@Link
@As(stdc_int)
@Critical(heap = true)
private static native int gethostname(@Out byte[] buffer, @As(size_t) int bufLen);

// Windows
@Link
@Critical(heap = true)
@Lib("kernel32")
private static native boolean GetComputerNameW(@Out char[] buffer, @In @Out int[] lenPtr);
}
104 changes: 0 additions & 104 deletions net/src/main/java/io/smallrye/common/net/Substitutions.java

This file was deleted.

12 changes: 9 additions & 3 deletions net/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import io.smallrye.common.annotation.NativeAccess;

/**
* Utilities for managing network addresses and information.
*/
@NativeAccess
module io.smallrye.common.net {
requires io.smallrye.common.constraint;
requires io.smallrye.common.os;
requires static io.smallrye.common.annotation;

requires io.smallrye.ffm;

requires org.jboss.logging;
requires static org.graalvm.nativeimage;
requires static org.graalvm.word;
requires static org.jboss.logging.annotations;

exports io.smallrye.common.net;
}
}
12 changes: 12 additions & 0 deletions net/src/test/java/io/smallrye/common/net/HostNameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.smallrye.common.net;

import org.junit.jupiter.api.Test;

public final class HostNameTest {
@Test
public void testHostName() {
System.out.println("Qualified name = " + HostName.getQualifiedHostName());
System.out.println("Host name = " + HostName.getHostName());
System.out.println("Node name = " + HostName.getNodeName());
}
}
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<version.vertx4>4.5.34</version.vertx4>
<version.vertx5>5.1.0</version.vertx5>
<version.maven-resolver>2.0.0</version.maven-resolver>
<version.smallrye-ffm>0.3</version.smallrye-ffm>
<version.org.jboss.logging>3.6.0.Final</version.org.jboss.logging>

<!-- Sonar settings -->
Expand Down Expand Up @@ -98,6 +99,11 @@
<version>${version.graalvm}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.smallrye.ffm</groupId>
<artifactId>smallrye-ffm</artifactId>
<version>${version.smallrye-ffm}</version>
</dependency>

<!-- External test dependencies -->
<dependency>
Expand Down Expand Up @@ -159,6 +165,19 @@
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>io.smallrye.ffm</groupId>
<artifactId>smallrye-ffm-maven-plugin</artifactId>
<version>${version.smallrye-ffm}</version>
<executions>
<execution>
<id>process-ffm</id>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.smallrye</groupId>
<artifactId>jandex-maven-plugin</artifactId>
Expand Down
Loading