~piastucki/bzr-eclipse/history-view-enhancements

« back to all changes in this revision

Viewing changes to org.vcs.bazaar.eclipse.core/src/org/vcs/bazaar/eclipse/core/Command.java

  • Committer: Guillermo Gonzalez
  • Date: 2007-03-14 12:15:50 UTC
  • Revision ID: guillo.gonzo@gmail.com-20070314121550-tltk3b6f3zblf0dh
Initial commit with the new code layout.
Now starts the real work

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 *     IBM Corporation - initial API and implementation
 
10
 *******************************************************************************/
 
11
package org.vcs.bazaar.eclipse.core;
 
12
 
 
13
import java.io.File;
 
14
import java.io.IOException;
 
15
import java.io.OutputStream;
 
16
import java.io.PrintWriter;
 
17
import java.io.StringWriter;
 
18
 
 
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;
 
29
 
 
30
/**
 
31
 * Abstract base class for command requests. Provides a framework for
 
32
 * implementing command execution.
 
33
 */
 
34
public abstract class Command implements ICommand {
 
35
 
 
36
        private OutputStream additionalOut;
 
37
        private OutputStream additionalErr;
 
38
        private String EXECUTABLE = "bzr"; //$NON-NLS-1$
 
39
        
 
40
        public abstract String execute() throws BazaarException;
 
41
 
 
42
        protected abstract String getCommandName();
 
43
 
 
44
        protected abstract LocalOption[] getLocalOptions();
 
45
 
 
46
        /**
 
47
         * Constucts the Bzr command invocation string corresponding to the arguments.
 
48
         * 
 
49
         * @param arguments
 
50
         *            the arguments
 
51
         * @return the command invocation string
 
52
         */
 
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)
 
63
                                continue;
 
64
                        commandLine.append(' ');
 
65
                        commandLine.append(option);
 
66
                }
 
67
                for (int i = 0; i < arguments.length; ++i) {
 
68
                        commandLine.append(' ');
 
69
                        commandLine.append(arguments[i]);
 
70
                }
 
71
                return commandLine.toString();
 
72
        }
 
73
 
 
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(""));
 
77
        }
 
78
        
 
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);
 
83
 
 
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);
 
94
                outRedirect.start();
 
95
                errRedirect.start();
 
96
                // wait for command to finish
 
97
                try {
 
98
                        proc.waitFor();
 
99
                } catch (InterruptedException irex) {
 
100
                        String msg = "Fatal error running command."; //$NON-NLS-1$
 
101
                        BazaarEclipsePlugin.log(msg, irex);
 
102
                }
 
103
 
 
104
                // wait until out stream content is redirected
 
105
                try {
 
106
                        outRedirect.join();
 
107
                        errRedirect.join();
 
108
                } catch (InterruptedException irex) {
 
109
                        String msg = "Fatal error running command."; //$NON-NLS-1$
 
110
                        BazaarEclipsePlugin.log(msg, irex);
 
111
                }
 
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);        
 
119
                                }
 
120
                        }
 
121
                }
 
122
                result = outStream.toString();
 
123
                writeToAdditionalOut(result);
 
124
                return result;
 
125
        }
 
126
 
 
127
        private void writeToAdditionalOut(final String content) {
 
128
                if (additionalOut != null) {
 
129
                        PrintWriter pw = new PrintWriter(additionalOut);
 
130
                        pw.print(content);
 
131
                        pw.flush();
 
132
                }
 
133
        }
 
134
        
 
135
        public void setAdditionalOut(OutputStream additionalOut) {
 
136
                this.additionalOut = additionalOut;
 
137
        }
 
138
        
 
139
        private void writeToAdditionalErr(final String content) {
 
140
                if (additionalErr != null) {
 
141
                        PrintWriter pw = new PrintWriter(additionalErr);
 
142
                        pw.print(content);
 
143
                        pw.flush();
 
144
                }
 
145
        }
 
146
        
 
147
        public void setAdditionalErr(OutputStream additionalErr) {
 
148
                this.additionalErr = additionalErr;
 
149
        }
 
150
        
 
151
        // Helpers 
 
152
        private Preferences getPrefs() {
 
153
                return BazaarEclipsePlugin.getDefault().getPluginPreferences();
 
154
        }
 
155
        
 
156
        protected String getExecutable() {
 
157
                String executable = getPrefs().getString(BazaarPreferenceConstants.EXECUTABLE);
 
158
                if(executable == null || executable.equals("")) {
 
159
                        executable = EXECUTABLE;
 
160
                }
 
161
                return executable;
 
162
        }
 
163
        
 
164
        /**
 
165
         * Find a specific option in an array of options
 
166
         * 
 
167
         * @param array
 
168
         *            the array of options
 
169
         * @param option
 
170
         *            the option string to search for
 
171
         * @return the first element matching the option string, or null if none
 
172
         */
 
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))
 
176
                                return array[i];
 
177
                }
 
178
                return null;
 
179
        }
 
180
 
 
181
        public void run(IProgressMonitor monitor) throws CoreException {
 
182
                this.execute();
 
183
        }
 
184
        
 
185
}