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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package io.github.eggy03.dmidecode.constant;

import org.jspecify.annotations.NonNull;

/**
* Enumeration of DMI types as defined by the
* {@code dmidecode} specification.
Expand Down Expand Up @@ -72,7 +74,8 @@ public enum DMIType {
this.value = value;
}

public static String getCommandFor(DMIType type) {
return "sudo /usr/sbin/dmidecode --type " + type.value;
@NonNull
public String getCommand() {
return "sudo /usr/sbin/dmidecode --type " + this.value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
*
* @since 0.1.0
*/
public class TerminalExecutionException extends RuntimeException {
public class TerminalIOException extends RuntimeException {

@SuppressWarnings("unused")
public TerminalExecutionException(String message, Throwable cause) {
public TerminalIOException(String message, Throwable cause) {
super(message, cause);
}

@SuppressWarnings("unused")
public TerminalExecutionException(String message) {
public TerminalIOException(String message) {
super(message);
}

@SuppressWarnings("unused")
public TerminalExecutionException(Throwable cause) {
public TerminalIOException(Throwable cause) {
super("Terminal Execution Failure", cause);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,24 @@
*/
public interface CommonDMIMapper<S> {

ObjectMapper jacksonMapper = new ObjectMapper();
/**
* Configure the {@link ObjectMapper} to be used for JSON processing.
*
* <p>
* The default implementation returns a new {@link ObjectMapper} instance with default configuration.
* </p>
*
* <p>
* Custom implementations may override this method to provide a custom-configured
* {@link ObjectMapper}.
* </p>
*
* @return the {@link ObjectMapper} to use
* @since 0.3.0
*/
default @NonNull ObjectMapper configureObjectMapper() {
return new ObjectMapper();
}

/**
* Maps raw {@code dmidecode} output into a single entity of type {@code <S>}.
Expand Down Expand Up @@ -96,6 +113,8 @@ public interface CommonDMIMapper<S> {
if (rawDMIData == null)
return Optional.empty();

ObjectMapper jacksonMapper = configureObjectMapper();

Map<String, Object> keyValueMap = new LinkedHashMap<>();

String key = null;
Expand Down Expand Up @@ -197,6 +216,8 @@ public interface CommonDMIMapper<S> {
if (rawDMIData == null)
return Collections.emptyList();

ObjectMapper jacksonMapper = configureObjectMapper();

List<S> entityList = new ArrayList<>();

Map<String, Object> keyValueMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
*
* @since 0.1.0
*/
public class DMIPortConnectionInformationMapper implements CommonDMIMapper<DMIPortConnectorInformation> {
public class DMIPortConnectorInformationMapper implements CommonDMIMapper<DMIPortConnectorInformation> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import io.github.eggy03.dmidecode.mapper.CommonDMIMapper;
import io.github.eggy03.dmidecode.mapper.board.DMIBIOSLanguageMapper;
import io.github.eggy03.dmidecode.service.OptionalCommonDMIServiceInterface;
import io.github.eggy03.dmidecode.utility.TerminalUtility;
import io.github.eggy03.dmidecode.terminal.TerminalResult;
import io.github.eggy03.dmidecode.terminal.TerminalService;
import org.jspecify.annotations.NonNull;

import java.util.Objects;
import java.util.Optional;

/**
Expand All @@ -34,6 +36,30 @@
*/
public class DMIBIOSLanguageService implements OptionalCommonDMIServiceInterface<DMIBIOSLanguage> {

private final TerminalService terminalService;
private final DMIBIOSLanguageMapper mapper;

/**
* Creates {@link DMIBIOSLanguageService} with default configuration.
*
* @since 0.3.0
*/
public DMIBIOSLanguageService() {
this(new TerminalService(), new DMIBIOSLanguageMapper());
}

/**
* Package Private constructor with injectable dependencies
*
* @param terminalService the {@link TerminalService} instance to use, must not be {@code null}
* @param mapper the mapper instance to use, must not be {@code null}
* @since 0.3.0
*/
DMIBIOSLanguageService(@NonNull TerminalService terminalService, @NonNull DMIBIOSLanguageMapper mapper) {
this.terminalService = Objects.requireNonNull(terminalService, "terminalService cannot be null");
this.mapper = Objects.requireNonNull(mapper, "mapper cannot be null");
}

/**
* Retrieves BIOS language information present in the system
* using an isolated {@code dmidecode} process with a configurable timeout.
Expand All @@ -50,9 +76,8 @@ public class DMIBIOSLanguageService implements OptionalCommonDMIServiceInterface
@Override
@InvokesFragileMethod(targetClass = CommonDMIMapper.class, methodType = MethodType.INTERFACE_DEFAULT_METHOD)
public @NonNull Optional<DMIBIOSLanguage> get(long timeout) {
return new DMIBIOSLanguageMapper().mapToEntity(
TerminalUtility.executeCommand(DMIType.getCommandFor(DMIType.BIOS_LANGUAGE), timeout),
DMIBIOSLanguage.class
);

TerminalResult result = terminalService.executeCommand(DMIType.BIOS_LANGUAGE, timeout);
return mapper.mapToEntity(result.getResult(), DMIBIOSLanguage.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
import io.github.eggy03.dmidecode.mapper.CommonDMIMapper;
import io.github.eggy03.dmidecode.mapper.board.DMIBIOSMapper;
import io.github.eggy03.dmidecode.service.CommonDMIServiceInterface;
import io.github.eggy03.dmidecode.utility.TerminalUtility;
import io.github.eggy03.dmidecode.terminal.TerminalResult;
import io.github.eggy03.dmidecode.terminal.TerminalService;
import org.jspecify.annotations.NonNull;

import java.util.List;
import java.util.Objects;

/**
* Service class for fetching BIOS information from the system.
Expand All @@ -35,6 +37,30 @@
*/
public class DMIBIOSService implements CommonDMIServiceInterface<DMIBIOS> {

private final TerminalService terminalService;
private final DMIBIOSMapper mapper;

/**
* Creates {@link DMIBIOSService} with default configuration.
*
* @since 0.3.0
*/
public DMIBIOSService() {
this(new TerminalService(), new DMIBIOSMapper());
}

/**
* Package Private constructor with injectable dependencies
*
* @param terminalService the {@link TerminalService} instance to use, must not be {@code null}
* @param mapper the mapper instance to use, must not be {@code null}
* @since 0.3.0
*/
DMIBIOSService(@NonNull TerminalService terminalService, @NonNull DMIBIOSMapper mapper) {
this.terminalService = Objects.requireNonNull(terminalService, "terminalService cannot be null");
this.mapper = Objects.requireNonNull(mapper, "mapper cannot be null");
}

/**
* Retrieves BIOS entries present in the system
* using an isolated {@code dmidecode} process with a configurable timeout.
Expand All @@ -51,9 +77,8 @@ public class DMIBIOSService implements CommonDMIServiceInterface<DMIBIOS> {
@Override
@InvokesFragileMethod(targetClass = CommonDMIMapper.class, methodType = MethodType.INTERFACE_DEFAULT_METHOD)
public @NonNull @Unmodifiable List<DMIBIOS> get(long timeout) {
return new DMIBIOSMapper().mapToList(
TerminalUtility.executeCommand(DMIType.getCommandFor(DMIType.BIOS), timeout),
DMIBIOS.class
);

TerminalResult result = terminalService.executeCommand(DMIType.BIOS, timeout);
return mapper.mapToList(result.getResult(), DMIBIOS.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import io.github.eggy03.dmidecode.mapper.CommonDMIMapper;
import io.github.eggy03.dmidecode.mapper.board.DMIBaseboardMapper;
import io.github.eggy03.dmidecode.service.OptionalCommonDMIServiceInterface;
import io.github.eggy03.dmidecode.utility.TerminalUtility;
import io.github.eggy03.dmidecode.terminal.TerminalResult;
import io.github.eggy03.dmidecode.terminal.TerminalService;
import org.jspecify.annotations.NonNull;

import java.util.Objects;
import java.util.Optional;

/**
Expand All @@ -35,6 +37,30 @@
*/
public class DMIBaseboardService implements OptionalCommonDMIServiceInterface<DMIBaseboard> {

private final TerminalService terminalService;
private final DMIBaseboardMapper mapper;

/**
* Creates {@link DMIBaseboardService} with default configuration.
*
* @since 0.3.0
*/
public DMIBaseboardService() {
this(new TerminalService(), new DMIBaseboardMapper());
}

/**
* Package Private constructor with injectable dependencies
*
* @param terminalService the {@link TerminalService} instance to use, must not be {@code null}
* @param mapper the mapper instance to use, must not be {@code null}
* @since 0.3.0
*/
DMIBaseboardService(@NonNull TerminalService terminalService, @NonNull DMIBaseboardMapper mapper) {
this.terminalService = Objects.requireNonNull(terminalService, "terminalService cannot be null");
this.mapper = Objects.requireNonNull(mapper, "mapper cannot be null");
}

/**
* Retrieves baseboard information present in the system
* using an isolated {@code dmidecode} process with a configurable timeout.
Expand All @@ -51,9 +77,8 @@ public class DMIBaseboardService implements OptionalCommonDMIServiceInterface<DM
@Override
@InvokesFragileMethod(targetClass = CommonDMIMapper.class, methodType = MethodType.INTERFACE_DEFAULT_METHOD)
public @NonNull Optional<DMIBaseboard> get(long timeout) {
return new DMIBaseboardMapper().mapToEntity(
TerminalUtility.executeCommand(DMIType.getCommandFor(DMIType.BASEBOARD), timeout),
DMIBaseboard.class
);

TerminalResult result = terminalService.executeCommand(DMIType.BASEBOARD, timeout);
return mapper.mapToEntity(result.getResult(), DMIBaseboard.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import io.github.eggy03.dmidecode.mapper.CommonDMIMapper;
import io.github.eggy03.dmidecode.mapper.board.DMIChassisMapper;
import io.github.eggy03.dmidecode.service.OptionalCommonDMIServiceInterface;
import io.github.eggy03.dmidecode.utility.TerminalUtility;
import io.github.eggy03.dmidecode.terminal.TerminalResult;
import io.github.eggy03.dmidecode.terminal.TerminalService;
import org.jspecify.annotations.NonNull;

import java.util.Objects;
import java.util.Optional;

/**
Expand All @@ -34,6 +36,30 @@
*/
public class DMIChassisService implements OptionalCommonDMIServiceInterface<DMIChassis> {

private final TerminalService terminalService;
private final DMIChassisMapper mapper;

/**
* Creates {@link DMIChassisService} with default configuration.
*
* @since 0.3.0
*/
public DMIChassisService() {
this(new TerminalService(), new DMIChassisMapper());
}

/**
* Package Private constructor with injectable dependencies
*
* @param terminalService the {@link TerminalService} instance to use, must not be {@code null}
* @param mapper the mapper instance to use, must not be {@code null}
* @since 0.3.0
*/
DMIChassisService(@NonNull TerminalService terminalService, @NonNull DMIChassisMapper mapper) {
this.terminalService = Objects.requireNonNull(terminalService, "terminalService cannot be null");
this.mapper = Objects.requireNonNull(mapper, "mapper cannot be null");
}

/**
* Retrieves chassis information present in the system
* using an isolated {@code dmidecode} process with a configurable timeout.
Expand All @@ -50,9 +76,8 @@ public class DMIChassisService implements OptionalCommonDMIServiceInterface<DMIC
@Override
@InvokesFragileMethod(targetClass = CommonDMIMapper.class, methodType = MethodType.INTERFACE_DEFAULT_METHOD)
public @NonNull Optional<DMIChassis> get(long timeout) {
return new DMIChassisMapper().mapToEntity(
TerminalUtility.executeCommand(DMIType.getCommandFor(DMIType.CHASSIS), timeout),
DMIChassis.class
);

TerminalResult result = terminalService.executeCommand(DMIType.CHASSIS, timeout);
return mapper.mapToEntity(result.getResult(), DMIChassis.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import io.github.eggy03.dmidecode.constant.DMIType;
import io.github.eggy03.dmidecode.entity.board.DMIPortConnectorInformation;
import io.github.eggy03.dmidecode.mapper.CommonDMIMapper;
import io.github.eggy03.dmidecode.mapper.board.DMIPortConnectionInformationMapper;
import io.github.eggy03.dmidecode.mapper.board.DMIPortConnectorInformationMapper;
import io.github.eggy03.dmidecode.service.CommonDMIServiceInterface;
import io.github.eggy03.dmidecode.utility.TerminalUtility;
import io.github.eggy03.dmidecode.terminal.TerminalResult;
import io.github.eggy03.dmidecode.terminal.TerminalService;
import org.jspecify.annotations.NonNull;

import java.util.List;
import java.util.Objects;

/**
* Service class for fetching port connector information from the system.
Expand All @@ -35,6 +37,30 @@
*/
public class DMIPortConnectorInformationService implements CommonDMIServiceInterface<DMIPortConnectorInformation> {

private final TerminalService terminalService;
private final DMIPortConnectorInformationMapper mapper;

/**
* Creates {@link DMIPortConnectorInformationService} with default configuration.
*
* @since 0.3.0
*/
public DMIPortConnectorInformationService() {
this(new TerminalService(), new DMIPortConnectorInformationMapper());
}

/**
* Package Private constructor with injectable dependencies
*
* @param terminalService the {@link TerminalService} instance to use, must not be {@code null}
* @param mapper the mapper instance to use, must not be {@code null}
* @since 0.3.0
*/
DMIPortConnectorInformationService(@NonNull TerminalService terminalService, @NonNull DMIPortConnectorInformationMapper mapper) {
this.terminalService = Objects.requireNonNull(terminalService, "terminalService cannot be null");
this.mapper = Objects.requireNonNull(mapper, "mapper cannot be null");
}

/**
* Retrieves port connector entries present in the system
* using an isolated {@code dmidecode} process with a configurable timeout.
Expand All @@ -52,9 +78,8 @@ public class DMIPortConnectorInformationService implements CommonDMIServiceInter
@Override
@InvokesFragileMethod(targetClass = CommonDMIMapper.class, methodType = MethodType.INTERFACE_DEFAULT_METHOD)
public @Unmodifiable @NonNull List<DMIPortConnectorInformation> get(long timeout) {
return new DMIPortConnectionInformationMapper().mapToList(
TerminalUtility.executeCommand(DMIType.getCommandFor(DMIType.PORT_CONNECTOR), timeout),
DMIPortConnectorInformation.class
);

TerminalResult result = terminalService.executeCommand(DMIType.PORT_CONNECTOR, timeout);
return mapper.mapToList(result.getResult(), DMIPortConnectorInformation.class);
}
}
Loading
Loading