trying to implement some sd_journal in JNA, without success

This commit is contained in:
Jörg Prante 2021-01-11 17:07:58 +01:00
parent 7c7303ed96
commit c39a8630bc
6 changed files with 30 additions and 27 deletions

View file

@ -2,6 +2,8 @@ package org.xbib.log4j.systemd;
import com.sun.jna.Native;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;
/**
@ -33,7 +35,7 @@ public class SystemdLibraryAPI {
private static SystemdLibrary loadLibrary() {
try {
return Native.load("systemd", SystemdLibrary.class);
return (SystemdLibrary) AccessController.doPrivileged((PrivilegedAction<Object>) () -> Native.load("systemd", SystemdLibrary.class));
} catch (UnsatisfiedLinkError e) {
throw new RuntimeException("Failed to load systemd library." +
" Please note that JNA requires an executable temporary folder." +

View file

@ -19,6 +19,6 @@ class SystemdJournalReaderTest {
entry -> logger.log(Level.INFO, entry.toString()));
Executors.newSingleThreadExecutor().submit(consumer);
// consuming for some seconds
Thread.sleep(10000L);
Thread.sleep(60000L);
}
}

View file

@ -1,7 +0,0 @@
package org.xbib.systemd.journal;
import com.sun.jna.Structure;
public class SdJournal extends Structure {
public int toplevel_fd;
}

View file

@ -13,7 +13,7 @@ import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.xbib.systemd.journal.SystemdLibraryAPI.SD_JOURNAL_LOCAL_ONLY;
import static org.xbib.systemd.journal.SystemdLibraryInstance.SD_JOURNAL_LOCAL_ONLY;
public class SystemdJournalConsumer implements Runnable {
@ -45,8 +45,8 @@ public class SystemdJournalConsumer implements Runnable {
}
private void loop() throws IOException {
SystemdLibraryAPI api = SystemdLibraryAPI.getInstance();
SdJournal sdJournal = new SdJournal();
SystemdLibraryInstance api = SystemdLibraryInstance.getInstance();
SystemdLibrary.SdJournal sdJournal = new SystemdLibrary.SdJournal();
logger.log(Level.INFO, "opening");
int rc = api.sd_journal_open(sdJournal, SD_JOURNAL_LOCAL_ONLY);
logger.log(Level.INFO, "open: " + rc);

View file

@ -3,6 +3,7 @@ package org.xbib.systemd.journal;
import com.sun.jna.Library;
import com.sun.jna.Pointer;
import com.sun.jna.StringArray;
import com.sun.jna.Structure;
public interface SystemdLibrary extends Library {
@ -29,4 +30,11 @@ public interface SystemdLibrary extends Library {
int sd_journal_enumerate_data(SdJournal sdJournal, Pointer data, Pointer length);
int sd_journal_restart_data(SdJournal sdJournal);
class SdJournal extends Structure {
public int top_level_fd;
public String path;
public String prefix;
public String namespace;
}
}

View file

@ -11,17 +11,17 @@ import java.util.List;
*
* The native library is loaded only once, so this class is a singleton.
*/
public class SystemdLibraryAPI {
public class SystemdLibraryInstance {
private static final SystemdLibraryAPI instance = new SystemdLibraryAPI();
private static final SystemdLibraryInstance instance = new SystemdLibraryInstance();
private final SystemdLibrary systemdLibrary;
private SystemdLibraryAPI() {
private SystemdLibraryInstance() {
this.systemdLibrary = loadLibrary();
}
public static SystemdLibraryAPI getInstance() {
public static SystemdLibraryInstance getInstance() {
return instance;
}
@ -49,47 +49,47 @@ public class SystemdLibraryAPI {
return systemdLibrary.sd_journal_send(format, args.toArray());
}
public int sd_journal_open(SdJournal sdJournal, int flag) {
public int sd_journal_open(SystemdLibrary.SdJournal sdJournal, int flag) {
return systemdLibrary.sd_journal_open(sdJournal, flag);
}
public int sd_journal_get_fd(SdJournal sdJournal) {
public int sd_journal_get_fd(SystemdLibrary.SdJournal sdJournal) {
return systemdLibrary.sd_journal_get_fd(sdJournal);
}
public int sd_journal_seek_tail(SdJournal sdJournal) {
public int sd_journal_seek_tail(SystemdLibrary.SdJournal sdJournal) {
return systemdLibrary.sd_journal_seek_tail(sdJournal);
}
public int sd_journal_previous(SdJournal sdJournal) {
public int sd_journal_previous(SystemdLibrary.SdJournal sdJournal) {
return systemdLibrary.sd_journal_previous(sdJournal);
}
public int sd_journal_next(SdJournal sdJournal) {
public int sd_journal_next(SystemdLibrary.SdJournal sdJournal) {
return systemdLibrary.sd_journal_next(sdJournal);
}
public int sd_journal_get_cursor(SdJournal sdJournal, StringArray cursor) {
public int sd_journal_get_cursor(SystemdLibrary.SdJournal sdJournal, StringArray cursor) {
return systemdLibrary.sd_journal_get_cursor(sdJournal, cursor);
}
public int sd_journal_add_match(SdJournal sdJournal, String match, int len) {
public int sd_journal_add_match(SystemdLibrary.SdJournal sdJournal, String match, int len) {
return systemdLibrary.sd_journal_add_match(sdJournal, match, len);
}
public int sd_journal_wait(SdJournal sdJournal, long timeout_musec) {
public int sd_journal_wait(SystemdLibrary.SdJournal sdJournal, long timeout_musec) {
return systemdLibrary.sd_journal_wait(sdJournal, timeout_musec);
}
public int sd_journal_get_data(SdJournal sdJournal, String field, Pointer data, Pointer length) {
public int sd_journal_get_data(SystemdLibrary.SdJournal sdJournal, String field, Pointer data, Pointer length) {
return systemdLibrary.sd_journal_get_data(sdJournal, field, data, length);
}
public int sd_journal_enumerate_data(SdJournal sdJournal, Pointer data, Pointer length) {
public int sd_journal_enumerate_data(SystemdLibrary.SdJournal sdJournal, Pointer data, Pointer length) {
return systemdLibrary.sd_journal_enumerate_data(sdJournal, data, length);
}
public int sd_journal_restart_data(SdJournal sdJournal) {
public int sd_journal_restart_data(SystemdLibrary.SdJournal sdJournal) {
return systemdLibrary.sd_journal_restart_data(sdJournal);
}