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