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