1
/*******************************************************************************
2
* Copyright (c) 2000, 2006 IBM Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Corporation - initial API and implementation
10
*******************************************************************************/
11
package org.vcs.bazaar.eclipse.core;
14
import java.io.IOException;
15
import java.io.OutputStream;
16
import java.io.PrintWriter;
17
import java.io.StringWriter;
19
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.core.runtime.IProgressMonitor;
21
import org.eclipse.core.runtime.Preferences;
22
import org.vcs.bazaar.eclipse.BazaarEclipsePlugin;
23
import org.vcs.bazaar.eclipse.core.commands.LocalOption;
24
import org.vcs.bazaar.eclipse.core.commands.Option;
25
import org.vcs.bazaar.eclipse.core.model.ICommand;
26
import org.vcs.bazaar.eclipse.core.preferences.BazaarPreferenceConstants;
27
import org.vcs.bazaar.eclipse.core.utils.Assert;
28
import org.vcs.bazaar.eclipse.core.utils.StreamRedirect;
31
* Abstract base class for command requests. Provides a framework for
32
* implementing command execution.
34
public abstract class Command implements ICommand {
36
private OutputStream additionalOut;
37
private OutputStream additionalErr;
38
private String EXECUTABLE = "bzr"; //$NON-NLS-1$
40
public abstract String execute() throws BazaarException;
42
protected abstract String getCommandName();
44
protected abstract LocalOption[] getLocalOptions();
47
* Constucts the Bzr command invocation string corresponding to the arguments.
51
* @return the command invocation string
53
protected String constructCommandInvocationString(String[] arguments) {
54
LocalOption[] localOptions = getLocalOptions();
55
// TODO: Separar el "executable" de lo que es command y ponerlo en el
56
// view? o en el que ejecuta el comando?
57
StringBuffer commandLine = new StringBuffer(getExecutable()); //$NON-NLS-1$
58
commandLine.append(' ');
59
commandLine.append(getCommandName());
60
for (int i = 0; i < localOptions.length; ++i) {
61
String option = localOptions[i].toString();
62
if (option.length() == 0)
64
commandLine.append(' ');
65
commandLine.append(option);
67
for (int i = 0; i < arguments.length; ++i) {
68
commandLine.append(' ');
69
commandLine.append(arguments[i]);
71
return commandLine.toString();
74
protected String runCommand(final String[] cmdLine) throws IOException {
75
// no need for env or WorkDir, because now uses subprocess
76
return runCommand(cmdLine, new String[0], new File(""));
79
private String runCommand(final String[] cmdLine, final String[] env, final File workDir) throws IOException {
80
Assert.isNotNull(cmdLine);
81
Assert.isTrue(cmdLine.length > 0);
82
// trace(cmdLine, env, workDir);
84
String result = ""; //$NON-NLS-1$
85
StringWriter outStream = new StringWriter();
86
StringWriter errStream = new StringWriter();
87
//Process proc = Runtime.getRuntime().exec(cmdLine, env, workDir);
88
ProcessBuilder pb = new ProcessBuilder(cmdLine);
89
Process proc = pb.start();
90
String outName = "output_redirect"; //$NON-NLS-1$
91
String errName = "error_redirect"; //$NON-NLS-1$
92
StreamRedirect outRedirect = new StreamRedirect(outName, proc.getInputStream(), outStream);
93
StreamRedirect errRedirect = new StreamRedirect(errName, proc.getErrorStream(), errStream);
96
// wait for command to finish
99
} catch (InterruptedException irex) {
100
String msg = "Fatal error running command."; //$NON-NLS-1$
101
BazaarEclipsePlugin.log(msg, irex);
104
// wait until out stream content is redirected
108
} catch (InterruptedException irex) {
109
String msg = "Fatal error running command."; //$NON-NLS-1$
110
BazaarEclipsePlugin.log(msg, irex);
112
String errorMsg = errStream.toString();
113
if (errorMsg != null && errorMsg.trim().length() > 0) {
114
writeToAdditionalErr(errorMsg);
115
String[] errorMsgs = errorMsg.split("\n");
116
for (int i = 0; i < errorMsgs.length; i++) {
117
if (!(errorMsgs[i].startsWith("read knit index:") || errorMsgs[i].startsWith("Build phase:"))) {
118
throw new IOException(errorMsg);
122
result = outStream.toString();
123
writeToAdditionalOut(result);
127
private void writeToAdditionalOut(final String content) {
128
if (additionalOut != null) {
129
PrintWriter pw = new PrintWriter(additionalOut);
135
public void setAdditionalOut(OutputStream additionalOut) {
136
this.additionalOut = additionalOut;
139
private void writeToAdditionalErr(final String content) {
140
if (additionalErr != null) {
141
PrintWriter pw = new PrintWriter(additionalErr);
147
public void setAdditionalErr(OutputStream additionalErr) {
148
this.additionalErr = additionalErr;
152
private Preferences getPrefs() {
153
return BazaarEclipsePlugin.getDefault().getPluginPreferences();
156
protected String getExecutable() {
157
String executable = getPrefs().getString(BazaarPreferenceConstants.EXECUTABLE);
158
if(executable == null || executable.equals("")) {
159
executable = EXECUTABLE;
165
* Find a specific option in an array of options
168
* the array of options
170
* the option string to search for
171
* @return the first element matching the option string, or null if none
173
public static Option findOption(Option[] array, String option) {
174
for (int i = 0; i < array.length; ++i) {
175
if (array[i].getOption().equals(option))
181
public void run(IProgressMonitor monitor) throws CoreException {