You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@continuum.apache.org by ba...@apache.org on 2015/01/31 04:33:39 UTC

svn commit: r1656154 - /continuum/trunk/continuum-commons/src/main/java/org/apache/continuum/installation/DefaultInstallationService.java

Author: batkinson
Date: Sat Jan 31 03:33:39 2015
New Revision: 1656154

URL: http://svn.apache.org/r1656154
Log:
Replaced install service usage of CommandlineUtils with shell helper.

Modified:
    continuum/trunk/continuum-commons/src/main/java/org/apache/continuum/installation/DefaultInstallationService.java

Modified: continuum/trunk/continuum-commons/src/main/java/org/apache/continuum/installation/DefaultInstallationService.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-commons/src/main/java/org/apache/continuum/installation/DefaultInstallationService.java?rev=1656154&r1=1656153&r2=1656154&view=diff
==============================================================================
--- continuum/trunk/continuum-commons/src/main/java/org/apache/continuum/installation/DefaultInstallationService.java (original)
+++ continuum/trunk/continuum-commons/src/main/java/org/apache/continuum/installation/DefaultInstallationService.java Sat Jan 31 03:33:39 2015
@@ -20,6 +20,8 @@ package org.apache.continuum.installatio
  */
 
 import org.apache.continuum.dao.InstallationDao;
+import org.apache.continuum.utils.shell.ExecutionResult;
+import org.apache.continuum.utils.shell.ShellCommandHelper;
 import org.apache.maven.continuum.execution.ExecutorConfigurator;
 import org.apache.maven.continuum.installation.AlreadyExistsInstallationException;
 import org.apache.maven.continuum.installation.InstallationException;
@@ -33,16 +35,11 @@ import org.apache.maven.continuum.store.
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
 import org.codehaus.plexus.util.StringUtils;
-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;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 
 import java.io.File;
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -69,6 +66,9 @@ public class DefaultInstallationService
     @Resource
     private ProfileService profileService;
 
+    @Resource
+    private ShellCommandHelper shellCommandHelper;
+
     private Map<String, ExecutorConfigurator> typesValues;
 
     // ---------------------------------------------
