Skip to content

Commit

Permalink
Fixes #36 new property "os.detected.bitness". (#41)
Browse files Browse the repository at this point in the history
Caveats:
  - might result in '31' for zOS legacy systems.
  - will report '32' for a 32-bit JVM running on a 64bit system.
  • Loading branch information
bmarwell authored May 23, 2020
1 parent b57a583 commit 3bed1ba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/kr/motd/maven/os/DetectExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
* <ul>
* <li>{@code os.detected.name} - normalized {@code os.name} (e.g. {@code linux}, {@code osx})</li>
* <li>{@code os.detected.arch} - normalized {@code os.arch} (e.g. {@code x86_64}, {@code x86_32})</li>
* <li>{@code os.detected.bitness} - bitness from wither {@code sun.arch.data.model} or {@code com.ibm.vm.bitmode} or {@code os.arch}
* (e.g. {@code 64}, {@code 32})</li>
* <li>{@code os.detected.version} - {@code os.detected.version.major}.{@code os.detected.version.minor}</li>
* <li>{@code os.detected.version.major} - major part of {@code os.version} (integer value)</li>
* <li>{@code os.detected.version.minor} - minor part of {@code os.version} (integer value)</li>
Expand Down Expand Up @@ -104,6 +106,7 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
final Map<String, String> dict = new LinkedHashMap<String, String>();
dict.put(Detector.DETECTED_NAME, sessionProps.getProperty(Detector.DETECTED_NAME));
dict.put(Detector.DETECTED_ARCH, sessionProps.getProperty(Detector.DETECTED_ARCH));
dict.put(Detector.DETECTED_BITNESS, sessionProps.getProperty(Detector.DETECTED_BITNESS));
dict.put(Detector.DETECTED_CLASSIFIER, sessionProps.getProperty(Detector.DETECTED_CLASSIFIER));
for (Map.Entry<Object, Object> entry : sessionProps.entrySet()) {
if (entry.getKey().toString().startsWith(Detector.DETECTED_RELEASE)) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/kr/motd/maven/os/DetectMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* <ul>
* <li>{@code os.detected.name} - normalized {@code os.name} (e.g. {@code linux}, {@code osx})</li>
* <li>{@code os.detected.arch} - normalized {@code os.arch} (e.g. {@code x86_64}, {@code x86_32})</li>
* <li>{@code os.detected.bitness} - bitness from wither {@code sun.arch.data.model} or {@code com.ibm.vm.bitmode} or {@code os.arch}
* (e.g. {@code 64}, {@code 32})</li>
* <li>{@code os.detected.classifier} - a shortcut for {@code 'os.detectedName'.'os.detectedArch'}
* (e.g. {@code linux-x86_64}). If the property {@code ${os.detection.classifierWithLikes}} is set,
* the first value for which a corresponding {@code os.detected.release.like.{variant}} property
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/kr/motd/maven/os/Detector.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public abstract class Detector {

public static final String DETECTED_NAME = "os.detected.name";
public static final String DETECTED_ARCH = "os.detected.arch";
public static final String DETECTED_BITNESS = "os.detected.bitness";
public static final String DETECTED_VERSION = "os.detected.version";
public static final String DETECTED_VERSION_MAJOR = DETECTED_VERSION + ".major";
public static final String DETECTED_VERSION_MINOR = DETECTED_VERSION + ".minor";
Expand Down Expand Up @@ -69,9 +70,11 @@ protected void detect(Properties props, List<String> classifierWithLikes) {

final String detectedName = normalizeOs(osName);
final String detectedArch = normalizeArch(osArch);
final int detectedBitness = determineBitness(detectedArch);

setProperty(props, DETECTED_NAME, detectedName);
setProperty(props, DETECTED_ARCH, detectedArch);
setProperty(props, DETECTED_BITNESS,"" + detectedBitness);

final Matcher versionMatcher = VERSION_REGEX.matcher(osVersion);
if (versionMatcher.matches()) {
Expand Down Expand Up @@ -364,6 +367,34 @@ private static String normalizeOsReleaseValue(String value) {
return value.trim().replace("\"", "");
}

public static int determineBitness(String architecture) {
// try the widely adopted sun specification first.
String bitness = System.getProperty("sun.arch.data.model", "");

if (!bitness.isEmpty() && bitness.matches("[0-9]+")) {
return Integer.parseInt(bitness, 10);
}

// bitness from sun.arch.data.model cannot be used. Try the IBM specification.
bitness = System.getProperty("com.ibm.vm.bitmode", "");

if (!bitness.isEmpty() && bitness.matches("[0-9]+")) {
return Integer.parseInt(bitness, 10);
}

// as a last resort, try to determine the bitness from the architecture.
return guessBitnessFromArchitecture(architecture);
}

public static int guessBitnessFromArchitecture(final String arch) {
if (arch.contains("64")) {
return 64;
}

return 32;
}


private static void closeQuietly(Closeable obj) {
try {
if (obj != null) {
Expand Down

0 comments on commit 3bed1ba

Please sign in to comment.