From e22ba6e73a47de1d31462eb158bd48cf934ed56c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Hohwiller?= Date: Fri, 1 Sep 2023 15:20:56 +0200 Subject: [PATCH] #1090: support to detect OS and arch --- .../devonfw/tools/ide/common/SystemInfo.java | 59 +++++++++ .../tools/ide/common/SystemInfoImpl.java | 112 ++++++++++++++++++ .../tools/ide/context/AbstractIdeContext.java | 10 ++ .../devonfw/tools/ide/context/IdeContext.java | 6 + 4 files changed, 187 insertions(+) create mode 100644 ide/src/main/java/com/devonfw/tools/ide/common/SystemInfo.java create mode 100644 ide/src/main/java/com/devonfw/tools/ide/common/SystemInfoImpl.java diff --git a/ide/src/main/java/com/devonfw/tools/ide/common/SystemInfo.java b/ide/src/main/java/com/devonfw/tools/ide/common/SystemInfo.java new file mode 100644 index 000000000..8229cdaaf --- /dev/null +++ b/ide/src/main/java/com/devonfw/tools/ide/common/SystemInfo.java @@ -0,0 +1,59 @@ +package com.devonfw.tools.ide.common; + +import com.devonfw.tools.ide.version.VersionIdentifier; + +/** + * Interface to get information about the current machine running the IDE. + */ +public interface SystemInfo { + + /** + * @return the current {@link OperatingSystem}. + */ + OperatingSystem getOs(); + + /** + * @return the raw name of the current {@link OperatingSystem}. + */ + String getOsName(); + + /** + * @return the version of the current {@link OperatingSystem}. + */ + VersionIdentifier getOsVersion(); + + /** + * @return the raw name of the current {@link SystemArchitecture}. + */ + String getArchitectureName(); + + /** + * @return the current {@link SystemArchitecture}. + */ + SystemArchitecture getArchitecture(); + + /** + * @return {@code true} if we are on {@link OperatingSystem#WINDOWS windows}. + */ + default boolean isWindows() { + + return getOs() == OperatingSystem.WINDOWS; + } + + /** + * @return {@code true} if we are on {@link OperatingSystem#MAC mac OS}. + */ + default boolean isMac() { + + return getOs() == OperatingSystem.MAC; + } + + /** + * @return {@code true} if we are on {@link OperatingSystem#LINUX linux}. + */ + default boolean isLinux() { + + return getOs() == OperatingSystem.LINUX; + } + +} diff --git a/ide/src/main/java/com/devonfw/tools/ide/common/SystemInfoImpl.java b/ide/src/main/java/com/devonfw/tools/ide/common/SystemInfoImpl.java new file mode 100644 index 000000000..bf820440c --- /dev/null +++ b/ide/src/main/java/com/devonfw/tools/ide/common/SystemInfoImpl.java @@ -0,0 +1,112 @@ +package com.devonfw.tools.ide.common; + +import java.util.Locale; + +import com.devonfw.tools.ide.version.VersionIdentifier; + +/** + * Implementation of {@link SystemInfo}. + */ +public class SystemInfoImpl implements SystemInfo { + + /** Name of {@link System#getProperty(String) system-property} for {@link #getOsName()}: {@value}. */ + private static final String PROPERTY_OS_NAME = "os.name"; + + /** Name of {@link System#getProperty(String) system-property} for #getArchitectureName()}: {@value}. */ + private static final String PROPERTY_OS_ARCHITECTURE = "os.arch"; + + /** Name of {@link System#getProperty(String) system-property} for {@link #getOsVersion()}: {@value}. */ + private static final String PROPERTY_OS_VERSION = "os.version"; + + private final String osName; + + private final VersionIdentifier osVersion; + + private final OperatingSystem os; + + private final String architectureName; + + private final SystemArchitecture architecture; + + /** + * The constructor. + */ + public SystemInfoImpl() { + + super(); + this.osName = System.getProperty(PROPERTY_OS_NAME).trim(); + this.osVersion = VersionIdentifier.of(System.getProperty(PROPERTY_OS_VERSION).trim()); + this.architectureName = System.getProperty(PROPERTY_OS_ARCHITECTURE).trim(); + this.os = detectOs(this.osName); + this.architecture = detectArchitecture(this.architectureName); + } + + private static OperatingSystem detectOs(String osName) { + + String os = osName.toLowerCase(Locale.US); + if (os.startsWith("windows")) { + return OperatingSystem.WINDOWS; + } else if (os.startsWith("mac")) { + return OperatingSystem.MAC; + } else if (os.contains("linux")) { + return OperatingSystem.LINUX; + } else if (os.contains("bsd")) { + return OperatingSystem.LINUX; + } else if (os.contains("solaris") || os.contains("hp-ux") || os.contains("nix") || os.contains("aix") + || os.contains("nextstep") || os.contains("sorix") || os.contains("irix")) { + return OperatingSystem.LINUX; + } else { + System.err.println("ERROR: Unknown operating system '" + osName + "'"); + // be tolerant: most of our users are working on windows + // in case of an odd JVM or virtualization issue let us better continue than failing + return OperatingSystem.WINDOWS; + } + } + + private static SystemArchitecture detectArchitecture(String architectureName) { + + if (architectureName.contains("arm") || architectureName.contains("aarch")) { + return SystemArchitecture.ARM64; + } else { + return SystemArchitecture.X64; + } + } + + @Override + public OperatingSystem getOs() { + + return this.os; + } + + @Override + public String getOsName() { + + return this.osName; + } + + @Override + public VersionIdentifier getOsVersion() { + + return this.osVersion; + } + + @Override + public String getArchitectureName() { + + return this.architectureName; + } + + @Override + public SystemArchitecture getArchitecture() { + + return this.architecture; + } + + @Override + public String toString() { + + return this.os + "@" + this.architecture + "(" + this.osName + "[" + this.osVersion + "]@" + this.architectureName + + ")"; + } + +} diff --git a/ide/src/main/java/com/devonfw/tools/ide/context/AbstractIdeContext.java b/ide/src/main/java/com/devonfw/tools/ide/context/AbstractIdeContext.java index 78f7ac974..7fe586e6a 100644 --- a/ide/src/main/java/com/devonfw/tools/ide/context/AbstractIdeContext.java +++ b/ide/src/main/java/com/devonfw/tools/ide/context/AbstractIdeContext.java @@ -15,6 +15,8 @@ import java.util.function.Function; import java.util.stream.Stream; +import com.devonfw.tools.ide.common.SystemInfo; +import com.devonfw.tools.ide.common.SystemInfoImpl; import com.devonfw.tools.ide.environment.AbstractEnvironmentVariables; import com.devonfw.tools.ide.environment.EnvironmentVariables; import com.devonfw.tools.ide.environment.EnvironmentVariablesType; @@ -61,6 +63,8 @@ public abstract class AbstractIdeContext implements IdeContext { private final String path; + private final SystemInfo systemInfo; + private final EnvironmentVariables variables; private boolean offlineMode; @@ -93,6 +97,7 @@ public AbstractIdeContext(IdeLogLevel minLogLevel, Function