View Javadoc
1   /*
2    * The Apache Software License, Version 1.1
3    *
4    * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
5    * reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 1. Redistributions of source code must retain the above copyright
12   *    notice, this list of conditions and the following disclaimer.
13   *
14   * 2. Redistributions in binary form must reproduce the above copyright
15   *    notice, this list of conditions and the following disclaimer in
16   *    the documentation and/or other materials provided with the
17   *    distribution.
18   *
19   * 3. The end-user documentation included with the redistribution, if
20   *    any, must include the following acknowlegement:
21   *       "This product includes software developed by the
22   *        Apache Software Foundation (http://www.apache.org/)."
23   *    Alternately, this acknowlegement may appear in the software itself,
24   *    if and wherever such third-party acknowlegements normally appear.
25   *
26   * 4. The names "Ant" and "Apache Software
27   *    Foundation" must not be used to endorse or promote products derived
28   *    from this software without prior written permission. For written
29   *    permission, please contact apache@apache.org.
30   *
31   * 5. Products derived from this software may not be called "Apache"
32   *    nor may "Apache" appear in their names without prior written
33   *    permission of the Apache Group.
34   *
35   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46   * SUCH DAMAGE.
47   * ====================================================================
48   *
49   * This software consists of voluntary contributions made by many
50   * individuals on behalf of the Apache Software Foundation.  For more
51   * information on the Apache Software Foundation, please see
52   * <http://www.apache.org/>.
53   */
54  
55  package org.codehaus.plexus.interpolation.os;
56  
57  import java.util.HashSet;
58  import java.util.Locale;
59  import java.util.Set;
60  
61  /**
62   * <b>NOTE:</b> This class was copied from plexus-utils, to allow this library
63   * to stand completely self-contained.
64   * <br/>
65   * Condition that tests the OS type.
66   *
67   * @author Stefan Bodewig
68   * @author Magesh Umasankar
69   * @author Brian Fox
70   * @since 1.0
71   * @version $Id$
72   */
73  public class Os
74  {
75      // define the families for easier reference
76      public static final String FAMILY_DOS = "dos";
77  
78      public static final String FAMILY_MAC = "mac";
79  
80      public static final String FAMILY_NETWARE = "netware";
81  
82      public static final String FAMILY_OS2 = "os/2";
83  
84      public static final String FAMILY_TANDEM = "tandem";
85  
86      public static final String FAMILY_UNIX = "unix";
87  
88      public static final String FAMILY_WINDOWS = "windows";
89  
90      public static final String FAMILY_WIN9X = "win9x";
91  
92      public static final String FAMILY_ZOS = "z/os";
93  
94      public static final String FAMILY_OS400 = "os/400";
95  
96      public static final String FAMILY_OPENVMS = "openvms";
97  
98      // store the valid families
99      private static final Set<String> validFamilies = setValidFamilies();
100 
101     // get the current info
102     private static final String PATH_SEP = System.getProperty( "path.separator" );
103 
104     public static final String OS_NAME = System.getProperty( "os.name" ).toLowerCase( Locale.US );
105 
106     public static final String OS_ARCH = System.getProperty( "os.arch" ).toLowerCase( Locale.US );
107 
108     public static final String OS_VERSION = System.getProperty( "os.version" ).toLowerCase( Locale.US );
109 
110     // Make sure this method is called after static fields it depends on have been set!
111     public static final String OS_FAMILY = getOsFamily();
112 
113     private String family;
114 
115     private String name;
116 
117     private String version;
118 
119     private String arch;
120 
121     /**
122      * Default constructor
123      */
124     public Os()
125     {
126     }
127 
128     /**
129      * Constructor that sets the family attribute
130      *
131      * @param family a String value
132      */
133     public Os( String family )
134     {
135         setFamily( family );
136     }
137 
138     /**
139      * Initializes the set of valid families.
140      */
141     private static Set<String> setValidFamilies()
142     {
143         Set<String> valid = new HashSet<String>();
144         valid.add( FAMILY_DOS );
145         valid.add( FAMILY_MAC );
146         valid.add( FAMILY_NETWARE );
147         valid.add( FAMILY_OS2 );
148         valid.add( FAMILY_TANDEM );
149         valid.add( FAMILY_UNIX );
150         valid.add( FAMILY_WINDOWS );
151         valid.add( FAMILY_WIN9X );
152         valid.add( FAMILY_ZOS );
153         valid.add( FAMILY_OS400 );
154         valid.add( FAMILY_OPENVMS );
155 
156         return valid;
157     }
158 
159     /**
160      * Sets the desired OS family type
161      *
162      * @param f The OS family type desired<br />
163      *            Possible values:<br />
164      *            <ul>
165      *            <li>dos</li>
166      *            <li>mac</li>
167      *            <li>netware</li>
168      *            <li>os/2</li>
169      *            <li>tandem</li>
170      *            <li>unix</li>
171      *            <li>windows</li>
172      *            <li>win9x</li>
173      *            <li>z/os</li>
174      *            <li>os/400</li>
175      *            <li>openvms</li>
176      *            </ul>
177      */
178     public void setFamily( String f )
179     {
180         family = f.toLowerCase( Locale.US );
181     }
182 
183     /**
184      * Sets the desired OS name
185      *
186      * @param name The OS name
187      */
188     public void setName( String name )
189     {
190         this.name = name.toLowerCase( Locale.US );
191     }
192 
193     /**
194      * Sets the desired OS architecture
195      *
196      * @param arch The OS architecture
197      */
198     public void setArch( String arch )
199     {
200         this.arch = arch.toLowerCase( Locale.US );
201     }
202 
203     /**
204      * Sets the desired OS version
205      *
206      * @param version The OS version
207      */
208     public void setVersion( String version )
209     {
210         this.version = version.toLowerCase( Locale.US );
211     }
212 
213     /**
214      * Determines if the current OS matches the type of that
215      * set in setFamily.
216      *
217      * @see Os#setFamily(String)
218      */
219     public boolean eval()
220         throws Exception
221     {
222         return isOs( family, name, arch, version );
223     }
224 
225     /**
226      * Determines if the current OS matches the given OS
227      * family.
228      *
229      * @param family the family to check for
230      * @return true if the OS matches
231      * @since 1.0
232      */
233     public static boolean isFamily( String family )
234     {
235         return isOs( family, null, null, null );
236     }
237 
238     /**
239      * Determines if the current OS matches the given OS
240      * name.
241      *
242      * @param name the OS name to check for
243      * @return true if the OS matches
244      * @since 1.0
245      */
246     public static boolean isName( String name )
247     {
248         return isOs( null, name, null, null );
249     }
250 
251     /**
252      * Determines if the current OS matches the given OS
253      * architecture.
254      *
255      * @param arch the OS architecture to check for
256      * @return true if the OS matches
257      * @since 1.0
258      */
259     public static boolean isArch( String arch )
260     {
261         return isOs( null, null, arch, null );
262     }
263 
264     /**
265      * Determines if the current OS matches the given OS
266      * version.
267      *
268      * @param version the OS version to check for
269      * @return true if the OS matches
270      * @since 1.0
271      */
272     public static boolean isVersion( String version )
273     {
274         return isOs( null, null, null, version );
275     }
276 
277     /**
278      * Determines if the current OS matches the given OS
279      * family, name, architecture and version.
280      *
281      * The name, archictecture and version are compared to
282      * the System properties os.name, os.version and os.arch
283      * in a case-independent way.
284      *
285      * @param family The OS family
286      * @param name The OS name
287      * @param arch The OS architecture
288      * @param version The OS version
289      * @return true if the OS matches
290      * @since 1.0
291      */
292     public static boolean isOs( String family, String name, String arch, String version )
293     {
294         boolean retValue = false;
295 
296         if ( family != null || name != null || arch != null || version != null )
297         {
298 
299             boolean isFamily = true;
300             boolean isName = true;
301             boolean isArch = true;
302             boolean isVersion = true;
303 
304             if ( family != null )
305             {
306                 if ( family.equalsIgnoreCase( FAMILY_WINDOWS ) )
307                 {
308                     isFamily = OS_NAME.contains( FAMILY_WINDOWS );
309                 }
310                 else if ( family.equalsIgnoreCase( FAMILY_OS2 ) )
311                 {
312                     isFamily = OS_NAME.contains( FAMILY_OS2 );
313                 }
314                 else if ( family.equalsIgnoreCase( FAMILY_NETWARE ) )
315                 {
316                     isFamily = OS_NAME.contains( FAMILY_NETWARE );
317                 }
318                 else if ( family.equalsIgnoreCase( FAMILY_DOS ) )
319                 {
320                     isFamily = PATH_SEP.equals( ";" ) && !isFamily( FAMILY_NETWARE );
321                 }
322                 else if ( family.equalsIgnoreCase( FAMILY_MAC ) )
323                 {
324                     isFamily = OS_NAME.contains( FAMILY_MAC );
325                 }
326                 else if ( family.equalsIgnoreCase( FAMILY_TANDEM ) )
327                 {
328                     isFamily = OS_NAME.contains( "nonstop_kernel" );
329                 }
330                 else if ( family.equalsIgnoreCase( FAMILY_UNIX ) )
331                 {
332                     isFamily = PATH_SEP.equals( ":" ) && !isFamily( FAMILY_OPENVMS )
333                         && ( !isFamily( FAMILY_MAC ) || OS_NAME.endsWith( "x" ) );
334                 }
335                 else if ( family.equalsIgnoreCase( FAMILY_WIN9X ) )
336                 {
337                     isFamily = isFamily( FAMILY_WINDOWS )
338                         && ( OS_NAME.contains( "95" ) || OS_NAME.contains( "98" )
339                             || OS_NAME.contains( "me" ) || OS_NAME.contains( "ce" ) );
340                 }
341                 else if ( family.equalsIgnoreCase( FAMILY_ZOS ) )
342                 {
343                     isFamily = OS_NAME.contains( FAMILY_ZOS ) || OS_NAME.contains( "os/390" );
344                 }
345                 else if ( family.equalsIgnoreCase( FAMILY_OS400 ) )
346                 {
347                     isFamily = OS_NAME.contains( FAMILY_OS400 );
348                 }
349                 else if ( family.equalsIgnoreCase( FAMILY_OPENVMS ) )
350                 {
351                     isFamily = OS_NAME.contains( FAMILY_OPENVMS );
352                 }
353                 else
354                 {
355                     isFamily = OS_NAME.contains( family.toLowerCase( Locale.US ) );
356                 }
357             }
358             if ( name != null )
359             {
360                 isName = name.toLowerCase( Locale.US ).equals( OS_NAME );
361             }
362             if ( arch != null )
363             {
364                 isArch = arch.toLowerCase( Locale.US ).equals( OS_ARCH );
365             }
366             if ( version != null )
367             {
368                 isVersion = version.toLowerCase( Locale.US ).equals( OS_VERSION );
369             }
370             retValue = isFamily && isName && isArch && isVersion;
371         }
372         return retValue;
373     }
374 
375     /**
376      * Helper method to determine the current OS family.
377      *
378      * @return name of current OS family.
379      * @since 1.4.2
380      */
381     private static String getOsFamily()
382     {
383         // in case the order of static initialization is
384         // wrong, get the list
385         // safely.
386         Set<String> families = null;
387         if ( !validFamilies.isEmpty() )
388         {
389             families = validFamilies;
390         }
391         else
392         {
393             families = setValidFamilies();
394         }
395         for ( String fam : families )
396         {
397             if ( Os.isFamily( fam ) )
398             {
399                 return fam;
400             }
401         }
402         return null;
403     }
404 
405     /**
406      * Helper method to check if the given family is in the
407      * following list:
408      * <ul>
409      * <li>dos</li>
410      * <li>mac</li>
411      * <li>netware</li>
412      * <li>os/2</li>
413      * <li>tandem</li>
414      * <li>unix</li>
415      * <li>windows</li>
416      * <li>win9x</li>
417      * <li>z/os</li>
418      * <li>os/400</li>
419      * <li>openvms</li>
420      * </ul>
421      *
422      * @param theFamily the family to check.
423      * @return true if one of the valid families.
424      * @since 1.4.2
425      */
426     public static boolean isValidFamily( String theFamily )
427     {
428         return ( validFamilies.contains( theFamily ) );
429     }
430 
431     /**
432      * @return a copy of the valid families
433      * @since 1.4.2
434      */
435     public static Set<String> getValidFamilies()
436     {
437         return new HashSet<String>( validFamilies );
438     }
439 }