You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by do...@apache.org on 2001/12/19 12:48:11 UTC

cvs commit: jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/exec WinNTCommandLauncher.java ScriptCommandLauncher.java Java13CommandLauncher.java Java11CommandLauncher.java CommandLauncherProxy.java CommandLauncher.java

donaldp     01/12/19 03:48:11

  Added:       proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/exec
                        WinNTCommandLauncher.java
                        ScriptCommandLauncher.java
                        Java13CommandLauncher.java
                        Java11CommandLauncher.java
                        CommandLauncherProxy.java CommandLauncher.java
  Log:
  Move command launchers to top level classes. Inner classes are evil.
  
  Revision  Changes    Path
  1.1                  jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/exec/WinNTCommandLauncher.java
  
  Index: WinNTCommandLauncher.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.exec;
  
  import org.apache.tools.ant.Project;
  import org.apache.myrmidon.api.TaskException;
  import java.io.File;
  import java.io.IOException;
  
  /**
   * A command launcher for Windows 2000/NT that uses 'cmd.exe' when launching
   * commands in directories other than the current working directory.
   */
  class WinNTCommandLauncher
      extends CommandLauncherProxy
  {
      WinNTCommandLauncher( CommandLauncher launcher )
      {
          super( launcher );
      }
  
      /**
       * Launches the given command in a new process, in the given working
       * directory.
       *
       * @param project Description of Parameter
       * @param cmd Description of Parameter
       * @param env Description of Parameter
       * @param workingDir Description of Parameter
       * @return Description of the Returned Value
       * @exception IOException Description of Exception
       */
      public Process exec( Project project, String[] cmd, String[] env, File workingDir )
          throws IOException, TaskException
      {
          File commandDir = workingDir;
          if( workingDir == null )
          {
              if( project != null )
              {
                  commandDir = project.getBaseDir();
              }
              else
              {
                  return exec( project, cmd, env );
              }
          }
  
          // Use cmd.exe to change to the specified directory before running
          // the command
          final int preCmdLength = 6;
          String[] newcmd = new String[ cmd.length + preCmdLength ];
          newcmd[ 0 ] = "cmd";
          newcmd[ 1 ] = "/c";
          newcmd[ 2 ] = "cd";
          newcmd[ 3 ] = "/d";
          newcmd[ 4 ] = commandDir.getAbsolutePath();
          newcmd[ 5 ] = "&&";
          System.arraycopy( cmd, 0, newcmd, preCmdLength, cmd.length );
  
          return exec( project, newcmd, env );
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/exec/ScriptCommandLauncher.java
  
  Index: ScriptCommandLauncher.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.exec;
  
  import org.apache.tools.ant.Project;
  import org.apache.myrmidon.api.TaskException;
  import org.apache.avalon.excalibur.io.FileUtil;
  import java.io.File;
  import java.io.IOException;
  
  /**
   * A command launcher that uses an auxiliary script to launch commands in
   * directories other than the current working directory.
   */
  class ScriptCommandLauncher
      extends CommandLauncherProxy
  {
      private String _script;
  
      ScriptCommandLauncher( String script, CommandLauncher launcher )
      {
          super( launcher );
          _script = script;
      }
  
      /**
       * Launches the given command in a new process, in the given working
       * directory
       *
       * @param project Description of Parameter
       * @param cmd Description of Parameter
       * @param env Description of Parameter
       * @param workingDir Description of Parameter
       * @return Description of the Returned Value
       * @exception IOException Description of Exception
       */
      public Process exec( Project project, String[] cmd, String[] env, File workingDir )
          throws IOException, TaskException
      {
          if( project == null )
          {
              if( workingDir == null )
              {
                  return exec( project, cmd, env );
              }
              throw new IOException( "Cannot locate antRun script: No project provided" );
          }
  
          // Locate the auxiliary script
          String antHome = project.getProperty( "ant.home" );
          if( antHome == null )
          {
              throw new IOException( "Cannot locate antRun script: Property 'ant.home' not found" );
          }
          String antRun = FileUtil.
              resolveFile( project.getBaseDir(), antHome + File.separator + _script ).toString();
  
          // Build the command
          File commandDir = workingDir;
          if( workingDir == null && project != null )
          {
              commandDir = project.getBaseDir();
          }
  
          String[] newcmd = new String[ cmd.length + 2 ];
          newcmd[ 0 ] = antRun;
          newcmd[ 1 ] = commandDir.getAbsolutePath();
          System.arraycopy( cmd, 0, newcmd, 2, cmd.length );
  
          return exec( project, newcmd, env );
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/exec/Java13CommandLauncher.java
  
  Index: Java13CommandLauncher.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.exec;
  
  import java.lang.reflect.Method;
  import java.lang.reflect.InvocationTargetException;
  import java.io.File;
  import java.io.IOException;
  import org.apache.tools.ant.Project;
  import org.apache.tools.ant.types.Commandline;
  import org.apache.myrmidon.api.TaskException;
  
  /**
   * A command launcher for JDK/JRE 1.3 (and higher). Uses the built-in
   * Runtime.exec() command
   *
   * @author RT
   */
  class Java13CommandLauncher
      extends CommandLauncher
  {
  
      private Method _execWithCWD;
  
      public Java13CommandLauncher()
          throws NoSuchMethodException
      {
          // Locate method Runtime.exec(String[] cmdarray, String[] envp, File dir)
          _execWithCWD = Runtime.class.getMethod( "exec", new Class[]{String[].class, String[].class, File.class} );
      }
  
      /**
       * Launches the given command in a new process, in the given working
       * directory
       *
       * @param project Description of Parameter
       * @param cmd Description of Parameter
       * @param env Description of Parameter
       * @param workingDir Description of Parameter
       * @return Description of the Returned Value
       * @exception IOException Description of Exception
       */
      public Process exec( Project project, String[] cmd, String[] env, File workingDir )
          throws IOException, TaskException
      {
          try
          {
              if( project != null )
              {
                  project.log( "Execute:Java13CommandLauncher: " +
                               Commandline.toString( cmd ), Project.MSG_DEBUG );
              }
              Object[] arguments = {cmd, env, workingDir};
              return (Process)_execWithCWD.invoke( Runtime.getRuntime(), arguments );
          }
          catch( InvocationTargetException exc )
          {
              Throwable realexc = exc.getTargetException();
              if( realexc instanceof ThreadDeath )
              {
                  throw (ThreadDeath)realexc;
              }
              else if( realexc instanceof IOException )
              {
                  throw (IOException)realexc;
              }
              else
              {
                  throw new TaskException( "Unable to execute command", realexc );
              }
          }
          catch( Exception exc )
          {
              // IllegalAccess, IllegalArgument, ClassCast
              throw new TaskException( "Unable to execute command", exc );
          }
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/exec/Java11CommandLauncher.java
  
  Index: Java11CommandLauncher.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.exec;
  
  import org.apache.tools.ant.Project;
  import org.apache.tools.ant.types.Commandline;
  import org.apache.myrmidon.api.TaskException;
  import java.io.IOException;
  
  /**
   * A command launcher for JDK/JRE 1.1 under Windows. Fixes quoting problems
   * in Runtime.exec(). Can only launch commands in the current working
   * directory
   */
  class Java11CommandLauncher
      extends CommandLauncher
  {
      /**
       * Launches the given command in a new process. Needs to quote arguments
       *
       * @param project Description of Parameter
       * @param cmd Description of Parameter
       * @param env Description of Parameter
       * @return Description of the Returned Value
       * @exception IOException Description of Exception
       */
      public Process exec( Project project, String[] cmd, String[] env )
          throws IOException, TaskException
      {
          // Need to quote arguments with spaces, and to escape quote characters
          String[] newcmd = new String[ cmd.length ];
          for( int i = 0; i < cmd.length; i++ )
          {
              newcmd[ i ] = Commandline.quoteArgument( cmd[ i ] );
          }
          if( project != null )
          {
              project.log( "Execute:Java11CommandLauncher: " +
                           Commandline.toString( newcmd ), Project.MSG_DEBUG );
          }
          return Runtime.getRuntime().exec( newcmd, env );
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/exec/CommandLauncherProxy.java
  
  Index: CommandLauncherProxy.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.exec;
  
  import org.apache.tools.ant.Project;
  import org.apache.myrmidon.api.TaskException;
  import java.io.IOException;
  
  /**
   * A command launcher that proxies another command launcher. Sub-classes
   * override exec(args, env, workdir)
   */
  class CommandLauncherProxy
      extends CommandLauncher
  {
  
      private CommandLauncher _launcher;
  
      CommandLauncherProxy( CommandLauncher launcher )
      {
          _launcher = launcher;
      }
  
      /**
       * Launches the given command in a new process. Delegates this method to
       * the proxied launcher
       *
       * @param project Description of Parameter
       * @param cmd Description of Parameter
       * @param env Description of Parameter
       * @return Description of the Returned Value
       * @exception IOException Description of Exception
       */
      public Process exec( Project project, String[] cmd, String[] env )
          throws IOException, TaskException
      {
          return _launcher.exec( project, cmd, env );
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/exec/CommandLauncher.java
  
  Index: CommandLauncher.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.exec;
  
  import org.apache.tools.ant.Project;
  import org.apache.tools.ant.types.Commandline;
  import org.apache.myrmidon.api.TaskException;
  import java.io.IOException;
  import java.io.File;
  
  /**
   * A command launcher for a particular JVM/OS platform. This class is a
   * general purpose command launcher which can only launch commands in the
   * current working directory.
   */
  class CommandLauncher
  {
      /**
       * Launches the given command in a new process.
       *
       * @param project The project that the command is part of
       * @param cmd The command to execute
       * @param env The environment for the new process. If null, the
       *      environment of the current proccess is used.
       * @return Description of the Returned Value
       * @exception IOException Description of Exception
       */
      public Process exec( Project project, String[] cmd, String[] env )
          throws IOException, TaskException
      {
          if( project != null )
          {
              project.log( "Execute:CommandLauncher: " +
                           Commandline.toString( cmd ), Project.MSG_DEBUG );
          }
          return Runtime.getRuntime().exec( cmd, env );
      }
  
      /**
       * Launches the given command in a new process, in the given working
       * directory.
       *
       * @param project The project that the command is part of
       * @param cmd The command to execute
       * @param env The environment for the new process. If null, the
       *      environment of the current proccess is used.
       * @param workingDir The directory to start the command in. If null, the
       *      current directory is used
       * @return Description of the Returned Value
       * @exception IOException Description of Exception
       */
      public Process exec( Project project, String[] cmd, String[] env, File workingDir )
          throws IOException, TaskException
      {
          if( workingDir == null )
          {
              return exec( project, cmd, env );
          }
          throw new IOException( "Cannot execute a process in different directory under this JVM" );
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>