Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 Jayway AB
   * Copyright (C) 2007-2008 JVending Masa
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.simpligility.maven.plugins.androidndk;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
 
 public interface CommandExecutor
 {
    
Sets the plexus logger.

Parameters:
logger the plexus logger
 
     void setLoggerLog logger );

    
Executes the command for the specified executable and list of command options.

Parameters:
executable the name of the executable (csc, xsd, etc).
commands the command options for the compiler/executable
Throws:
ExecutionException if compiler or executable writes anything to the standard error stream or if the process returns a process result != 0.
 
     void executeCommandString executableListString > commands ) throws ExecutionException;

    
Executes the command for the specified executable and list of command options.

Parameters:
executable the name of the executable (csc, xsd, etc).
commands the commands options for the compiler/executable
failsOnErrorOutput if true, throws an ExecutionException if there the compiler or executable writes anything to the error output stream. By default, this value is true
Throws:
ExecutionException if compiler or executable writes anything to the standard error stream (provided the failsOnErrorOutput is not false) or if the process returns a process result != 0.
 
     void executeCommandString executableListString > commandsboolean failsOnErrorOutput )
             throws ExecutionException;

    
Executes the command for the specified executable and list of command options. If the compiler or executable is not within the environmental path, you should use this method to specify the working directory. Always use this method for executables located within the local maven repository.

Parameters:
executable the name of the executable (csc, xsd, etc).
commands the command options for the compiler/executable
workingDirectory the directory where the command will be executed
Throws:
ExecutionException if compiler or executable writes anything to the standard error stream (provided the failsOnErrorOutput is not false) or if the process returns a process result != 0.
 
     void executeCommandString executableListString > commandsFile workingDirectoryboolean failsOnErrorOutput )
             throws ExecutionException;

    
Returns the process result of executing the command. Typically a value of 0 means that the process executed successfully.

Returns:
the process result of executing the command
 
     int getResult();

    

Returns:
the process id for the executed command.
    long getPid();

    
Returns the standard output from executing the command.

Returns:
the standard output from executing the command
    String getStandardOut();

    
Returns the standard error from executing the command.

Returns:
the standard error from executing the command
    String getStandardError();

    
Adds an environment variable with the specified name and value to the executor.
    void addEnvironmentString nameString value );
    void setErrorListenerErrorListener errorListener );
    void setCustomShellShell s );
    void setCaptureStdOutboolean captureStdOut );
    void setCaptureStdErrboolean captureStdErr );

    
    public interface ErrorListener
    {
        boolean isErrorString error );
    }

    
Provides factory services for creating a default instance of the command executor.
    public static class Factory
    {

        
Constructor
        private Factory()
        {
        }
        private static final class DefaultCommandExecutor implements CommandExecutor
        {
            private MapStringString > environment;
            
Instance of a plugin logger.
            private Log logger;
            
Standard Out
            private StreamConsumer stdOut;
            
Standard Error
            private ErrorStreamConsumer stdErr;
            
Process result
            private int result;
            /*
             */
            private ErrorListener errorListener;
            long pid;
            private Commandline commandline;
            private Shell customShell;
            private boolean captureStdOut;
            private boolean captureStdErr;
            @Override
            public void setLoggerLog logger )
            {
                this. = logger;
            }
            @Override
            public void executeCommandString executableListString > commands ) throws ExecutionException
            {
                executeCommandexecutablecommandsnulltrue );
            }
            @Override
            public void executeCommandString executableListString > commandsboolean failsOnErrorOutput )
                    throws ExecutionException
            {
                executeCommandexecutablecommandsnullfailsOnErrorOutput );
            }
            @Override
            public void executeCommandString executableListString > commandsFile workingDirectory,
                    boolean failsOnErrorOutput ) throws ExecutionException
            {
                if ( commands == null )
                {
                    commands = new ArrayListString >();
                }
                 = new StreamConsumerImpl );
                 = new ErrorStreamConsumer );
                 = new Commandline();
                if (  != null )
                {
                    .setShell );
                }
                .setExecutableexecutable );
                // Add the environment variables as needed
                if (  != null )
                {
                    for ( Map.EntryStringString > entry : .entrySet() )
                    {
                        .addEnvironmententry.getKey(), entry.getValue() );
                    }
                }
                .addArgumentscommands.toArraynew Stringcommands.size() ] ) );
                if ( workingDirectory != null && workingDirectory.exists() )
                {
                    .setWorkingDirectoryworkingDirectory.getAbsolutePath() );
                }
                try
                {
                    .debug"ANDROID-040-000: Executing command: Commandline = " +  );
                     = CommandLineUtils.executeCommandLine );
                    if (  != null )
                    {
                        .debug"ANDROID-040-000: Executed command: Commandline = " +  + ", Result = "
                                +  );
                    }
                    else
                    {
                        ..println"ANDROID-040-000: Executed command: Commandline = " + 
                                + ", Result = " +  );
                    }
                    if ( failsOnErrorOutput && .hasError() ||  != 0 )
                    {
                        throw new ExecutionException"ANDROID-040-001: Could not execute: Command = "
                                + .toString() + ", Result = " +  );
                    }
                }
                catch ( CommandLineException e )
                {
                    throw new ExecutionException"ANDROID-040-002: Could not execute: Command = "
                            + .toString() + ", Error message = " + e.getMessage() );
                }
                setPid.getPid() );
            }
            @Override
            public int getResult()
            {
                return ;
            }
            @Override
            public String getStandardOut()
            {
                if ( !  )
                {
                    throw new IllegalStateException"Unable to provide StdOut since it was not captured" );
                }
                return .toString();
            }
            @Override
            public String getStandardError()
            {
                if ( !  )
                {
                    throw new IllegalStateException"Unable to provide StdOut since it was not captured" );
                }
                return .toString();
            }
            @Override
            public void addEnvironmentString nameString value )
            {
                if (  == null )
                {
                     = new HashMapStringString >();
                }
                .putnamevalue );
            }
            @Override
            public void setErrorListenerErrorListener errorListener )
            {
                this. = errorListener;
            }
            public void setPidlong pid )
            {
                this. = pid;
            }
            @Override
            public long getPid()
            {
                return ;
            }
            @Override
            public void setCustomShellShell shell )
            {
                this. = shell;
            }
            @Override
            public void setCaptureStdOutboolean captureStdOut )
            {
                this. = captureStdOut;
            }
            @Override
            public void setCaptureStdErrboolean captureStdErr )
            {
                this. = captureStdErr;
            }
        }

        
StreamConsumer instance that buffers the entire output
        static class StreamConsumerImpl implements StreamConsumer
        {
            private StringBuffer sb = new StringBuffer();
            private final Log logger;
            private boolean captureStdOut;
            public StreamConsumerImplLog loggerboolean captureStdOut )
            {
                this. = logger;
                this. = captureStdOut;
            }
            @Override
            public void consumeLineString line )
            {
                if (  )
                {
                    .appendline );
                }
                if (  != null )
                {
                    .debugline );
                }
            }

            
Returns the stream

Returns:
the stream
            @Override
            public String toString()
            {
                return .toString();
            }
        }

        
Provides behavior for determining whether the command utility wrote anything to the Standard Error Stream. NOTE: I am using this to decide whether to fail the NMaven build. If the compiler implementation chooses to write warnings to the error stream, then the build will fail on warnings!!!
        static class ErrorStreamConsumer implements StreamConsumer
        {
            
Is true if there was anything consumed from the stream, otherwise false
            private boolean error;
            
Buffer to store the stream
            private StringBuffer sbe = new StringBuffer();
            private final Log logger;
            private final ErrorListener errorListener;
            private boolean captureStdErr;
            public ErrorStreamConsumerLog loggerErrorListener errorListenerboolean captureStdErr )
            {
                this. = logger;
                this. = errorListener;
                this. = captureStdErr;
                if ( logger == null )
                {
                    ..println"ANDROID-040-003: Error Log not set: Will not output error logs" );
                }
                 = false;
            }
            @Override
            public void consumeLineString line )
            {
                if (  )
                {
                    .appendline );
                }
                if (  != null )
                {
                    .infoline );
                }
                if (  != null )
                {
                     = .isErrorline );
                }
                else
                {
                     = true;
                }
            }

            
Returns false if the command utility wrote to the Standard Error Stream, otherwise returns true.

Returns:
false if the command utility wrote to the Standard Error Stream, otherwise returns true.
            public boolean hasError()
            {
                return ;
            }

            
Returns the error stream

Returns:
error stream
            @Override
            public String toString()
            {
                return .toString();
            }
        }

        
Returns a default instance of the command executor

Returns:
a default instance of the command executor
        public static CommandExecutor createDefaultCommmandExecutor()
        {
            return new DefaultCommandExecutor();
        }
    }
New to GrepCode? Check out our FAQ X