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.apache.maven.it.util;
56  
57  import java.util.Locale;
58  
59  /**
60   * Condition that tests the OS type.
61   *
62   * @author Stefan Bodewig
63   * @author Magesh Umasankar
64   * @author Brian Fox
65   * @since 1.0
66   * @version $Revision: 697419 $
67   */
68  public class Os
69  {
70      // define the families for easier reference
71      public static final String FAMILY_DOS = "dos";
72  
73      public static final String FAMILY_MAC = "mac";
74  
75      public static final String FAMILY_NETWARE = "netware";
76  
77      public static final String FAMILY_OS2 = "os/2";
78  
79      public static final String FAMILY_TANDEM = "tandem";
80  
81      public static final String FAMILY_UNIX = "unix";
82  
83      public static final String FAMILY_WINDOWS = "windows";
84  
85      public static final String FAMILY_WIN9X = "win9x";
86  
87      public static final String FAMILY_ZOS = "z/os";
88  
89      public static final String FAMILY_OS400 = "os/400";
90  
91      public static final String FAMILY_OPENVMS = "openvms";
92  
93      // get the current info
94      private static final String PATH_SEP = System.getProperty( "path.separator" );
95  
96      public static final String OS_NAME = System.getProperty( "os.name" ).toLowerCase( Locale.US );
97  
98      public static final String OS_ARCH = System.getProperty( "os.arch" ).toLowerCase( Locale.US );
99  
100     public static final String OS_VERSION = System.getProperty( "os.version" ).toLowerCase( Locale.US );
101 
102     private String family;
103 
104     private String name;
105 
106     private String version;
107 
108     private String arch;
109 
110     /**
111      * Default constructor
112      */
113     public Os()
114     {
115     }
116 
117     /**
118      * Constructor that sets the family attribute
119      * 
120      * @param family a String value
121      */
122     public Os( String family )
123     {
124         setFamily( family );
125     }
126 
127     /**
128      * Sets the desired OS family type
129      * 
130      * @param f The OS family type desired<br />
131      *            Possible values:<br />
132      *            <ul>
133      *            <li>dos</li>
134      *            <li>mac</li>
135      *            <li>netware</li>
136      *            <li>os/2</li>
137      *            <li>tandem</li>
138      *            <li>unix</li>
139      *            <li>windows</li>
140      *            <li>win9x</li>
141      *            <li>z/os</li>
142      *            <li>os/400</li>
143      *            <li>openvms</li>
144      *            </ul>
145      */
146     public void setFamily( String f )
147     {
148         family = f.toLowerCase( Locale.US );
149     }
150 
151     /**
152      * Sets the desired OS name
153      * 
154      * @param name The OS name
155      */
156     public void setName( String name )
157     {
158         this.name = name.toLowerCase( Locale.US );
159     }
160 
161     /**
162      * Sets the desired OS architecture
163      * 
164      * @param arch The OS architecture
165      */
166     public void setArch( String arch )
167     {
168         this.arch = arch.toLowerCase( Locale.US );
169     }
170 
171     /**
172      * Sets the desired OS version
173      * 
174      * @param version The OS version
175      */
176     public void setVersion( String version )
177     {
178         this.version = version.toLowerCase( Locale.US );
179     }
180 
181     /**
182      * Determines if the current OS matches the type of that
183      * set in setFamily.
184      * 
185      * @see Os#setFamily(String)
186      */
187     public boolean eval()
188         throws Exception
189     {
190         return isOs( family, name, arch, version );
191     }
192 
193     /**
194      * Determines if the current OS matches the given OS
195      * family.
196      * 
197      * @param family the family to check for
198      * @return true if the OS matches
199      * @since 1.0
200      */
201     public static boolean isFamily( String family )
202     {
203         return isOs( family, null, null, null );
204     }
205 
206     /**
207      * Determines if the current OS matches the given OS
208      * name.
209      * 
210      * @param name the OS name to check for
211      * @return true if the OS matches
212      * @since 1.0
213      */
214     public static boolean isName( String name )
215     {
216         return isOs( null, name, null, null );
217     }
218 
219     /**
220      * Determines if the current OS matches the given OS
221      * architecture.
222      * 
223      * @param arch the OS architecture to check for
224      * @return true if the OS matches
225      * @since 1.0
226      */
227     public static boolean isArch( String arch )
228     {
229         return isOs( null, null, arch, null );
230     }
231 
232     /**
233      * Determines if the current OS matches the given OS
234      * version.
235      * 
236      * @param version the OS version to check for
237      * @return true if the OS matches
238      * @since 1.0
239      */
240     public static boolean isVersion( String version )
241     {
242         return isOs( null, null, null, version );
243     }
244 
245     /**
246      * Determines if the current OS matches the given OS
247      * family, name, architecture and version.
248      * 
249      * The name, archictecture and version are compared to
250      * the System properties os.name, os.version and os.arch
251      * in a case-independent way.
252      * 
253      * @param family The OS family
254      * @param name The OS name
255      * @param arch The OS architecture
256      * @param version The OS version
257      * @return true if the OS matches
258      * @since 1.0
259      */
260     public static boolean isOs( String family, String name, String arch, String version )
261     {
262         boolean retValue = false;
263 
264         if ( family != null || name != null || arch != null || version != null )
265         {
266 
267             boolean isFamily = true;
268             boolean isName = true;
269             boolean isArch = true;
270             boolean isVersion = true;
271 
272             if ( family != null )
273             {
274                 if ( family.equalsIgnoreCase( FAMILY_WINDOWS ) )
275                 {
276                     isFamily = OS_NAME.indexOf( FAMILY_WINDOWS ) > -1;
277                 }
278                 else if ( family.equalsIgnoreCase( FAMILY_OS2 ) )
279                 {
280                     isFamily = OS_NAME.indexOf( FAMILY_OS2 ) > -1;
281                 }
282                 else if ( family.equalsIgnoreCase( FAMILY_NETWARE ) )
283                 {
284                     isFamily = OS_NAME.indexOf( FAMILY_NETWARE ) > -1;
285                 }
286                 else if ( family.equalsIgnoreCase( FAMILY_DOS ) )
287                 {
288                     isFamily = PATH_SEP.equals( ";" ) && !isFamily( FAMILY_NETWARE );
289                 }
290                 else if ( family.equalsIgnoreCase( FAMILY_MAC ) )
291                 {
292                     isFamily = OS_NAME.indexOf( FAMILY_MAC ) > -1;
293                 }
294                 else if ( family.equalsIgnoreCase( FAMILY_TANDEM ) )
295                 {
296                     isFamily = OS_NAME.indexOf( "nonstop_kernel" ) > -1;
297                 }
298                 else if ( family.equalsIgnoreCase( FAMILY_UNIX ) )
299                 {
300                     isFamily = PATH_SEP.equals( ":" ) && !isFamily( FAMILY_OPENVMS )
301                         && ( !isFamily( FAMILY_MAC ) || OS_NAME.endsWith( "x" ) );
302                 }
303                 else if ( family.equalsIgnoreCase( FAMILY_WIN9X ) )
304                 {
305                     isFamily = isFamily( FAMILY_WINDOWS )
306                         && ( OS_NAME.indexOf( "95" ) >= 0 || OS_NAME.indexOf( "98" ) >= 0
307                             || OS_NAME.indexOf( "me" ) >= 0 || OS_NAME.indexOf( "ce" ) >= 0 );
308                 }
309                 else if ( family.equalsIgnoreCase( FAMILY_ZOS ) )
310                 {
311                     isFamily = OS_NAME.indexOf( FAMILY_ZOS ) > -1 || OS_NAME.indexOf( "os/390" ) > -1;
312                 }
313                 else if ( family.equalsIgnoreCase( FAMILY_OS400 ) )
314                 {
315                     isFamily = OS_NAME.indexOf( FAMILY_OS400 ) > -1;
316                 }
317                 else if ( family.equalsIgnoreCase( FAMILY_OPENVMS ) )
318                 {
319                     isFamily = OS_NAME.indexOf( FAMILY_OPENVMS ) > -1;
320                 }
321                 else
322                 {
323                     isFamily = OS_NAME.indexOf( family.toLowerCase( Locale.US ) ) > -1;
324                 }
325             }
326             if ( name != null )
327             {
328                 isName = name.toLowerCase( Locale.US ).equals( OS_NAME );
329             }
330             if ( arch != null )
331             {
332                 isArch = arch.toLowerCase( Locale.US ).equals( OS_ARCH );
333             }
334             if ( version != null )
335             {
336                 isVersion = version.toLowerCase( Locale.US ).equals( OS_VERSION );
337             }
338             retValue = isFamily && isName && isArch && isVersion;
339         }
340         return retValue;
341     }
342 
343 }