|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.fluss.server.utils; |
| 19 | + |
| 20 | +import org.apache.fluss.annotation.Internal; |
| 21 | + |
| 22 | +/** An enumeration indicating the operating system that the JVM runs on. */ |
| 23 | +@Internal |
| 24 | +public enum OperatingSystem { |
| 25 | + LINUX, |
| 26 | + WINDOWS, |
| 27 | + MAC_OS, |
| 28 | + FREE_BSD, |
| 29 | + SOLARIS, |
| 30 | + UNKNOWN; |
| 31 | + |
| 32 | + // ------------------------------------------------------------------------ |
| 33 | + |
| 34 | + /** |
| 35 | + * Gets the operating system that the JVM runs on from the java system properties. this method |
| 36 | + * returns <tt>UNKNOWN</tt>, if the operating system was not successfully determined. |
| 37 | + * |
| 38 | + * @return The enum constant for the operating system, or <tt>UNKNOWN</tt>, if it was not |
| 39 | + * possible to determine. |
| 40 | + */ |
| 41 | + public static OperatingSystem getCurrentOperatingSystem() { |
| 42 | + return os; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Checks whether the operating system this JVM runs on is Windows. |
| 47 | + * |
| 48 | + * @return <code>true</code> if the operating system this JVM runs on is Windows, <code>false |
| 49 | + * </code> otherwise |
| 50 | + */ |
| 51 | + public static boolean isWindows() { |
| 52 | + return getCurrentOperatingSystem() == WINDOWS; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Checks whether the operating system this JVM runs on is Linux. |
| 57 | + * |
| 58 | + * @return <code>true</code> if the operating system this JVM runs on is Linux, <code>false |
| 59 | + * </code> otherwise |
| 60 | + */ |
| 61 | + public static boolean isLinux() { |
| 62 | + return getCurrentOperatingSystem() == LINUX; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Checks whether the operating system this JVM runs on is Windows. |
| 67 | + * |
| 68 | + * @return <code>true</code> if the operating system this JVM runs on is Windows, <code>false |
| 69 | + * </code> otherwise |
| 70 | + */ |
| 71 | + public static boolean isMac() { |
| 72 | + return getCurrentOperatingSystem() == MAC_OS; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Checks whether the operating system this JVM runs on is FreeBSD. |
| 77 | + * |
| 78 | + * @return <code>true</code> if the operating system this JVM runs on is FreeBSD, <code>false |
| 79 | + * </code> otherwise |
| 80 | + */ |
| 81 | + public static boolean isFreeBSD() { |
| 82 | + return getCurrentOperatingSystem() == FREE_BSD; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Checks whether the operating system this JVM runs on is Solaris. |
| 87 | + * |
| 88 | + * @return <code>true</code> if the operating system this JVM runs on is Solaris, <code>false |
| 89 | + * </code> otherwise |
| 90 | + */ |
| 91 | + public static boolean isSolaris() { |
| 92 | + return getCurrentOperatingSystem() == SOLARIS; |
| 93 | + } |
| 94 | + |
| 95 | + /** The enum constant for the operating system. */ |
| 96 | + private static final OperatingSystem os = readOSFromSystemProperties(); |
| 97 | + |
| 98 | + /** |
| 99 | + * Parses the operating system that the JVM runs on from the java system properties. If the |
| 100 | + * operating system was not successfully determined, this method returns {@code UNKNOWN}. |
| 101 | + * |
| 102 | + * @return The enum constant for the operating system, or {@code UNKNOWN}, if it was not |
| 103 | + * possible to determine. |
| 104 | + */ |
| 105 | + private static OperatingSystem readOSFromSystemProperties() { |
| 106 | + String osName = System.getProperty(OS_KEY); |
| 107 | + |
| 108 | + if (osName.startsWith(LINUX_OS_PREFIX)) { |
| 109 | + return LINUX; |
| 110 | + } |
| 111 | + if (osName.startsWith(WINDOWS_OS_PREFIX)) { |
| 112 | + return WINDOWS; |
| 113 | + } |
| 114 | + if (osName.startsWith(MAC_OS_PREFIX)) { |
| 115 | + return MAC_OS; |
| 116 | + } |
| 117 | + if (osName.startsWith(FREEBSD_OS_PREFIX)) { |
| 118 | + return FREE_BSD; |
| 119 | + } |
| 120 | + String osNameLowerCase = osName.toLowerCase(); |
| 121 | + if (osNameLowerCase.contains(SOLARIS_OS_INFIX_1) |
| 122 | + || osNameLowerCase.contains(SOLARIS_OS_INFIX_2)) { |
| 123 | + return SOLARIS; |
| 124 | + } |
| 125 | + |
| 126 | + return UNKNOWN; |
| 127 | + } |
| 128 | + |
| 129 | + // -------------------------------------------------------------------------------------------- |
| 130 | + // Constants to extract the OS type from the java environment |
| 131 | + // -------------------------------------------------------------------------------------------- |
| 132 | + |
| 133 | + /** The key to extract the operating system name from the system properties. */ |
| 134 | + private static final String OS_KEY = "os.name"; |
| 135 | + |
| 136 | + /** The expected prefix for Linux operating systems. */ |
| 137 | + private static final String LINUX_OS_PREFIX = "Linux"; |
| 138 | + |
| 139 | + /** The expected prefix for Windows operating systems. */ |
| 140 | + private static final String WINDOWS_OS_PREFIX = "Windows"; |
| 141 | + |
| 142 | + /** The expected prefix for Mac OS operating systems. */ |
| 143 | + private static final String MAC_OS_PREFIX = "Mac"; |
| 144 | + |
| 145 | + /** The expected prefix for FreeBSD. */ |
| 146 | + private static final String FREEBSD_OS_PREFIX = "FreeBSD"; |
| 147 | + |
| 148 | + /** One expected infix for Solaris. */ |
| 149 | + private static final String SOLARIS_OS_INFIX_1 = "sunos"; |
| 150 | + |
| 151 | + /** One expected infix for Solaris. */ |
| 152 | + private static final String SOLARIS_OS_INFIX_2 = "solaris"; |
| 153 | +} |
0 commit comments