summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteMachine.java
blob: 92cfc15b08f0944e54d82a02a2ea16338148c789 (plain)
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
/*******************************************************************************
 * Copyright (c) 2013 Intel Corporation.
 * 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:
 * Ioana Grigoropol(Intel) - initial API and implementation
 *******************************************************************************/
package org.yocto.remote.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.rse.core.model.IHost;
import org.eclipse.rse.core.subsystems.ISubSystem;
import org.eclipse.rse.internal.services.local.shells.LocalShellService;
import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
import org.eclipse.rse.services.files.IFileService;
import org.eclipse.rse.services.shells.IHostShell;
import org.eclipse.rse.services.shells.IShellService;
import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem;
import org.eclipse.rse.subsystems.shells.core.subsystems.servicesubsystem.IShellServiceSubSystem;
import org.eclipse.ui.console.MessageConsole;

public class RemoteMachine {
	public static final String PROXY = "proxy";

	private Map<String, String> environment;
	private MessageConsole console;
	private CommandResponseHandler cmdHandler;
	private IShellService shellService;
	private IHost connection;

	private ISubSystem fileSubSystem;
	private IFileService fileService;

	public RemoteMachine(IHost connection) {
		setConnection(connection);
	}

	public String[] prepareEnvString(IProgressMonitor monitor){
		String[] env = null;
		try {
			if (shellService instanceof LocalShellService) {
				env  = shellService.getHostEnvironment();
			} else {
				List<String> envList = new ArrayList<String>();
				getRemoteEnvProxyVars(monitor);
				String value = "";
				for (String varName : environment.keySet()){
					value = varName + "=" + environment.get(varName);
					envList.add(value);
				}
				env = envList.toArray(new String[envList.size()]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return env;
	}
	public void getRemoteEnvProxyVars(IProgressMonitor monitor){
		try {
			if (environment != null && !environment.isEmpty())
				return;

			environment = new HashMap<String, String>();

			IShellService shellService = getShellService(new SubProgressMonitor(monitor, 7));

			ProcessStreamBuffer buffer = null;
			try {
				SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 3);
				IHostShell hostShell = shellService.runCommand("", "env" + " ; echo " + RemoteHelper.TERMINATOR + "; exit;", new String[]{}, subMonitor);
				buffer = RemoteHelper.processOutput(subMonitor, hostShell, cmdHandler);
				for(int i = 0; i < buffer.getOutputLines().size(); i++) {
					String out = buffer.getOutputLines().get(i);
					String[] tokens = out.split("=");
					if (tokens.length != 2)
						continue;
					String varName = tokens[0];
					String varValue = tokens[1];
					if (varName.contains(PROXY))
						environment.put(varName, varValue);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Map<String, String> getEnvironment() {
		return environment;
	}
	public void setEnvironment(Map<String, String> environment) {
		this.environment = environment;
	}
	public MessageConsole getConsole() {
		if (console == null)
			console = ConsoleHelper.findConsole(ConsoleHelper.YOCTO_CONSOLE);

		ConsoleHelper.showConsole(console);
		return console;
	}
	public CommandResponseHandler getCmdHandler() {
		if (cmdHandler == null)
			cmdHandler = new CommandResponseHandler(getConsole());
		return cmdHandler;
	}

	public IShellService getShellService(IProgressMonitor monitor) throws Exception {
		if (shellService != null)
			return shellService;

		final ISubSystem subsystem = getShellSubsystem();

		if (subsystem == null)
			throw new Exception(Messages.ErrorNoSubsystem);

		try {
			subsystem.connect(monitor, false);
		} catch (CoreException e) {
			throw e;
		} catch (OperationCanceledException e) {
			throw new CoreException(Status.CANCEL_STATUS);
		}

		if (!subsystem.isConnected())
			throw new Exception(Messages.ErrorConnectSubsystem);

		shellService = ((IShellServiceSubSystem) subsystem).getShellService();
		return shellService;
	}

	private ISubSystem getShellSubsystem() {
		if (connection == null)
			return null;
		ISubSystem[] subSystems = connection.getSubSystems();
		for (int i = 0; i < subSystems.length; i++) {
			if (subSystems[i] instanceof IShellServiceSubSystem)
				return subSystems[i];
		}
		return null;
	}

	public IHost getConnection() {
		return connection;
	}
	public void setConnection(IHost connection) {
		this.connection = connection;
	}

	public IFileService getRemoteFileService(IProgressMonitor monitor) throws Exception {
		if (fileService == null) {

			while(getFileSubsystem() == null)
				Thread.sleep(2);
			try {
				getFileSubsystem().connect(monitor, false);
			} catch (CoreException e) {
				throw e;
			} catch (OperationCanceledException e) {
				throw new CoreException(Status.CANCEL_STATUS);
			}

			if (!getFileSubsystem().isConnected())
				throw new Exception(Messages.ErrorConnectSubsystem);

			fileService = ((IFileServiceSubSystem) getFileSubsystem()).getFileService();
		}
		return fileService;
	}

	public ISubSystem getFileSubsystem() {
		if (fileSubSystem == null) {
			if (connection == null)
				return null;
			ISubSystem[] subSystems = connection.getSubSystems();
			for (int i = 0; i < subSystems.length; i++) {
				if (subSystems[i] instanceof IFileServiceSubSystem) {
					fileSubSystem = subSystems[i];
					break;
				}
			}
		}
		return fileSubSystem;
	}

}