continuum-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From batkin...@apache.org
Subject svn commit: r1656151 - in /continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell: DefaultShellCommandHelper.java ShellCommandHelper.java
Date Sat, 31 Jan 2015 03:33:22 GMT
Author: batkinson
Date: Sat Jan 31 03:33:21 2015
New Revision: 1656151

URL: http://svn.apache.org/r1656151
Log:
Expanded shell helper to encapsulate all uses of CommandLineUtils.

Modified:
    continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java
    continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ShellCommandHelper.java

Modified: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java?rev=1656151&r1=1656150&r2=1656151&view=diff
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java
(original)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/DefaultShellCommandHelper.java
Sat Jan 31 03:33:21 2015
@@ -27,16 +27,17 @@ import org.codehaus.plexus.util.cli.Comm
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 import org.codehaus.plexus.util.cli.Commandline;
 import org.codehaus.plexus.util.cli.StreamConsumer;
-import org.codehaus.plexus.util.cli.WriterStreamConsumer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 
 import java.io.File;
 import java.io.FileWriter;
-import java.io.Writer;
+import java.io.IOException;
+import java.io.PrintWriter;
 import java.util.Arrays;
 import java.util.Map;
+import java.util.Properties;
 
 /**
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
@@ -52,6 +53,19 @@ public class DefaultShellCommandHelper
     // ShellCommandHelper Implementation
     // ----------------------------------------------------------------------
 
+    public Properties getSystemEnvVars()
+    {
+        try
+        {
+            return CommandLineUtils.getSystemEnvVars( false );
+        }
+        catch ( IOException e )
+        {
+            log.warn( "failed to get system environment", e );
+        }
+        return new Properties();
+    }
+
     public ExecutionResult executeShellCommand( File workingDirectory, String executable,
String arguments, File output,
                                                 long idCommand, Map<String, String>
environments )
         throws Exception
@@ -66,6 +80,82 @@ public class DefaultShellCommandHelper
                                     environments );
     }
 
+    private static class IOConsumerWrapper
+        implements StreamConsumer
+    {
+        private IOConsumer userConsumer;
+
+        public IOConsumerWrapper( IOConsumer userConsumer )
+        {
+            this.userConsumer = userConsumer;
+        }
+
+        public void consumeLine( String line )
+        {
+            if ( userConsumer != null )
+            {
+                userConsumer.consume( line );
+            }
+        }
+    }
+
+    private static class FileIOConsumer
+        implements IOConsumer
+    {
+        private PrintWriter writer;
+
+        public FileIOConsumer( File outputFile )
+        {
+            try
+            {
+                this.writer = new PrintWriter( new FileWriter( outputFile ) );
+            }
+            catch ( IOException e )
+            {
+                log.warn( "failed to create file-based io consumer", e );
+            }
+        }
+
+        public void consume( String line )
+        {
+            if ( writer != null )
+            {
+                writer.println( line );
+            }
+        }
+
+        public void flush()
+        {
+            if ( writer != null )
+                writer.flush();
+        }
+
+        public void close()
+        {
+            if ( writer != null )
+                writer.close();
+        }
+    }
+
+    public ExecutionResult executeShellCommand( File workingDirectory, String executable,
String[] arguments,
+                                                IOConsumer io, long idCommand,
+                                                Map<String, String> environments )
+        throws Exception
+    {
+        Commandline cl = createCommandline( workingDirectory, executable, arguments, idCommand,
environments );
+
+        log.info( "Executing: " + cl );
+        File clWorkDir = cl.getWorkingDirectory();
+        log.info( "Working directory: " + ( clWorkDir != null ? clWorkDir.getAbsolutePath()
: "default" ) );
+        log.debug( "EnvironmentVariables " + Arrays.asList( cl.getEnvironmentVariables()
) );
+
+        StreamConsumer consumer = new IOConsumerWrapper( io );
+
+        int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, consumer );
+
+        return new ExecutionResult( exitCode );
+    }
+
     /**
      * Make the command line
      *
@@ -117,34 +207,17 @@ public class DefaultShellCommandHelper
                                                 File output, long idCommand, Map<String,
String> environments )
         throws Exception
     {
-
-        Commandline cl = createCommandline( workingDirectory, executable, arguments, idCommand,
environments );
-
-        log.info( "Executing: " + cl );
-        log.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
-        log.debug( "EnvironmentVariables " + Arrays.asList( cl.getEnvironmentVariables()
) );
-
-        // ----------------------------------------------------------------------
-        //
-        // ----------------------------------------------------------------------
-
-        //CommandLineUtils.StringStreamConsumer consumer = new CommandLineUtils.StringStreamConsumer();
-
-        Writer writer = new FileWriter( output );
-
-        StreamConsumer consumer = new WriterStreamConsumer( writer );
-
-        int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, consumer );
-
-        writer.flush();
-
-        writer.close();
-
-        // ----------------------------------------------------------------------
-        //
-        // ----------------------------------------------------------------------
-
-        return new ExecutionResult( exitCode );
+        FileIOConsumer fileConsumer = new FileIOConsumer( output );
+        try
+        {
+            return executeShellCommand( workingDirectory, executable, arguments, fileConsumer,
idCommand,
+                                        environments );
+        }
+        finally
+        {
+            fileConsumer.flush();
+            fileConsumer.close();
+        }
     }
 
     public boolean isRunning( long idCommand )

Modified: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ShellCommandHelper.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ShellCommandHelper.java?rev=1656151&r1=1656150&r2=1656151&view=diff
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ShellCommandHelper.java
(original)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/ShellCommandHelper.java
Sat Jan 31 03:33:21 2015
@@ -23,6 +23,7 @@ import org.apache.maven.shared.release.R
 
 import java.io.File;
 import java.util.Map;
+import java.util.Properties;
 
 /**
  * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
@@ -32,10 +33,21 @@ public interface ShellCommandHelper
 {
     String ROLE = ShellCommandHelper.class.getName();
 
+    public static interface IOConsumer
+    {
+        void consume( String line );
+    }
+
+    Properties getSystemEnvVars();
+
     ExecutionResult executeShellCommand( File workingDirectory, String executable, String
arguments, File output,
                                          long idCommand, Map<String, String> environments
)
         throws Exception;
 
+    ExecutionResult executeShellCommand( File workingDirectory, String executable, String[]
arguments, IOConsumer io,
+                                         long idCommand, Map<String, String> environments
)
+        throws Exception;
+
     ExecutionResult executeShellCommand( File workingDirectory, String executable, String[]
arguments, File output,
                                          long idCommand, Map<String, String> environments
)
         throws Exception;



Mime
View raw message