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.io.ByteArrayOutputStream;
|
|
20 |
import java.io.IOException;
|
|
21 |
import java.io.InputStream;
|
|
22 |
import java.io.OutputStream;
|
|
23 |
|
|
24 |
/**
|
|
25 |
* ToolChecker is a generic class used to verify tool versions.
|
|
26 |
*
|
|
27 |
*/
|
|
28 |
public class ToolChecker {
|
|
29 |
|
|
30 |
private int errorCount;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Method verifies whether the expected version is set or not.
|
|
34 |
*
|
|
35 |
* @param expVersion
|
|
36 |
* is the version string to verify.
|
|
37 |
* @param errorMsg
|
|
38 |
* is the message to be displayed on failure.
|
|
39 |
*/
|
|
40 |
public void verifyIsExpectedToolVersionConfigured(String expVersion,
|
628
|
41 |
String errorMsg) throws CheckToolException {
|
587
|
42 |
if (expVersion == null
|
|
43 |
|| (expVersion != null && expVersion.trim().isEmpty())) {
|
|
44 |
raiseError(errorMsg);
|
|
45 |
}
|
|
46 |
}
|
|
47 |
|
|
48 |
/**
|
|
49 |
* Method verifies the tool version.
|
|
50 |
*
|
|
51 |
* @param command
|
|
52 |
* is the command string to verify tool version.
|
|
53 |
* @param expVersion
|
|
54 |
* is the expected tool version.
|
|
55 |
* @param versionString2match
|
|
56 |
* is the version string to match
|
|
57 |
* @param errorMsg
|
|
58 |
* is the message to be displayed on failure.
|
|
59 |
*/
|
|
60 |
public void verifyToolVersion(String command, String expVersion,
|
628
|
61 |
String versionString2match, String errorMsg) throws CheckToolException {
|
587
|
62 |
String[] versions = expVersion.split(",");
|
|
63 |
String installedVersion = getInstalledToolVersion(command);
|
|
64 |
boolean valid = false;
|
|
65 |
for (String expver : versions) {
|
628
|
66 |
if (!valid) {
|
587
|
67 |
valid = installedVersion.contains(versionString2match + expver);
|
628
|
68 |
}
|
587
|
69 |
}
|
|
70 |
if (!valid) {
|
628
|
71 |
HeliumToolsCheckerMain.println("Installed Version : " + installedVersion);
|
587
|
72 |
raiseError(errorMsg);
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Method throws a CheckToolException with the given message.
|
|
78 |
*
|
|
79 |
* @param message
|
|
80 |
* is the failure message.
|
|
81 |
*/
|
628
|
82 |
public void raiseError(String message) throws CheckToolException {
|
587
|
83 |
incrementErrorCount();
|
|
84 |
throw new CheckToolException(message);
|
|
85 |
}
|
|
86 |
|
|
87 |
/**
|
|
88 |
* Method returns the actual version of the tool installed.
|
|
89 |
*
|
|
90 |
* @param cmd
|
|
91 |
* is the command string to execute.
|
|
92 |
* @return the actual tool version.
|
|
93 |
*/
|
628
|
94 |
public String getInstalledToolVersion(String cmd) throws CheckToolException {
|
587
|
95 |
String input = null;
|
|
96 |
String error = null;
|
|
97 |
try {
|
|
98 |
Process toolProcess = Runtime.getRuntime().exec(cmd);
|
|
99 |
input = toString(toolProcess.getInputStream());
|
|
100 |
error = toString(toolProcess.getErrorStream());
|
|
101 |
} catch (IOException ex) {
|
|
102 |
throw new CheckToolException(ex);
|
|
103 |
}
|
|
104 |
return (input.isEmpty()) ? error : input;
|
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* Method returns a string read from the given input stream.
|
|
109 |
*
|
|
110 |
* @param is
|
|
111 |
* is the input stream to read from.
|
|
112 |
* @return the contents read from the input stream.
|
|
113 |
* @throws Exception
|
|
114 |
*/
|
|
115 |
private String toString(InputStream is) throws IOException {
|
|
116 |
OutputStream os = null;
|
|
117 |
String versionString = null;
|
|
118 |
try {
|
|
119 |
|
|
120 |
byte[] buffer = new byte[4096];
|
|
121 |
os = new ByteArrayOutputStream();
|
|
122 |
|
|
123 |
while (true) {
|
|
124 |
int read = is.read(buffer);
|
|
125 |
if (read == -1) {
|
|
126 |
break;
|
|
127 |
}
|
|
128 |
os.write(buffer, 0, read);
|
|
129 |
}
|
|
130 |
versionString = os.toString();
|
|
131 |
} finally {
|
|
132 |
try {
|
628
|
133 |
if (os != null) {
|
587
|
134 |
os.close();
|
628
|
135 |
}
|
|
136 |
if (is != null) {
|
587
|
137 |
is.close();
|
628
|
138 |
}
|
587
|
139 |
} catch (IOException e) {
|
|
140 |
e = null; // ignore the exception
|
|
141 |
}
|
|
142 |
}
|
|
143 |
return versionString;
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Method increases error count by one.
|
|
148 |
*/
|
|
149 |
private void incrementErrorCount() {
|
|
150 |
errorCount++;
|
|
151 |
}
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Return the error count.
|
|
155 |
*
|
|
156 |
* @return the error count.
|
|
157 |
*/
|
|
158 |
public int getErrorCount() {
|
|
159 |
return errorCount;
|
|
160 |
}
|
|
161 |
|
|
162 |
}
|