java/ant/test/org/apidesign/infra/ant/ExecuteUtils.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 03 Apr 2020 16:32:36 +0200
changeset 416 9ed8788a1a4e
permissions -rw-r--r--
Using HTTPS to download the libraries
     1 /*
     2  * Copyright 2007 Jaroslav Tulach  All Rights Reserved.
     3  *
     4  * This code is free software; you can redistribute it and/or modify it
     5  * under the terms of the GNU General Public License version 2 only, as
     6  * published by the Free Software Foundation.  Sun designates this
     7  * particular file as subject to the "Classpath" exception as provided
     8  * by Sun in the LICENSE file that accompanied this code.
     9  *
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    13  * version 2 for more details (a copy is included in the LICENSE file that
    14  * accompanied this code).
    15  *
    16  * You should have received a copy of the GNU General Public License version
    17  * 2 along with this work; if not, write to the Free Software Foundation,
    18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    19  *
    20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    21  * CA 95054 USA or visit www.sun.com if you need additional information or
    22  * have any questions.
    23  */
    24 package org.apidesign.infra.ant;
    25 
    26 import java.io.ByteArrayOutputStream;
    27 import java.io.File;
    28 import java.io.FileDescriptor;
    29 import java.io.PrintStream;
    30 import java.io.PrintWriter;
    31 import java.net.InetAddress;
    32 import java.security.Permission;
    33 import java.util.ArrayList;
    34 import java.util.Arrays;
    35 import java.util.List;
    36 import junit.framework.AssertionFailedError;
    37 import org.junit.Assert;
    38 
    39 /**
    40  *
    41  * @author Jaroslav Tulach
    42  */
    43 final class ExecuteUtils {
    44     
    45     private ExecuteUtils() {
    46     }
    47 /*    
    48     final static void execute (String res, String[] args) throws Exception {
    49         execute (extractResource (res), args);
    50     }
    51   */  
    52     private static ByteArrayOutputStream out;
    53     private static ByteArrayOutputStream err;
    54     
    55     final static String getStdOut() {
    56         return out.toString();
    57     }
    58     final static String getStdErr() {
    59         return err.toString();
    60     }
    61     
    62     final static void execute(File f, String... args) throws Exception {
    63         // we need security manager to prevent System.exit
    64         if (! (System.getSecurityManager () instanceof MySecMan)) {
    65             out = new java.io.ByteArrayOutputStream ();
    66             err = new java.io.ByteArrayOutputStream ();
    67             System.setOut (new java.io.PrintStream (out));
    68             System.setErr (new java.io.PrintStream (err));
    69             
    70             System.setSecurityManager (new MySecMan ());
    71         }
    72         
    73         MySecMan sec = (MySecMan)System.getSecurityManager();
    74         
    75         // Jesse claims that this is not the right way how the execution
    76         // of an ant script should be invoked:
    77         //
    78         // better IMHO to just run the task directly
    79         // (setProject() and similar, configure its bean properties, and call
    80         // execute()), or just make a new Project and initialize it.
    81         // ant.Main.main is not intended for embedded use. Then you could get rid
    82         // of the SecurityManager stuff, would be cleaner I think.
    83         //
    84         // If I had to write this once again, I would try to follow the
    85         // "just make a new Project and initialize it", but as this works
    86         // for me now, I leave it for the time when somebody really
    87         // needs that...
    88         
    89         List<String> arr = new ArrayList<String>();
    90         arr.add ("-f");
    91         arr.add (f.toString ());
    92         arr.addAll(Arrays.asList(args));
    93         arr.add ("-verbose");
    94         
    95         
    96         out.reset ();
    97         err.reset ();
    98         
    99         try {
   100             sec.setActive(true);
   101             org.apache.tools.ant.Main.main (arr.toArray(new String[0]));
   102         } catch (MySecExc ex) {
   103             Assert.assertNotNull ("The only one to throw security exception is MySecMan and should set exitCode", sec.exitCode);
   104             ExecutionError.assertExitCode (
   105                 "Execution has to finish without problems",
   106                 sec.exitCode.intValue ()
   107             );
   108         } finally {
   109             sec.setActive(false);
   110         }
   111     }
   112     
   113     static class ExecutionError extends AssertionFailedError {
   114         public final int exitCode;
   115         
   116         public ExecutionError (String msg, int e) {
   117             super (msg);
   118             this.exitCode = e;
   119         }
   120         
   121         public static void assertExitCode (String msg, int e) {
   122             if (e != 0) {
   123                 throw new ExecutionError (
   124                     msg + " was: " + e + "\nOutput: " + out.toString () +
   125                     "\nError: " + err.toString (),
   126                     e
   127                 );
   128             }
   129         }
   130     }
   131     
   132     private static class MySecExc extends SecurityException {
   133         @Override
   134         public void printStackTrace() {
   135         }
   136         @Override
   137         public void printStackTrace(PrintStream ps) {
   138         }
   139         public void printStackTrace(PrintWriter ps) {
   140         }
   141     }
   142     
   143     private static class MySecMan extends SecurityManager {
   144         public Integer exitCode;
   145         
   146         private boolean active;
   147         
   148         public void checkExit (int status) {
   149             if (active) {
   150                 exitCode = new Integer (status);
   151                 throw new MySecExc ();
   152             }
   153         }
   154 
   155         public void checkPermission(Permission perm, Object context) {
   156         }
   157 
   158         public void checkPermission(Permission perm) {
   159         /*
   160             if (perm instanceof RuntimePermission) {
   161                 if (perm.getName ().equals ("setIO")) {
   162                     throw new MySecExc ();
   163                 }
   164             }
   165          */
   166         }
   167 
   168         public void checkMulticast(InetAddress maddr) {
   169         }
   170 
   171         public void checkAccess (ThreadGroup g) {
   172         }
   173 
   174         public void checkWrite (String file) {
   175         }
   176 
   177         public void checkLink (String lib) {
   178         }
   179 
   180         public void checkExec (String cmd) {
   181         }
   182 
   183         public void checkDelete (String file) {
   184         }
   185 
   186         public void checkPackageAccess (String pkg) {
   187         }
   188 
   189         public void checkPackageDefinition (String pkg) {
   190         }
   191 
   192         public void checkPropertyAccess (String key) {
   193         }
   194 
   195         public void checkRead (String file) {
   196         }
   197 
   198         public void checkSecurityAccess (String target) {
   199         }
   200 
   201         public void checkWrite(FileDescriptor fd) {
   202         }
   203 
   204         public void checkListen (int port) {
   205         }
   206 
   207         public void checkRead(FileDescriptor fd) {
   208         }
   209 
   210         public void checkMulticast(InetAddress maddr, byte ttl) {
   211         }
   212 
   213         public void checkAccess (Thread t) {
   214         }
   215 
   216         public void checkConnect (String host, int port, Object context) {
   217         }
   218 
   219         public void checkRead (String file, Object context) {
   220         }
   221 
   222         public void checkConnect (String host, int port) {
   223         }
   224 
   225         public void checkAccept (String host, int port) {
   226         }
   227 
   228         public void checkMemberAccess (Class clazz, int which) {
   229         }
   230 
   231         public void checkSystemClipboardAccess () {
   232         }
   233 
   234         public void checkSetFactory () {
   235         }
   236 
   237         public void checkCreateClassLoader () {
   238         }
   239 
   240         public void checkAwtEventQueueAccess () {
   241         }
   242 
   243         public void checkPrintJobAccess () {
   244         }
   245 
   246         public void checkPropertiesAccess () {
   247         }
   248 
   249         void setActive(boolean b) {
   250             active = b;
   251         }
   252     } // end of MySecMan
   253 
   254 }