ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alexey Mel'nikoff" <alexey.melnik...@gmail.com>
Subject When using ExecuteJava in other class, JVM don't allow to create streams.
Date Fri, 14 Sep 2007 11:39:47 GMT
I try to create ant task which launch java in separate VM with
specified paramaters.
I try to execute class, which creates output and input streams.
When I did it in <java/> task, all works, but when I try to use it
in my task, streams are not created.
And class from lib can't work with files and user inputs.

There a part of my code:

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;
   }
 }

---
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