~ubuntu-branches/ubuntu/natty/electric/natty

« back to all changes in this revision

Viewing changes to com/sun/electric/tool/io/ExecProcess.java

  • Committer: Bazaar Package Importer
  • Author(s): Onkar Shinde
  • Date: 2010-01-09 16:26:04 UTC
  • mfrom: (1.1.4 upstream) (3.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100109162604-1ypvmy8ijmlc6oq7
Tags: 8.10-1
* New upstream version.
* debian/control
  - Add libjava3d-java and quilt build dependencies.
  - Update standards version to 3.8.3.
  - Add libjava3d-java as recommends to binary package.
* debian/rules
  - Use quilt patch system instead of simple patchsys.
  - Add java3d related jar files to DEB_JARS.
* debian/patches/*
  - Update as per current upstream source. Convert to quilt.
* debian/ant.properties
  - Do not disable 3D plugin anymore.
  - Use new property to disable compilation of OS X related classes.
* debian/wrappers/electric
  - Add java3d related jar files to runtime classpath.
* debian/README.source
  - Change text to the appropriate one for quilt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- tab-width: 4 -*-
 
2
 *
 
3
 * Electric(tm) VLSI Design System
 
4
 *
 
5
 * File: ExecProcess.java
 
6
 *
 
7
 * Copyright (c) 2009 Sun Microsystems and Static Free Software
 
8
 *
 
9
 * Electric(tm) is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 3 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * Electric(tm) is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with Electric(tm); see the file COPYING.  If not, write to
 
21
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 
22
 * Boston, Mass 02111-1307, USA.
 
23
 */
 
24
package com.sun.electric.tool.io;
 
25
 
 
26
import java.net.*;
 
27
import java.util.*;
 
28
import java.io.*;
 
29
 
 
30
/**
 
31
 * This class provides the same functionality as Runtime.exec(), but
 
32
 * with extra safeguards and utilities.  Includes the ability to
 
33
 * execute a command on a remote machine via ssh, optionally rsync'ing
 
34
 * the working directory to the remote machine before execution and
 
35
 * back afterwards.
 
36
 *
 
37
 * This class should not depend on other Electric classes.
 
38
 *
 
39
 * @author megacz (heavily influenced by gainsley's ExecProcess)
 
40
 */
 
41
public class ExecProcess {
 
42
 
 
43
    /** an OutputStream that discards anything written to it */
 
44
    public static final OutputStream devNull = new OutputStream() {
 
45
            public void write(int b) { }
 
46
            public void write(byte[] b, int ofs, int len) { }
 
47
        };
 
48
 
 
49
    /** an InputStream that always returns EOF */
 
50
    public static final InputStream eofInputStream = new InputStream() {
 
51
            public int  read() { return -1; }
 
52
            public int  read(byte[] buf, int ofs, int len) { return -1; }
 
53
            public long skip(long ofs) { return 0; }
 
54
            public int  available() { return 0; }
 
55
        };
 
56
 
 
57
    /**
 
58
     *  @param command the command to run (separated into argv[])
 
59
     *  @param workingDirectory the working directory on the LOCAL machine
 
60
     */
 
61
    public ExecProcess(String[] command, File workingDirectory) {
 
62
        this.command = command;
 
63
 
 
64
        // Using java.io.tmpdir as the default working directory leads
 
65
        // to far more predictable behavior than simply using the
 
66
        // JVM's working directory.  Electric already has a lot of
 
67
        // bugs and quirks that result from doing that -- let's not
 
68
        // add more!
 
69
        if (workingDirectory==null)
 
70
            workingDirectory = new File(System.getProperty("java.io.tmpdir"));
 
71
 
 
72
        this.workingDirectory = workingDirectory;
 
73
    }
 
74
 
 
75
    /**
 
76
     *  @param host the hostname to run on
 
77
     *  @param user the username on the remote machine (or null to use
 
78
     *         whatever default ssh chooses)
 
79
     *  @param remoteWorkingDirectory the directory to work in on the remote machine
 
80
     *  @param syncBefore if true then "rsync --delete
 
81
     *         workingDirectory host:remoteWorkingDirectory" before
 
82
     *         invoking command.
 
83
     *  @param syncAfter if true and the command terminates with exit
 
84
     *         code zero, then "rsync --delete
 
85
     *         host:remoteWorkingDirectory workingDirectory" after
 
86
     *         invoking command.
 
87
     */
 
88
    public synchronized void setRemote(String host, String user,
 
89
                                       File remoteWorkingDirectory,
 
90
                                       boolean syncBefore, boolean syncAfter) {
 
91
        if (proc!=null) throw new RuntimeException("you cannot invoke ExecProcess.setRemote() after ExecProcess.start()");
 
92
        throw new RuntimeException("not implemented");
 
93
    }
 
94
 
 
95
    /** undoes setRemote() */
 
96
    public synchronized void setLocal() { }
 
97
 
 
98
    public synchronized void redirectStdin(InputStream in) {
 
99
        if (proc!=null) throw new RuntimeException("you cannot invoke ExecProcess.redirectStdin() after ExecProcess.start()");
 
100
        this.redirectStdin = in;
 
101
    }
 
102
 
 
103
    public synchronized void redirectStdout(OutputStream os) {
 
104
        if (proc!=null) throw new RuntimeException("you cannot invoke ExecProcess.redirectStdout() after ExecProcess.start()");
 
105
        this.redirectStdout = os;
 
106
    }
 
107
 
 
108
    public synchronized void redirectStderr(OutputStream os) {
 
109
        if (proc!=null) throw new RuntimeException("you cannot invoke ExecProcess.redirectStderr() after ExecProcess.start()");
 
110
        this.redirectStderr = os;
 
111
    }
 
112
 
 
113
    public synchronized void start() throws IOException {
 
114
        if (proc!=null) throw new RuntimeException("you cannot invoke ExecProcess.start() twice");
 
115
        proc = Runtime.getRuntime().exec(command, null, workingDirectory);
 
116
        if (redirectStdin != null) new StreamCopier(redirectStdin, proc.getOutputStream()).start();
 
117
        if (redirectStdout != null) new StreamCopier(proc.getInputStream(), redirectStdout).start();
 
118
        if (redirectStderr != null) new StreamCopier(proc.getErrorStream(), redirectStderr).start();
 
119
    }
 
120
 
 
121
    public synchronized void destroy() throws IOException {
 
122
        if (proc==null) throw new RuntimeException("you must invoke ExecProcess.start() first");
 
123
        proc.destroy();
 
124
    }
 
125
 
 
126
    public int waitFor() throws IOException {
 
127
        if (proc==null) throw new RuntimeException("you must invoke ExecProcess.start() first");
 
128
        try {
 
129
            return proc.waitFor();
 
130
        } catch (InterruptedException ie) {
 
131
            throw new RuntimeException(ie);
 
132
        }
 
133
    }
 
134
 
 
135
    public synchronized OutputStream getStdin() {
 
136
        if (proc==null) throw new RuntimeException("you must invoke ExecProcess.start() first");
 
137
        if (redirectStdin!=null) throw new RuntimeException("you cannot invoke getStdin() after redirectStdin()");
 
138
        return proc.getOutputStream();
 
139
    }
 
140
 
 
141
    public synchronized InputStream getStdout() {
 
142
        if (proc==null) throw new RuntimeException("you must invoke ExecProcess.start() first");
 
143
        if (redirectStdout!=null) throw new RuntimeException("you cannot invoke getStdout() after redirectStdout()");
 
144
        return proc.getInputStream();
 
145
    }
 
146
 
 
147
    public synchronized InputStream getStderr() {
 
148
        if (proc==null) throw new RuntimeException("you must invoke ExecProcess.start() first");
 
149
        if (redirectStderr!=null) throw new RuntimeException("you cannot invoke getStderr() after redirectStderr()");
 
150
        return proc.getErrorStream();
 
151
    }
 
152
 
 
153
    private Process      proc;
 
154
    private InputStream  redirectStdin;
 
155
    private OutputStream redirectStdout;
 
156
    private OutputStream redirectStderr;
 
157
    private String[]     command;
 
158
    private File         workingDirectory;
 
159
 
 
160
    /**
 
161
     *  Copies from an InputStream to an OutputStream; used to implement redirectXXX().
 
162
     */
 
163
    private static class StreamCopier extends Thread {
 
164
        private final byte[] buf = new byte[16 * 1024];
 
165
        private final InputStream is;
 
166
        private final OutputStream os;
 
167
        public StreamCopier(InputStream is, OutputStream os) {
 
168
            setDaemon(true);
 
169
            this.is = is;
 
170
            this.os = os;
 
171
        }
 
172
        public void run() {
 
173
            try {
 
174
                while(true) {
 
175
                    int numread = is.read(buf, 0, buf.length);
 
176
                    if (numread==-1) break;
 
177
                    os.write(buf, 0, numread);
 
178
                }
 
179
            } catch (Exception e) { throw new RuntimeException(e); }
 
180
        }
 
181
    }
 
182
 
 
183
}