@@ -8,14 +8,13 @@
package org.apache.tools.ant.taskdefs;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import org.apache.myrmidon.api.TaskException;
import org.apache.myrmidon.api.AbstractTask;
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.Argument;
import org.apache.tools.ant.types.CommandlineJava;
import org.apache.tools.ant.types.EnvironmentVariable;
@@ -29,41 +28,29 @@ import org.apache.tools.ant.types.Path;
* stefano@apache.org</a>
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
*/
public class Java extends Task
public class Java
extends AbstractTask
{
private CommandlineJava cmdl = new CommandlineJava();
private boolean fork = false;
private File dir = null;
private PrintStream outStream = null;
private boolean failOnError = false;
private File out;
private CommandlineJava m_cmdl = new CommandlineJava();
private boolean m_fork;
private File m_dir;
private PrintStream m_outStream;
/**
* Set the class name.
*
* @param s The new Classname value
* @exception TaskException Description of Exception
*/
public void setClassname( String s )
throws TaskException
{
if( cmdl.getJar() != null )
{
throw new TaskException( "Cannot use 'jar' and 'classname' attributes in same command" );
}
cmdl.setClassname( s );
m_cmdl.setClassname( s );
}
/**
* Set the classpath to be used for this compilation.
*
* @param s The new Classpath value
*/
public void setClasspath( Path s )
public void setClasspath( final Path classpath )
throws TaskException
{
createClasspath().append( s );
createClasspath().append( cla sspath );
}
/**
@@ -71,145 +58,83 @@ public class Java extends Task
*
* @param d The new Dir value
*/
public void setDir( File d )
public void setDir( final File dir )
{
this.dir = d;
}
/**
* Throw a TaskException if process returns non 0.
*
* @param fail The new Failonerror value
*/
public void setFailonerror( boolean fail )
{
failOnError = fail;
m_dir = dir;
}
/**
* Set the forking flag.
*
* @param s The new Fork value
*/
public void setFork( boolean s )
public void setFork( final boolean fork )
{
this.fork = s ;
m_fork = fork;
}
/**
* set the jar name...
*
* @param jarfile The new Jar value
* @exception TaskException Description of Exception
*/
public void setJar( File jarfile )
throws TaskException
public void setJar( final File jar )
{
if( cmdl.getClassname() != null )
{
throw new TaskException( "Cannot use 'jar' and 'classname' attributes in same command." );
}
cmdl.setJar( jarfile.getAbsolutePath() );
m_cmdl.setJar( jar.getAbsolutePath() );
}
/**
* Set the command used to start the VM (only if fork==false).
*
* @param s The new Jvm value
*/
public void setJvm( String s )
public void setJvm( final String jvm )
{
cmdl.setVm( s );
m_cmdl.setVm( jvm );
}
/**
* -mx or -Xmx depending on VM version
*
* @param max The new Maxmemory value
*/
public void setMaxmemory( String max )
{
cmdl.setMaxmemory( max );
}
/**
* File the output of the process is redirected to.
*
* @param out The new Output value
*/
public void setOutput( File out )
public void setMaxmemory( final String max )
{
this.out = out ;
m_cmdl.setMaxmemory( max );
}
/**
* Add a nested sysproperty element.
*
* @param sysp The feature to be added to the Sysproperty attribute
*/
public void addSysproperty( EnvironmentVariable sysp )
public void addSysproperty( final EnvironmentVariable sysp )
{
cmdl.addSysproperty( sysp );
}
/**
* Clear out the arguments to this java task.
*/
public void clearArgs()
{
cmdl.clearJavaArgs();
m_cmdl.addSysproperty( sysp );
}
/**
* Creates a nested arg element.
*
* @return Description of the Returned Value
*/
public Argument createArg()
{
return cmdl.createArgument();
return m_cmdl.createArgument();
}
/**
* Creates a nested classpath element
*
* @return Description of the Returned Value
*/
public Path createClasspath()
throws TaskException
{
return cmdl.createClasspath().createPath();
return m_ cmdl.createClasspath().createPath();
}
/**
* Creates a nested jvmarg element.
*
* @return Description of the Returned Value
*/
public Argument createJvmarg()
{
return cmdl.createVmArgument();
return m_ cmdl.createVmArgument();
}
/**
* Do the execution.
*
* @exception TaskException Description of Exception
*/
public void execute()
throws TaskException
{
int err = -1 ;
if( ( err = executeJava() ) != 0 )
final int err = executeJava();
if( 0 != err )
{
if( failOnError )
{
throw new TaskException( "Java returned: " + err );
}
else
{
getLogger().error( "Java Result: " + err );
}
throw new TaskException( "Java returned: " + err );
}
}
@@ -223,188 +148,107 @@ public class Java extends Task
public int executeJava()
throws TaskException
{
String classname = cmdl.getClassname();
if( classname == null && cmdl.getJar() == null )
final String classname = m_cmdl.getClassname();
final String jar = m_cmdl.getJar();
if( classname != null && jar != null )
{
throw new TaskException( "Only one of Classname and Jar can be set." );
}
else if( classname == null && jar == null )
{
throw new TaskException( "Classname must not be null." );
}
if( !fork && cmdl.getJar() != null )
if( !m_fork && jar != null )
{
throw new TaskException( "Cannot execute a jar in non-forked mode. Please set fork='true'. " );
}
if( fork )
if( m_ fork )
{
getLogger().debug( "Forking " + cmdl.toString() );
getLogger().debug( "Forking " + m_ cmdl.toString() );
return run( cmdl.getCommandline() );
return run( m_ cmdl.getCommandline() );
}
else
{
if( cmdl.getVmCommand().size() > 1 )
if( m_ cmdl.getVmCommand().size() > 1 )
{
getLogger().warn( "JVM args ignored when same JVM is used." );
}
if( dir != null )
if( m_ dir != null )
{
getLogger().warn( "Working directory ignored when same JVM is used." );
}
getLogger().debug( "Running in same VM " + cmdl.getJavaCommand().toString() );
run( cmdl );
getLogger().debug( "Running in same VM " + m_ cmdl.getJavaCommand().toString() );
run( m_ cmdl );
return 0;
}
}
protected void handleErrorOutput( String line )
{
if( outStream != null )
{
outStream.println( line );
}
else
{
super.handleErrorOutput( line );
}
}
protected void handleOutput( String line )
{
if( outStream != null )
{
outStream.println( line );
}
else
{
super.handleOutput( line );
}
}
/**
* Executes the given classname with the given arguments as it was a command
* line application.
*
* @param classname Description of Parameter
* @param args Description of Parameter
* @exception TaskException Description of Exception
*/
protected void run( String classname, ArrayList args )
protected void run( final String classname, final ArrayList args )
throws TaskException
{
CommandlineJava cmdj = new CommandlineJava();
cmdj.setClassname( classname );
for( int i = 0; i < args.size(); i++ )
final CommandlineJava java = new CommandlineJava();
java.setClassname( classname );
final int size = args.size();
for( int i = 0; i < size; i++ )
{
cmdj.createArgument().setValue( (String)args.get( i ) );
final String arg = (String)args.get( i );
java.createArgument().setValue( arg );
}
run( cmd j );
run( java );
}
/**
* Executes the given classname with the given arguments as it was a command
* line application.
*
* @param command Description of Parameter
* @exception TaskException Description of Exception
*/
private void run( CommandlineJava command )
private void run( final CommandlineJava command )
throws TaskException
{
ExecuteJava exe = new ExecuteJava();
final ExecuteJava exe = new ExecuteJava();
exe.setJavaCommand( command.getJavaCommand() );
exe.setClasspath( command.getClasspath() );
exe.setSystemProperties( command.getSystemProperties() );
if( out != null )
{
try
{
outStream = new PrintStream( new FileOutputStream( out ) );
exe.execute( getProject() );
}
catch( IOException io )
{
throw new TaskException( "Error", io );
}
finally
{
if( outStream != null )
{
outStream.close();
}
}
}
else
{
exe.execute( getProject() );
}
exe.execute();
}
/**
* Executes the given classname with the given arguments in a separate VM.
*
* @param command Description of Parameter
* @return Description of the Returned Value
* @exception TaskException Description of Exception
*/
private int run( String[] command )
private int run( final String[] command )
throws TaskException
{
FileOutputStream fos = null;
try
{
Execute exe = null;
if( out == null )
{
exe = new Execute();
exe.setOutput( new LogOutputStream( getLogger(), false ) );
exe.setError( new LogOutputStream( getLogger(), true ) );
}
else
{
fos = new FileOutputStream( out );
exe = new Execute();
exe.setOutput( fos );
exe.setError( fos );
}
if( dir == null )
{
dir = getBaseDirectory();
}
else if( !dir.exists() || !dir.isDirectory() )
{
throw new TaskException( dir.getAbsolutePath() + " is not a valid directory" );
}
exe.setWorkingDirectory( dir );
final Execute2 exe = new Execute2();
setupLogger( exe );
exe.setCommandline( command );
try
{
return exe.execute();
}
catch( IOException e )
{
throw new TaskException( "Error", e );
}
if( m_dir == null )
{
m_dir = getBaseDirectory();
}
catch( IOException io )
else if( !m_dir.exists() || !m_dir.isDirectory() )
{
throw new TaskException( "Error", io );
final String message = m_dir.getAbsolutePath() + " is not a valid directory";
throw new TaskException( message );
}
finally
exe.setWorkingDirectory( m_dir );
exe.setCommandline( command );
try
{
if( fos != null )
{
try
{
fos.close();
}
catch( IOException io )
{
}
}
return exe.execute();
}
catch( IOException e )
{
final String message = "Error executing class";
throw new TaskException( message, e );
}
}
}