buildframework/helium/sf/java/core/src/com/nokia/helium/core/plexus/CommandBase.java
changeset 628 7c4a911dc066
parent 587 85df38eb4012
--- a/buildframework/helium/sf/java/core/src/com/nokia/helium/core/plexus/CommandBase.java	Wed Jun 16 16:51:40 2010 +0300
+++ b/buildframework/helium/sf/java/core/src/com/nokia/helium/core/plexus/CommandBase.java	Fri Aug 13 14:59:05 2010 +0300
@@ -1,19 +1,20 @@
 /*
-* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
-* All rights reserved.
-* This component and the accompanying materials are made available
-* under the terms of the License "Eclipse Public License v1.0"
-* which accompanies this distribution, and is available
-* at the URL "http://www.eclipse.org/legal/epl-v10.html".
-*
-* Initial Contributors:
-* Nokia Corporation - initial contribution.
-*
-* Contributors:
-*
-* Description:  
-*
-*/
+ * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
+ * All rights reserved.
+ * This component and the accompanying materials are made available
+ * under the terms of the License "Eclipse Public License v1.0"
+ * which accompanies this distribution, and is available
+ * at the URL "http://www.eclipse.org/legal/epl-v10.html".
+ *
+ * Initial Contributors:
+ * Nokia Corporation - initial contribution.
+ *
+ * Contributors:
+ *
+ * Description:  
+ *
+ */
+
 package com.nokia.helium.core.plexus;
 
 import java.io.File;
@@ -21,18 +22,17 @@
 import java.util.Vector;
 
 import org.apache.log4j.Logger;
-import org.codehaus.plexus.util.Os;
+import org.apache.tools.ant.taskdefs.condition.Os;
 import org.codehaus.plexus.util.cli.CommandLineException;
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 import org.codehaus.plexus.util.cli.Commandline;
 import org.codehaus.plexus.util.cli.StreamConsumer;
 
 /**
- * This abstract class implements some basic support to execute commands and 
- * redirect outputs to StreamConsumer. You can have common stream consumers or
- * execution base streamconsumers. The exception type raised 
- * can be controlled by the implementing class.
- *
+ * This abstract class implements some basic support to execute commands and redirect outputs to
+ * StreamConsumer. You can have common stream consumers or execution base streamconsumers. The
+ * exception type raised can be controlled by the implementing class.
+ * 
  * @param <T>
  */
 public abstract class CommandBase<T extends Exception> {
@@ -42,12 +42,14 @@
 
     /**
      * Get the executable name.
+     * 
      * @return
      */
     protected abstract String getExecutable();
 
     /**
      * Throw an exception with message and cause.
+     * 
      * @param message
      * @param t
      * @throws T
@@ -56,6 +58,7 @@
 
     /**
      * Throw an exception with message only.
+     * 
      * @param message
      * @throws T
      */
@@ -64,18 +67,18 @@
     }
 
     /**
-     * Location where to execute the command. The default location
-     * is the current directory.
+     * Location where to execute the command. The default location is the current directory.
+     * 
      * @return a File object pointing to a directory.
      */
     public File getWorkingDir() {
         return new File(".");
     }
-    
+
     /**
-     * Add a LineHandler to the CommandBase instance.
-     * LineHandlers could be used to record/log the output stream
-     * command invocation.
+     * Add a LineHandler to the CommandBase instance. LineHandlers could be used to record/log the
+     * output stream command invocation.
+     * 
      * @param lineHandler a lineHandle instance
      */
     public void addOutputLineHandler(StreamConsumer lineHandler) {
@@ -83,11 +86,11 @@
             outputHandlers.add(lineHandler);
         }
     }
