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