Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7c221f4257762ec6176a7d2d88e8b4efd428e84f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation              - initial API and implementation
 * Anna Dushistova (MontaVista) - adapted from org.eclipse.debug.core.model.RuntimeProcess
 *******************************************************************************/

package org.eclipse.tcf.te.tcf.launch.cdt.utils;

import java.util.EventObject;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.IStreamsProxy;
import org.eclipse.debug.internal.core.DebugCoreMessages;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.services.IProcesses.ProcessContext;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.runtime.events.EventManager;
import org.eclipse.tcf.te.runtime.interfaces.events.IEventListener;
import org.eclipse.tcf.te.tcf.launch.cdt.activator.Activator;
import org.eclipse.tcf.te.tcf.processes.core.launcher.ProcessLauncher;
import org.eclipse.tcf.te.tcf.processes.core.launcher.ProcessStateChangeEvent;

@SuppressWarnings("restriction")
public class TERunProcess extends PlatformObject implements IProcess,
		IEventListener {

	private ProcessLauncher prLauncher;
	private String prName;
	private boolean terminated;
	private ILaunch launch;
	private ProcessContext context;
	private int exitValue;

	public TERunProcess(ILaunch launch, String remoteExePath, String arguments,
			String label, IPeer peer, SubProgressMonitor monitor) {
		this.launch = launch;
		// initializeAttributes(attributes);
		prName = remoteExePath;
		terminated = false;
		launch.addProcess(this);
		EventManager.getInstance().addEventListener(this,
				ProcessStateChangeEvent.class);
		try {
			prLauncher = TEHelper.launchCmd(peer, null, remoteExePath, arguments,
					null, monitor, new Callback());
		} catch (CoreException e) {
			Activator.getDefault().getLog().log(
					new Status(IStatus.ERROR, Activator.getUniqueIdentifier(),
							"Error launching remote process: "+prName, e)); //$NON-NLS-1$
			String reason = e.getMessage();
			if (reason == null)
				reason = "Unknown Reason"; //$NON-NLS-1$
			prName += " (Failed to start: " + reason + ')'; //$NON-NLS-1$
		}
		fireCreationEvent();
	}

	@Override
    public Object getAdapter(Class adapter) {
		if (adapter.equals(IProcess.class)) {
			return this;
		}
		if (adapter.equals(IDebugTarget.class)) {
			ILaunch launch = getLaunch();
			IDebugTarget[] targets = launch.getDebugTargets();
			for (int i = 0; i < targets.length; i++) {
				if (this.equals(targets[i].getProcess())) {
					return targets[i];
				}
			}
			return null;
		}
		if (adapter.equals(ILaunch.class)) {
			return getLaunch();
		}
		// CONTEXTLAUNCHING
		if (adapter.equals(ILaunchConfiguration.class)) {
			return getLaunch().getLaunchConfiguration();
		}
		return super.getAdapter(adapter);
	}

	@Override
    public boolean canTerminate() {
		return prLauncher != null && !terminated;
	}

	@Override
    public boolean isTerminated() {
		return prLauncher == null || terminated;
	}

	@Override
    public void terminate() throws DebugException {
		if (!isTerminated()) {
			prLauncher.terminate();
		}
	}

	@Override
    public String getLabel() {
		return prName;
	}

	@Override
    public ILaunch getLaunch() {
		return launch;
	}

	@Override
    public IStreamsProxy getStreamsProxy() {
		// NOT SUPPORTED
		return null;
	}

	@Override
    public void setAttribute(String key, String value) {
		// NOT SUPPORTED FOR NOW

	}

	@Override
    public String getAttribute(String key) {
		// NOT SUPPORTED FOR NOW
		return null;
	}

	@Override
    public int getExitValue() throws DebugException {
		if (isTerminated()) {
			return exitValue;
		}
		throw new DebugException(
				new Status(
						IStatus.ERROR,
						DebugPlugin.getUniqueIdentifier(),
						DebugException.TARGET_REQUEST_FAILED,
						DebugCoreMessages.RuntimeProcess_Exit_value_not_available_until_process_terminates__1,
						null));
	}

	@Override
    public void eventFired(EventObject event) {
		if (event instanceof ProcessStateChangeEvent) {
			ProcessStateChangeEvent pscEvent = (ProcessStateChangeEvent) event;
			if (pscEvent.getEventId().equals(
					ProcessStateChangeEvent.EVENT_PROCESS_CREATED)) {
				if ((pscEvent.getSource() instanceof ProcessContext)) {
					if (prLauncher != null && prLauncher.getAdapter(ProcessContext.class) == pscEvent.getSource())
						context = (ProcessContext) pscEvent.getSource();
				}
			} else if (pscEvent.getEventId().equals(
					ProcessStateChangeEvent.EVENT_PROCESS_TERMINATED)) {
				if ((pscEvent.getSource() instanceof ProcessContext)) {
					if (((ProcessContext) pscEvent.getSource()).getID().equals(
							context.getID())) {
						exitValue = pscEvent.getExitCode();
						terminated = true;
						fireTerminateEvent();
					}
				}
			}
		}

	}

	/**
	 * Fires a creation event.
	 */
	protected void fireCreationEvent() {
		fireEvent(new DebugEvent(this, DebugEvent.CREATE));
	}

	/**
	 * Fires the given debug event.
	 *
	 * @param event
	 *            debug event to fire
	 */
	protected void fireEvent(DebugEvent event) {
		DebugPlugin manager = DebugPlugin.getDefault();
		if (manager != null) {
			manager.fireDebugEventSet(new DebugEvent[] { event });
		}
	}

	/**
	 * Fires a terminate event.
	 */
	protected void fireTerminateEvent() {
		fireEvent(new DebugEvent(this, DebugEvent.TERMINATE));
	}

	/**
	 * Fires a change event.
	 */
	protected void fireChangeEvent() {
		fireEvent(new DebugEvent(this, DebugEvent.CHANGE));
	}

}

Back to the top