diff options
Diffstat (limited to 'plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteMachine.java')
-rw-r--r-- | plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteMachine.java | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteMachine.java b/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteMachine.java new file mode 100644 index 0000000..92cfc15 --- /dev/null +++ b/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteMachine.java | |||
@@ -0,0 +1,202 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2013 Intel Corporation. | ||
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 | * Ioana Grigoropol(Intel) - initial API and implementation | ||
10 | *******************************************************************************/ | ||
11 | package org.yocto.remote.utils; | ||
12 | |||
13 | import java.util.ArrayList; | ||
14 | import java.util.HashMap; | ||
15 | import java.util.List; | ||
16 | import java.util.Map; | ||
17 | |||
18 | import org.eclipse.core.runtime.CoreException; | ||
19 | import org.eclipse.core.runtime.IProgressMonitor; | ||
20 | import org.eclipse.core.runtime.NullProgressMonitor; | ||
21 | import org.eclipse.core.runtime.OperationCanceledException; | ||
22 | import org.eclipse.core.runtime.Status; | ||
23 | import org.eclipse.core.runtime.SubProgressMonitor; | ||
24 | import org.eclipse.rse.core.model.IHost; | ||
25 | import org.eclipse.rse.core.subsystems.ISubSystem; | ||
26 | import org.eclipse.rse.internal.services.local.shells.LocalShellService; | ||
27 | import org.eclipse.rse.services.clientserver.messages.SystemMessageException; | ||
28 | import org.eclipse.rse.services.files.IFileService; | ||
29 | import org.eclipse.rse.services.shells.IHostShell; | ||
30 | import org.eclipse.rse.services.shells.IShellService; | ||
31 | import org.eclipse.rse.subsystems.files.core.servicesubsystem.IFileServiceSubSystem; | ||
32 | import org.eclipse.rse.subsystems.shells.core.subsystems.servicesubsystem.IShellServiceSubSystem; | ||
33 | import org.eclipse.ui.console.MessageConsole; | ||
34 | |||
35 | public class RemoteMachine { | ||
36 | public static final String PROXY = "proxy"; | ||
37 | |||
38 | private Map<String, String> environment; | ||
39 | private MessageConsole console; | ||
40 | private CommandResponseHandler cmdHandler; | ||
41 | private IShellService shellService; | ||
42 | private IHost connection; | ||
43 | |||
44 | private ISubSystem fileSubSystem; | ||
45 | private IFileService fileService; | ||
46 | |||
47 | public RemoteMachine(IHost connection) { | ||
48 | setConnection(connection); | ||
49 | } | ||
50 | |||
51 | public String[] prepareEnvString(IProgressMonitor monitor){ | ||
52 | String[] env = null; | ||
53 | try { | ||
54 | if (shellService instanceof LocalShellService) { | ||
55 | env = shellService.getHostEnvironment(); | ||
56 | } else { | ||
57 | List<String> envList = new ArrayList<String>(); | ||
58 | getRemoteEnvProxyVars(monitor); | ||
59 | String value = ""; | ||
60 | for (String varName : environment.keySet()){ | ||
61 | value = varName + "=" + environment.get(varName); | ||
62 | envList.add(value); | ||
63 | } | ||
64 | env = envList.toArray(new String[envList.size()]); | ||
65 | } | ||
66 | } catch (Exception e) { | ||
67 | e.printStackTrace(); | ||
68 | } | ||
69 | |||
70 | return env; | ||
71 | } | ||
72 | public void getRemoteEnvProxyVars(IProgressMonitor monitor){ | ||
73 | try { | ||
74 | if (environment != null && !environment.isEmpty()) | ||
75 | return; | ||
76 | |||
77 | environment = new HashMap<String, String>(); | ||
78 | |||
79 | IShellService shellService = getShellService(new SubProgressMonitor(monitor, 7)); | ||
80 | |||
81 | ProcessStreamBuffer buffer = null; | ||
82 | try { | ||
83 | SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 3); | ||
84 | IHostShell hostShell = shellService.runCommand("", "env" + " ; echo " + RemoteHelper.TERMINATOR + "; exit;", new String[]{}, subMonitor); | ||
85 | buffer = RemoteHelper.processOutput(subMonitor, hostShell, cmdHandler); | ||
86 | for(int i = 0; i < buffer.getOutputLines().size(); i++) { | ||
87 | String out = buffer.getOutputLines().get(i); | ||
88 | String[] tokens = out.split("="); | ||
89 | if (tokens.length != 2) | ||
90 | continue; | ||
91 | String varName = tokens[0]; | ||
92 | String varValue = tokens[1]; | ||
93 | if (varName.contains(PROXY)) | ||
94 | environment.put(varName, varValue); | ||
95 | } | ||
96 | } catch (Exception e) { | ||
97 | e.printStackTrace(); | ||
98 | } | ||
99 | |||
100 | } catch (Exception e) { | ||
101 | e.printStackTrace(); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | public Map<String, String> getEnvironment() { | ||
106 | return environment; | ||
107 | } | ||
108 | public void setEnvironment(Map<String, String> environment) { | ||
109 | this.environment = environment; | ||
110 | } | ||
111 | public MessageConsole getConsole() { | ||
112 | if (console == null) | ||
113 | console = ConsoleHelper.findConsole(ConsoleHelper.YOCTO_CONSOLE); | ||
114 | |||
115 | ConsoleHelper.showConsole(console); | ||
116 | return console; | ||
117 | } | ||
118 | public CommandResponseHandler getCmdHandler() { | ||
119 | if (cmdHandler == null) | ||
120 | cmdHandler = new CommandResponseHandler(getConsole()); | ||
121 | return cmdHandler; | ||
122 | } | ||
123 | |||
124 | public IShellService getShellService(IProgressMonitor monitor) throws Exception { | ||
125 | if (shellService != null) | ||
126 | return shellService; | ||
127 | |||
128 | final ISubSystem subsystem = getShellSubsystem(); | ||
129 | |||
130 | if (subsystem == null) | ||
131 | throw new Exception(Messages.ErrorNoSubsystem); | ||
132 | |||
133 | try { | ||
134 | subsystem.connect(monitor, false); | ||
135 | } catch (CoreException e) { | ||
136 | throw e; | ||
137 | } catch (OperationCanceledException e) { | ||
138 | throw new CoreException(Status.CANCEL_STATUS); | ||
139 | } | ||
140 | |||
141 | if (!subsystem.isConnected()) | ||
142 | throw new Exception(Messages.ErrorConnectSubsystem); | ||
143 | |||
144 | shellService = ((IShellServiceSubSystem) subsystem).getShellService(); | ||
145 | return shellService; | ||
146 | } | ||
147 | |||
148 | private ISubSystem getShellSubsystem() { | ||
149 | if (connection == null) | ||
150 | return null; | ||
151 | ISubSystem[] subSystems = connection.getSubSystems(); | ||
152 | for (int i = 0; i < subSystems.length; i++) { | ||
153 | if (subSystems[i] instanceof IShellServiceSubSystem) | ||
154 | return subSystems[i]; | ||
155 | } | ||
156 | return null; | ||
157 | } | ||
158 | |||
159 | public IHost getConnection() { | ||
160 | return connection; | ||
161 | } | ||
162 | public void setConnection(IHost connection) { | ||
163 | this.connection = connection; | ||
164 | } | ||
165 | |||
166 | public IFileService getRemoteFileService(IProgressMonitor monitor) throws Exception { | ||
167 | if (fileService == null) { | ||
168 | |||
169 | while(getFileSubsystem() == null) | ||
170 | Thread.sleep(2); | ||
171 | try { | ||
172 | getFileSubsystem().connect(monitor, false); | ||
173 | } catch (CoreException e) { | ||
174 | throw e; | ||
175 | } catch (OperationCanceledException e) { | ||
176 | throw new CoreException(Status.CANCEL_STATUS); | ||
177 | } | ||
178 | |||
179 | if (!getFileSubsystem().isConnected()) | ||
180 | throw new Exception(Messages.ErrorConnectSubsystem); | ||
181 | |||
182 | fileService = ((IFileServiceSubSystem) getFileSubsystem()).getFileService(); | ||
183 | } | ||
184 | return fileService; | ||
185 | } | ||
186 | |||
187 | public ISubSystem getFileSubsystem() { | ||
188 | if (fileSubSystem == null) { | ||
189 | if (connection == null) | ||
190 | return null; | ||
191 | ISubSystem[] subSystems = connection.getSubSystems(); | ||
192 | for (int i = 0; i < subSystems.length; i++) { | ||
193 | if (subSystems[i] instanceof IFileServiceSubSystem) { | ||
194 | fileSubSystem = subSystems[i]; | ||
195 | break; | ||
196 | } | ||
197 | } | ||
198 | } | ||
199 | return fileSubSystem; | ||
200 | } | ||
201 | |||
202 | } | ||