2 * Copyright 2007 Jaroslav Tulach All Rights Reserved.
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.
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).
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.
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
24 package org.apidesign.infra.ant;
26 import java.io.ByteArrayOutputStream;
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;
41 * @author Jaroslav Tulach
43 final class ExecuteUtils {
45 private ExecuteUtils() {
48 final static void execute (String res, String[] args) throws Exception {
49 execute (extractResource (res), args);
52 private static ByteArrayOutputStream out;
53 private static ByteArrayOutputStream err;
55 final static String getStdOut() {
56 return out.toString();
58 final static String getStdErr() {
59 return err.toString();
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));
70 System.setSecurityManager (new MySecMan ());
73 MySecMan sec = (MySecMan)System.getSecurityManager();
75 // Jesse claims that this is not the right way how the execution
76 // of an ant script should be invoked:
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.
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
89 List<String> arr = new ArrayList<String>();
91 arr.add (f.toString ());
92 arr.addAll(Arrays.asList(args));
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 ()
109 sec.setActive(false);
113 static class ExecutionError extends AssertionFailedError {
114 public final int exitCode;
116 public ExecutionError (String msg, int e) {
121 public static void assertExitCode (String msg, int e) {
123 throw new ExecutionError (
124 msg + " was: " + e + "\nOutput: " + out.toString () +
125 "\nError: " + err.toString (),
132 private static class MySecExc extends SecurityException {
134 public void printStackTrace() {
137 public void printStackTrace(PrintStream ps) {
139 public void printStackTrace(PrintWriter ps) {
143 private static class MySecMan extends SecurityManager {
144 public Integer exitCode;
146 private boolean active;
148 public void checkExit (int status) {
150 exitCode = new Integer (status);
151 throw new MySecExc ();
155 public void checkPermission(Permission perm, Object context) {
158 public void checkPermission(Permission perm) {
160 if (perm instanceof RuntimePermission) {
161 if (perm.getName ().equals ("setIO")) {
162 throw new MySecExc ();
168 public void checkMulticast(InetAddress maddr) {
171 public void checkAccess (ThreadGroup g) {
174 public void checkWrite (String file) {
177 public void checkLink (String lib) {
180 public void checkExec (String cmd) {
183 public void checkDelete (String file) {
186 public void checkPackageAccess (String pkg) {
189 public void checkPackageDefinition (String pkg) {
192 public void checkPropertyAccess (String key) {
195 public void checkRead (String file) {
198 public void checkSecurityAccess (String target) {
201 public void checkWrite(FileDescriptor fd) {
204 public void checkListen (int port) {
207 public void checkRead(FileDescriptor fd) {
210 public void checkMulticast(InetAddress maddr, byte ttl) {
213 public void checkAccess (Thread t) {
216 public void checkConnect (String host, int port, Object context) {
219 public void checkRead (String file, Object context) {
222 public void checkConnect (String host, int port) {
225 public void checkAccept (String host, int port) {
228 public void checkMemberAccess (Class clazz, int which) {
231 public void checkSystemClipboardAccess () {
234 public void checkSetFactory () {
237 public void checkCreateClassLoader () {
240 public void checkAwtEventQueueAccess () {
243 public void checkPrintJobAccess () {
246 public void checkPropertiesAccess () {
249 void setActive(boolean b) {