jtulach@268: /* jtulach@268: * Copyright 2007 Jaroslav Tulach All Rights Reserved. jtulach@268: * jtulach@268: * This code is free software; you can redistribute it and/or modify it jtulach@268: * under the terms of the GNU General Public License version 2 only, as jtulach@268: * published by the Free Software Foundation. Sun designates this jtulach@268: * particular file as subject to the "Classpath" exception as provided jtulach@268: * by Sun in the LICENSE file that accompanied this code. jtulach@268: * jtulach@268: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@268: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@268: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@268: * version 2 for more details (a copy is included in the LICENSE file that jtulach@268: * accompanied this code). jtulach@268: * jtulach@268: * You should have received a copy of the GNU General Public License version jtulach@268: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@268: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@268: * jtulach@268: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jtulach@268: * CA 95054 USA or visit www.sun.com if you need additional information or jtulach@268: * have any questions. jtulach@268: */ jtulach@268: package org.apidesign.infra.ant; jtulach@268: jtulach@268: import java.io.ByteArrayOutputStream; jtulach@268: import java.io.File; jtulach@268: import java.io.FileDescriptor; jtulach@268: import java.io.PrintStream; jtulach@268: import java.io.PrintWriter; jtulach@268: import java.net.InetAddress; jtulach@268: import java.security.Permission; jtulach@268: import java.util.ArrayList; jtulach@268: import java.util.Arrays; jtulach@268: import java.util.List; jtulach@268: import junit.framework.AssertionFailedError; jtulach@268: import org.junit.Assert; jtulach@268: jtulach@268: /** jtulach@268: * jtulach@268: * @author Jaroslav Tulach jtulach@268: */ jtulach@268: final class ExecuteUtils { jtulach@268: jtulach@268: private ExecuteUtils() { jtulach@268: } jtulach@268: /* jtulach@268: final static void execute (String res, String[] args) throws Exception { jtulach@268: execute (extractResource (res), args); jtulach@268: } jtulach@268: */ jtulach@268: private static ByteArrayOutputStream out; jtulach@268: private static ByteArrayOutputStream err; jtulach@268: jtulach@268: final static String getStdOut() { jtulach@268: return out.toString(); jtulach@268: } jtulach@268: final static String getStdErr() { jtulach@268: return err.toString(); jtulach@268: } jtulach@268: jtulach@268: final static void execute(File f, String... args) throws Exception { jtulach@268: // we need security manager to prevent System.exit jtulach@268: if (! (System.getSecurityManager () instanceof MySecMan)) { jtulach@268: out = new java.io.ByteArrayOutputStream (); jtulach@268: err = new java.io.ByteArrayOutputStream (); jtulach@268: System.setOut (new java.io.PrintStream (out)); jtulach@268: System.setErr (new java.io.PrintStream (err)); jtulach@268: jtulach@268: System.setSecurityManager (new MySecMan ()); jtulach@268: } jtulach@268: jtulach@268: MySecMan sec = (MySecMan)System.getSecurityManager(); jtulach@268: jtulach@268: // Jesse claims that this is not the right way how the execution jtulach@268: // of an ant script should be invoked: jtulach@268: // jtulach@268: // better IMHO to just run the task directly jtulach@268: // (setProject() and similar, configure its bean properties, and call jtulach@268: // execute()), or just make a new Project and initialize it. jtulach@268: // ant.Main.main is not intended for embedded use. Then you could get rid jtulach@268: // of the SecurityManager stuff, would be cleaner I think. jtulach@268: // jtulach@268: // If I had to write this once again, I would try to follow the jtulach@268: // "just make a new Project and initialize it", but as this works jtulach@268: // for me now, I leave it for the time when somebody really jtulach@268: // needs that... jtulach@268: jtulach@268: List arr = new ArrayList(); jtulach@268: arr.add ("-f"); jtulach@268: arr.add (f.toString ()); jtulach@268: arr.addAll(Arrays.asList(args)); jtulach@268: arr.add ("-verbose"); jtulach@268: jtulach@268: jtulach@268: out.reset (); jtulach@268: err.reset (); jtulach@268: jtulach@268: try { jtulach@268: sec.setActive(true); jtulach@268: org.apache.tools.ant.Main.main (arr.toArray(new String[0])); jtulach@268: } catch (MySecExc ex) { jtulach@268: Assert.assertNotNull ("The only one to throw security exception is MySecMan and should set exitCode", sec.exitCode); jtulach@268: ExecutionError.assertExitCode ( jtulach@268: "Execution has to finish without problems", jtulach@268: sec.exitCode.intValue () jtulach@268: ); jtulach@268: } finally { jtulach@268: sec.setActive(false); jtulach@268: } jtulach@268: } jtulach@268: jtulach@268: static class ExecutionError extends AssertionFailedError { jtulach@268: public final int exitCode; jtulach@268: jtulach@268: public ExecutionError (String msg, int e) { jtulach@268: super (msg); jtulach@268: this.exitCode = e; jtulach@268: } jtulach@268: jtulach@268: public static void assertExitCode (String msg, int e) { jtulach@268: if (e != 0) { jtulach@268: throw new ExecutionError ( jtulach@268: msg + " was: " + e + "\nOutput: " + out.toString () + jtulach@268: "\nError: " + err.toString (), jtulach@268: e jtulach@268: ); jtulach@268: } jtulach@268: } jtulach@268: } jtulach@268: jtulach@268: private static class MySecExc extends SecurityException { jtulach@268: @Override jtulach@268: public void printStackTrace() { jtulach@268: } jtulach@268: @Override jtulach@268: public void printStackTrace(PrintStream ps) { jtulach@268: } jtulach@268: public void printStackTrace(PrintWriter ps) { jtulach@268: } jtulach@268: } jtulach@268: jtulach@268: private static class MySecMan extends SecurityManager { jtulach@268: public Integer exitCode; jtulach@268: jtulach@268: private boolean active; jtulach@268: jtulach@268: public void checkExit (int status) { jtulach@268: if (active) { jtulach@268: exitCode = new Integer (status); jtulach@268: throw new MySecExc (); jtulach@268: } jtulach@268: } jtulach@268: jtulach@268: public void checkPermission(Permission perm, Object context) { jtulach@268: } jtulach@268: jtulach@268: public void checkPermission(Permission perm) { jtulach@268: /* jtulach@268: if (perm instanceof RuntimePermission) { jtulach@268: if (perm.getName ().equals ("setIO")) { jtulach@268: throw new MySecExc (); jtulach@268: } jtulach@268: } jtulach@268: */ jtulach@268: } jtulach@268: jtulach@268: public void checkMulticast(InetAddress maddr) { jtulach@268: } jtulach@268: jtulach@268: public void checkAccess (ThreadGroup g) { jtulach@268: } jtulach@268: jtulach@268: public void checkWrite (String file) { jtulach@268: } jtulach@268: jtulach@268: public void checkLink (String lib) { jtulach@268: } jtulach@268: jtulach@268: public void checkExec (String cmd) { jtulach@268: } jtulach@268: jtulach@268: public void checkDelete (String file) { jtulach@268: } jtulach@268: jtulach@268: public void checkPackageAccess (String pkg) { jtulach@268: } jtulach@268: jtulach@268: public void checkPackageDefinition (String pkg) { jtulach@268: } jtulach@268: jtulach@268: public void checkPropertyAccess (String key) { jtulach@268: } jtulach@268: jtulach@268: public void checkRead (String file) { jtulach@268: } jtulach@268: jtulach@268: public void checkSecurityAccess (String target) { jtulach@268: } jtulach@268: jtulach@268: public void checkWrite(FileDescriptor fd) { jtulach@268: } jtulach@268: jtulach@268: public void checkListen (int port) { jtulach@268: } jtulach@268: jtulach@268: public void checkRead(FileDescriptor fd) { jtulach@268: } jtulach@268: jtulach@268: public void checkMulticast(InetAddress maddr, byte ttl) { jtulach@268: } jtulach@268: jtulach@268: public void checkAccess (Thread t) { jtulach@268: } jtulach@268: jtulach@268: public void checkConnect (String host, int port, Object context) { jtulach@268: } jtulach@268: jtulach@268: public void checkRead (String file, Object context) { jtulach@268: } jtulach@268: jtulach@268: public void checkConnect (String host, int port) { jtulach@268: } jtulach@268: jtulach@268: public void checkAccept (String host, int port) { jtulach@268: } jtulach@268: jtulach@268: public void checkMemberAccess (Class clazz, int which) { jtulach@268: } jtulach@268: jtulach@268: public void checkSystemClipboardAccess () { jtulach@268: } jtulach@268: jtulach@268: public void checkSetFactory () { jtulach@268: } jtulach@268: jtulach@268: public void checkCreateClassLoader () { jtulach@268: } jtulach@268: jtulach@268: public void checkAwtEventQueueAccess () { jtulach@268: } jtulach@268: jtulach@268: public void checkPrintJobAccess () { jtulach@268: } jtulach@268: jtulach@268: public void checkPropertiesAccess () { jtulach@268: } jtulach@268: jtulach@268: void setActive(boolean b) { jtulach@268: active = b; jtulach@268: } jtulach@268: } // end of MySecMan jtulach@268: jtulach@268: }