Skip to content

Commit

Permalink
Fixes #12
Browse files Browse the repository at this point in the history
Reintroduces checks to ensure that the native library is not loaded unless we are running under Linux with a notify socket available.
  • Loading branch information
jpmsilva committed Jan 24, 2024
1 parent df4c674 commit 13ac154
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.github.jpmsilva.jsystemd;

import static com.github.jpmsilva.jsystemd.SystemdUtilities.hasNotifySocket;
import static com.github.jpmsilva.jsystemd.SystemdUtilities.isLinux;
import static java.lang.invoke.MethodHandles.lookup;
import static java.util.Objects.requireNonNull;
import static org.slf4j.LoggerFactory.getLogger;
Expand Down Expand Up @@ -133,7 +135,7 @@ public static void registerShutdownHook() {
* However, you may wish of explicitly close the integration channel, to ensure that all closeable resources are effectively closed.<br> As such, this method
* should only be called at most once during the lifecycle of the JVM.
*
* <p>Currenty this does nothing, as currently the only supported integration channel is the native libsystemd library, which does not need any cleanup.
* <p>Currently this does nothing, as currently the only supported integration channel is the native libsystemd library, which does not need any cleanup.
*/
public static void close() {
}
Expand All @@ -143,10 +145,12 @@ private static class Library {
private static boolean initialized = false;

static {
try {
Native.register("systemd");
initialized = true;
} catch (UnsatisfiedLinkError ignored) {
if (isLinux() && hasNotifySocket()) {
try {
Native.register("systemd");
initialized = true;
} catch (UnsatisfiedLinkError ignored) {
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static boolean isLinux() {
}

static boolean hasNotifySocket() {
return notifySocketPath != null;
return notifySocketPath != null && notifySocketPath.toFile().exists();
}

private static final String[] unitPrefixes = new String[]{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei"};
Expand Down
13 changes: 12 additions & 1 deletion jsystemd-spring-boot-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ limitations under the License.
<parent>
<groupId>com.github.jpmsilva.jsystemd</groupId>
<artifactId>jsystemd</artifactId>
<version>3.0.1-SNAPSHOT</version>
<version>3.1.1-SNAPSHOT</version>
</parent>

<name>jsystemd sample Spring Boot application</name>
Expand Down Expand Up @@ -69,6 +69,17 @@ limitations under the License.
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${org.springframework.boot.version}</version>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.github.jpmsilva.jsystemd.SampleApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class SystemdAutoConfiguration {
*/
@EventListener
public void started(@SuppressWarnings("unused") AvailabilityChangeEvent<ReadinessState> event) {
if(event.getState() == ReadinessState.ACCEPTING_TRAFFIC) {
if (event.getState() == ReadinessState.ACCEPTING_TRAFFIC) {
systemd.ready();
}
}
Expand All @@ -84,12 +84,6 @@ SystemdLifecycle systemdLifecycle() {
return new SystemdLifecycle(systemd);
}

@Bean
@NotNull
SystemdServletContextListener systemdServletContextListener() {
return new SystemdServletContextListener();
}

@Bean
@NotNull
SystemdStatusProvider systemdNotifyHeapStatus() {
Expand Down
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ limitations under the License.
<profile>
<id>sample</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>!performRelease</name>
</property>
</activation>
<modules>
<module>jsystemd-spring-boot-sample</module>
Expand Down

0 comments on commit 13ac154

Please sign in to comment.