View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.utils;
20  
21  import java.io.File;
22  import java.util.Locale;
23  import java.util.stream.Stream;
24  
25  /**
26   * OS support
27   *
28   * @deprecated use {@code org.apache.maven.api.services.ModelBuilder} instead
29   */
30  @Deprecated(since = "4.0.0")
31  public class Os {
32  
33      /**
34       * The OS Name.
35       */
36      public static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
37  
38      /**
39       * The OA architecture.
40       */
41      public static final String OS_ARCH = System.getProperty("os.arch").toLowerCase(Locale.ENGLISH);
42  
43      /**
44       * The OS version.
45       */
46      public static final String OS_VERSION = System.getProperty("os.version").toLowerCase(Locale.ENGLISH);
47  
48      /**
49       * OS Family
50       */
51      public static final String OS_FAMILY;
52  
53      /**
54       * Boolean indicating if the running OS is a Windows system.
55       */
56      public static final boolean IS_WINDOWS;
57  
58      /**
59       * OS family that can be tested for. {@value}
60       */
61      private static final String FAMILY_WINDOWS = "windows";
62  
63      /**
64       * OS family that can be tested for. {@value}
65       */
66      private static final String FAMILY_WIN9X = "win9x";
67  
68      /**
69       * OS family that can be tested for. {@value}
70       */
71      public static final String FAMILY_NT = "winnt";
72  
73      /**
74       * OS family that can be tested for. {@value}
75       */
76      private static final String FAMILY_OS2 = "os/2";
77  
78      /**
79       * OS family that can be tested for. {@value}
80       */
81      private static final String FAMILY_NETWARE = "netware";
82  
83      /**
84       * OS family that can be tested for. {@value}
85       */
86      private static final String FAMILY_DOS = "dos";
87  
88      /**
89       * OS family that can be tested for. {@value}
90       */
91      private static final String FAMILY_MAC = "mac";
92  
93      /**
94       * OS family that can be tested for. {@value}
95       */
96      private static final String FAMILY_TANDEM = "tandem";
97  
98      /**
99       * OS family that can be tested for. {@value}
100      */
101     private static final String FAMILY_UNIX = "unix";
102 
103     /**
104      * OS family that can be tested for. {@value}
105      */
106     private static final String FAMILY_OPENVMS = "openvms";
107 
108     /**
109      * OS family that can be tested for. {@value}
110      */
111     private static final String FAMILY_ZOS = "z/os";
112 
113     /**
114      * OS family that can be tested for. {@value}
115      */
116     private static final String FAMILY_OS390 = "os/390";
117 
118     /**
119      * OS family that can be tested for. {@value}
120      */
121     private static final String FAMILY_OS400 = "os/400";
122 
123     /**
124      * OpenJDK is reported to call MacOS X "Darwin"
125      *
126      * @see <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=44889">bugzilla issue</a>
127      * @see <a href="https://issues.apache.org/jira/browse/HADOOP-3318">HADOOP-3318</a>
128      */
129     private static final String DARWIN = "darwin";
130 
131     /**
132      * The path separator.
133      */
134     private static final String PATH_SEP = System.getProperty("path.separator");
135 
136     static {
137         // Those two public constants are initialized here, as they need all the private constants
138         // above to be initialized first, but the code style imposes the public constants to be
139         // defined above the private ones...
140         OS_FAMILY = getOsFamily();
141         IS_WINDOWS = isFamily(FAMILY_WINDOWS);
142     }
143 
144     private Os() {}
145 
146     /**
147      * Determines if the OS on which Maven is executing matches the
148      * given OS family.
149      *
150      * @param family the family to check for
151      * @return true if the OS matches
152      *
153      */
154     public static boolean isFamily(String family) {
155         return isFamily(family, OS_NAME);
156     }
157 
158     /**
159      * Determines if the OS on which Maven is executing matches the
160      * given OS family derived from the given OS name
161      *
162      * @param family the family to check for
163      * @param actualOsName the OS name to check against
164      * @return true if the OS matches
165      *
166      */
167     public static boolean isFamily(String family, String actualOsName) {
168         // windows probing logic relies on the word 'windows' in the OS
169         boolean isWindows = actualOsName.contains(FAMILY_WINDOWS);
170         boolean is9x = false;
171         boolean isNT = false;
172         if (isWindows) {
173             // there are only four 9x platforms that we look for
174             is9x = (actualOsName.contains("95")
175                     || actualOsName.contains("98")
176                     || actualOsName.contains("me")
177                     // wince isn't really 9x, but crippled enough to
178                     // be a muchness. Maven doesnt run on CE, anyway.
179                     || actualOsName.contains("ce"));
180             isNT = !is9x;
181         }
182         return switch (family) {
183             case FAMILY_WINDOWS -> isWindows;
184             case FAMILY_WIN9X -> isWindows && is9x;
185             case FAMILY_NT -> isWindows && isNT;
186             case FAMILY_OS2 -> actualOsName.contains(FAMILY_OS2);
187             case FAMILY_NETWARE -> actualOsName.contains(FAMILY_NETWARE);
188             case FAMILY_DOS -> File.pathSeparatorChar == ';' && !isFamily(FAMILY_NETWARE, actualOsName) && !isWindows;
189             case FAMILY_MAC -> actualOsName.contains(FAMILY_MAC) || actualOsName.contains(DARWIN);
190             case FAMILY_TANDEM -> actualOsName.contains("nonstop_kernel");
191             case FAMILY_UNIX -> File.pathSeparatorChar == ':'
192                     && !isFamily(FAMILY_OPENVMS, actualOsName)
193                     && (!isFamily(FAMILY_MAC, actualOsName) || actualOsName.endsWith("x"));
194             case FAMILY_ZOS -> actualOsName.contains(FAMILY_ZOS) || actualOsName.contains(FAMILY_OS390);
195             case FAMILY_OS400 -> actualOsName.contains(FAMILY_OS400);
196             case FAMILY_OPENVMS -> actualOsName.contains(FAMILY_OPENVMS);
197             default -> actualOsName.contains(family.toLowerCase(Locale.US));
198         };
199     }
200 
201     /**
202      * Helper method to determine the current OS family.
203      *
204      * @return name of current OS family.
205      */
206     private static String getOsFamily() {
207         return Stream.of(
208                         FAMILY_DOS,
209                         FAMILY_MAC,
210                         FAMILY_NETWARE,
211                         FAMILY_NT,
212                         FAMILY_OPENVMS,
213                         FAMILY_OS2,
214                         FAMILY_OS400,
215                         FAMILY_TANDEM,
216                         FAMILY_UNIX,
217                         FAMILY_WIN9X,
218                         FAMILY_WINDOWS,
219                         FAMILY_ZOS)
220                 .filter(Os::isFamily)
221                 .findFirst()
222                 .orElse(null);
223     }
224 }