001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.api.execution;
043:
044: import java.io.File;
045: import java.io.FileNotFoundException;
046: import java.io.FileReader;
047: import java.io.IOException;
048: import java.io.PrintWriter;
049: import java.text.MessageFormat;
050: import java.util.ArrayList;
051: import java.util.ResourceBundle;
052: import org.netbeans.modules.cnd.execution.NativeExecution;
053: import org.netbeans.modules.cnd.execution.OutputWindowWriter;
054: import org.openide.ErrorManager;
055: import org.openide.awt.StatusDisplayer;
056: import org.openide.execution.ExecutionEngine;
057: import org.openide.execution.ExecutorTask;
058: import org.openide.filesystems.FileUtil;
059: import org.openide.util.NbBundle;
060: import org.openide.windows.IOProvider;
061: import org.openide.windows.InputOutput;
062:
063: public class NativeExecutor implements Runnable {
064: private ArrayList listeners = new ArrayList();
065:
066: private String runDir;
067: private String executable;
068: private String arguments;
069: private String[] envp;
070: private String tabName;
071: private String actionName;
072: private String rcfile;
073: private boolean parseOutputForErrors;
074: private boolean showInput;
075:
076: private boolean showHeader = true;
077: private boolean showFooter = true;
078:
079: /** I/O class for writing output to a build tab */
080: private InputOutput io;
081: private PrintWriter out;
082:
083: /** targets may be null to indicate default target */
084: public NativeExecutor(String runDir, String executable,
085: String arguments, String[] envp, String tabName,
086: String actionName, boolean parseOutputForErrors) {
087: this (runDir, executable, arguments, envp, tabName, actionName,
088: parseOutputForErrors, false);
089: }
090:
091: /** targets may be null to indicate default target */
092: public NativeExecutor(String runDir, String executable,
093: String arguments, String[] envp, String tabName,
094: String actionName, boolean parseOutputForErrors,
095: boolean showInput) {
096: //this.pae = pae;
097: this .runDir = runDir;
098: this .executable = executable;
099: this .arguments = arguments;
100: this .envp = envp;
101: this .tabName = tabName;
102: this .actionName = actionName;
103: this .parseOutputForErrors = parseOutputForErrors;
104: this .showInput = showInput;
105: }
106:
107: public NativeExecutor(String runDir, String executable,
108: String arguments, String[] envp, String tabName,
109: String actionName, boolean parseOutputForErrors,
110: boolean showInput, boolean showHeader, boolean showFooter) {
111: this (runDir, executable, arguments, envp, tabName, actionName,
112: parseOutputForErrors, showInput);
113:
114: this .showHeader = showHeader;
115: this .showFooter = showFooter;
116: }
117:
118: /** Start it going. */
119: public ExecutorTask execute() throws IOException {
120: return execute(getTab(tabName));
121: }
122:
123: /** Start it going. */
124: public ExecutorTask execute(InputOutput io) throws IOException {
125: final ExecutorTask task;
126: synchronized (this ) {
127: // OutputWindow
128: this .io = io;
129: io.select();
130: //io.getOut().reset();
131: task = ExecutionEngine.getDefault().execute(tabName, this ,
132: InputOutput.NULL);
133: }
134:
135: return task;
136: }
137:
138: private InputOutput getTab(String tabName) {
139: return IOProvider.getDefault().getIO(tabName, true);
140: }
141:
142: public void setExitValueOverride(String rcfile) {
143: this .rcfile = rcfile;
144: }
145:
146: /**
147: * Call execute(), not this method directly!
148: */
149: synchronized public void run() {
150: io.setFocusTaken(true);
151: io.setErrVisible(false);
152: io.setErrSeparated(false);
153: if (showInput) {
154: io.setInputVisible(true);
155: }
156:
157: File runDirFile = new File(runDir);
158: if (parseOutputForErrors)
159: out = new PrintWriter(new OutputWindowWriter(io.getOut(),
160: FileUtil.toFileObject(runDirFile),
161: parseOutputForErrors));
162: else
163: out = io.getOut();
164: executionStarted();
165: int rc = 0;
166:
167: try {
168: // Execute the selected command
169: rc = new NativeExecution().executeCommand(runDirFile,
170: executable, arguments, envp, out, showInput ? io
171: .getIn() : null);
172: } catch (ThreadDeath td) {
173: StatusDisplayer.getDefault().setStatusText(
174: getString("MSG_FailedStatus"));
175: executionFinished(-1);
176: out.close();
177: throw td;
178: } catch (Throwable t) {
179: StatusDisplayer.getDefault().setStatusText(
180: getString("MSG_FailedStatus"));
181: ErrorManager.getDefault().notify(t);
182: rc = -1;
183: } finally {
184: if (showInput) {
185: io.setInputVisible(false);
186: try {
187: io.getIn().close();
188: } catch (IOException ex) {
189: ex.printStackTrace();
190: }
191: }
192: }
193: if (rcfile != null) {
194: File file = null;
195:
196: try {
197: file = new File(rcfile);
198:
199: if (file.exists()) {
200: FileReader fr = new FileReader(file);
201:
202: if (fr.ready()) {
203: char[] cbuf = new char[256];
204: int i = fr.read(cbuf);
205: if (i > 0) {
206: rc = Integer.valueOf(
207: String.valueOf(cbuf, 0, i - 1))
208: .intValue();
209: }
210:
211: }
212: fr.close();
213: file.delete();
214: }
215: } catch (FileNotFoundException ex) {
216: } catch (IOException ex) {
217: } finally {
218: if (file != null && file.exists()) {
219: file.delete();
220: }
221: }
222: }
223: executionFinished(rc);
224: out.close();
225: }
226:
227: private void executionStarted() {
228: if (showHeader) {
229: String preText = MessageFormat.format(getString("PRETEXT"),
230: new Object[] {
231: exePlusArgsQuoted(executable, arguments),
232: runDir });
233: out.println(preText);
234: out.println("");
235: }
236: fireExecutionStarted();
237: }
238:
239: private void executionFinished(int exitValue) {
240: if (showFooter) {
241: String failedOrSucceded = MessageFormat
242: .format(getString(exitValue == 0 ? "SUCCESSFUL"
243: : "FAILED"), new Object[] { actionName });
244: String postText = MessageFormat.format(
245: getString("POSTTEXT"), new Object[] {
246: failedOrSucceded, "" + exitValue }); // NOI18N
247: out.println(""); // NOI18N
248: out.println(postText);
249: out.println(""); // NOI18N
250: StatusDisplayer.getDefault()
251: .setStatusText(failedOrSucceded);
252: }
253: fireExecutionFinished(exitValue);
254: }
255:
256: public void addExecutionListener(ExecutionListener l) {
257: listeners.add(l);
258: }
259:
260: public void removeExecutionListener(ExecutionListener l) {
261: listeners.remove(listeners.indexOf(l));
262: }
263:
264: /**
265: * Send a ExecutionEvent to each executionStarted method
266: */
267: private void fireExecutionStarted() {
268: for (int i = 0; i < listeners.size(); i++) {
269: ExecutionListener listener = (ExecutionListener) listeners
270: .get(i);
271: listener.executionStarted();
272: }
273: }
274:
275: /**
276: * Send a ExecutionEvent to each executionFinished method
277: */
278: private void fireExecutionFinished(int rc) {
279: for (int i = 0; i < listeners.size(); i++) {
280: ExecutionListener listener = (ExecutionListener) listeners
281: .get(i);
282: listener.executionFinished(rc);
283: }
284: }
285:
286: private String exePlusArgsQuoted(String exe, String args) {
287: String ret = exe;
288: // add quoted arguments
289: if (args == null || args.length() == 0)
290: ret = "\"" + ret + "\""; // NOI18N
291: else
292: ret = "\"" + ret + " " + args + "\""; // NOI18N
293:
294: return ret;
295: }
296:
297: private static ResourceBundle bundle = null;
298:
299: private static String getString(String s) {
300: if (bundle == null) {
301: bundle = NbBundle.getBundle(NativeExecutor.class);
302: }
303: return bundle.getString(s);
304: }
305: }
|