From ceaf662ffb39a0a16066240d1d27da3563de9ec0 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Sun, 6 Jan 2002 01:58:11 +0000 Subject: [PATCH] Refactor and clean code Also start using Execute2 abstraction git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270563 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/unix/Rpm.java | 196 +++++------------- .../apache/tools/ant/taskdefs/unix/Rpm.java | 196 +++++------------- 2 files changed, 114 insertions(+), 278 deletions(-) diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/unix/Rpm.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/unix/Rpm.java index 8ee40cf00..57c7a4011 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/unix/Rpm.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/unix/Rpm.java @@ -7,220 +7,138 @@ */ package org.apache.tools.ant.taskdefs.unix; -import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; +import org.apache.myrmidon.api.AbstractTask; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.Task; -import org.apache.tools.ant.taskdefs.exec.Execute; -import org.apache.tools.ant.taskdefs.exec.LogOutputStream; +import org.apache.tools.ant.taskdefs.exec.Execute2; import org.apache.tools.ant.types.Commandline; /** * @author lucas@collab.net */ -public class Rpm extends Task +public class Rpm + extends AbstractTask { - /** * the rpm command to use */ - private String command = "-bb"; + private String m_command = "-bb"; /** * clean BUILD directory */ - private boolean cleanBuildDir = false; + private boolean m_cleanBuildDir; /** * remove spec file */ - private boolean removeSpec = false; + private boolean m_removeSpec; /** * remove sources */ - private boolean removeSource = false; - - /** - * the file to direct standard error from the command - */ - private File error; - - /** - * the file to direct standard output from the command - */ - private File output; + private boolean m_removeSource; /** * the spec file */ - private String specFile; + private String m_specFile; /** * the rpm top dir */ - private File topDir; - - public void setCleanBuildDir( boolean cbd ) - { - cleanBuildDir = cbd; - } - - public void setCommand( String c ) - { - this.command = c; - } + private File m_topDir; - public void setError( File error ) + public void setCleanBuildDir( boolean cleanBuildDir ) { - this.error = error; + m_cleanBuildDir = cleanBuildDir; } - public void setOutput( File output ) + public void setCommand( final String command ) { - this.output = output; + m_command = command; } - public void setRemoveSource( boolean rs ) + public void setRemoveSource( final boolean removeSource ) { - removeSource = rs; + m_removeSource = removeSource; } - public void setRemoveSpec( boolean rs ) + public void setRemoveSpec( final boolean removeSpec ) { - removeSpec = rs; + m_removeSpec = removeSpec; } - public void setSpecFile( String sf ) + public void setSpecFile( final String specFile ) throws TaskException { - if( ( sf == null ) || ( sf.trim().equals( "" ) ) ) + if( ( specFile == null ) || ( specFile.trim().equals( "" ) ) ) { throw new TaskException( "You must specify a spec file" ); } - this.specFile = sf; + m_specFile = specFile; } - public void setTopDir( File td ) + public void setTopDir( final File topDir ) { - this.topDir = td; + m_topDir = topDir; } public void execute() throws TaskException { + final Commandline cmd = createCommand(); + final Execute2 exe = new Execute2(); + setupLogger( exe ); - Commandline toExecute = new Commandline(); + if( m_topDir == null ) m_topDir = getBaseDirectory(); + exe.setWorkingDirectory( m_topDir ); - toExecute.setExecutable( "rpm" ); - if( topDir != null ) + exe.setCommandline( cmd.getCommandline() ); + try { - toExecute.createArgument().setValue( "--define" ); - toExecute.createArgument().setValue( "_topdir" + topDir ); - } + final String message = "Building the RPM based on the " + m_specFile + " file"; + getLogger().info( message ); - toExecute.createArgument().setLine( command ); - - if( cleanBuildDir ) - { - toExecute.createArgument().setValue( "--clean" ); - } - if( removeSpec ) - { - toExecute.createArgument().setValue( "--rmspec" ); + if( 0 != exe.execute() ) + { + throw new TaskException( "Failed to execute rpm" ); + } } - if( removeSource ) + catch( IOException e ) { - toExecute.createArgument().setValue( "--rmsource" ); + throw new TaskException( "Error", e ); } + } - toExecute.createArgument().setValue( "SPECS/" + specFile ); - - OutputStream outputstream = null; - OutputStream errorstream = null; - - if( error == null && output == null ) + private Commandline createCommand() + throws TaskException + { + final Commandline cmd = new Commandline(); + cmd.setExecutable( "rpm" ); + if( m_topDir != null ) { - outputstream = new LogOutputStream( getLogger(), false ); - errorstream = new LogOutputStream( getLogger(), true ); + cmd.createArgument().setValue( "--define" ); + cmd.createArgument().setValue( "_topdir" + m_topDir ); } - else - { - if( output != null ) - { - try - { - outputstream = new PrintStream( new BufferedOutputStream( new FileOutputStream( output ) ) ); - } - catch( IOException e ) - { - throw new TaskException( "Error", e ); - } - } - else - { - outputstream = new LogOutputStream( getLogger(), false ); - } - if( error != null ) - { - try - { - errorstream = new PrintStream( new BufferedOutputStream( new FileOutputStream( error ) ) ); - } - catch( IOException e ) - { - throw new TaskException( "Error", e ); - } - } - else - { - errorstream = new LogOutputStream( getLogger(), true ); - } - } - - Execute exe = new Execute(); - exe.setOutput( outputstream ); - exe.setError( errorstream ); - if( topDir == null ) topDir = getBaseDirectory(); - exe.setWorkingDirectory( topDir ); + cmd.createArgument().setLine( m_command ); - exe.setCommandline( toExecute.getCommandline() ); - try + if( m_cleanBuildDir ) { - exe.execute(); - getLogger().info( "Building the RPM based on the " + specFile + " file" ); + cmd.createArgument().setValue( "--clean" ); } - catch( IOException e ) + if( m_removeSpec ) { - throw new TaskException( "Error", e ); + cmd.createArgument().setValue( "--rmspec" ); } - finally + if( m_removeSource ) { - if( output != null ) - { - try - { - outputstream.close(); - } - catch( IOException e ) - { - } - } - if( error != null ) - { - try - { - errorstream.close(); - } - catch( IOException e ) - { - } - } + cmd.createArgument().setValue( "--rmsource" ); } + + cmd.createArgument().setValue( "SPECS/" + m_specFile ); + return cmd; } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/unix/Rpm.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/unix/Rpm.java index 8ee40cf00..57c7a4011 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/unix/Rpm.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/unix/Rpm.java @@ -7,220 +7,138 @@ */ package org.apache.tools.ant.taskdefs.unix; -import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; +import org.apache.myrmidon.api.AbstractTask; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.Task; -import org.apache.tools.ant.taskdefs.exec.Execute; -import org.apache.tools.ant.taskdefs.exec.LogOutputStream; +import org.apache.tools.ant.taskdefs.exec.Execute2; import org.apache.tools.ant.types.Commandline; /** * @author lucas@collab.net */ -public class Rpm extends Task +public class Rpm + extends AbstractTask { - /** * the rpm command to use */ - private String command = "-bb"; + private String m_command = "-bb"; /** * clean BUILD directory */ - private boolean cleanBuildDir = false; + private boolean m_cleanBuildDir; /** * remove spec file */ - private boolean removeSpec = false; + private boolean m_removeSpec; /** * remove sources */ - private boolean removeSource = false; - - /** - * the file to direct standard error from the command - */ - private File error; - - /** - * the file to direct standard output from the command - */ - private File output; + private boolean m_removeSource; /** * the spec file */ - private String specFile; + private String m_specFile; /** * the rpm top dir */ - private File topDir; - - public void setCleanBuildDir( boolean cbd ) - { - cleanBuildDir = cbd; - } - - public void setCommand( String c ) - { - this.command = c; - } + private File m_topDir; - public void setError( File error ) + public void setCleanBuildDir( boolean cleanBuildDir ) { - this.error = error; + m_cleanBuildDir = cleanBuildDir; } - public void setOutput( File output ) + public void setCommand( final String command ) { - this.output = output; + m_command = command; } - public void setRemoveSource( boolean rs ) + public void setRemoveSource( final boolean removeSource ) { - removeSource = rs; + m_removeSource = removeSource; } - public void setRemoveSpec( boolean rs ) + public void setRemoveSpec( final boolean removeSpec ) { - removeSpec = rs; + m_removeSpec = removeSpec; } - public void setSpecFile( String sf ) + public void setSpecFile( final String specFile ) throws TaskException { - if( ( sf == null ) || ( sf.trim().equals( "" ) ) ) + if( ( specFile == null ) || ( specFile.trim().equals( "" ) ) ) { throw new TaskException( "You must specify a spec file" ); } - this.specFile = sf; + m_specFile = specFile; } - public void setTopDir( File td ) + public void setTopDir( final File topDir ) { - this.topDir = td; + m_topDir = topDir; } public void execute() throws TaskException { + final Commandline cmd = createCommand(); + final Execute2 exe = new Execute2(); + setupLogger( exe ); - Commandline toExecute = new Commandline(); + if( m_topDir == null ) m_topDir = getBaseDirectory(); + exe.setWorkingDirectory( m_topDir ); - toExecute.setExecutable( "rpm" ); - if( topDir != null ) + exe.setCommandline( cmd.getCommandline() ); + try { - toExecute.createArgument().setValue( "--define" ); - toExecute.createArgument().setValue( "_topdir" + topDir ); - } + final String message = "Building the RPM based on the " + m_specFile + " file"; + getLogger().info( message ); - toExecute.createArgument().setLine( command ); - - if( cleanBuildDir ) - { - toExecute.createArgument().setValue( "--clean" ); - } - if( removeSpec ) - { - toExecute.createArgument().setValue( "--rmspec" ); + if( 0 != exe.execute() ) + { + throw new TaskException( "Failed to execute rpm" ); + } } - if( removeSource ) + catch( IOException e ) { - toExecute.createArgument().setValue( "--rmsource" ); + throw new TaskException( "Error", e ); } + } - toExecute.createArgument().setValue( "SPECS/" + specFile ); - - OutputStream outputstream = null; - OutputStream errorstream = null; - - if( error == null && output == null ) + private Commandline createCommand() + throws TaskException + { + final Commandline cmd = new Commandline(); + cmd.setExecutable( "rpm" ); + if( m_topDir != null ) { - outputstream = new LogOutputStream( getLogger(), false ); - errorstream = new LogOutputStream( getLogger(), true ); + cmd.createArgument().setValue( "--define" ); + cmd.createArgument().setValue( "_topdir" + m_topDir ); } - else - { - if( output != null ) - { - try - { - outputstream = new PrintStream( new BufferedOutputStream( new FileOutputStream( output ) ) ); - } - catch( IOException e ) - { - throw new TaskException( "Error", e ); - } - } - else - { - outputstream = new LogOutputStream( getLogger(), false ); - } - if( error != null ) - { - try - { - errorstream = new PrintStream( new BufferedOutputStream( new FileOutputStream( error ) ) ); - } - catch( IOException e ) - { - throw new TaskException( "Error", e ); - } - } - else - { - errorstream = new LogOutputStream( getLogger(), true ); - } - } - - Execute exe = new Execute(); - exe.setOutput( outputstream ); - exe.setError( errorstream ); - if( topDir == null ) topDir = getBaseDirectory(); - exe.setWorkingDirectory( topDir ); + cmd.createArgument().setLine( m_command ); - exe.setCommandline( toExecute.getCommandline() ); - try + if( m_cleanBuildDir ) { - exe.execute(); - getLogger().info( "Building the RPM based on the " + specFile + " file" ); + cmd.createArgument().setValue( "--clean" ); } - catch( IOException e ) + if( m_removeSpec ) { - throw new TaskException( "Error", e ); + cmd.createArgument().setValue( "--rmspec" ); } - finally + if( m_removeSource ) { - if( output != null ) - { - try - { - outputstream.close(); - } - catch( IOException e ) - { - } - } - if( error != null ) - { - try - { - errorstream.close(); - } - catch( IOException e ) - { - } - } + cmd.createArgument().setValue( "--rmsource" ); } + + cmd.createArgument().setValue( "SPECS/" + m_specFile ); + return cmd; } }