buildframework/helium/sf/java/core/src/com/nokia/helium/core/plexus/CommandBase.java
changeset 628 7c4a911dc066
parent 587 85df38eb4012
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
     1 /*
     1 /*
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3  * All rights reserved.
     4 * This component and the accompanying materials are made available
     4  * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     5  * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6  * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     8  *
     9 * Initial Contributors:
     9  * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10  * Nokia Corporation - initial contribution.
    11 *
    11  *
    12 * Contributors:
    12  * Contributors:
    13 *
    13  *
    14 * Description:  
    14  * Description:  
    15 *
    15  *
    16 */
    16  */
       
    17 
    17 package com.nokia.helium.core.plexus;
    18 package com.nokia.helium.core.plexus;
    18 
    19 
    19 import java.io.File;
    20 import java.io.File;
    20 import java.util.Map;
    21 import java.util.Map;
    21 import java.util.Vector;
    22 import java.util.Vector;
    22 
    23 
    23 import org.apache.log4j.Logger;
    24 import org.apache.log4j.Logger;
    24 import org.codehaus.plexus.util.Os;
    25 import org.apache.tools.ant.taskdefs.condition.Os;
    25 import org.codehaus.plexus.util.cli.CommandLineException;
    26 import org.codehaus.plexus.util.cli.CommandLineException;
    26 import org.codehaus.plexus.util.cli.CommandLineUtils;
    27 import org.codehaus.plexus.util.cli.CommandLineUtils;
    27 import org.codehaus.plexus.util.cli.Commandline;
    28 import org.codehaus.plexus.util.cli.Commandline;
    28 import org.codehaus.plexus.util.cli.StreamConsumer;
    29 import org.codehaus.plexus.util.cli.StreamConsumer;
    29 
    30 
    30 /**
    31 /**
    31  * This abstract class implements some basic support to execute commands and 
    32  * This abstract class implements some basic support to execute commands and redirect outputs to
    32  * redirect outputs to StreamConsumer. You can have common stream consumers or
    33  * StreamConsumer. You can have common stream consumers or execution base streamconsumers. The
    33  * execution base streamconsumers. The exception type raised 
    34  * exception type raised can be controlled by the implementing class.
    34  * can be controlled by the implementing class.
    35  * 
    35  *
       
    36  * @param <T>
    36  * @param <T>
    37  */
    37  */
    38 public abstract class CommandBase<T extends Exception> {
    38 public abstract class CommandBase<T extends Exception> {
    39     private final Logger log = Logger.getLogger(getClass());
    39     private final Logger log = Logger.getLogger(getClass());
    40     private Vector<StreamConsumer> outputHandlers = new Vector<StreamConsumer>();
    40     private Vector<StreamConsumer> outputHandlers = new Vector<StreamConsumer>();
    41     private Vector<StreamConsumer> errorHandlers = new Vector<StreamConsumer>();
    41     private Vector<StreamConsumer> errorHandlers = new Vector<StreamConsumer>();
    42 
    42 
    43     /**
    43     /**
    44      * Get the executable name.
    44      * Get the executable name.
       
    45      * 
    45      * @return
    46      * @return
    46      */
    47      */
    47     protected abstract String getExecutable();
    48     protected abstract String getExecutable();
    48 
    49 
    49     /**
    50     /**
    50      * Throw an exception with message and cause.
    51      * Throw an exception with message and cause.
       
    52      * 
    51      * @param message
    53      * @param message
    52      * @param t
    54      * @param t
    53      * @throws T
    55      * @throws T
    54      */
    56      */
    55     protected abstract void throwException(String message, Throwable t) throws T;
    57     protected abstract void throwException(String message, Throwable t) throws T;
    56 
    58 
    57     /**
    59     /**
    58      * Throw an exception with message only.
    60      * Throw an exception with message only.
       
    61      * 
    59      * @param message
    62      * @param message
    60      * @throws T
    63      * @throws T
    61      */
    64      */
    62     protected void throwException(String message) throws T {
    65     protected void throwException(String message) throws T {
    63         throwException(message, null);
    66         throwException(message, null);
    64     }
    67     }
    65 
    68 
    66     /**
    69     /**
    67      * Location where to execute the command. The default location
    70      * Location where to execute the command. The default location is the current directory.
    68      * is the current directory.
    71      * 
    69      * @return a File object pointing to a directory.
    72      * @return a File object pointing to a directory.
    70      */
    73      */
    71     public File getWorkingDir() {
    74     public File getWorkingDir() {
    72         return new File(".");
    75         return new File(".");
    73     }
    76     }
    74     
    77 
    75     /**
    78     /**
    76      * Add a LineHandler to the CommandBase instance.
    79      * Add a LineHandler to the CommandBase instance. LineHandlers could be used to record/log the
    77      * LineHandlers could be used to record/log the output stream
    80      * output stream command invocation.
    78      * command invocation.
    81      * 
    79      * @param lineHandler a lineHandle instance
    82      * @param lineHandler a lineHandle instance
    80      */
    83      */
    81     public void addOutputLineHandler(StreamConsumer lineHandler) {
    84     public void addOutputLineHandler(StreamConsumer lineHandler) {
    82         if (lineHandler != null) {
    85         if (lineHandler != null) {
    83             outputHandlers.add(lineHandler);
    86             outputHandlers.add(lineHandler);
    84         }
    87         }
    85     }
    88     }
    86     
    89 
    87     /**
    90     /**
    88      * Add a LineHandler to the CommandBase instance.
    91      * Add a LineHandler to the CommandBase instance. LineHandlers could be used to record/log the
    89      * LineHandlers could be used to record/log the output error stream
    92      * output error stream command invocation.
    90      * command invocation.
    93      * 
    91      * @param lineHandler a lineHandle instance
    94      * @param lineHandler a lineHandle instance
    92      */
    95      */
    93     public void addErrorLineHandler(StreamConsumer lineHandler) {
    96     public void addErrorLineHandler(StreamConsumer lineHandler) {
    94         if (lineHandler != null) {
    97         if (lineHandler != null) {
    95             errorHandlers.add(lineHandler);
    98             errorHandlers.add(lineHandler);
   105     public void execute(String[] args) throws T {
   108     public void execute(String[] args) throws T {
   106         execute(args, null);
   109         execute(args, null);
   107     }
   110     }
   108 
   111 
   109     /**
   112     /**
   110      * Execute the command given by getExecutable with args as list of arguments and custom StreamConsumer.
   113      * Execute the command given by getExecutable with args as list of arguments and custom
   111      * 
   114      * StreamConsumer.
   112      * @param args
   115      * 
   113      *            an array representing blocks arguments
   116      * @param args an array representing blocks arguments
   114      * @param output
   117      * @param output the StreamConsumer to analyze the output with. If null it is ignored.
   115      *            the StreamConsumer to analyze the output with. If null it is
       
   116      *            ignored.
       
   117      * @throws T
   118      * @throws T
   118      */
   119      */
   119     public void execute(String[] args, StreamConsumer output) throws T {
   120     public void execute(String[] args, StreamConsumer output) throws T {
   120         execute(args, null, output);
   121         execute(args, null, output);
   121     }
   122     }
   122 
   123 
   123     /**
   124     /**
   124      * Execute the command given by getExecutable with args as list of arguments and custom StreamConsumer.
   125      * Execute the command given by getExecutable with args as list of arguments and custom
   125      * Also env content will be added to the environment. 
   126      * StreamConsumer. Also env content will be added to the environment.
   126      * 
   127      * 
   127      * @param args
   128      * @param args an array representing blocks arguments
   128      *            an array representing blocks arguments
   129      * @param env additional key to add the environment
   129      * @param env
   130      * @param output the StreamConsumer to analyze the output with. If null it is ignored.
   130      *            additional key to add the environment
       
   131      * @param output
       
   132      *            the StreamConsumer to analyze the output with. If null it is
       
   133      *            ignored.
       
   134      * @throws T
   131      * @throws T
   135      */
   132      */
   136     public void executeCmdLine(String argLine, Map<String, String> env, StreamConsumer output)
   133     public void executeCmdLine(String argLine, Map<String, String> env, StreamConsumer output)
   137             throws T {
   134         throws T {
   138         Commandline cmdLine = new Commandline();
   135         Commandline cmdLine = new Commandline();
   139         cmdLine.createArg().setValue(getExecutable());
   136         cmdLine.createArg().setValue(getExecutable());
   140         if (argLine != null) {
   137         if (argLine != null) {
   141             cmdLine.createArg().setLine(argLine);
   138             cmdLine.createArg().setLine(argLine);
   142         }
   139         }
   143         executeCmd(cmdLine, env, output);
   140         executeCmd(cmdLine, env, output);
   144     }
   141     }
   145 
   142 
   146     private void executeCmd(Commandline cmdLine, Map<String, String> env, StreamConsumer output)     throws T  {
   143     private void executeCmd(Commandline cmdLine, Map<String, String> env, StreamConsumer output)
       
   144         throws T {
   147         if (env != null) {
   145         if (env != null) {
   148             for (Map.Entry<String, String> entry : env.entrySet()) {
   146             for (Map.Entry<String, String> entry : env.entrySet()) {
   149                 cmdLine.addEnvironment(entry.getKey(), entry.getValue());
   147                 cmdLine.addEnvironment(entry.getKey(), entry.getValue());
   150             }
   148             }
   151         }
   149         }
   152         cmdLine.setWorkingDirectory(getWorkingDir());
   150         cmdLine.setWorkingDirectory(getWorkingDir());
   153         
   151 
   154         // This is only needed on windows.
   152         // This is only needed on windows.
   155         if (Os.isFamily(Os.FAMILY_WINDOWS)) {
   153         if (Os.isFamily(Os.FAMILY_WINDOWS)) {
   156             cmdLine.createArg().setLine("&& exit %%ERRORLEVEL%%");    
   154             cmdLine.createArg().setLine("&& exit %%ERRORLEVEL%%");
   157         }
   155         }
   158         
       
   159 
   156 
   160         StreamMultiplexer inputMux = new StreamMultiplexer();
   157         StreamMultiplexer inputMux = new StreamMultiplexer();
   161         if (output != null) {
   158         if (output != null) {
   162             inputMux.addHandler(output);
   159             inputMux.addHandler(output);
   163         }
   160         }
   171         for (StreamConsumer lh : errorHandlers) {
   168         for (StreamConsumer lh : errorHandlers) {
   172             errorMux.addHandler(lh);
   169             errorMux.addHandler(lh);
   173         }
   170         }
   174 
   171 
   175         try {
   172         try {
   176             int err = CommandLineUtils.executeCommandLine(cmdLine, inputMux,
   173             int err = CommandLineUtils.executeCommandLine(cmdLine, inputMux, errorMux);
   177                     errorMux);
       
   178             // check its exit value
   174             // check its exit value
   179             log.debug("Execution of " + getExecutable() + " returned: " + err);
   175             log.debug("Execution of " + getExecutable() + " returned: " + err);
   180             if (err != 0) {
   176             if (err != 0) {
   181                 throwException(errorRecorder.getBuffer() + " (return code: " + err
   177                 throwException(errorRecorder.getBuffer() + " (return code: " + err + ")");
   182                         + ")");
       
   183             }
   178             }
   184         } catch (CommandLineException e) {
   179         }
   185             throwException(
   180         catch (CommandLineException e) {
   186                     "Error executing " + getExecutable() + ": "
   181             throwException("Error executing " + getExecutable() + ": " + e.toString());
   187                             + e.toString());
   182         }
   188         }
   183     }
   189     }
   184 
   190 
   185     /**
   191     /**
   186      * Execute the command given by getExecutable with args as list of arguments and custom
   192      * Execute the command given by getExecutable with args as list of arguments and custom StreamConsumer.
   187      * StreamConsumer. Also env content will be added to the environment.
   193      * Also env content will be added to the environment. 
   188      * 
   194      * 
   189      * @param args an array representing blocks arguments
   195      * @param args
   190      * @param env additional key to add the environment
   196      *            an array representing blocks arguments
   191      * @param output the StreamConsumer to analyze the output with. If null it is ignored.
   197      * @param env
   192      * @throws T
   198      *            additional key to add the environment
   193      */
   199      * @param output
   194     public void execute(String[] args, Map<String, String> env, StreamConsumer output) throws T {
   200      *            the StreamConsumer to analyze the output with. If null it is
       
   201      *            ignored.
       
   202      * @throws T
       
   203      */
       
   204     public void execute(String[] args, Map<String, String> env, StreamConsumer output)
       
   205             throws T {
       
   206         Commandline cmdLine = new Commandline();
   195         Commandline cmdLine = new Commandline();
   207         cmdLine.createArg().setValue(getExecutable());
   196         cmdLine.createArg().setValue(getExecutable());
   208         if (args != null) {
   197         if (args != null) {
   209             cmdLine.addArguments(args);
   198             cmdLine.addArguments(args);
   210         }
   199         }
   211         executeCmd(cmdLine, env, output);
   200         executeCmd(cmdLine, env, output);
   212     }
   201     }
   213     
   202 
   214 }
   203 }