ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexey Melnikoff <alexey.melnik...@gmail.com>
Subject How to extend java task?
Date Sun, 23 Sep 2007 20:30:28 GMT
I've tryed to ask at user@ant.apache.org.
But I don't have answers.
I try to extend java task in two ways: extending java task, or using 
ExecuteJava, but I have problems with using output and input streams.
I execute java for running class which placed in jar, this class tryes 
to open output and input streams, but it can't do it.
This my code of two tryes:

FIRST implementation:

import java.io.File;
import java.util.ArrayList;
import java.util.Collection ;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ExitStatusException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.ExecuteJava ;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;

public class InstallDocAppsTask extends Task {

    private boolean failonerror = false;
    private File docapppath;
    private Reference classpathref;
    private String classname;
    private String docbase;
    private String domain;
    private String installlog;
    private String username;
    private String userpass;


    public InstallDocAppsTask() {
        super();
    }


    /**
     * Do the execution.
     *
     * @throws BuildException
     *             if failOnError is set to true and the application returns a
     *             nonzero result code.
     */
    public void execute() throws BuildException {

        String[] files = docapppath.list();
        Collection<File> dirs = new ArrayList<File>();
        for (int i = 0; i < files.length; i++) {
            File tmp = new File(docapppath.getAbsolutePath() + "\\" + files[i]);
            if (tmp.isDirectory()) {
                dirs.add(tmp);
            }
        }
        if (dirs.size() != 0) {
            int err = -1;
            for (File dir : dirs) {
                ExecuteJava exe = new ExecuteJava();
                exe.setClasspath ((Path) classpathref.getReferencedObject());
                Cmdline cmd = new Cmdline();
                cmd.setExecutable(classname);
                cmd.createArgument().setValue("-d " + docbase);
                cmd.createArgument().setValue("-n " + username);
                cmd.createArgument().setValue("-p " + userpass);
                if (domain != null && domain.trim().length() != 0) {
                    cmd.createArgument().setValue("-m " + domain);
                }
                cmd.createArgument().setValue("-a " + "\"" + dir.getAbsolutePath() + "\"");

                if (installlog != null && installlog.trim().length() != 0) {
                    cmd.createArgument().setValue("-l " + "\"" + (installlog.endsWith("\\")
?
                                            installlog : installlog + "\\") + dir.getName()
+ "\"");
                }

                exe.setJavaCommand(cmd);
                err =  exe.fork(this);
                if (err != 0) {
                    if (isFailonerror()) {
                        throw new ExitStatusException("Java returned: " + err, err, getLocation());
                    } else {
                        log("Java Result: " + err, Project.MSG_ERR);
                    }
                }
            }
        } else {
            log("Direcory " + docapppath + " does not contain subdirectories, wich may contain
DocApps",  Project.MSG_ERR);
        }

    }

    public void setDocbase(String docbase) {
        this.docbase = docbase;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setUserpass(String userpass) {
        this.userpass = userpass;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public void setInstalllog(String installlog) {
        this.installlog = installlog;
    }

    public void setDocapppath(File docapppath) {
        this.docapppath = docapppath;
    }

    public void setFailonerror(boolean failonerror) {
         this.failonerror = failonerror;
    }

    public void setClasspathref(Reference classpathref) {
        this.classpathref = classpathref;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }

    public class Cmdline extends Commandline {

        @Override
        public String describeCommand() {
            return "";
         }
        @Override
        public String describeArguments() {
            return "";
        }
    }

    public boolean isFailonerror() {
        return failonerror;
    }
 }

SECOND implementation:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ExitStatusException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.CommandlineJava;
import org.apache.tools.ant.types.Reference;

public class InstallDocAppsTask extends Java {
   
    private boolean failonerror = false;
    private File docapppath;
    private Reference classpathref;
    private String classname;
    private String docbase;
    private String domain;
    private String installlog;
    private String username;
    private String userpass;
    private Cmdline cmdl = new Cmdline();
   
    public InstallDocAppsTask() {
        super();
    }
   
   
    /**
     * Do the execution.
     *
     * @throws BuildException
     *             if failOnError is set to true and the application 
returns a
     *             nonzero result code.
     */
    public void execute() throws BuildException {

        String[] files = docapppath.list();
        Collection<File> dirs = new ArrayList<File>();
        for (int i = 0; i < files.length; i++) {
            File tmp = new File(docapppath.getAbsolutePath() + "\\" + 
files[i]);
            if (tmp.isDirectory()) {
                dirs.add(tmp);
            }
        }

        if (dirs.size() != 0) {
            int err = -1;
            for (File dir : dirs) {
               
                Cmdline cmd = new Cmdline();
                cmd.setClassname(classname);
                
cmd.createClasspath(getProject()).createPath().setRefid(classpathref);
                cmd.createArgument().setValue("-d " + docbase);
                cmd.createArgument().setValue("-n " + username);
                cmd.createArgument().setValue("-p " + userpass);
                if (domain != null && domain.trim().length() != 0) {
                    cmd.createArgument().setValue("-m " + domain);
                }
                cmd.createArgument().setValue("-a " + "\"" + 
dir.getAbsolutePath() + "\"");

                if (installlog != null && installlog.trim().length() != 0) {
                    cmd.createArgument().setValue("-l " + "\"" + 
(installlog.endsWith("\\") ?
                                            installlog : installlog + 
"\\") + dir.getName() + "\"");
                }

                setCommandLine(cmd);

                err = executeJava();
               
                if (err != 0) {
                    if (isFailonerror()) {
                        throw new ExitStatusException("Java returned: " 
+ err,
                                err, getLocation());
                    } else {
                        log("Java Result: " + err, Project.MSG_ERR);
                    }
                }
            }
        } else {
            log("Direcory " + docapppath + " does not contain 
subdirectories, wich may contain DocApps", Project.MSG_ERR);
        }

    }

    public void setDocbase(String docbase) {
        this.docbase = docbase;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setUserpass(String userpass) {
        this.userpass = userpass;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public void setInstalllog(String installlog) {
        this.installlog = installlog;
    }

    public void setDocapppath(File docapppath) {
        this.docapppath = docapppath;
    }

    public void setFailonerror(boolean failonerror) {
        this.failonerror = failonerror;
    }
   
    public void setClasspathrefId(Reference classpathref) {
        this.classpathref = classpathref;
    }
   
    public void setClassname(String classname) {
        this.classname = classname;
    }
   
    public class Cmdline extends CommandlineJava {
       
        @Override
        public String describeCommand() {
            return "";
        }
    }

    public boolean isFailonerror() {
        return failonerror;
    }
   
    public Cmdline getCommandLine() {
        return cmdl;
    }
    public void setCommandLine(Cmdline cmdline) {
        this.cmdl = cmdline;
    }
}

In all cases I've problem with opening streams, running class can't 
create file, and can't get user input.
But when I simply use java task all works fine.
Where in my code errors?

Sorry for my bad English.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message