forked from devonfw/ide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
devonfw#1090: support to detect OS and arch
- Loading branch information
Showing
4 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
ide/src/main/java/com/devonfw/tools/ide/common/SystemInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
112 changes: 112 additions & 0 deletions
112
ide/src/main/java/com/devonfw/tools/ide/common/SystemInfoImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
+ ")"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters