You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by dk...@apache.org on 2007/07/07 23:18:38 UTC

svn commit: r554268 - in /maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec: ForkedMavenExecutor.java RawStreamPumper.java TeeOutputStream.java

Author: dkulp
Date: Sat Jul  7 14:18:37 2007
New Revision: 554268

URL: http://svn.apache.org/viewvc?view=rev&rev=554268
Log:
[MRELEASE-97, MRELEASE-263, MGPG-9]
Use raw streams (instead of line based buffered IO) for interacting with forked process. 
Redirect System.in into forked process.

Added:
    maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/RawStreamPumper.java   (with props)
    maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/TeeOutputStream.java   (with props)
Modified:
    maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/ForkedMavenExecutor.java

Modified: maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/ForkedMavenExecutor.java
URL: http://svn.apache.org/viewvc/maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/ForkedMavenExecutor.java?view=diff&rev=554268&r1=554267&r2=554268
==============================================================================
--- maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/ForkedMavenExecutor.java (original)
+++ maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/ForkedMavenExecutor.java Sat Jul  7 14:18:37 2007
@@ -26,8 +26,13 @@
 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.StreamFeeder;
+import org.codehaus.plexus.util.cli.StreamPumper;
 
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 
 /**
  * Fork Maven to executed a series of goals.
@@ -87,16 +92,16 @@
             cl.createArgument().setLine( additionalArguments );
         }
 
-        StreamConsumer stdOut = new TeeConsumer( System.out );
+        TeeOutputStream stdOut = new TeeOutputStream( System.out );
 
-        StreamConsumer stdErr = new TeeConsumer( System.err );
+        TeeOutputStream stdErr = new TeeOutputStream( System.err );
 
         try
         {
             relResult.appendInfo( "Executing: " + cl.toString() );
             getLogger().info( "Executing: " + cl.toString() );
-
-            int result = CommandLineUtils.executeCommandLine( cl, stdOut, stdErr );
+            
+            int result = executeCommandLine( cl, System.in, stdOut, stdErr );
 
             if ( result != 0 )
             {
@@ -125,4 +130,93 @@
     {
         this.commandLineFactory = commandLineFactory;
     }
+    
+    
+    
+    
+    public static int executeCommandLine( Commandline cl, InputStream systemIn, 
+                                          OutputStream systemOut, OutputStream systemErr )
+        throws CommandLineException
+    {
+        if ( cl == null )
+        {
+            throw new IllegalArgumentException( "cl cannot be null." );
+        }
+    
+        Process p = cl.execute();
+    
+        //processes.put( new Long( cl.getPid() ), p );
+    
+        RawStreamPumper inputFeeder = null;
+    
+        if ( systemIn != null )
+        {
+            inputFeeder = new RawStreamPumper( systemIn, p.getOutputStream(), true );
+        }
+    
+        RawStreamPumper outputPumper = new RawStreamPumper( p.getInputStream(), systemOut );
+        RawStreamPumper errorPumper = new RawStreamPumper( p.getErrorStream(), systemErr );
+    
+        if ( inputFeeder != null )
+        {
+            inputFeeder.start();
+        }
+    
+        outputPumper.start();
+    
+        errorPumper.start();
+    
+        try
+        {
+            int returnValue = p.waitFor();
+    
+            if ( inputFeeder != null )
+            {
+                inputFeeder.setDone();
+            }
+            outputPumper.setDone();
+            errorPumper.setDone();
+    
+            //processes.remove( new Long( cl.getPid() ) );
+    
+            return returnValue;
+        }
+        catch ( InterruptedException ex )
+        {
+            //killProcess( cl.getPid() );
+            throw new CommandLineException( "Error while executing external command, process killed.", ex );
+        } 
+        finally
+        {
+            try
+            {
+                errorPumper.closeInput();
+            } 
+            catch ( IOException e )
+            {
+                //ignore
+            }
+            try
+            {
+                outputPumper.closeInput();
+            } 
+            catch ( IOException e )
+            {
+                //ignore
+            }
+            if ( inputFeeder != null )
+            {
+                try
+                {
+                    inputFeeder.closeOutput();
+                }
+                catch ( IOException e )
+                {
+                    //ignore
+                }
+            }
+        }
+    }
+
+
 }

Added: maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/RawStreamPumper.java
URL: http://svn.apache.org/viewvc/maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/RawStreamPumper.java?view=auto&rev=554268
==============================================================================
--- maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/RawStreamPumper.java (added)
+++ maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/RawStreamPumper.java Sat Jul  7 14:18:37 2007
@@ -0,0 +1,123 @@
+package org.apache.maven.shared.release.exec;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public class RawStreamPumper
+    extends Thread 
+{
+    private InputStream in;
+
+    private OutputStream out;
+
+    boolean done;
+    
+    boolean poll;
+    
+    byte buffer[] = new byte[256];
+
+    public RawStreamPumper( InputStream in , OutputStream out, boolean poll )
+    {
+        this.in = in;
+        this.out = out;
+        this.poll = poll;
+    }
+    public RawStreamPumper( InputStream in , OutputStream out )
+    {
+        this.in = in;
+        this.out = out;
+        this.poll = false;
+    }
+    
+    public void setDone()
+    {
+        done = true;
+    }
+    
+    public void closeInput() 
+        throws IOException 
+    {
+        in.close();
+    }
+    
+    public void closeOutput() 
+        throws IOException 
+    {
+        out.close();
+    }
+    
+    public void run()
+    {
+        try
+        {
+            if ( poll )
+            {
+                while ( !done )
+                {
+                    if ( in.available() > 0 )
+                    {
+                        int i = in.read( buffer );
+                        if (i != -1) 
+                        {
+                            out.write( buffer, 0, i );
+                            out.flush();
+                        } 
+                        else
+                        {
+                            done = true;
+                        }
+                    } 
+                    else
+                    {
+                        Thread.sleep( 1 );  
+                    }
+                }
+            }
+            else
+            {
+                int i = in.read( buffer );
+                while ( i != -1 && !done )
+                {
+                    if ( i != -1 ) 
+                    {
+                        out.write( buffer, 0, i );
+                        out.flush();
+                    } 
+                    else
+                    {
+                        done = true;
+                    }
+                    i = in.read( buffer );
+                }                
+            }
+        }
+        catch ( Throwable e )
+        {
+            // Catched everything
+        }
+        finally
+        {
+            done = true;
+        }
+    }
+}
\ No newline at end of file

Propchange: maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/RawStreamPumper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/RawStreamPumper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/TeeOutputStream.java
URL: http://svn.apache.org/viewvc/maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/TeeOutputStream.java?view=auto&rev=554268
==============================================================================
--- maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/TeeOutputStream.java (added)
+++ maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/TeeOutputStream.java Sat Jul  7 14:18:37 2007
@@ -0,0 +1,86 @@
+package org.apache.maven.shared.release.exec;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import java.io.ByteArrayOutputStream;
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class TeeOutputStream 
+    extends FilterOutputStream 
+{
+    ByteArrayOutputStream bout = new ByteArrayOutputStream( 1024 );
+    byte indent[];
+    
+    public TeeOutputStream( OutputStream out )
+    {
+        this( out, "    " );
+    }
+    
+    public TeeOutputStream( OutputStream out, String i )
+    {
+        super( out );
+        indent = i.getBytes();
+    }
+    
+    public void write( byte[] b, int off, int len ) 
+        throws IOException
+    {
+        for ( int x = 0; x < len; x++ )
+        {
+            if ( b[off + x] == '\n' )
+            {
+                super.write( b, off, x + 1);
+                bout.write( b, off, x + 1);
+                super.write( indent );
+                bout.write( indent );
+                off += ( x + 1 );
+                len -= ( x + 1 );
+                x = 0;
+            }
+        }
+        super.write( b, off, len );
+        bout.write( b, off, len );
+    }
+
+    public void write( int b )
+        throws IOException
+    {
+        super.write( b );
+        bout.write( b );
+        if ( b == '\n' )
+        {
+            super.write( indent );
+            bout.write( indent );
+        }
+    }
+    
+    public String toString() 
+    {
+        return bout.toString();
+    }
+
+    public String getContent()
+    {
+        return bout.toString();
+    }
+
+}

Propchange: maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/TeeOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/release/trunk/maven-release-manager/src/main/java/org/apache/maven/shared/release/exec/TeeOutputStream.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date