org.conqat.lib.commons.system.SystemUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-lib-commons Show documentation
Show all versions of teamscale-lib-commons Show documentation
Provides common utility functions
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.conqat.lib.commons.system;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Utility class providing functionality regarding the current system.
*/
public class SystemUtils {
/**
* Architecture of ARM based machines such as M1 Macs.
*/
private static final String ARM64_ARCHITECTURE = "aarch64";
/** Enumeration of operating systems. */
public enum EOperatingSystem {
/** Identifies a Microsoft Windows operating system. */
WINDOWS,
/** Identifies a Linux operating system. */
LINUX,
/** Identifies an Apple Mac operating system. */
MAC,
/** Identifies an unknown operating system. */
UNKNOWN;
/** Returns the operating system the Java VM runs in. */
private static EOperatingSystem getCurrent() {
String osName = getOperatingSystemName().toUpperCase();
for (EOperatingSystem system : values()) {
if (osName.startsWith(system.name())) {
return system;
}
}
return UNKNOWN;
}
}
/** Returns the operating system the Java VM runs in. */
public static EOperatingSystem getOperatingSystem() {
return EOperatingSystem.getCurrent();
}
/** Returns the operating system name the Java VM runs in. */
public static String getOperatingSystemName() {
return System.getProperty("os.name");
}
/** Returns true if the current operating system is Microsoft Windows. */
public static boolean isWindows() {
return getOperatingSystem() == EOperatingSystem.WINDOWS;
}
/** Returns true if the current operating system is Mac OS. */
public static boolean isMac() {
return getOperatingSystem() == EOperatingSystem.MAC;
}
/** Returns true if the current operating system is Linux. */
public static boolean isLinux() {
return getOperatingSystem() == EOperatingSystem.LINUX;
}
/**
* Returns the architecture name of the Java VM. This is neither returns the architecture of the
* processor nor the architecture of the operating system, although the property is prefixed with
* 'os.' (@see this site for more information). I.e. running a 32 bit JVM on a 64 bit Windows operating
* system will return 'x86'.
*/
public static String getJVMArchitectureName() {
return System.getProperty("os.arch");
}
/**
* Returns true
if the current Java VM runs with a 64 bit architecture. E.g. will
* return false
for a 32 bit JVM on a 64 bit operating system.
*/
public static boolean is64BitJVM() {
return getJVMArchitectureName().contains("64");
}
/**
* Returns true
if the current JVM runs natively on an ARM64 based CPU i.e. Apple
* Silicon.
*/
public static boolean isArm64Architecture() {
return ARM64_ARCHITECTURE.equals(getJVMArchitectureName());
}
/**
* Returns the host name of the machine running this JVM. Will return unknown
if the
* host cannot be determined.
*/
public static String getHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
return "unknown";
}
}
/**
* Returns an error message about visual studio runtime missing, if we are running on windows.
*/
public static String getUnsatisfiedLinkageErrorMessage(String libraryName) {
String baseMessage = "Failed to load " + libraryName + " native library.";
if (isWindows()) {
return baseMessage + " As of Teamscale 5.6, the 'Microsoft Visual C++ "
+ "Redistributable for Visual Studio 2015 - 2019' have to be installed and may be missing. "
+ "Please download and install it from here: http://cqse.eu/cpp-redistributables";
}
if (isLinux()) {
return baseMessage
+ " Please ensure that you have glibc >= 2.15 and glibc++ >= 3.4.22 installed (e.g., Ubuntu 2018.04 or RHEL 8) or use our docker images.";
}
return baseMessage;
}
}