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 {@link 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     static {
132         // Those two public constants are initialized here, as they need all the private constants
133         // above to be initialized first, but the code style imposes the public constants to be
134         // defined above the private ones...
135         OS_FAMILY = getOsFamily();
136         IS_WINDOWS = isFamily(FAMILY_WINDOWS);
137     }
138 
139     private Os() {}
140 
141     /**
142      * Determines if the OS on which Maven is executing matches the
143      * given OS family.
144      *
145      * @param family the family to check for
146      * @return true if the OS matches
147      *
148      */
149     public static boolean isFamily(String family) {
150         return isFamily(family, OS_NAME);
151     }
152 
153     /**
154      * Determines if the OS on which Maven is executing matches the
155      * given OS family derived from the given OS name
156      *
157      * @param family the family to check for
158      * @param actualOsName the OS name to check against
159      * @return true if the OS matches
160      *
161      */
162     public static boolean isFamily(String family, String actualOsName) {
163         // windows probing logic relies on the word 'windows' in the OS
164         boolean isWindows = actualOsName.contains(FAMILY_WINDOWS);
165         boolean is9x = false;
166         boolean isNT = false;
167         if (isWindows) {
168             // there are only four 9x platforms that we look for
169             is9x = (actualOsName.contains("95")
170                     || actualOsName.contains("98")
171                     || actualOsName.contains("me")
172                     // wince isn't really 9x, but crippled enough to
173                     // be a muchness. Maven doesnt run on CE, anyway.
174                     || actualOsName.contains("ce"));
175             isNT = !is9x;
176         }
177         switch (family) {
178             case FAMILY_WINDOWS:
179                 return isWindows;
180             case FAMILY_WIN9X:
181                 return isWindows && is9x;
182             case FAMILY_NT:
183                 return isWindows && isNT;
184             case FAMILY_OS2:
185                 return actualOsName.contains(FAMILY_OS2);
186             case FAMILY_NETWARE:
187                 return actualOsName.contains(FAMILY_NETWARE);
188             case FAMILY_DOS:
189                 return File.pathSeparatorChar == ';' && !isFamily(FAMILY_NETWARE, actualOsName) && !isWindows;
190             case FAMILY_MAC:
191                 return actualOsName.contains(FAMILY_MAC) || actualOsName.contains(DARWIN);
192             case FAMILY_TANDEM:
193                 return actualOsName.contains("nonstop_kernel");
194             case FAMILY_UNIX:
195                 return File.pathSeparatorChar == ':'
196                         && !isFamily(FAMILY_OPENVMS, actualOsName)
197                         && (!isFamily(FAMILY_MAC, actualOsName) || actualOsName.endsWith("x"));
198             case FAMILY_ZOS:
199                 return actualOsName.contains(FAMILY_ZOS) || actualOsName.contains(FAMILY_OS390);
200             case FAMILY_OS400:
201                 return actualOsName.contains(FAMILY_OS400);
202             case FAMILY_OPENVMS:
203                 return actualOsName.contains(FAMILY_OPENVMS);
204             default:
205                 return actualOsName.contains(family.toLowerCase(Locale.US));
206         }
207     }
208 
209     /**
210      * Helper method to determine the current OS family.
211      *
212      * @return name of current OS family.
213      */
214     private static String getOsFamily() {
215         return Stream.of(
216                         FAMILY_DOS,
217                         FAMILY_MAC,
218                         FAMILY_NETWARE,
219                         FAMILY_NT,
220                         FAMILY_OPENVMS,
221                         FAMILY_OS2,
222                         FAMILY_OS400,
223                         FAMILY_TANDEM,
224                         FAMILY_UNIX,
225                         FAMILY_WIN9X,
226                         FAMILY_WINDOWS,
227                         FAMILY_ZOS)
228                 .filter(Os::isFamily)
229                 .findFirst()
230                 .orElse(null);
231     }
232 }