@@ -288,10 +288,9 @@ public class DefaultInstallationService
     {
         try
         {
-            Properties systemEnvVars = CommandLineUtils.getSystemEnvVars( false );
-
+            Properties systemEnvVars = shellCommandHelper.getSystemEnvVars();
             String javaHome = (String) systemEnvVars.get( "JAVA_HOME" );
-            // olamy : JAVA_HOME can not exists with a mac user
+            // olamy : JAVA_HOME may not exist for mac users
             if ( StringUtils.isEmpty( javaHome ) )
             {
                 return getJavaVersionInfo( System.getProperty( "java.home" ) );
@@ -299,11 +298,7 @@ public class DefaultInstallationService
             return getJavaVersionInfo( javaHome );
 
         }
-        catch ( IOException e )
-        {
-            throw new InstallationException( e.getMessage(), e );
-        }
-        catch ( CommandLineException e )
+        catch ( Exception e )
         {
             throw new InstallationException( e.getMessage(), e );
         }
@@ -329,7 +324,7 @@ public class DefaultInstallationService
         {
             return getJavaVersionInfo( installation.getVarValue() );
         }
-        catch ( CommandLineException e )
+        catch ( Exception e )
         {
             throw new InstallationException( e.getMessage(), e );
         }
@@ -341,33 +336,25 @@ public class DefaultInstallationService
      * @throws Exception
      */
     private List<String> getJavaVersionInfo( String homePath )
-        throws CommandLineException
+        throws Exception
     {
-        Commandline commandline = new Commandline();
 
-        String executable = javaHome + File.separator + "bin" + File.separator + "java";
-
-        commandline.setExecutable( executable );
-        commandline.addArguments( new String[]{"-version"} );
+        String executable = homePath + File.separator + "bin" + File.separator + "java";
         final List<String> cliOutput = new ArrayList<String>();
-        //TODO ShellCommandHelper ?
-        int result = CommandLineUtils.executeCommandLine( commandline, new StreamConsumer()
-                                                          {
-                                                              public void consumeLine( String line )
-                                                              {
-                                                                  cliOutput.add( line );
-                                                              }
-                                                          }, new StreamConsumer()
-                                                          {
-                                                              public void consumeLine( String line )
-                                                              {
-                                                                  cliOutput.add( line );
-                                                              }
-                                                          }
-        );
-        if ( result != 0 )
-        {
-            throw new CommandLineException( "cli to get JAVA_HOME informations return code " + result );
+        ExecutionResult result = shellCommandHelper.executeShellCommand( null, executable, new String[] { "-version" },
+                                                                         new ShellCommandHelper.IOConsumer()
+                                                                         {
+                                                                             public void consume( String line )
+                                                                             {
+                                                                                 cliOutput.add( line );
+                                                                             }
+                                                                         }, -1, null );
+        int exitCode = result.getExitCode();
+        if ( exitCode != 0 )
+        {
+            throw new Exception(
+                String.format( "failed to get java version information, %s returned exit code %s", executable,
+                               exitCode ) );
         }
         return cliOutput;
     }
@@ -400,69 +387,57 @@ public class DefaultInstallationService
     /**
      * @see org.apache.maven.continuum.installation.InstallationService#getExecutorVersionInfo(java.lang.String, org.apache.maven.continuum.execution.ExecutorConfigurator, Profile)
      */
-    @SuppressWarnings( "unchecked" )
     public List<String> getExecutorVersionInfo( String path, ExecutorConfigurator executorConfigurator,
                                                 Profile profile )
         throws InstallationException
     {
-
-        if ( executorConfigurator == null )
+        if ( executorConfigurator == null || executorConfigurator.getExecutable() == null )
         {
             return Collections.EMPTY_LIST;
         }
-        if ( executorConfigurator.getExecutable() == null )
+
+        StringBuilder executable = new StringBuilder();
+
+        Map<String, String> env = new HashMap<String, String>();
+
+        if ( StringUtils.isNotEmpty( path ) )
         {
-            return Collections.EMPTY_LIST;
+            executable.append( path ).append( File.separator );
+            executable.append( executorConfigurator.getRelativePath() ).append( File.separator );
+            env.put( executorConfigurator.getEnvVar(), path );
         }
-        StringBuilder executable = new StringBuilder();
+        executable = executable.append( executorConfigurator.getExecutable() );
+
+        env.putAll( getEnvVars( profile ) );
+
+        final List<String> cliOutput = new ArrayList<String>();
+        ExecutionResult result;
         try
         {
-            Commandline commandline = new Commandline();
-            if ( StringUtils.isNotEmpty( path ) )
-            {
-                executable.append( path ).append( File.separator );
-                executable.append( executorConfigurator.getRelativePath() ).append( File.separator );
-                commandline.addEnvironment( executorConfigurator.getEnvVar(), path );
-            }
-            //Installations are env var they must be add if exists
-            Map<String, String> environments = getEnvVars( profile );
-            // no null check we use a private method just here
-            for ( String key : environments.keySet() )
-            {
-                String value = environments.get( key );
-                commandline.addEnvironment( key, value );
-            }
-
-            executable = executable.append( executorConfigurator.getExecutable() );
-            commandline.setExecutable( executable.toString() );
-            commandline.addArguments( new String[]{executorConfigurator.getVersionArgument()} );
-            final List<String> cliOutput = new ArrayList<String>();
-            //TODO ShellCommandHelper ?
-            int result = CommandLineUtils.executeCommandLine( commandline, new StreamConsumer()
-                                                              {
-                                                                  public void consumeLine( String line )
-                                                                  {
-                                                                      cliOutput.add( line );
-                                                                  }
-                                                              }, new StreamConsumer()
-                                                              {
-                                                                  public void consumeLine( String line )
-                                                                  {
-                                                                      cliOutput.add( line );
-                                                                  }
-                                                              }
-            );
-            if ( result != 0 )
-            {
-                throw new InstallationException( "cli to get " + executable + " version return code " + result );
-            }
-            return cliOutput;
+            result = shellCommandHelper.executeShellCommand( null, executable.toString(), new String[] {
+                                                                 executorConfigurator.getVersionArgument() },
+                                                             new ShellCommandHelper.IOConsumer()
+                                                             {
+                                                                 public void consume( String line )
+                                                                 {
+                                                                     cliOutput.add( line );
+                                                                 }
+                                                             }, -1, null );
         }
-        catch ( CommandLineException e )
+        catch ( Exception e )
         {
-            log.error( "fail to execute " + executable + " with arg " + executorConfigurator.getVersionArgument() );
+            log.error( "failed to get executor version info", e );
             throw new InstallationException( e.getMessage(), e );
         }
+        int exitCode = result.getExitCode();
+        if ( exitCode != 0 )
+        {
+
+            throw new InstallationException(
+                String.format( "failed to get executor version info, %s returned exit code %s", executable,
+                               exitCode ) );
+        }
+        return cliOutput;
     }
 
     private boolean alreadyExistInstallationName( Installation installation )