diff options
Diffstat (limited to 'plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteShellExec.java')
-rw-r--r-- | plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteShellExec.java | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteShellExec.java b/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteShellExec.java new file mode 100644 index 0000000..711088c --- /dev/null +++ b/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteShellExec.java | |||
@@ -0,0 +1,140 @@ | |||
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 | * Intel - initial API and implementation | ||
10 | *******************************************************************************/ | ||
11 | package org.yocto.remote.utils; | ||
12 | |||
13 | import java.io.BufferedReader; | ||
14 | import java.io.IOException; | ||
15 | import java.io.InputStream; | ||
16 | import java.io.InputStreamReader; | ||
17 | import java.io.OutputStream; | ||
18 | |||
19 | import org.eclipse.core.runtime.IProgressMonitor; | ||
20 | import org.eclipse.rse.core.model.IHost; | ||
21 | |||
22 | public class RemoteShellExec { | ||
23 | |||
24 | public static final int | ||
25 | STATE_NULL = 0, | ||
26 | STATE_RUNNING = 1, | ||
27 | STATE_EXITED = 2; | ||
28 | |||
29 | private final String command; | ||
30 | private final IHost host; | ||
31 | |||
32 | private InputStream fInStream; | ||
33 | private OutputStream fOutStream; | ||
34 | private InputStream fErrStream; | ||
35 | private Process remoteShellProcess; | ||
36 | |||
37 | private int exitCode = 0; | ||
38 | private int status = STATE_NULL; | ||
39 | |||
40 | private final String RETURN_VALUE_TAG = "org.yocto.sdk.remotetools.RVTAG"; | ||
41 | private final String RETURN_VALUE_CMD = ";echo \"" + RETURN_VALUE_TAG + "$?\""; | ||
42 | |||
43 | public RemoteShellExec(IHost host, String command) { | ||
44 | assert(host != null); | ||
45 | this.host = host; | ||
46 | this.command = command; | ||
47 | } | ||
48 | |||
49 | public int getStatus() { | ||
50 | return status; | ||
51 | } | ||
52 | |||
53 | public int getExitCode() { | ||
54 | return exitCode; | ||
55 | } | ||
56 | |||
57 | private void reset() { | ||
58 | fInStream = null; | ||
59 | fOutStream = null; | ||
60 | fErrStream = null; | ||
61 | |||
62 | remoteShellProcess = null; | ||
63 | exitCode = 0; | ||
64 | status = STATE_NULL; | ||
65 | } | ||
66 | |||
67 | public InputStream getInputStream() { | ||
68 | return fInStream; | ||
69 | } | ||
70 | |||
71 | public OutputStream getOutputStream() { | ||
72 | return fOutStream; | ||
73 | } | ||
74 | |||
75 | public InputStream getErrStream() { | ||
76 | return fErrStream; | ||
77 | } | ||
78 | |||
79 | public synchronized void start(String prelaunchCmd, String argument, IProgressMonitor monitor) throws Exception { | ||
80 | if(status == STATE_RUNNING) | ||
81 | return; | ||
82 | |||
83 | reset(); | ||
84 | argument = (argument == null ? RETURN_VALUE_CMD : argument + RETURN_VALUE_CMD); | ||
85 | remoteShellProcess = RemoteHelper.remoteShellExec(this.host, prelaunchCmd, this.command, argument, monitor); | ||
86 | fInStream = remoteShellProcess.getInputStream(); | ||
87 | fOutStream = remoteShellProcess.getOutputStream(); | ||
88 | fErrStream = remoteShellProcess.getErrorStream(); | ||
89 | status = STATE_RUNNING; | ||
90 | } | ||
91 | |||
92 | public synchronized void terminate() throws Exception { | ||
93 | if(status != STATE_RUNNING || remoteShellProcess != null) | ||
94 | return; | ||
95 | |||
96 | remoteShellProcess.destroy(); | ||
97 | reset(); | ||
98 | } | ||
99 | |||
100 | public int waitFor(IProgressMonitor monitor) throws InterruptedException { | ||
101 | while(status == STATE_RUNNING) { | ||
102 | if(monitor != null) { | ||
103 | if(monitor.isCanceled()) { | ||
104 | throw new InterruptedException("User Cancelled"); | ||
105 | } | ||
106 | } | ||
107 | |||
108 | try { | ||
109 | remoteShellProcess.waitFor(); | ||
110 | }catch(InterruptedException e){ | ||
111 | //get the return value | ||
112 | try { | ||
113 | if(fInStream.available() != 0) { | ||
114 | BufferedReader in = new BufferedReader(new InputStreamReader(fInStream)); | ||
115 | String thisline; | ||
116 | int idx; | ||
117 | while((thisline = in.readLine()) != null) { | ||
118 | if(thisline.indexOf(RETURN_VALUE_CMD) == -1) { | ||
119 | idx = thisline.indexOf(RETURN_VALUE_TAG); | ||
120 | if(idx != -1) { | ||
121 | try { | ||
122 | exitCode=(new Integer(thisline.substring(idx+RETURN_VALUE_TAG.length()))).intValue(); | ||
123 | }catch(NumberFormatException e2) { | ||
124 | } | ||
125 | break; | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | }catch(IOException e1) { | ||
131 | //do nothing | ||
132 | } | ||
133 | }finally { | ||
134 | status=STATE_EXITED; | ||
135 | } | ||
136 | } | ||
137 | return exitCode; | ||
138 | } | ||
139 | } | ||
140 | |||