001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 the ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.system;
018
019import java.net.InetAddress;
020import java.net.UnknownHostException;
021
022/**
023 * Utility class providing functionality regarding the current system.
024 */
025public class SystemUtils {
026
027        /** Enumeration of operating systems. */
028        public static enum EOperatingSystem {
029
030                /** Identifies a Microsoft Windows operating system. */
031                WINDOWS,
032
033                /** Identifies a Linux operating system. */
034                LINUX,
035
036                /** Identifies an Apple Mac operating system. */
037                MAC,
038
039                /** Identifies an unknown operating system. */
040                UNKNOWN;
041
042                /** Returns the operating system the Java VM runs in. */
043                private static EOperatingSystem getCurrent() {
044                        String osName = getOperatingSystemName().toUpperCase();
045                        for (EOperatingSystem system : values()) {
046                                if (osName.startsWith(system.name())) {
047                                        return system;
048                                }
049                        }
050                        return UNKNOWN;
051                }
052        }
053
054        /** Returns the operating system the Java VM runs in. */
055        public static EOperatingSystem getOperatingSystem() {
056                return EOperatingSystem.getCurrent();
057        }
058
059        /** Returns the operating system name the Java VM runs in. */
060        public static String getOperatingSystemName() {
061                return System.getProperty("os.name");
062        }
063
064        /** Returns true if the current operating system is Microsoft Windows. */
065        public static boolean isWindows() {
066                return SystemUtils.getOperatingSystem() == EOperatingSystem.WINDOWS;
067        }
068
069        /** Returns true if the current operating system is Mac OS. */
070        public static boolean isMac() {
071                return SystemUtils.getOperatingSystem() == EOperatingSystem.MAC;
072        }
073
074        /**
075         * Returns the architecture name of the Java VM. This is neither returns the
076         * architecture of the processor nor the architecture of the operating system,
077         * although the property is prefixed with 'os.' (@see <a href=
078         * "http://mark.koli.ch/javas-osarch-system-property-is-the-bitness-of-the-jre-not-the-operating-system"
079         * >this site</a> for more information). I.e. running a 32 bit JVM on a 64 bit
080         * Windows operating system will return 'x86'.
081         */
082        public static String getJVMArchitectureName() {
083                return System.getProperty("os.arch");
084        }
085
086        /**
087         * Returns <code>true</code> if the current Java VM runs with a 64 bit
088         * architecture. E.g. will return <code>false</code> for a 32 bit JVM on a 64
089         * bit operating system.
090         */
091        public static boolean is64BitJVM() {
092                return getJVMArchitectureName().contains("64");
093        }
094
095        /**
096         * Returns the host name of the machine running this JVM. Will return
097         * <code>unknown</code> if the host cannot be determined.
098         */
099        public static String getHostName() {
100                try {
101                        return InetAddress.getLocalHost().getHostName();
102                } catch (UnknownHostException e) {
103                        return "unknown";
104                }
105        }
106
107        /**
108         * Returns an error message about visual studio runtime missing, if we are
109         * running on windows.
110         */
111        public static String getVisualStudioRuntimeErrorMessage(String libraryName) {
112                if (isWindows()) {
113                        return "Failed to load " + libraryName + " native library. As of Teamscale 5.6, the 'Microsoft Visual C++ "
114                                        + "Redistributable for Visual Studio 2015 - 2019' have to be installed and may be missing. "
115                                        + "Please download and install it from here: http://cqse.eu/cpp-redistributables";
116                }
117                return "Failed to load " + libraryName + " native library.";
118        }
119}