buildframework/helium/sf/java/checktools/src/com/nokia/helium/checktools/OSResolver.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 587 85df38eb4012
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
/*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
 * All rights reserved.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
 * Initial Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
 * Nokia Corporation - initial contribution.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
 * Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
 * Description: 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
package com.nokia.helium.checktools;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
import java.util.Locale;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    21
/**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
 * OSResolver is a utility class containing useful methods related to OS.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
 * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
public final class OSResolver {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
    public static final String FAMILY_WINDOWS = "windows";
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
    public static final String FAMILY_9X = "win9x";
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
    public static final String FAMILY_NT = "winnt";
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
    public static final String FAMILY_MAC = "mac";
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
    public static final String FAMILY_UNIX = "unix";
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
    private static final String OS_NAME = System.getProperty("os.name")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
            .toLowerCase(Locale.US);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
    private static final String PATH_SEP = System.getProperty("path.separator");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
     * Must not be instantiated.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
     */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
    private OSResolver() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
     * Returns a formatted command string specific to the underlying OS.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
     * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
     * @param cmd
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
     *            is the command to be formatted.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
     * @return the formatted OS specific command string.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
     */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    51
    public static String getCommand(String cmd) throws CheckToolException {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
        StringBuffer buffer = new StringBuffer();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
        if (isOs("windows")) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
            if (!isOs("win9x")) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
                // Windows XP/2000/NT
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
                buffer.append("cmd /c ");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
            } else {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
                // Windows 98/95
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
                buffer.append("command /c ");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
        // generic
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
        buffer.append(cmd);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
        return buffer.toString();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
     * Method verifies whether the underlying OS belongs to the given OS family.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
     * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
     * @param family
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
     *            The OS family
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
     * @return true if the OS matches; otherwise false.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
     */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    74
    public static boolean isOs(String family) throws CheckToolException {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
        boolean retValue = false;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
        if (family != null) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
            // windows probing logic relies on the word 'windows' in
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
            // the OS
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
            boolean isWindows = OS_NAME.indexOf(FAMILY_WINDOWS) > -1;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
            boolean is9x = false;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
            boolean isNT = false;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
            if (isWindows) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
                // there are only four 9x platforms that we look for
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
                is9x = OS_NAME.indexOf("95") >= 0
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
                        || OS_NAME.indexOf("98") >= 0
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
                        || OS_NAME.indexOf("me") >= 0 || OS_NAME.indexOf("ce") >= 0;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
                isNT = !is9x;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    89
            if (family.equals(FAMILY_WINDOWS)) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    90
                retValue = isWindows;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    91
            } else if (family.equals(FAMILY_9X)) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
                retValue = isWindows && is9x;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
            } else if (family.equals(FAMILY_NT)) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
                retValue = isWindows && isNT;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
            } else if (family.equals(FAMILY_MAC)) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
                retValue = OS_NAME.indexOf(FAMILY_MAC) > -1;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
            } else if (family.equals(FAMILY_UNIX)) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    98
                retValue = PATH_SEP.equals(":")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
                        && (!isOs(FAMILY_MAC) || OS_NAME.endsWith("x"));
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
            } else {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   101
                throw new CheckToolException(
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   102
                        "Don\'t know how to detect os family \"" + family
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   103
                                + "\"");
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   104
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   105
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   106
        return retValue;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   107
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   108
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   109
}