ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/launchers DefaultCommandLauncher.java ExecUtil.java MacCommandLauncher.java Resources.properties ScriptCommandLauncher.java WinNTCommandLauncher.java
Date Mon, 21 Jan 2002 11:44:24 GMT
donaldp     02/01/21 03:44:24

  Modified:    proposal/myrmidon/src/java/org/apache/aut/nativelib
                        ExecMetaData.java
               proposal/myrmidon/src/java/org/apache/aut/nativelib/impl
                        DefaultExecManager.java
  Added:       proposal/myrmidon/src/java/org/apache/aut/nativelib/impl
                        CommandLauncher.java
               proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/launchers
                        DefaultCommandLauncher.java ExecUtil.java
                        MacCommandLauncher.java Resources.properties
                        ScriptCommandLauncher.java
                        WinNTCommandLauncher.java
  Removed:     proposal/myrmidon/src/java/org/apache/aut/nativelib
                        CommandLauncher.java
               proposal/myrmidon/src/java/org/apache/aut/nativelib/launchers
                        DefaultCommandLauncher.java ExecUtil.java
                        MacCommandLauncher.java Resources.properties
                        ScriptCommandLauncher.java
                        WinNTCommandLauncher.java
  Log:
  Move the launchers package into the impl package.
  
  Move the CommandLauncher interface into the impl package
  
  Revision  Changes    Path
  1.2       +8 -6      jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/ExecMetaData.java
  
  Index: ExecMetaData.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/ExecMetaData.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ExecMetaData.java	12 Jan 2002 04:51:07 -0000	1.1
  +++ ExecMetaData.java	21 Jan 2002 11:44:24 -0000	1.2
  @@ -17,7 +17,7 @@
    * <code>ExecManager</code> to actually launch the native executable.
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version $Revision: 1.1 $ $Date: 2002/01/12 04:51:07 $
  + * @version $Revision: 1.2 $ $Date: 2002/01/21 11:44:24 $
    */
   public class ExecMetaData
   {
  @@ -32,14 +32,16 @@
       private String[] m_command;
   
       /**
  -     * The array of strings that make up the command line for the command.
  -     * Note that these variables are in the ugly format expected by the
  -     * Runtime.exec() call. For most systems this means that each entry
  -     * must be of the form <code>key=value</code>
  +     * The array of strings that make up the native environment for the
  +     * command.
  +     *
  +     * <p>Note that these variables are yet to be translated into the ugly
  +     * format expected by the Runtime.exec() call. For most systems this means
  +     * that each entry must be translated into the form <code>key=value</code>.</p>
        *
        * <p>This set of variables is combined with the environment of current
        * process if <code>isEnvironmentAdditive=true</code> else it specifies
  -     * full environment.
  +     * full environment.</p>
        */
       private Properties m_environment;
   
  
  
  
  1.3       +5 -6      jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/DefaultExecManager.java
  
  Index: DefaultExecManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/DefaultExecManager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultExecManager.java	12 Jan 2002 05:52:52 -0000	1.2
  +++ DefaultExecManager.java	21 Jan 2002 11:44:24 -0000	1.3
  @@ -12,16 +12,15 @@
   import java.io.InputStream;
   import java.io.OutputStream;
   import java.util.Locale;
  -import org.apache.aut.nativelib.CommandLauncher;
   import org.apache.aut.nativelib.ExecException;
   import org.apache.aut.nativelib.ExecManager;
   import org.apache.aut.nativelib.ExecMetaData;
   import org.apache.aut.nativelib.ExecOutputHandler;
   import org.apache.aut.nativelib.Os;
  -import org.apache.aut.nativelib.launchers.DefaultCommandLauncher;
  -import org.apache.aut.nativelib.launchers.MacCommandLauncher;
  -import org.apache.aut.nativelib.launchers.ScriptCommandLauncher;
  -import org.apache.aut.nativelib.launchers.WinNTCommandLauncher;
  +import org.apache.aut.nativelib.impl.launchers.DefaultCommandLauncher;
  +import org.apache.aut.nativelib.impl.launchers.MacCommandLauncher;
  +import org.apache.aut.nativelib.impl.launchers.ScriptCommandLauncher;
  +import org.apache.aut.nativelib.impl.launchers.WinNTCommandLauncher;
   import org.apache.avalon.excalibur.io.FileUtil;
   
   /**
  @@ -30,7 +29,7 @@
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
    * @author <a href="mailto:thomas.haas@softwired-inc.com">Thomas Haas</a>
  - * @version $Revision: 1.2 $ $Date: 2002/01/12 05:52:52 $
  + * @version $Revision: 1.3 $ $Date: 2002/01/21 11:44:24 $
    * @see ExecManager
    * @see ExecMetaData
    */
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/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.aut.nativelib.impl;
  
  import java.io.IOException;
  import org.apache.aut.nativelib.ExecMetaData;
  import org.apache.aut.nativelib.ExecException;
  
  /**
   * This is the interface implemented by objects which are capable of
   * lauching a native command. Each different implementation is likely
   * to have a different strategy or be restricted to specific platform.
   *
   * <p>It is expected that the user will get a reference to the
   * <code>CommandLauncher</code> most appropriate for their environment.</p>
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/01/21 11:44:24 $
   */
  public interface CommandLauncher
  {
      /**
       * Execute the specified native command.
       *
       * @param metaData the native command to execute
       * @return the Process launched by the CommandLauncher
       * @exception IOException is thrown when the native code can not
       *            launch the application for some reason. Usually due
       *            to the command not being fully specified and not in
       *            the PATH env var.
       * @exception ExecException if the command launcher detects that
       *            it can not execute the native command for some reason.
       */
      Process exec( ExecMetaData metaData )
          throws IOException, ExecException;
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/launchers/DefaultCommandLauncher.java
  
  Index: DefaultCommandLauncher.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.aut.nativelib.impl.launchers;
  
  import java.io.File;
  import java.io.IOException;
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.aut.nativelib.impl.CommandLauncher;
  
  import org.apache.aut.nativelib.ExecException;
  import org.apache.aut.nativelib.ExecMetaData;
  
  /**
   * 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.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:thomas.haas@softwired-inc.com">Thomas Haas</a>
   * @version $Revision: 1.1 $ $Date: 2002/01/21 11:44:24 $
   */
  public class DefaultCommandLauncher
      implements CommandLauncher
  {
      private final static Resources REZ =
          ResourceManager.getPackageResources( DefaultCommandLauncher.class );
  
      private final static Method c_execWithCWD;
  
      static
      {
          // Locate method Runtime.exec(String[] cmdarray, String[] envp, File dir)
          Method method = null;
          try
          {
              final Class[] types =
                  new Class[]{String[].class, String[].class, File.class};
              method = Runtime.class.getMethod( "exec", types );
          }
          catch( final NoSuchMethodException nsme )
          {
              //ignore
          }
  
          c_execWithCWD = method;
      }
  
      /**
       * Execute the specified native command.
       *
       * @param metaData the native command to execute
       * @return the Process launched by the CommandLauncher
       * @exception IOException is thrown when the native code can not
       *            launch the application for some reason. Usually due
       *            to the command not being fully specified and not in
       *            the PATH env var.
       * @exception ExecException if the command launcher detects that
       *            it can not execute the native command for some reason.
       */
      public Process exec( final ExecMetaData metaData )
          throws IOException, ExecException
      {
          if( ExecUtil.isCwd( metaData.getWorkingDirectory() ) )
          {
              final String[] env = ExecUtil.getEnvironmentSpec( metaData );
              return Runtime.getRuntime().exec( metaData.getCommand(), env );
          }
          else if( null == c_execWithCWD )
          {
              final String message = REZ.getString( "default.bad-dir.error" );
              throw new ExecException( message );
          }
          else
          {
              return execJava13( metaData );
          }
      }
  
      /**
       * Execute the Java1.3 Runtime.exec() 3 parame method that sets working
       * directory. This needs to be done via reflection so that it can compile
       * under 1.2.
       */
      private Process execJava13( final ExecMetaData metaData )
          throws IOException, ExecException
      {
          final String[] env = ExecUtil.getEnvironmentSpec( metaData );
          final Object[] args =
              {metaData.getCommand(),
               env,
               metaData.getWorkingDirectory()};
          try
          {
              return (Process)c_execWithCWD.invoke( Runtime.getRuntime(), args );
          }
          catch( final IllegalAccessException iae )
          {
              throw new ExecException( iae.getMessage(), iae );
          }
          catch( final IllegalArgumentException iae )
          {
              throw new ExecException( iae.getMessage(), iae );
          }
          catch( final InvocationTargetException ite )
          {
              final Throwable t = ite.getTargetException();
              if( t instanceof IOException )
              {
                  t.fillInStackTrace();
                  throw (IOException)t;
              }
              else
              {
                  throw new ExecException( t.getMessage(), t );
              }
          }
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/launchers/ExecUtil.java
  
  Index: ExecUtil.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.aut.nativelib.impl.launchers;
  
  import java.io.File;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.Properties;
  import org.apache.aut.nativelib.Environment;
  import org.apache.aut.nativelib.ExecException;
  import org.apache.aut.nativelib.ExecMetaData;
  
  /**
   * A set of utility functions useful when writing CommandLaunchers.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/01/21 11:44:24 $
   */
  class ExecUtil
  {
      /**
       * The file representing the current working directory.
       */
      private final static File c_cwd;
  
      static
      {
          try
          {
              c_cwd = ( new File( "." ) ).getCanonicalFile();
          }
          catch( final IOException ioe )
          {
              //Should never happen
              throw new IllegalStateException();
          }
      }
  
      /**
       * Private constructor to block instantiation.
       */
      private ExecUtil()
      {
      }
  
      /**
       * Create a new ExecMetaData representing the same command with the specified
       * prefix. This is useful when you are launching the native executable via a
       * script of some sort.
       */
      protected static ExecMetaData prepend( final ExecMetaData metaData,
                                             final String[] prefix )
      {
          final String[] original = metaData.getCommand();
          final String[] command = new String[ original.length + prefix.length ];
  
          System.arraycopy( prefix, 0, command, 0, prefix.length );
          System.arraycopy( original, 0, command, prefix.length, original.length );
  
          return new ExecMetaData( command,
                                   metaData.getEnvironment(),
                                   metaData.getWorkingDirectory(),
                                   metaData.isEnvironmentAdditive() );
      }
  
      /**
       * Utility method to check if specified file is equal
       * to the current working directory.
       */
      protected static boolean isCwd( final File file )
          throws IOException
      {
          return file.getCanonicalFile().equals( getCwd() );
      }
  
      private static String[] toNativeEnvironment( final Properties environment )
          throws ExecException
      {
          if( null == environment )
          {
              return null;
          }
          else
          {
              final ArrayList newEnvironment = new ArrayList();
  
              final Iterator keys = environment.keySet().iterator();
              while( keys.hasNext() )
              {
                  final String key = (String)keys.next();
                  final String value = environment.getProperty( key );
                  newEnvironment.add( key + '=' + value );
              }
  
              return (String[])newEnvironment.toArray( new String[ newEnvironment.size() ]
);
          }
      }
  
      /**
       * Return the current working directory of the JVM.
       * This value is initialized when this class is first loaded.
       */
      protected static File getCwd()
      {
          return c_cwd;
      }
  
      /**
       * Get the native environment according to proper rules.
       * Return null if no environment specified, return environment combined
       * with native environment if environment data is additive else just return
       * converted environment data.
       */
      protected static String[] getEnvironmentSpec( final ExecMetaData metaData )
          throws ExecException, IOException
      {
          final Properties environment = metaData.getEnvironment();
          if( 0 == environment.size() )
          {
              return null;
          }
          else
          {
              if( metaData.isEnvironmentAdditive() )
              {
                  final Properties newEnvironment = new Properties();
                  newEnvironment.putAll( Environment.getNativeEnvironment() );
                  newEnvironment.putAll( environment );
                  return toNativeEnvironment( newEnvironment );
              }
              else
              {
                  return toNativeEnvironment( environment );
              }
          }
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/launchers/MacCommandLauncher.java
  
  Index: MacCommandLauncher.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.aut.nativelib.impl.launchers;
  
  import java.io.File;
  import java.io.IOException;
  import org.apache.aut.nativelib.impl.CommandLauncher;
  import org.apache.aut.nativelib.ExecException;
  import org.apache.aut.nativelib.ExecMetaData;
  
  /**
   * A command launcher for Mac that uses a dodgy mechanism to change working
   * directory before launching commands. This class changes the value of the
   * System property "user.dir" before the command is executed and then resets
   * it after the command is executed. This can have really unhealthy side-effects
   * if there are multiple threads in JVM and should be used with extreme caution.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:thomas.haas@softwired-inc.com">Thomas Haas</a>
   * @version $Revision: 1.1 $ $Date: 2002/01/21 11:44:24 $
   */
  public class MacCommandLauncher
      implements CommandLauncher
  {
      /**
       * Execute the specified native command.
       */
      public Process exec( final ExecMetaData metaData )
          throws IOException, ExecException
      {
          final File directory = metaData.getWorkingDirectory().getCanonicalFile();
          if( ExecUtil.isCwd( directory ) )
          {
              final String[] env = ExecUtil.getEnvironmentSpec( metaData );
              return Runtime.getRuntime().exec( metaData.getCommand(), env );
          }
  
          //WARNING: This is an ugly hack and not thread safe in the slightest way
          //It can have really really undersirable side-effects if multiple threads
          //are running in the JVM
          try
          {
              System.setProperty( "user.dir", directory.toString() );
              final String[] env = ExecUtil.getEnvironmentSpec( metaData );
              return Runtime.getRuntime().exec( metaData.getCommand(), env );
          }
          finally
          {
              System.setProperty( "user.dir", ExecUtil.getCwd().toString() );
          }
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/launchers/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  default.bad-dir.error=Unable to launch native command in a working directory other than
"." on Java 1.2 JVMs.
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/launchers/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.aut.nativelib.impl.launchers;
  
  import java.io.IOException;
  import org.apache.aut.nativelib.impl.CommandLauncher;
  import org.apache.aut.nativelib.ExecException;
  import org.apache.aut.nativelib.ExecMetaData;
  
  /**
   * A command launcher that uses an auxiliary script to launch commands in
   * directories other than the current working directory. The script specified
   * in the constructor is invoked with the directory passed as second argument
   * and the actual command as subsequent arguments.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:thomas.haas@softwired-inc.com">Thomas Haas</a>
   * @version $Revision: 1.1 $ $Date: 2002/01/21 11:44:24 $
   */
  public class ScriptCommandLauncher
      implements CommandLauncher
  {
      private String[] m_script;
  
      /**
       * Create a command launcher whos script is a single
       * command. An example would be "bin/antRun.bat".
       */
      public ScriptCommandLauncher( final String script )
      {
          this( new String[]{script} );
      }
  
      /**
       * Create a command launcher whos script takes multiple
       * commands. Examples would be "perl bin/antRun.pl",
       * "python bin/antRun.py", ""tcl8 bin/antRun.tcl" etc
       */
      public ScriptCommandLauncher( final String[] script )
      {
          m_script = script;
          if( null == m_script )
          {
              throw new NullPointerException( "script" );
          }
          if( 0 == m_script.length )
          {
              throw new IllegalArgumentException( "script" );
          }
      }
  
      /**
       * Launches the given command in a new process using cmd.exe to
       * set the working directory.
       */
      public Process exec( final ExecMetaData metaData )
          throws IOException, ExecException
      {
          // Build the command
          final String[] prefix = new String[ m_script.length + 1 ];
          for( int i = 0; i < m_script.length; i++ )
          {
              prefix[ i ] = m_script[ i ];
          }
          prefix[ m_script.length ] = metaData.getWorkingDirectory().getCanonicalPath();
  
          final ExecMetaData newMetaData = ExecUtil.prepend( metaData, prefix );
          final String[] env = ExecUtil.getEnvironmentSpec( metaData );
          return Runtime.getRuntime().exec( newMetaData.getCommand(), env );
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/aut/nativelib/impl/launchers/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.aut.nativelib.impl.launchers;
  
  import java.io.IOException;
  import org.apache.aut.nativelib.impl.CommandLauncher;
  import org.apache.aut.nativelib.ExecException;
  import org.apache.aut.nativelib.ExecMetaData;
  
  /**
   * A command launcher for Windows 2000/NT that uses 'cmd.exe' when launching
   * commands in directories other than the current working directory.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @author <a href="mailto:thomas.haas@softwired-inc.com">Thomas Haas</a>
   * @version $Revision: 1.1 $ $Date: 2002/01/21 11:44:24 $
   */
  public class WinNTCommandLauncher
      implements CommandLauncher
  {
      /**
       * Launches the given command in a new process using cmd.exe to
       * set the working directory.
       */
      public Process exec( final ExecMetaData metaData )
          throws IOException, ExecException
      {
          // Use cmd.exe to change to the specified directory before running
          // the command
          final String[] prefix = new String[ 6 ];
          prefix[ 0 ] = "cmd";
          prefix[ 1 ] = "/c";
          prefix[ 2 ] = "cd";
          prefix[ 3 ] = "/d";
          prefix[ 4 ] = metaData.getWorkingDirectory().getCanonicalPath();
          prefix[ 5 ] = "&&";
  
          final ExecMetaData newMetaData = ExecUtil.prepend( metaData, prefix );
          final String[] env = ExecUtil.getEnvironmentSpec( metaData );
          return Runtime.getRuntime().exec( newMetaData.getCommand(), env );
      }
  }
  
  
  

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


Mime
View raw message