-    
+
     /**
-     * Add a LineHandler to the CommandBase instance.
-     * LineHandlers could be used to record/log the output error stream
-     * command invocation.
+     * Add a LineHandler to the CommandBase instance. LineHandlers could be used to record/log the
+     * output error stream command invocation.
+     * 
      * @param lineHandler a lineHandle instance
      */
     public void addErrorLineHandler(StreamConsumer lineHandler) {
@@ -107,13 +110,11 @@
     }
 
     /**
-     * Execute the command given by getExecutable with args as list of arguments and custom StreamConsumer.
+     * Execute the command given by getExecutable with args as list of arguments and custom
+     * StreamConsumer.
      * 
-     * @param args
-     *            an array representing blocks arguments
-     * @param output
-     *            the StreamConsumer to analyze the output with. If null it is
-     *            ignored.
+     * @param args an array representing blocks arguments
+     * @param output the StreamConsumer to analyze the output with. If null it is ignored.
      * @throws T
      */
     public void execute(String[] args, StreamConsumer output) throws T {
@@ -121,20 +122,16 @@
     }
 
     /**
-     * Execute the command given by getExecutable with args as list of arguments and custom StreamConsumer.
-     * Also env content will be added to the environment. 
+     * Execute the command given by getExecutable with args as list of arguments and custom
+     * StreamConsumer. Also env content will be added to the environment.
      * 
-     * @param args
-     *            an array representing blocks arguments
-     * @param env
-     *            additional key to add the environment
-     * @param output
-     *            the StreamConsumer to analyze the output with. If null it is
-     *            ignored.
+     * @param args an array representing blocks arguments
+     * @param env additional key to add the environment
+     * @param output the StreamConsumer to analyze the output with. If null it is ignored.
      * @throws T
      */
     public void executeCmdLine(String argLine, Map<String, String> env, StreamConsumer output)
-            throws T {
+        throws T {
         Commandline cmdLine = new Commandline();
         cmdLine.createArg().setValue(getExecutable());
         if (argLine != null) {
@@ -143,19 +140,19 @@
         executeCmd(cmdLine, env, output);
     }
 
-    private void executeCmd(Commandline cmdLine, Map<String, String> env, StreamConsumer output)     throws T  {
+    private void executeCmd(Commandline cmdLine, Map<String, String> env, StreamConsumer output)
+        throws T {
         if (env != null) {
             for (Map.Entry<String, String> entry : env.entrySet()) {
                 cmdLine.addEnvironment(entry.getKey(), entry.getValue());
             }
         }
         cmdLine.setWorkingDirectory(getWorkingDir());
-        
+
         // This is only needed on windows.
         if (Os.isFamily(Os.FAMILY_WINDOWS)) {
-            cmdLine.createArg().setLine("&& exit %%ERRORLEVEL%%");    
+            cmdLine.createArg().setLine("&& exit %%ERRORLEVEL%%");
         }
-        
 
         StreamMultiplexer inputMux = new StreamMultiplexer();
         if (output != null) {
@@ -173,36 +170,28 @@
         }
 
         try {
-            int err = CommandLineUtils.executeCommandLine(cmdLine, inputMux,
-                    errorMux);
+            int err = CommandLineUtils.executeCommandLine(cmdLine, inputMux, errorMux);
             // check its exit value
             log.debug("Execution of " + getExecutable() + " returned: " + err);
             if (err != 0) {
-                throwException(errorRecorder.getBuffer() + " (return code: " + err
-                        + ")");
+                throwException(errorRecorder.getBuffer() + " (return code: " + err + ")");
             }
-        } catch (CommandLineException e) {
-            throwException(
-                    "Error executing " + getExecutable() + ": "
-                            + e.toString());
+        }
+        catch (CommandLineException e) {
+            throwException("Error executing " + getExecutable() + ": " + e.toString());
         }
     }
 
     /**
-     * Execute the command given by getExecutable with args as list of arguments and custom StreamConsumer.
-     * Also env content will be added to the environment. 
+     * Execute the command given by getExecutable with args as list of arguments and custom
+     * StreamConsumer. Also env content will be added to the environment.
      * 
-     * @param args
-     *            an array representing blocks arguments
-     * @param env
-     *            additional key to add the environment
-     * @param output
-     *            the StreamConsumer to analyze the output with. If null it is
-     *            ignored.
+     * @param args an array representing blocks arguments
+     * @param env additional key to add the environment
+     * @param output the StreamConsumer to analyze the output with. If null it is ignored.
      * @throws T
      */
-    public void execute(String[] args, Map<String, String> env, StreamConsumer output)
-            throws T {
+    public void execute(String[] args, Map<String, String> env, StreamConsumer output) throws T {
         Commandline cmdLine = new Commandline();
         cmdLine.createArg().setValue(getExecutable());
         if (args != null) {
@@ -210,5 +199,5 @@
         }
         executeCmd(cmdLine, env, output);
     }
-    
+
 }