1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/ant/test/org/apidesign/infra/ant/ExecuteUtils.java Fri Apr 03 16:32:36 2020 +0200
1.3 @@ -0,0 +1,254 @@
1.4 +/*
1.5 + * Copyright 2007 Jaroslav Tulach All Rights Reserved.
1.6 + *
1.7 + * This code is free software; you can redistribute it and/or modify it
1.8 + * under the terms of the GNU General Public License version 2 only, as
1.9 + * published by the Free Software Foundation. Sun designates this
1.10 + * particular file as subject to the "Classpath" exception as provided
1.11 + * by Sun in the LICENSE file that accompanied this code.
1.12 + *
1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.16 + * version 2 for more details (a copy is included in the LICENSE file that
1.17 + * accompanied this code).
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License version
1.20 + * 2 along with this work; if not, write to the Free Software Foundation,
1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.22 + *
1.23 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1.24 + * CA 95054 USA or visit www.sun.com if you need additional information or
1.25 + * have any questions.
1.26 + */
1.27 +package org.apidesign.infra.ant;
1.28 +
1.29 +import java.io.ByteArrayOutputStream;
1.30 +import java.io.File;
1.31 +import java.io.FileDescriptor;
1.32 +import java.io.PrintStream;
1.33 +import java.io.PrintWriter;
1.34 +import java.net.InetAddress;
1.35 +import java.security.Permission;
1.36 +import java.util.ArrayList;
1.37 +import java.util.Arrays;
1.38 +import java.util.List;
1.39 +import junit.framework.AssertionFailedError;
1.40 +import org.junit.Assert;
1.41 +
1.42 +/**
1.43 + *
1.44 + * @author Jaroslav Tulach
1.45 + */
1.46 +final class ExecuteUtils {
1.47 +
1.48 + private ExecuteUtils() {
1.49 + }
1.50 +/*
1.51 + final static void execute (String res, String[] args) throws Exception {
1.52 + execute (extractResource (res), args);
1.53 + }
1.54 + */
1.55 + private static ByteArrayOutputStream out;
1.56 + private static ByteArrayOutputStream err;
1.57 +
1.58 + final static String getStdOut() {
1.59 + return out.toString();
1.60 + }
1.61 + final static String getStdErr() {
1.62 + return err.toString();
1.63 + }
1.64 +
1.65 + final static void execute(File f, String... args) throws Exception {
1.66 + // we need security manager to prevent System.exit
1.67 + if (! (System.getSecurityManager () instanceof MySecMan)) {
1.68 + out = new java.io.ByteArrayOutputStream ();
1.69 + err = new java.io.ByteArrayOutputStream ();
1.70 + System.setOut (new java.io.PrintStream (out));
1.71 + System.setErr (new java.io.PrintStream (err));
1.72 +
1.73 + System.setSecurityManager (new MySecMan ());
1.74 + }
1.75 +
1.76 + MySecMan sec = (MySecMan)System.getSecurityManager();
1.77 +
1.78 + // Jesse claims that this is not the right way how the execution
1.79 + // of an ant script should be invoked:
1.80 + //
1.81 + // better IMHO to just run the task directly
1.82 + // (setProject() and similar, configure its bean properties, and call
1.83 + // execute()), or just make a new Project and initialize it.
1.84 + // ant.Main.main is not intended for embedded use. Then you could get rid
1.85 + // of the SecurityManager stuff, would be cleaner I think.
1.86 + //
1.87 + // If I had to write this once again, I would try to follow the
1.88 + // "just make a new Project and initialize it", but as this works
1.89 + // for me now, I leave it for the time when somebody really
1.90 + // needs that...
1.91 +
1.92 + List<String> arr = new ArrayList<String>();
1.93 + arr.add ("-f");
1.94 + arr.add (f.toString ());
1.95 + arr.addAll(Arrays.asList(args));
1.96 + arr.add ("-verbose");
1.97 +
1.98 +
1.99 + out.reset ();
1.100 + err.reset ();
1.101 +
1.102 + try {
1.103 + sec.setActive(true);
1.104 + org.apache.tools.ant.Main.main (arr.toArray(new String[0]));
1.105 + } catch (MySecExc ex) {
1.106 + Assert.assertNotNull ("The only one to throw security exception is MySecMan and should set exitCode", sec.exitCode);
1.107 + ExecutionError.assertExitCode (
1.108 + "Execution has to finish without problems",
1.109 + sec.exitCode.intValue ()
1.110 + );
1.111 + } finally {
1.112 + sec.setActive(false);
1.113 + }
1.114 + }
1.115 +
1.116 + static class ExecutionError extends AssertionFailedError {
1.117 + public final int exitCode;
1.118 +
1.119 + public ExecutionError (String msg, int e) {
1.120 + super (msg);
1.121 + this.exitCode = e;
1.122 + }
1.123 +
1.124 + public static void assertExitCode (String msg, int e) {
1.125 + if (e != 0) {
1.126 + throw new ExecutionError (
1.127 + msg + " was: " + e + "\nOutput: " + out.toString () +
1.128 + "\nError: " + err.toString (),
1.129 + e
1.130 + );
1.131 + }
1.132 + }
1.133 + }
1.134 +
1.135 + private static class MySecExc extends SecurityException {
1.136 + @Override
1.137 + public void printStackTrace() {
1.138 + }
1.139 + @Override
1.140 + public void printStackTrace(PrintStream ps) {
1.141 + }
1.142 + public void printStackTrace(PrintWriter ps) {
1.143 + }
1.144 + }
1.145 +
1.146 + private static class MySecMan extends SecurityManager {
1.147 + public Integer exitCode;
1.148 +
1.149 + private boolean active;
1.150 +
1.151 + public void checkExit (int status) {
1.152 + if (active) {
1.153 + exitCode = new Integer (status);
1.154 + throw new MySecExc ();
1.155 + }
1.156 + }
1.157 +
1.158 + public void checkPermission(Permission perm, Object context) {
1.159 + }
1.160 +
1.161 + public void checkPermission(Permission perm) {
1.162 + /*
1.163 + if (perm instanceof RuntimePermission) {
1.164 + if (perm.getName ().equals ("setIO")) {
1.165 + throw new MySecExc ();
1.166 + }
1.167 + }
1.168 + */
1.169 + }
1.170 +
1.171 + public void checkMulticast(InetAddress maddr) {
1.172 + }
1.173 +
1.174 + public void checkAccess (ThreadGroup g) {
1.175 + }
1.176 +
1.177 + public void checkWrite (String file) {
1.178 + }
1.179 +
1.180 + public void checkLink (String lib) {
1.181 + }
1.182 +
1.183 + public void checkExec (String cmd) {
1.184 + }
1.185 +
1.186 + public void checkDelete (String file) {
1.187 + }
1.188 +
1.189 + public void checkPackageAccess (String pkg) {
1.190 + }
1.191 +
1.192 + public void checkPackageDefinition (String pkg) {
1.193 + }
1.194 +
1.195 + public void checkPropertyAccess (String key) {
1.196 + }
1.197 +
1.198 + public void checkRead (String file) {
1.199 + }
1.200 +
1.201 + public void checkSecurityAccess (String target) {
1.202 + }
1.203 +
1.204 + public void checkWrite(FileDescriptor fd) {
1.205 + }
1.206 +
1.207 + public void checkListen (int port) {
1.208 + }
1.209 +
1.210 + public void checkRead(FileDescriptor fd) {
1.211 + }
1.212 +
1.213 + public void checkMulticast(InetAddress maddr, byte ttl) {
1.214 + }
1.215 +
1.216 + public void checkAccess (Thread t) {
1.217 + }
1.218 +
1.219 + public void checkConnect (String host, int port, Object context) {
1.220 + }
1.221 +
1.222 + public void checkRead (String file, Object context) {
1.223 + }
1.224 +
1.225 + public void checkConnect (String host, int port) {
1.226 + }
1.227 +
1.228 + public void checkAccept (String host, int port) {
1.229 + }
1.230 +
1.231 + public void checkMemberAccess (Class clazz, int which) {
1.232 + }
1.233 +
1.234 + public void checkSystemClipboardAccess () {
1.235 + }
1.236 +
1.237 + public void checkSetFactory () {
1.238 + }
1.239 +
1.240 + public void checkCreateClassLoader () {
1.241 + }
1.242 +
1.243 + public void checkAwtEventQueueAccess () {
1.244 + }
1.245 +
1.246 + public void checkPrintJobAccess () {
1.247 + }
1.248 +
1.249 + public void checkPropertiesAccess () {
1.250 + }
1.251 +
1.252 + void setActive(boolean b) {
1.253 + active = b;
1.254 + }
1.255 + } // end of MySecMan
1.256 +
1.257 +}