Coverage Report - org.codehaus.plexus.interpolation.os.Os
 
Classes in this File Line Coverage Branch Coverage Complexity
Os
0%
0/89
0%
0/78
3.188
 
 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  
  * <p><b>NOTE:</b> This class was copied from plexus-utils, to allow this library
 63  
  * to stand completely self-contained.</p>
 64  
  * <p>Condition that tests the OS type.</p>
 65  
  *
 66  
  * @author Stefan Bodewig
 67  
  * @author Magesh Umasankar
 68  
  * @author Brian Fox
 69  
  * @since 1.0
 70  
  */
 71  
 public class Os
 72  
 {
 73  
     // define the families for easier reference
 74  
     public static final String FAMILY_DOS = "dos";
 75  
 
 76  
     public static final String FAMILY_MAC = "mac";
 77  
 
 78  
     public static final String FAMILY_NETWARE = "netware";
 79  
 
 80  
     public static final String FAMILY_OS2 = "os/2";
 81  
 
 82  
     public static final String FAMILY_TANDEM = "tandem";
 83  
 
 84  
     public static final String FAMILY_UNIX = "unix";
 85  
 
 86  
     public static final String FAMILY_WINDOWS = "windows";
 87  
 
 88  
     public static final String FAMILY_WIN9X = "win9x";
 89  
 
 90  
     public static final String FAMILY_ZOS = "z/os";
 91  
 
 92  
     public static final String FAMILY_OS400 = "os/400";
 93  
 
 94  
     public static final String FAMILY_OPENVMS = "openvms";
 95  
 
 96  
     // store the valid families
 97  0
     private static final Set<String> validFamilies = setValidFamilies();
 98  
 
 99  
     // get the current info
 100  0
     private static final String PATH_SEP = System.getProperty( "path.separator" );
 101  
 
 102  0
     public static final String OS_NAME = System.getProperty( "os.name" ).toLowerCase( Locale.US );
 103  
 
 104  0
     public static final String OS_ARCH = System.getProperty( "os.arch" ).toLowerCase( Locale.US );
 105  
 
 106  0
     public static final String OS_VERSION = System.getProperty( "os.version" ).toLowerCase( Locale.US );
 107  
 
 108  
     // Make sure this method is called after static fields it depends on have been set!
 109  0
     public static final String OS_FAMILY = getOsFamily();
 110  
 
 111  
     private String family;
 112  
 
 113  
     private String name;
 114  
 
 115  
     private String version;
 116  
 
 117  
     private String arch;
 118  
 
 119  
     /**
 120  
      * Default constructor
 121  
      */
 122  
     public Os()
 123  0
     {
 124  0
     }
 125  
 
 126  
     /**
 127  
      * Constructor that sets the family attribute
 128  
      *
 129  
      * @param family a String value
 130  
      */
 131  
     public Os( String family )
 132  0
     {
 133  0
         setFamily( family );
 134  0
     }
 135  
 
 136  
     /**
 137  
      * Initializes the set of valid families.
 138  
      */
 139  
     private static Set<String> setValidFamilies()
 140  
     {
 141  0
         Set<String> valid = new HashSet<String>();
 142  0
         valid.add( FAMILY_DOS );
 143  0
         valid.add( FAMILY_MAC );
 144  0
         valid.add( FAMILY_NETWARE );
 145  0
         valid.add( FAMILY_OS2 );
 146  0
         valid.add( FAMILY_TANDEM );
 147  0
         valid.add( FAMILY_UNIX );
 148  0
         valid.add( FAMILY_WINDOWS );
 149  0
         valid.add( FAMILY_WIN9X );
 150  0
         valid.add( FAMILY_ZOS );
 151  0
         valid.add( FAMILY_OS400 );
 152  0
         valid.add( FAMILY_OPENVMS );
 153  
 
 154  0
         return valid;
 155  
     }
 156  
 
 157  
     /**
 158  
      * Sets the desired OS family type
 159  
      *
 160  
      * @param f The OS family type desired
 161  
      *            Possible values:
 162  
      *            <ul>
 163  
      *            <li>dos</li>
 164  
      *            <li>mac</li>
 165  
      *            <li>netware</li>
 166  
      *            <li>os/2</li>
 167  
      *            <li>tandem</li>
 168  
      *            <li>unix</li>
 169  
      *            <li>windows</li>
 170  
      *            <li>win9x</li>
 171  
      *            <li>z/os</li>
 172  
      *            <li>os/400</li>
 173  
      *            <li>openvms</li>
 174  
      *            </ul>
 175  
      */
 176  
     public void setFamily( String f )
 177  
     {
 178  0
         family = f.toLowerCase( Locale.US );
 179  0
     }
 180  
 
 181  
     /**
 182  
      * Sets the desired OS name
 183  
      *
 184  
      * @param name The OS name
 185  
      */
 186  
     public void setName( String name )
 187  
     {
 188  0
         this.name = name.toLowerCase( Locale.US );
 189  0
     }
 190  
 
 191  
     /**
 192  
      * Sets the desired OS architecture
 193  
      *
 194  
      * @param arch The OS architecture
 195  
      */
 196  
     public void setArch( String arch )
 197  
     {
 198  0
         this.arch = arch.toLowerCase( Locale.US );
 199  0
     }
 200  
 
 201  
     /**
 202  
      * Sets the desired OS version
 203  
      *
 204  
      * @param version The OS version
 205  
      */
 206  
     public void setVersion( String version )
 207  
     {
 208  0
         this.version = version.toLowerCase( Locale.US );
 209  0
     }
 210  
 
 211  
     /**
 212  
      * Determines if the current OS matches the type of that
 213  
      * set in setFamily.
 214  
      *
 215  
      * @see Os#setFamily(String)
 216  
      * @return true/false.
 217  
      * @throws Exception in case of an error.
 218  
      */
 219  
     public boolean eval()
 220  
         throws Exception
 221  
     {
 222  0
         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  0
         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  0
         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  0
         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  0
         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  0
         boolean retValue = false;
 295  
 
 296  0
         if ( family != null || name != null || arch != null || version != null )
 297  
         {
 298  
 
 299  0
             boolean isFamily = true;
 300  0
             boolean isName = true;
 301  0
             boolean isArch = true;
 302  0
             boolean isVersion = true;
 303  
 
 304  0
             if ( family != null )
 305  
             {
 306  0
                 if ( family.equalsIgnoreCase( FAMILY_WINDOWS ) )
 307  
                 {
 308  0
                     isFamily = OS_NAME.contains( FAMILY_WINDOWS );
 309  
                 }
 310  0
                 else if ( family.equalsIgnoreCase( FAMILY_OS2 ) )
 311  
                 {
 312  0
                     isFamily = OS_NAME.contains( FAMILY_OS2 );
 313  
                 }
 314  0
                 else if ( family.equalsIgnoreCase( FAMILY_NETWARE ) )
 315  
                 {
 316  0
                     isFamily = OS_NAME.contains( FAMILY_NETWARE );
 317  
                 }
 318  0
                 else if ( family.equalsIgnoreCase( FAMILY_DOS ) )
 319  
                 {
 320  0
                     isFamily = PATH_SEP.equals( ";" ) && !isFamily( FAMILY_NETWARE );
 321  
                 }
 322  0
                 else if ( family.equalsIgnoreCase( FAMILY_MAC ) )
 323  
                 {
 324  0
                     isFamily = OS_NAME.contains( FAMILY_MAC );
 325  
                 }
 326  0
                 else if ( family.equalsIgnoreCase( FAMILY_TANDEM ) )
 327  
                 {
 328  0
                     isFamily = OS_NAME.contains( "nonstop_kernel" );
 329  
                 }
 330  0
                 else if ( family.equalsIgnoreCase( FAMILY_UNIX ) )
 331  
                 {
 332  0
                     isFamily = PATH_SEP.equals( ":" ) && !isFamily( FAMILY_OPENVMS )
 333  0
                         && ( !isFamily( FAMILY_MAC ) || OS_NAME.endsWith( "x" ) );
 334  
                 }
 335  0
                 else if ( family.equalsIgnoreCase( FAMILY_WIN9X ) )
 336  
                 {
 337  0
                     isFamily = isFamily( FAMILY_WINDOWS )
 338  0
                         && ( OS_NAME.contains( "95" ) || OS_NAME.contains( "98" )
 339  0
                             || OS_NAME.contains( "me" ) || OS_NAME.contains( "ce" ) );
 340  
                 }
 341  0
                 else if ( family.equalsIgnoreCase( FAMILY_ZOS ) )
 342  
                 {
 343  0
                     isFamily = OS_NAME.contains( FAMILY_ZOS ) || OS_NAME.contains( "os/390" );
 344  
                 }
 345  0
                 else if ( family.equalsIgnoreCase( FAMILY_OS400 ) )
 346  
                 {
 347  0
                     isFamily = OS_NAME.contains( FAMILY_OS400 );
 348  
                 }
 349  0
                 else if ( family.equalsIgnoreCase( FAMILY_OPENVMS ) )
 350  
                 {
 351  0
                     isFamily = OS_NAME.contains( FAMILY_OPENVMS );
 352  
                 }
 353  
                 else
 354  
                 {
 355  0
                     isFamily = OS_NAME.contains( family.toLowerCase( Locale.US ) );
 356  
                 }
 357  
             }
 358  0
             if ( name != null )
 359  
             {
 360  0
                 isName = name.toLowerCase( Locale.US ).equals( OS_NAME );
 361  
             }
 362  0
             if ( arch != null )
 363  
             {
 364  0
                 isArch = arch.toLowerCase( Locale.US ).equals( OS_ARCH );
 365  
             }
 366  0
             if ( version != null )
 367  
             {
 368  0
                 isVersion = version.toLowerCase( Locale.US ).equals( OS_VERSION );
 369  
             }
 370  0
             retValue = isFamily && isName && isArch && isVersion;
 371  
         }
 372  0
         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  0
         Set<String> families = null;
 387  0
         if ( !validFamilies.isEmpty() )
 388  
         {
 389  0
             families = validFamilies;
 390  
         }
 391  
         else
 392  
         {
 393  0
             families = setValidFamilies();
 394  
         }
 395  0
         for ( String fam : families )
 396  
         {
 397  0
             if ( Os.isFamily( fam ) )
 398  
             {
 399  0
                 return fam;
 400  
             }
 401  0
         }
 402  0
         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  0
         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  0
         return new HashSet<String>( validFamilies );
 438  
     }
 439  
 }